Repository navigation
Expand file tree
/
Copy pathsetup_ollama_model.py
More file actions
334 lines (274 loc) · 9.63 KB
/
Copy pathsetup_ollama_model.py
File metadata and controls
334 lines (274 loc) · 9.63 KB
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
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
#!/usr/bin/env python3
"""
Ollama Model Setup Script
Simple script to check GPU and pull Qwen model
Usage:
python setup_ollama_model.py
python setup_ollama_model.py --model qwen3.5:4b
"""
import subprocess
import time
import sys
import argparse
def print_header(title):
"""Print formatted header"""
print("\n" + "="*70)
print(f" {title}")
print("="*70)
def check_gpu():
"""Check GPU availability"""
print_header("GPU INFORMATION")
try:
result = subprocess.run(
['nvidia-smi', '--query-gpu=name,memory.total,memory.free', '--format=csv,noheader'],
capture_output=True,
text=True,
timeout=5
)
if result.returncode == 0:
gpu_info = result.stdout.strip().split(', ')
print(f" GPU: {gpu_info[0]}")
print(f" Total VRAM: {gpu_info[1]}")
print(f" Free VRAM: {gpu_info[2]}")
print(" GPU is available for Ollama")
return True
else:
print(" No GPU detected")
print(" CPU mode will be used (slower)")
return False
except FileNotFoundError:
print(" nvidia-smi not found - no NVIDIA GPU detected")
print(" CPU mode will be used (slower)")
return False
except subprocess.TimeoutExpired:
print(" GPU check timed out")
return False
except Exception as e:
print(f" Error checking GPU: {e}")
return False
def check_ollama():
"""Check if Ollama is installed and running"""
print_header("OLLAMA STATUS")
# Check if Ollama is installed
try:
result = subprocess.run(
['ollama', '--version'],
capture_output=True,
text=True,
timeout=5
)
if result.returncode == 0:
version = result.stdout.strip()
print(f" Ollama installed: {version}")
else:
print(" Ollama not found")
print("\nPlease install Ollama:")
print(" Windows: https://ollama.com/download")
print(" Linux: curl -fsSL https://ollama.com/install.sh | sh")
print(" macOS: brew install ollama")
return False
except FileNotFoundError:
print(" Ollama not found")
print("\nPlease install Ollama:")
print(" Windows: https://ollama.com/download")
print(" Linux: curl -fsSL https://ollama.com/install.sh | sh")
print(" macOS: brew install ollama")
return False
except Exception as e:
print(f" Error checking Ollama: {e}")
return False
# Check if Ollama server is running
print("\n Checking Ollama server...")
try:
import requests
response = requests.get('http://localhost:11434/api/tags', timeout=3)
if response.status_code == 200:
print(" Ollama server is running")
return True
else:
print(" Ollama server not responding properly")
print(" Please start Ollama: ollama serve")
return False
except ImportError:
print(" 'requests' module not found")
print(" Installing requests...")
subprocess.run([sys.executable, '-m', 'pip', 'install', 'requests', '-q'])
print(" Please run this script again")
return False
except Exception:
print(" Ollama server not running")
print(" Please start Ollama in another terminal:")
print(" ollama serve")
return False
def list_models():
"""List currently installed models"""
print_header("INSTALLED MODELS")
try:
result = subprocess.run(
['ollama', 'list'],
capture_output=True,
text=True,
timeout=10
)
if result.returncode == 0:
output = result.stdout.strip()
if output:
print(output)
else:
print("No models installed yet")
return True
else:
print(" Failed to list models")
return False
except Exception as e:
print(f" Error listing models: {e}")
return False
def pull_model(model_name):
"""Pull Ollama model"""
print_header(f"PULLING MODEL: {model_name}")
# Check if model already exists
try:
result = subprocess.run(
['ollama', 'list'],
capture_output=True,
text=True,
timeout=10
)
if model_name in result.stdout:
print(f"✅ Model {model_name} is already downloaded")
# Ask if user wants to update
response = input("\n🔄 Do you want to update it? (y/N): ").strip().lower()
if response != 'y':
print("Skipping download")
return True
except Exception:
pass
# Pull the model
print(f"\n📥 Downloading model: {model_name}")
print("⏳ This may take several minutes depending on model size...")
print("💡 You can check progress below:\n")
start_time = time.time()
try:
# Run ollama pull and show real-time output
process = subprocess.Popen(
['ollama', 'pull', model_name],
stdout=subprocess.PIPE,
stderr=subprocess.STDOUT,
text=True,
bufsize=1
)
# Print output in real-time
for line in process.stdout:
print(line, end='', flush=True)
process.wait()
if process.returncode == 0:
elapsed = time.time() - start_time
print(f"\n✅ Model {model_name} downloaded successfully!")
print(f"⏱ Download time: {elapsed:.1f} seconds")
return True
else:
print(f"\n Failed to download model {model_name}")
return False
except KeyboardInterrupt:
print("\n\n Download interrupted by user")
return False
except Exception as e:
print(f"\n Error downloading model: {e}")
return False
def show_model_info(model_name):
"""Show model information"""
print_header(f"MODEL INFO: {model_name}")
try:
result = subprocess.run(
['ollama', 'show', model_name],
capture_output=True,
text=True,
timeout=10
)
if result.returncode == 0:
print(result.stdout)
return True
else:
print(f" Model {model_name} not found")
return False
except Exception as e:
print(f" Error getting model info: {e}")
return False
def main():
"""Main function"""
parser = argparse.ArgumentParser(
description='Setup Ollama and pull Qwen model',
formatter_class=argparse.RawDescriptionHelpFormatter,
epilog="""
Examples:
python setup_ollama_model.py # Pull default qwen3.5:4b
python setup_ollama_model.py --model qwen3.5:4b # Pull specific model
python setup_ollama_model.py --list-only # Just list installed models
"""
)
parser.add_argument(
'--model',
default='qwen3.5:4b',
help='Model to pull (default: qwen3.5:4b)'
)
parser.add_argument(
'--list-only',
action='store_true',
help='Only list installed models without pulling'
)
parser.add_argument(
'--skip-gpu-check',
action='store_true',
help='Skip GPU check'
)
args = parser.parse_args()
print("="*70)
print(" OLLAMA MODEL SETUP")
print("="*70)
# Check GPU
if not args.skip_gpu_check:
gpu_available = check_gpu()
else:
gpu_available = None
# Check Ollama
if not check_ollama():
print("\n Setup failed: Ollama not available")
print("\nPlease:")
print(" 1. Install Ollama from https://ollama.com")
print(" 2. Start Ollama server: ollama serve")
print(" 3. Run this script again")
sys.exit(1)
# List models
list_models()
# Pull model if not list-only
if not args.list_only:
print()
success = pull_model(args.model)
if success:
print()
show_model_info(args.model)
print_header(" SETUP COMPLETE")
print(f"Model '{args.model}' is ready to use!")
print(f"\nNext steps:")
print(f" 1. Test the model:")
print(f" ollama run {args.model}")
print(f" 2. Or use it via API:")
print(f" python llm_api_standalone.py")
print(f" # or")
print(f" cd LLM-Model && python -m api.main")
else:
print_header(" SETUP FAILED")
print("Please check the error messages above")
sys.exit(1)
else:
print_header("✅ LIST COMPLETE")
print("Use --model to pull a specific model")
if __name__ == "__main__":
try:
main()
except KeyboardInterrupt:
print("\n\n Setup interrupted by user")
sys.exit(1)
except Exception as e:
print(f"\n Unexpected error: {e}")
sys.exit(1)