Update interence_test_with_intent_detection.py
Browse files
interence_test_with_intent_detection.py
CHANGED
|
@@ -1,4 +1,4 @@
|
|
| 1 |
-
#
|
| 2 |
import os
|
| 3 |
import json
|
| 4 |
import re
|
|
@@ -27,7 +27,6 @@ from transformers import (
|
|
| 27 |
)
|
| 28 |
from peft import PeftModel
|
| 29 |
|
| 30 |
-
# === Ayarlar ===
|
| 31 |
HF_TOKEN = os.getenv("HF_TOKEN")
|
| 32 |
MODEL_BASE = "malhajar/Mistral-7B-Instruct-v0.2-turkish"
|
| 33 |
USE_FINE_TUNE = False
|
|
@@ -51,10 +50,8 @@ model = None
|
|
| 51 |
tokenizer = None
|
| 52 |
chat_history = []
|
| 53 |
|
| 54 |
-
# === FastAPI Uygulaması ===
|
| 55 |
app = FastAPI()
|
| 56 |
|
| 57 |
-
# === Yardımcı Fonksiyonlar ===
|
| 58 |
def log(msg):
|
| 59 |
print(f"[{datetime.now().strftime('%H:%M:%S')}] {msg}", flush=True)
|
| 60 |
|
|
@@ -95,7 +92,7 @@ def root():
|
|
| 95 |
</script>
|
| 96 |
</body>
|
| 97 |
</html>
|
| 98 |
-
"""
|
| 99 |
|
| 100 |
@app.post("/train_intents")
|
| 101 |
def train_intents(train_input: TrainInput):
|
|
@@ -165,7 +162,10 @@ async def detect_intent(text):
|
|
| 165 |
return id2label[pred_id]
|
| 166 |
|
| 167 |
async def generate_response(text):
|
| 168 |
-
messages = [
|
|
|
|
|
|
|
|
|
|
| 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 = {
|
|
@@ -220,7 +220,6 @@ async def chat(input: ChatInput):
|
|
| 220 |
traceback.print_exc()
|
| 221 |
return JSONResponse(content={"error": str(e)}, status_code=500)
|
| 222 |
|
| 223 |
-
# === Model setup ===
|
| 224 |
def setup_model():
|
| 225 |
global model, tokenizer
|
| 226 |
try:
|
|
@@ -250,7 +249,6 @@ def setup_model():
|
|
| 250 |
log(f"❌ LLM model yükleme hatası: {e}")
|
| 251 |
traceback.print_exc()
|
| 252 |
|
| 253 |
-
# === Sunucu başlat ===
|
| 254 |
def run():
|
| 255 |
log("===== Application Startup =====")
|
| 256 |
threading.Thread(target=setup_model, daemon=True).start()
|
|
@@ -258,4 +256,5 @@ def run():
|
|
| 258 |
while True:
|
| 259 |
time.sleep(60)
|
| 260 |
|
|
|
|
| 261 |
run()
|
|
|
|
| 1 |
+
# Fine-tune + Intent + LLM + System Prompt
|
| 2 |
import os
|
| 3 |
import json
|
| 4 |
import re
|
|
|
|
| 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
|
|
|
|
| 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 |
|
|
|
|
| 92 |
</script>
|
| 93 |
</body>
|
| 94 |
</html>
|
| 95 |
+
"""
|
| 96 |
|
| 97 |
@app.post("/train_intents")
|
| 98 |
def train_intents(train_input: TrainInput):
|
|
|
|
| 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 = {
|
|
|
|
| 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:
|
|
|
|
| 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()
|
|
|
|
| 256 |
while True:
|
| 257 |
time.sleep(60)
|
| 258 |
|
| 259 |
+
# Uygulamayı çalıştır
|
| 260 |
run()
|