Delete interence_test_with_intent_detection.py
Browse files
interence_test_with_intent_detection.py
DELETED
@@ -1,260 +0,0 @@
|
|
1 |
-
# Fine-tune + Intent + LLM + System Prompt
|
2 |
-
import os
|
3 |
-
import json
|
4 |
-
import re
|
5 |
-
import torch
|
6 |
-
import asyncio
|
7 |
-
import shutil
|
8 |
-
import zipfile
|
9 |
-
import threading
|
10 |
-
import uvicorn
|
11 |
-
import time
|
12 |
-
import traceback
|
13 |
-
import random
|
14 |
-
from fastapi import FastAPI, Request
|
15 |
-
from fastapi.responses import JSONResponse, HTMLResponse
|
16 |
-
from pydantic import BaseModel
|
17 |
-
from datetime import datetime
|
18 |
-
from datasets import Dataset
|
19 |
-
from huggingface_hub import hf_hub_download
|
20 |
-
from transformers import (
|
21 |
-
AutoTokenizer,
|
22 |
-
AutoModelForSequenceClassification,
|
23 |
-
AutoModelForCausalLM,
|
24 |
-
Trainer,
|
25 |
-
TrainingArguments,
|
26 |
-
pipeline
|
27 |
-
)
|
28 |
-
from peft import PeftModel
|
29 |
-
|
30 |
-
HF_TOKEN = os.getenv("HF_TOKEN")
|
31 |
-
MODEL_BASE = "malhajar/Mistral-7B-Instruct-v0.2-turkish"
|
32 |
-
USE_FINE_TUNE = False
|
33 |
-
FINE_TUNE_REPO = "UcsTurkey/trained-zips"
|
34 |
-
FINE_TUNE_ZIP = "trained_model_000_009.zip"
|
35 |
-
USE_SAMPLING = False
|
36 |
-
CONFIDENCE_THRESHOLD = -1.5
|
37 |
-
FALLBACK_ANSWERS = [
|
38 |
-
"Bu konuda maalesef bilgim yok.",
|
39 |
-
"Ne demek istediğinizi tam anlayamadım.",
|
40 |
-
"Bu soruya şu an yanıt veremiyorum."
|
41 |
-
]
|
42 |
-
|
43 |
-
INTENT_MODEL_PATH = "intent_model"
|
44 |
-
INTENT_MODEL_ID = "dbmdz/bert-base-turkish-cased"
|
45 |
-
USE_CUDA = torch.cuda.is_available()
|
46 |
-
INTENT_MODEL = None
|
47 |
-
INTENT_TOKENIZER = None
|
48 |
-
LABEL2ID = {}
|
49 |
-
model = None
|
50 |
-
tokenizer = None
|
51 |
-
chat_history = []
|
52 |
-
|
53 |
-
app = FastAPI()
|
54 |
-
|
55 |
-
def log(msg):
|
56 |
-
print(f"[{datetime.now().strftime('%H:%M:%S')}] {msg}", flush=True)
|
57 |
-
|
58 |
-
def pattern_to_regex(pattern):
|
59 |
-
return re.sub(r"\{(\w+?)\}", r"(?P<\1>.+?)", pattern)
|
60 |
-
|
61 |
-
class ChatInput(BaseModel):
|
62 |
-
user_input: str
|
63 |
-
|
64 |
-
class TrainInput(BaseModel):
|
65 |
-
intents: list
|
66 |
-
|
67 |
-
@app.get("/")
|
68 |
-
def health():
|
69 |
-
return {"status": "ok"}
|
70 |
-
|
71 |
-
@app.get("/start", response_class=HTMLResponse)
|
72 |
-
def root():
|
73 |
-
return """
|
74 |
-
<html>
|
75 |
-
<body>
|
76 |
-
<h2>Mistral 7B Instruct Chat</h2>
|
77 |
-
<textarea id="input" rows="4" cols="60" placeholder="Write your instruction..."></textarea><br>
|
78 |
-
<button onclick="send()">Gönder</button><br><br>
|
79 |
-
<label>Model Cevabı:</label><br>
|
80 |
-
<textarea id="output" rows="10" cols="80" readonly style="white-space: pre-wrap;"></textarea>
|
81 |
-
<script>
|
82 |
-
async function send() {
|
83 |
-
const input = document.getElementById("input").value;
|
84 |
-
const res = await fetch('/chat', {
|
85 |
-
method: 'POST',
|
86 |
-
headers: { 'Content-Type': 'application/json' },
|
87 |
-
body: JSON.stringify({ user_input: input })
|
88 |
-
});
|
89 |
-
const data = await res.json();
|
90 |
-
document.getElementById('output').value = data.answer || data.response || data.error || 'Hata oluştu.';
|
91 |
-
}
|
92 |
-
</script>
|
93 |
-
</body>
|
94 |
-
</html>
|
95 |
-
"""
|
96 |
-
|
97 |
-
@app.post("/train_intents")
|
98 |
-
def train_intents(train_input: TrainInput):
|
99 |
-
try:
|
100 |
-
intents = train_input.intents
|
101 |
-
log(f"🎯 Intent eğitimi başlatıldı. Intent sayısı: {len(intents)}")
|
102 |
-
|
103 |
-
texts, labels = [], []
|
104 |
-
label2id = {}
|
105 |
-
for idx, intent in enumerate(intents):
|
106 |
-
label2id[intent["name"]] = idx
|
107 |
-
for ex in intent["examples"]:
|
108 |
-
if "{" not in ex:
|
109 |
-
texts.append(ex)
|
110 |
-
labels.append(idx)
|
111 |
-
|
112 |
-
dataset = Dataset.from_dict({"text": texts, "label": labels})
|
113 |
-
|
114 |
-
tokenizer = AutoTokenizer.from_pretrained(INTENT_MODEL_ID)
|
115 |
-
model = AutoModelForSequenceClassification.from_pretrained(INTENT_MODEL_ID, num_labels=len(label2id))
|
116 |
-
|
117 |
-
def tokenize(batch):
|
118 |
-
return tokenizer(batch["text"], truncation=True, padding=True)
|
119 |
-
|
120 |
-
tokenized = dataset.map(tokenize, batched=True)
|
121 |
-
args = TrainingArguments("./intent_train_output", per_device_train_batch_size=4, num_train_epochs=3, logging_steps=10, save_strategy="no", report_to=[])
|
122 |
-
trainer = Trainer(model=model, args=args, train_dataset=tokenized)
|
123 |
-
trainer.train()
|
124 |
-
|
125 |
-
if os.path.exists(INTENT_MODEL_PATH):
|
126 |
-
shutil.rmtree(INTENT_MODEL_PATH)
|
127 |
-
model.save_pretrained(INTENT_MODEL_PATH)
|
128 |
-
tokenizer.save_pretrained(INTENT_MODEL_PATH)
|
129 |
-
with open(os.path.join(INTENT_MODEL_PATH, "label2id.json"), "w") as f:
|
130 |
-
json.dump(label2id, f)
|
131 |
-
|
132 |
-
log("✅ Intent modeli kaydedildi.")
|
133 |
-
return {"status": "ok", "message": "Intent modeli eğitildi ve kaydedildi."}
|
134 |
-
|
135 |
-
except Exception as e:
|
136 |
-
log(f"❌ Intent eğitimi hatası: {e}")
|
137 |
-
return JSONResponse(content={"error": str(e)}, status_code=500)
|
138 |
-
|
139 |
-
@app.post("/load_intent_model")
|
140 |
-
def load_intent_model():
|
141 |
-
global INTENT_MODEL, INTENT_TOKENIZER, LABEL2ID
|
142 |
-
try:
|
143 |
-
if not os.path.exists(INTENT_MODEL_PATH):
|
144 |
-
return JSONResponse(content={"error": "intent_model klasörü bulunamadı."}, status_code=400)
|
145 |
-
|
146 |
-
INTENT_TOKENIZER = AutoTokenizer.from_pretrained(INTENT_MODEL_PATH)
|
147 |
-
INTENT_MODEL = AutoModelForSequenceClassification.from_pretrained(INTENT_MODEL_PATH)
|
148 |
-
with open(os.path.join(INTENT_MODEL_PATH, "label2id.json")) as f:
|
149 |
-
LABEL2ID = json.load(f)
|
150 |
-
log("✅ Intent modeli belleğe yüklendi.")
|
151 |
-
return {"status": "ok", "message": "Intent modeli yüklendi."}
|
152 |
-
|
153 |
-
except Exception as e:
|
154 |
-
log(f"❌ Intent modeli yükleme hatası: {e}")
|
155 |
-
return JSONResponse(content={"error": str(e)}, status_code=500)
|
156 |
-
|
157 |
-
async def detect_intent(text):
|
158 |
-
inputs = INTENT_TOKENIZER(text, return_tensors="pt")
|
159 |
-
outputs = INTENT_MODEL(**inputs)
|
160 |
-
pred_id = outputs.logits.argmax().item()
|
161 |
-
id2label = {v: k for k, v in LABEL2ID.items()}
|
162 |
-
return id2label[pred_id]
|
163 |
-
|
164 |
-
async def generate_response(text):
|
165 |
-
messages = [
|
166 |
-
{"role": "system", "content": "Sen yardımcı bir Türkçe yapay zeka asistanısın. Soruları açık ve doğru şekilde yanıtla."},
|
167 |
-
{"role": "user", "content": text}
|
168 |
-
]
|
169 |
-
inputs = tokenizer.apply_chat_template(messages, return_tensors="pt", add_generation_prompt=True)
|
170 |
-
inputs = {k: v.to(model.device) for k, v in inputs.items()}
|
171 |
-
generate_args = {
|
172 |
-
"max_new_tokens": 512,
|
173 |
-
"return_dict_in_generate": True,
|
174 |
-
"output_scores": True,
|
175 |
-
"do_sample": USE_SAMPLING
|
176 |
-
}
|
177 |
-
if USE_SAMPLING:
|
178 |
-
generate_args.update({"temperature": 0.7, "top_p": 0.9, "top_k": 50})
|
179 |
-
|
180 |
-
with torch.no_grad():
|
181 |
-
output = model.generate(**inputs, **generate_args)
|
182 |
-
|
183 |
-
prompt_text = tokenizer.decode(inputs["input_ids"][0], skip_special_tokens=True)
|
184 |
-
decoded = tokenizer.decode(output.sequences[0], skip_special_tokens=True)
|
185 |
-
answer = decoded.replace(prompt_text, "").strip()
|
186 |
-
|
187 |
-
if output.scores and len(output.scores) > 0:
|
188 |
-
first_token_score = output.scores[0][0]
|
189 |
-
if torch.isnan(first_token_score).any() or torch.isinf(first_token_score).any():
|
190 |
-
log("⚠️ Geçersiz logit (NaN/Inf) tespit edildi.")
|
191 |
-
return random.choice(FALLBACK_ANSWERS)
|
192 |
-
max_score = torch.max(first_token_score).item()
|
193 |
-
log(f"🔍 İlk token skoru: {max_score:.4f}")
|
194 |
-
if max_score < CONFIDENCE_THRESHOLD:
|
195 |
-
return random.choice(FALLBACK_ANSWERS)
|
196 |
-
|
197 |
-
return answer
|
198 |
-
|
199 |
-
@app.post("/chat")
|
200 |
-
async def chat(input: ChatInput):
|
201 |
-
user_input = input.user_input.strip()
|
202 |
-
try:
|
203 |
-
if model is None or tokenizer is None:
|
204 |
-
return {"error": "Model veya tokenizer henüz yüklenmedi."}
|
205 |
-
|
206 |
-
if INTENT_MODEL:
|
207 |
-
intent_task = asyncio.create_task(detect_intent(user_input))
|
208 |
-
response_task = asyncio.create_task(generate_response(user_input))
|
209 |
-
intent = await intent_task
|
210 |
-
response = await response_task
|
211 |
-
log(f"✅ Intent: {intent}")
|
212 |
-
return {"intent": intent, "response": response}
|
213 |
-
else:
|
214 |
-
response = await generate_response(user_input)
|
215 |
-
log("💬 Intent modeli yok, yalnızca LLM cevabı verildi.")
|
216 |
-
return {"response": response}
|
217 |
-
|
218 |
-
except Exception as e:
|
219 |
-
log(f"❌ /chat hatası: {e}")
|
220 |
-
traceback.print_exc()
|
221 |
-
return JSONResponse(content={"error": str(e)}, status_code=500)
|
222 |
-
|
223 |
-
def setup_model():
|
224 |
-
global model, tokenizer
|
225 |
-
try:
|
226 |
-
device = "cuda" if torch.cuda.is_available() else "cpu"
|
227 |
-
dtype = torch.float32
|
228 |
-
|
229 |
-
if USE_FINE_TUNE:
|
230 |
-
log("📦 Fine-tune zip indiriliyor...")
|
231 |
-
zip_path = hf_hub_download(repo_id=FINE_TUNE_REPO, filename=FINE_TUNE_ZIP, repo_type="model", token=HF_TOKEN)
|
232 |
-
extract_dir = "/app/extracted"
|
233 |
-
os.makedirs(extract_dir, exist_ok=True)
|
234 |
-
with zipfile.ZipFile(zip_path, "r") as zip_ref:
|
235 |
-
zip_ref.extractall(extract_dir)
|
236 |
-
|
237 |
-
tokenizer = AutoTokenizer.from_pretrained(os.path.join(extract_dir, "output"), use_fast=False)
|
238 |
-
base_model = AutoModelForCausalLM.from_pretrained(MODEL_BASE, torch_dtype=dtype).to(device)
|
239 |
-
model = PeftModel.from_pretrained(base_model, os.path.join(extract_dir, "output")).to(device)
|
240 |
-
else:
|
241 |
-
log("🧠 Ana model indiriliyor...")
|
242 |
-
tokenizer = AutoTokenizer.from_pretrained(MODEL_BASE, use_fast=False)
|
243 |
-
model = AutoModelForCausalLM.from_pretrained(MODEL_BASE, torch_dtype=dtype).to(device)
|
244 |
-
|
245 |
-
tokenizer.pad_token = tokenizer.pad_token or tokenizer.eos_token
|
246 |
-
model.eval()
|
247 |
-
log("✅ LLM model başarıyla yüklendi.")
|
248 |
-
except Exception as e:
|
249 |
-
log(f"❌ LLM model yükleme hatası: {e}")
|
250 |
-
traceback.print_exc()
|
251 |
-
|
252 |
-
def run():
|
253 |
-
log("===== Application Startup =====")
|
254 |
-
threading.Thread(target=setup_model, daemon=True).start()
|
255 |
-
threading.Thread(target=lambda: uvicorn.run(app, host="0.0.0.0", port=7860), daemon=True).start()
|
256 |
-
while True:
|
257 |
-
time.sleep(60)
|
258 |
-
|
259 |
-
# Uygulamayı çalıştır
|
260 |
-
run()
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
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