File size: 7,332 Bytes
d54e6a9 208adf4 d54e6a9 208adf4 eb70b46 208adf4 d54e6a9 eb70b46 d54e6a9 |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 |
"""
EXAONE-3.0-7.8B-Instruct ๋ชจ๋ธ์ Ollama์ ์ถ๊ฐํ๋ ์คํฌ๋ฆฝํธ (ํ ํฐ ํฌํจ)
"""
import os
import subprocess
from pathlib import Path
import sys
# ํ๋ก์ ํธ ๋ฃจํธ๋ฅผ Python ๊ฒฝ๋ก์ ์ถ๊ฐ
sys.path.insert(0, os.path.dirname(os.path.dirname(os.path.abspath(__file__))))
def get_huggingface_token():
"""
Hugging Face ํ ํฐ ๊ฐ์ ธ์ค๊ธฐ (ํ๊ฒฝ ๋ณ์ ์ฐ์ , ์์ผ๋ฉด DB์์)
Google API ํค ๊ด๋ฆฌ ๋ฐฉ์๊ณผ ๋์ผํ ํจํด
"""
# ํ๊ฒฝ ๋ณ์์์ ๋จผ์ ํ์ธ
token = os.getenv('HUGGINGFACE_HUB_TOKEN', '').strip()
if not token:
token = os.getenv('HF_TOKEN', '').strip()
if token:
print(f"[Hugging Face] ํ๊ฒฝ ๋ณ์์์ ํ ํฐ ๊ฐ์ ธ์ด (๊ธธ์ด: {len(token)}์)")
return token
# DB์์ ๊ฐ์ ธ์ค๊ธฐ (์ํ ์ฐธ์กฐ ๋ฐฉ์ง๋ฅผ ์ํด ์ฌ๊ธฐ์ ์ํฌํธ)
try:
from app.database import SystemConfig
# Flask ์ ํ๋ฆฌ์ผ์ด์
์ปจํ
์คํธ ์์ด๋ ์๋ํ๋๋ก ์๋
token = SystemConfig.get_config('huggingface_token', '').strip()
if token:
print(f"[Hugging Face] DB์์ ํ ํฐ ๊ฐ์ ธ์ด (๊ธธ์ด: {len(token)}์)")
return token
except Exception as e:
print(f"[Hugging Face] DB์์ ํ ํฐ ์กฐํ ์คํจ (ํ๊ฒฝ ๋ณ์ ์ฌ์ฉ): {e}")
return ''
# Hugging Face ํ ํฐ ๊ฐ์ ธ์ค๊ธฐ
HF_TOKEN = get_huggingface_token()
if not HF_TOKEN:
raise ValueError("HUGGINGFACE_HUB_TOKEN ๋๋ HF_TOKEN ํ๊ฒฝ ๋ณ์๋ฅผ ์ค์ ํ๊ฑฐ๋, ๊ด๋ฆฌ ํ์ด์ง์์ ํ ํฐ์ ์ค์ ํด์ฃผ์ธ์.")
def set_huggingface_token():
"""Hugging Face ํ ํฐ ์ค์ """
os.environ['HUGGINGFACE_HUB_TOKEN'] = HF_TOKEN
print(f"[OK] Hugging Face ํ ํฐ ์ค์ ์๋ฃ")
def create_ollama_modelfile():
"""Ollama Modelfile ์์ฑ"""
modelfile_content = f"""FROM huggingface:LGAI-EXAONE/EXAONE-3.0-7.8B-Instruct
# ๋ชจ๋ธ ์ค์
PARAMETER temperature 0.7
PARAMETER top_p 0.9
PARAMETER top_k 40
PARAMETER num_ctx 4096
PARAMETER num_predict 512
# ์์คํ
ํ๋กฌํํธ
SYSTEM \"\"\"You are EXAONE, a helpful AI assistant developed by LG AI Research.
You are designed to be helpful, harmless, and honest.
You can communicate in both Korean and English.\"\"\"
# EXAONE ๋ชจ๋ธ์ ์ฑํ
ํ
ํ๋ฆฟ
TEMPLATE \"\"\"{{{{ if .System }}}}<|im_start|>system
{{{{ .System }}}}<|im_end|>
{{{{ end }}}}{{{{ if .Prompt }}}}<|im_start|>user
{{{{ .Prompt }}}}<|im_end|>
{{{{ end }}}}<|im_start|>assistant
{{{{ .Response }}}}<|im_end|>
\"\"\"
"""
modelfile_path = Path("EXAONE-3.0-7.8B-Instruct.modelfile")
modelfile_path.write_text(modelfile_content, encoding='utf-8')
print(f"[OK] Modelfile ์์ฑ ์๋ฃ: {modelfile_path.absolute()}")
return modelfile_path
def create_model_with_modelfile(modelfile_path):
"""Modelfile์ ์ฌ์ฉํ์ฌ Ollama ๋ชจ๋ธ ์์ฑ"""
try:
print("\n" + "=" * 60)
print("Ollama ๋ชจ๋ธ ์์ฑ ์์...")
print("=" * 60)
# ํ๊ฒฝ ๋ณ์ ์ค์
env = os.environ.copy()
env['HUGGINGFACE_HUB_TOKEN'] = HF_TOKEN
# ollama create ๋ช
๋ น์ด ์คํ
cmd = ['ollama', 'create', 'EXAONE-3.0-7.8B-Instruct', '-f', str(modelfile_path)]
print(f"์คํ ๋ช
๋ น์ด: {' '.join(cmd)}")
result = subprocess.run(
cmd,
env=env,
capture_output=True,
text=True,
timeout=3600 # 1์๊ฐ ํ์์์
)
if result.returncode == 0:
print("[OK] ๋ชจ๋ธ ์์ฑ ์ฑ๊ณต!")
print(result.stdout)
return True
else:
print("[ERROR] ๋ชจ๋ธ ์์ฑ ์คํจ")
print("์ค๋ฅ ์ถ๋ ฅ:")
print(result.stderr)
return False
except subprocess.TimeoutExpired:
print("[ERROR] ๋ชจ๋ธ ์์ฑ ์๊ฐ ์ด๊ณผ (1์๊ฐ)")
return False
except Exception as e:
print(f"[ERROR] ์ค๋ฅ ๋ฐ์: {e}")
return False
def check_ollama_installation():
"""Ollama ์ค์น ํ์ธ"""
try:
result = subprocess.run(['ollama', '--version'],
capture_output=True, text=True, timeout=5)
if result.returncode == 0:
version = result.stdout.strip()
print(f"[OK] Ollama ์ค์น ํ์ธ: {version}")
return True
else:
print("[ERROR] Ollama๊ฐ ์ค์น๋์ด ์์ง ์์ต๋๋ค.")
return False
except FileNotFoundError:
print("[ERROR] Ollama๊ฐ ์ค์น๋์ด ์์ง ์์ต๋๋ค.")
print(" ์ค์น ๋ฐฉ๋ฒ: https://ollama.ai/download")
return False
except Exception as e:
print(f"[ERROR] Ollama ํ์ธ ์ค ์ค๋ฅ: {e}")
return False
def verify_model():
"""์์ฑ๋ ๋ชจ๋ธ ํ์ธ"""
try:
result = subprocess.run(['ollama', 'list'],
capture_output=True, text=True, timeout=5)
if result.returncode == 0:
if 'EXAONE-3.0-7.8B-Instruct' in result.stdout:
print("\n[OK] ๋ชจ๋ธ์ด ์ฑ๊ณต์ ์ผ๋ก ์ถ๊ฐ๋์์ต๋๋ค!")
print("\n์ค์น๋ ๋ชจ๋ธ ๋ชฉ๋ก:")
print(result.stdout)
return True
else:
print("\n[WARNING] ๋ชจ๋ธ์ด ๋ชฉ๋ก์ ๋ํ๋์ง ์์ต๋๋ค.")
print("\nํ์ฌ ์ค์น๋ ๋ชจ๋ธ:")
print(result.stdout)
return False
return False
except Exception as e:
print(f"[WARNING] ๋ชจ๋ธ ํ์ธ ์ค ์ค๋ฅ: {e}")
return False
if __name__ == "__main__":
print("\n" + "=" * 60)
print("EXAONE-3.0-7.8B-Instruct Ollama ์ถ๊ฐ ์คํฌ๋ฆฝํธ")
print("=" * 60 + "\n")
# ์ฌ์ ํ์ธ
if not check_ollama_installation():
print("\n[WARNING] Ollama๋ฅผ ๋จผ์ ์ค์นํด์ฃผ์ธ์.")
exit(1)
# ํ ํฐ ์ค์
set_huggingface_token()
# Modelfile ์์ฑ
modelfile_path = create_ollama_modelfile()
# ๋ชจ๋ธ ์์ฑ ํ์ธ
print("\n[WARNING] ์ฃผ์์ฌํญ:")
print("- ๋ชจ๋ธ ํฌ๊ธฐ๊ฐ ์ฝ 15GB์ด๋ฏ๋ก ๋ค์ด๋ก๋์ ์๊ฐ์ด ๊ฑธ๋ฆด ์ ์์ต๋๋ค.")
print("- ์ถฉ๋ถํ ๋์คํฌ ๊ณต๊ฐ๊ณผ GPU ๋ฉ๋ชจ๋ฆฌ๊ฐ ํ์ํฉ๋๋ค.")
print("- Hugging Face์์ ๋ชจ๋ธ ์ก์ธ์ค ๊ถํ์ด ํ์ํฉ๋๋ค.")
# ๋ชจ๋ธ ์์ฑ ์์
print("\n๋ชจ๋ธ ์์ฑ์ ์์ํฉ๋๋ค...")
# ๋ชจ๋ธ ์์ฑ
success = create_model_with_modelfile(modelfile_path)
if success:
verify_model()
print("\n" + "=" * 60)
print("[OK] ๋ชจ๋ธ ์ถ๊ฐ ์๋ฃ!")
print("=" * 60)
print("\n์ด์ ์น ์ ํ๋ฆฌ์ผ์ด์
์์ ๋ชจ๋ธ์ ์ฌ์ฉํ ์ ์์ต๋๋ค.")
print("๋ชจ๋ธ ํ
์คํธ:")
print(" ollama run EXAONE-3.0-7.8B-Instruct \"์๋
ํ์ธ์\"")
print("=" * 60)
else:
print("\n" + "=" * 60)
print("[ERROR] ๋ชจ๋ธ ์์ฑ ์คํจ")
print("=" * 60)
print("\n์๋์ผ๋ก ๋ชจ๋ธ์ ์์ฑํ๋ ค๋ฉด:")
print(f" 1. Hugging Face ํ ํฐ ์ค์ :")
print(f" $env:HUGGINGFACE_HUB_TOKEN='YOUR_TOKEN_HERE'")
print(f" 2. Modelfile๋ก ๋ชจ๋ธ ์์ฑ:")
print(f" ollama create EXAONE-3.0-7.8B-Instruct -f {modelfile_path}")
print("=" * 60)
|