File size: 5,972 Bytes
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
"""
EXAONE-3.0-7.8B-Instruct ๋ชจ๋ธ์„ Ollama์— ์ถ”๊ฐ€ํ•˜๋Š” ์Šคํฌ๋ฆฝํŠธ (ํ† ํฐ ํฌํ•จ)
"""

import os
import subprocess
from pathlib import Path

# Hugging Face ํ† ํฐ
HF_TOKEN = "YOUR_HUGGINGFACE_TOKEN_HERE"

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='{HF_TOKEN}'")
        print(f"  2. Modelfile๋กœ ๋ชจ๋ธ ์ƒ์„ฑ:")
        print(f"     ollama create EXAONE-3.0-7.8B-Instruct -f {modelfile_path}")
        print("=" * 60)