1from llama_cpp import Llama
2from huggingface_hub import hf_hub_download
3
4# 모델 다운로드 (Q8_0 추천)
5model_path = hf_hub_download(
6 "gyung/lfm2-1.2b-koen-mt-v8-rl-10k-merged-GGUF",
7 "lfm2-1.2b-koen-mt-v8-rl-10k-merged-Q8_0.gguf"
8)
9
10# 모델 로드
11llm = Llama(
12 model_path=model_path,
13 n_ctx=4096,
14 n_gpu_layers=-1, # GPU 사용 (-1: 전체 레이어)
15 verbose=False
16)
17
18def translate(text, direction="en2ko"):
19 if direction == "en2ko":
20 system = "Translate the following text to Korean."
21 else:
22 system = "Translate the following text to English."
23
24 prompt = f"""<|im_start|>system
25{system}<|im_end|>
26<|im_start|>user
27{text}<|im_end|>
28<|im_start|>assistant
29"""
30 output = llm(prompt, max_tokens=256, stop=["<|im_end|>"], temperature=0.3)
31 return output['choices'][0]['text'].strip()
32
33# 사용 예시
34print(translate("The weather is beautiful today."))
35# → 오늘 날씨가 정말 아름답습니다.
36
37print(translate("한국 음식이 정말 맛있어요.", "ko2en"))
38# → Korean food is really delicious.
1# 1. CUDA 지원 llama-cpp-python 설치 (중요!)
2!pip uninstall llama-cpp-python -y
3!pip install llama-cpp-python==0.3.16 \
4 --extra-index-url https://github.com/abetlen/llama-cpp-python/releases/download/v0.3.16-cu124
5
6# 2. 위 코드 실행
1llama-cli -hf gyung/lfm2-1.2b-koen-mt-v8-rl-10k-merged-GGUF \
2 -p "Translate to Korean: Hello world"