Update fine_tune_inference_test_mistral.py
Browse files
fine_tune_inference_test_mistral.py
CHANGED
@@ -1,4 +1,4 @@
|
|
1 |
-
import os, torch, zipfile, threading, uvicorn
|
2 |
from fastapi import FastAPI
|
3 |
from fastapi.responses import HTMLResponse, JSONResponse
|
4 |
from pydantic import BaseModel
|
@@ -6,7 +6,6 @@ from transformers import AutoTokenizer, AutoModelForCausalLM
|
|
6 |
from peft import PeftModel
|
7 |
from huggingface_hub import hf_hub_download
|
8 |
from datetime import datetime
|
9 |
-
import time
|
10 |
import random
|
11 |
|
12 |
# === Sabitler ===
|
@@ -14,8 +13,8 @@ HF_TOKEN = os.environ.get("HF_TOKEN")
|
|
14 |
MODEL_BASE = "mistralai/Mistral-7B-Instruct-v0.2"
|
15 |
FINE_TUNE_ZIP = "trained_model_000_009.zip"
|
16 |
FINE_TUNE_REPO = "UcsTurkey/trained-zips"
|
|
|
17 |
USE_SAMPLING = False
|
18 |
-
USE_FINE_TUNE = False # ✅ Ana model ile test için False yap
|
19 |
CONFIDENCE_THRESHOLD = -1.5
|
20 |
FALLBACK_ANSWERS = [
|
21 |
"Bu konuda maalesef bilgim yok.",
|
@@ -71,13 +70,16 @@ def root():
|
|
71 |
def chat(msg: Message):
|
72 |
global model, tokenizer
|
73 |
try:
|
74 |
-
if model is None:
|
75 |
-
return {"error": "Model yüklenmedi"}
|
|
|
76 |
user_input = msg.user_input.strip()
|
77 |
if not user_input:
|
78 |
return {"error": "Boş giriş"}
|
|
|
79 |
prompt = f"SORU: {user_input}\nCEVAP:"
|
80 |
inputs = tokenizer(prompt, return_tensors="pt").to(model.device)
|
|
|
81 |
with torch.no_grad():
|
82 |
output = model.generate(
|
83 |
**inputs,
|
@@ -90,6 +92,7 @@ def chat(msg: Message):
|
|
90 |
output_scores=True,
|
91 |
suppress_tokens=[tokenizer.pad_token_id]
|
92 |
)
|
|
|
93 |
decoded = tokenizer.decode(output.sequences[0], skip_special_tokens=True)
|
94 |
answer = decoded[len(prompt):].strip()
|
95 |
|
@@ -106,8 +109,10 @@ def chat(msg: Message):
|
|
106 |
chat_history.append({"user": user_input, "bot": answer})
|
107 |
log(f"Soru: {user_input} → Cevap: {answer[:60]}...")
|
108 |
return {"answer": answer, "chat_history": chat_history}
|
|
|
109 |
except Exception as e:
|
110 |
log(f"❌ /chat hatası: {e}")
|
|
|
111 |
return {"error": str(e)}
|
112 |
|
113 |
def detect_env():
|
@@ -118,44 +123,52 @@ def detect_env():
|
|
118 |
def setup_model():
|
119 |
global model, tokenizer
|
120 |
try:
|
121 |
-
extract_path = "/app/extracted"
|
122 |
device, supports_bf16 = detect_env()
|
123 |
dtype = torch.bfloat16 if supports_bf16 else torch.float32
|
124 |
log(f"🧠 Ortam: {device.upper()}, dtype: {dtype}")
|
125 |
|
126 |
if USE_FINE_TUNE:
|
127 |
-
log("📦
|
128 |
zip_path = hf_hub_download(
|
129 |
repo_id=FINE_TUNE_REPO,
|
130 |
filename=FINE_TUNE_ZIP,
|
131 |
repo_type="model",
|
132 |
token=HF_TOKEN
|
133 |
)
|
|
|
134 |
os.makedirs(extract_path, exist_ok=True)
|
135 |
with zipfile.ZipFile(zip_path, "r") as zip_ref:
|
136 |
zip_ref.extractall(extract_path)
|
|
|
137 |
tokenizer = AutoTokenizer.from_pretrained(os.path.join(extract_path, "output"))
|
138 |
if tokenizer.pad_token is None:
|
139 |
tokenizer.pad_token = tokenizer.eos_token
|
|
|
140 |
base = AutoModelForCausalLM.from_pretrained(MODEL_BASE, torch_dtype=dtype).to(device)
|
141 |
peft = PeftModel.from_pretrained(base, os.path.join(extract_path, "output"))
|
142 |
model = peft.model.to(device)
|
|
|
143 |
else:
|
144 |
-
log("🧪 Sadece ana model yüklenecek
|
145 |
tokenizer = AutoTokenizer.from_pretrained(MODEL_BASE, use_fast=False)
|
|
|
|
|
146 |
model = AutoModelForCausalLM.from_pretrained(MODEL_BASE, torch_dtype=dtype).to(device)
|
147 |
|
148 |
model.eval()
|
149 |
-
log("✅ Model yüklendi.")
|
|
|
150 |
except Exception as e:
|
151 |
log(f"❌ Model setup hatası: {e}")
|
|
|
152 |
|
153 |
def run_server():
|
154 |
log("🌐 Uvicorn başlatılıyor...")
|
155 |
uvicorn.run(app, host="0.0.0.0", port=7860)
|
156 |
|
157 |
-
log("
|
158 |
threading.Thread(target=setup_model, daemon=True).start()
|
159 |
threading.Thread(target=run_server, daemon=True).start()
|
|
|
160 |
while True:
|
161 |
-
time.sleep(60)
|
|
|
1 |
+
import os, torch, zipfile, threading, uvicorn, time, traceback
|
2 |
from fastapi import FastAPI
|
3 |
from fastapi.responses import HTMLResponse, JSONResponse
|
4 |
from pydantic import BaseModel
|
|
|
6 |
from peft import PeftModel
|
7 |
from huggingface_hub import hf_hub_download
|
8 |
from datetime import datetime
|
|
|
9 |
import random
|
10 |
|
11 |
# === Sabitler ===
|
|
|
13 |
MODEL_BASE = "mistralai/Mistral-7B-Instruct-v0.2"
|
14 |
FINE_TUNE_ZIP = "trained_model_000_009.zip"
|
15 |
FINE_TUNE_REPO = "UcsTurkey/trained-zips"
|
16 |
+
USE_FINE_TUNE = False # ✅ Ana modeli test etmek için False yap
|
17 |
USE_SAMPLING = False
|
|
|
18 |
CONFIDENCE_THRESHOLD = -1.5
|
19 |
FALLBACK_ANSWERS = [
|
20 |
"Bu konuda maalesef bilgim yok.",
|
|
|
70 |
def chat(msg: Message):
|
71 |
global model, tokenizer
|
72 |
try:
|
73 |
+
if model is None or tokenizer is None:
|
74 |
+
return {"error": "Model veya tokenizer henüz yüklenmedi."}
|
75 |
+
|
76 |
user_input = msg.user_input.strip()
|
77 |
if not user_input:
|
78 |
return {"error": "Boş giriş"}
|
79 |
+
|
80 |
prompt = f"SORU: {user_input}\nCEVAP:"
|
81 |
inputs = tokenizer(prompt, return_tensors="pt").to(model.device)
|
82 |
+
|
83 |
with torch.no_grad():
|
84 |
output = model.generate(
|
85 |
**inputs,
|
|
|
92 |
output_scores=True,
|
93 |
suppress_tokens=[tokenizer.pad_token_id]
|
94 |
)
|
95 |
+
|
96 |
decoded = tokenizer.decode(output.sequences[0], skip_special_tokens=True)
|
97 |
answer = decoded[len(prompt):].strip()
|
98 |
|
|
|
109 |
chat_history.append({"user": user_input, "bot": answer})
|
110 |
log(f"Soru: {user_input} → Cevap: {answer[:60]}...")
|
111 |
return {"answer": answer, "chat_history": chat_history}
|
112 |
+
|
113 |
except Exception as e:
|
114 |
log(f"❌ /chat hatası: {e}")
|
115 |
+
traceback.print_exc()
|
116 |
return {"error": str(e)}
|
117 |
|
118 |
def detect_env():
|
|
|
123 |
def setup_model():
|
124 |
global model, tokenizer
|
125 |
try:
|
|
|
126 |
device, supports_bf16 = detect_env()
|
127 |
dtype = torch.bfloat16 if supports_bf16 else torch.float32
|
128 |
log(f"🧠 Ortam: {device.upper()}, dtype: {dtype}")
|
129 |
|
130 |
if USE_FINE_TUNE:
|
131 |
+
log("📦 Fine-tune zip indiriliyor...")
|
132 |
zip_path = hf_hub_download(
|
133 |
repo_id=FINE_TUNE_REPO,
|
134 |
filename=FINE_TUNE_ZIP,
|
135 |
repo_type="model",
|
136 |
token=HF_TOKEN
|
137 |
)
|
138 |
+
extract_path = "/app/extracted"
|
139 |
os.makedirs(extract_path, exist_ok=True)
|
140 |
with zipfile.ZipFile(zip_path, "r") as zip_ref:
|
141 |
zip_ref.extractall(extract_path)
|
142 |
+
|
143 |
tokenizer = AutoTokenizer.from_pretrained(os.path.join(extract_path, "output"))
|
144 |
if tokenizer.pad_token is None:
|
145 |
tokenizer.pad_token = tokenizer.eos_token
|
146 |
+
|
147 |
base = AutoModelForCausalLM.from_pretrained(MODEL_BASE, torch_dtype=dtype).to(device)
|
148 |
peft = PeftModel.from_pretrained(base, os.path.join(extract_path, "output"))
|
149 |
model = peft.model.to(device)
|
150 |
+
|
151 |
else:
|
152 |
+
log("🧪 Sadece ana model yüklenecek...")
|
153 |
tokenizer = AutoTokenizer.from_pretrained(MODEL_BASE, use_fast=False)
|
154 |
+
if tokenizer.pad_token is None:
|
155 |
+
tokenizer.pad_token = tokenizer.eos_token
|
156 |
model = AutoModelForCausalLM.from_pretrained(MODEL_BASE, torch_dtype=dtype).to(device)
|
157 |
|
158 |
model.eval()
|
159 |
+
log("✅ Model başarıyla yüklendi.")
|
160 |
+
|
161 |
except Exception as e:
|
162 |
log(f"❌ Model setup hatası: {e}")
|
163 |
+
traceback.print_exc()
|
164 |
|
165 |
def run_server():
|
166 |
log("🌐 Uvicorn başlatılıyor...")
|
167 |
uvicorn.run(app, host="0.0.0.0", port=7860)
|
168 |
|
169 |
+
log("===== Application Startup =====")
|
170 |
threading.Thread(target=setup_model, daemon=True).start()
|
171 |
threading.Thread(target=run_server, daemon=True).start()
|
172 |
+
|
173 |
while True:
|
174 |
+
time.sleep(60)
|