-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathstart_tinyllm.py
More file actions
executable file
·203 lines (160 loc) · 5.91 KB
/
Copy pathstart_tinyllm.py
File metadata and controls
executable file
·203 lines (160 loc) · 5.91 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
# start_tinyllm.py - Skrypt do uruchomienia serwisu TinyLLM
import os
import sys
import argparse
import subprocess
import time
from pathlib import Path
# Domyślny model do użycia (mały model, który może działać na CPU)
DEFAULT_MODEL = "TinyLlama-1.1B-Chat-v1.0"
DEFAULT_PORT = 8080
def check_dependencies():
"""Sprawdza, czy wymagane zależności są zainstalowane"""
dependencies = ["pip", "python"]
for dep in dependencies:
try:
subprocess.run([dep, "--version"], check=True, stdout=subprocess.PIPE, stderr=subprocess.PIPE)
except (subprocess.SubprocessError, FileNotFoundError):
print(f"Błąd: {dep} nie jest zainstalowany lub nie znajduje się w PATH")
return False
return True
def install_packages():
"""Instaluje wymagane pakiety"""
packages = ["llama-cpp-python", "fastapi", "uvicorn", "huggingface_hub"]
print(f"Instalowanie wymaganych pakietów: {', '.join(packages)}")
try:
subprocess.run([sys.executable, "-m", "pip", "install"] + packages, check=True)
return True
except subprocess.SubprocessError as e:
print(f"Błąd instalacji pakietów: {e}")
return False
def start_tinyllm_server(model_path, port):
"""Uruchamia serwer TinyLLM"""
server_code = """
import sys
import os
import time
from fastapi import FastAPI, HTTPException
from pydantic import BaseModel
from typing import List, Optional, Dict
import uvicorn
import json
from llama_cpp import Llama
# Modele danych dla API
class CompletionRequest(BaseModel):
prompt: str
max_tokens: int = 100
temperature: float = 0.7
stop: Optional[List[str]] = None
class CompletionChoice(BaseModel):
text: str
index: int = 0
finish_reason: str = "stop"
class CompletionResponse(BaseModel):
id: str
object: str = "text_completion"
created: int
model: str
choices: List[CompletionChoice]
# Inicjalizacja aplikacji FastAPI
app = FastAPI(title="TinyLLM API")
# Ładowanie modelu
model_path = "{model_path}"
model_name = os.path.basename(model_path)
print(f"Ładowanie modelu z {model_path}...")
try:
# Znajdź plik .gguf w katalogu modelu
model_files = []
for ext in [".gguf", ".bin"]:
model_files.extend(list(Path(model_path).glob(f"*{ext}")))
if not model_files:
raise FileNotFoundError(f"Nie znaleziono pliku modelu w {model_path}")
# Użyj pierwszego znalezionego pliku modelu
model_file = str(model_files[0])
print(f"Znaleziono plik modelu: {model_file}")
# Załaduj model
llm = Llama(model_path=model_file, n_ctx=2048)
print(f"Model załadowany z {model_file}")
except Exception as e:
print(f"Błąd ładowania modelu: {e}")
sys.exit(1)
@app.get("/")
def read_root():
return {"message": "TinyLLM API is running", "model": model_name}
@app.post("/v1/completions", response_model=CompletionResponse)
async def create_completion(request: CompletionRequest):
try:
# Uzyskaj uzupełnienie z modelu
output = llm(
request.prompt,
max_tokens=request.max_tokens,
temperature=request.temperature,
stop=request.stop
)
# Formatuj odpowiedź
text = output["choices"][0]["text"]
return CompletionResponse(
id=f"cmpl-{{hash(text) & 0xffffffff}}",
created=int(time.time()),
model=model_name,
choices=[CompletionChoice(text=text)]
)
except Exception as e:
raise HTTPException(status_code=500, detail=str(e))
# Uruchom serwer
if __name__ == "__main__":
uvicorn.run(app, host="0.0.0.0", port={port})
""".format(model_path=model_path, port=port)
# Zapisz kod serwera do pliku
server_file = "tinyllm_server.py"
with open(server_file, "w") as f:
f.write(server_code)
# Uruchom serwer
print(f"Uruchamianie serwera TinyLLM na porcie {port}...")
process = subprocess.Popen([sys.executable, server_file])
# Poczekaj na uruchomienie serwera
print("Czekanie na uruchomienie serwera...")
time.sleep(5)
return process
def main():
parser = argparse.ArgumentParser(description='Uruchomienie serwisu TinyLLM')
parser.add_argument('--model', default=DEFAULT_MODEL, help='Nazwa modelu do użycia')
parser.add_argument('--port', type=int, default=DEFAULT_PORT, help='Port, na którym uruchomić serwer')
parser.add_argument('--model-dir', default='./models', help='Katalog przechowywania modeli')
args = parser.parse_args()
print("Konfiguracja serwisu TinyLLM...")
# Sprawdź zależności
if not check_dependencies():
sys.exit(1)
# Zainstaluj wymagane pakiety
if not install_packages():
sys.exit(1)
# Przygotuj ścieżkę do modelu
model_path = os.path.join(args.model_dir, args.model)
os.makedirs(model_path, exist_ok=True)
# Pobierz model z HuggingFace, jeśli to konieczne
try:
print(f"Sprawdzanie modelu {args.model}...")
import huggingface_hub
# Sprawdź, czy katalog modelu istnieje i czy zawiera pliki modelu
if not any(Path(model_path).glob("*.gguf")) and not any(Path(model_path).glob("*.bin")):
print(f"Pobieranie modelu {args.model} do {model_path}...")
huggingface_hub.snapshot_download(
repo_id=f"TinyLlama/{args.model}",
local_dir=model_path
)
except Exception as e:
print(f"Ostrzeżenie: Nie udało się pobrać modelu: {e}")
print("Kontynuowanie bez pobierania modelu...")
# Uruchom serwer
server_process = start_tinyllm_server(model_path, args.port)
print(f"Serwis TinyLLM działa na http://localhost:{args.port}")
print("Naciśnij Ctrl+C, aby zatrzymać serwis")
try:
server_process.wait()
except KeyboardInterrupt:
print("Zatrzymywanie serwisu TinyLLM...")
server_process.terminate()
server_process.wait()
if __name__ == "__main__":
main()