Spaces:
Running
Running
Update inference_test.py
Browse files- inference_test.py +15 -111
inference_test.py
CHANGED
@@ -4,19 +4,23 @@ from fastapi.responses import HTMLResponse, JSONResponse
|
|
4 |
from transformers import AutoTokenizer, AutoModelForSequenceClassification
|
5 |
import intent_test_runner
|
6 |
from service_config import ServiceConfig
|
7 |
-
import intent, llm_model
|
8 |
from log import log
|
|
|
9 |
|
10 |
s_config = ServiceConfig()
|
11 |
s_config.setup_environment()
|
12 |
|
13 |
# === FastAPI
|
14 |
app = FastAPI()
|
|
|
15 |
|
16 |
@app.get("/")
|
17 |
def health():
|
18 |
return {"status": "ok"}
|
19 |
|
|
|
|
|
20 |
@app.post("/run_tests", status_code=202)
|
21 |
def run_tests():
|
22 |
log("🚦 /run_tests çağrıldı. Testler başlatılıyor...")
|
@@ -25,6 +29,7 @@ def run_tests():
|
|
25 |
|
26 |
@app.get("/start", response_class=HTMLResponse)
|
27 |
def root():
|
|
|
28 |
session_id = str(uuid.uuid4())
|
29 |
session_info = {
|
30 |
"session_id": session_id,
|
@@ -33,10 +38,15 @@ def root():
|
|
33 |
"last_intent": None,
|
34 |
"awaiting_variable": None
|
35 |
}
|
|
|
|
|
36 |
if not hasattr(app.state, "session_store"):
|
37 |
app.state.session_store = {}
|
38 |
app.state.session_store[session_id] = session_info
|
|
|
39 |
log(f"🌐 /start ile yeni session başlatıldı: {session_id}")
|
|
|
|
|
40 |
return f"""
|
41 |
<html><body>
|
42 |
<h2>Turkcell LLM Chat</h2>
|
@@ -68,8 +78,10 @@ def root():
|
|
68 |
def start_chat():
|
69 |
if llm_model.model is None or llm_model.tokenizer is None:
|
70 |
return {"error": "Model yüklenmedi."}
|
|
|
71 |
if not hasattr(app.state, "session_store"):
|
72 |
app.state.session_store = {}
|
|
|
73 |
session_id = str(uuid.uuid4())
|
74 |
session_info = {
|
75 |
"session_id": session_id,
|
@@ -103,117 +115,9 @@ def load_intent_model():
|
|
103 |
|
104 |
@app.post("/chat")
|
105 |
async def chat(msg: llm_model.Message, request: Request):
|
106 |
-
|
107 |
-
session_id = request.headers.get("X-Session-ID", "demo-session")
|
108 |
|
109 |
-
if not hasattr(app.state, "session_store"):
|
110 |
-
app.state.session_store = {}
|
111 |
-
|
112 |
-
session_store = getattr(app.state, "session_store", {})
|
113 |
-
session = session_store.get(session_id, {
|
114 |
-
"session_id": session_id,
|
115 |
-
"variables": {},
|
116 |
-
"auth_tokens": {},
|
117 |
-
"last_intent": None,
|
118 |
-
"awaiting_variable": None
|
119 |
-
})
|
120 |
-
|
121 |
-
try:
|
122 |
-
if llm_model.model is None or llm_model.tokenizer is None:
|
123 |
-
return {"error": "Model yüklenmedi."}
|
124 |
-
|
125 |
-
detected_intent, intent_conf = None, 0.0
|
126 |
-
if s_config.INTENT_MODEL:
|
127 |
-
detected_intent, intent_conf = await intent.detect_intent(user_input)
|
128 |
-
log(f"🎯 Intent tespit edildi: {detected_intent} (conf={intent_conf:.2f})")
|
129 |
-
|
130 |
-
current_intent = session.get("last_intent")
|
131 |
-
awaiting_variable = session.get("awaiting_variable")
|
132 |
-
|
133 |
-
if (
|
134 |
-
awaiting_variable and
|
135 |
-
detected_intent and
|
136 |
-
detected_intent != current_intent and
|
137 |
-
intent_conf > s_config.INTENT_CONFIDENCE_THRESHOLD
|
138 |
-
):
|
139 |
-
log(f"🧹 Yeni intent: {detected_intent}, önceki: {current_intent} — awaiting sıfırlanıyor.")
|
140 |
-
session["awaiting_variable"] = None
|
141 |
-
session["variables"] = {}
|
142 |
-
session["last_intent"] = detected_intent
|
143 |
-
current_intent = detected_intent
|
144 |
-
|
145 |
-
if (
|
146 |
-
detected_intent and
|
147 |
-
intent_conf > s_config.INTENT_CONFIDENCE_THRESHOLD and
|
148 |
-
detected_intent in s_config.INTENT_DEFINITIONS
|
149 |
-
):
|
150 |
-
definition = s_config.INTENT_DEFINITIONS[detected_intent]
|
151 |
-
pattern_list = definition.get("variables", [])
|
152 |
-
data_formats = s_config.DATA_FORMATS
|
153 |
-
variable_format_map = definition.get("variable_formats", {})
|
154 |
-
|
155 |
-
if awaiting_variable:
|
156 |
-
extracted = intent.extract_parameters(pattern_list, user_input)
|
157 |
-
for p in extracted:
|
158 |
-
if p["key"] == awaiting_variable:
|
159 |
-
session["variables"][awaiting_variable] = p["value"]
|
160 |
-
session["awaiting_variable"] = None
|
161 |
-
log(f"✅ Awaiting parametre tamamlandı: {awaiting_variable} = {p['value']}")
|
162 |
-
break
|
163 |
-
|
164 |
-
extracted = intent.extract_parameters(pattern_list, user_input)
|
165 |
-
variables = {p["key"]: p["value"] for p in extracted}
|
166 |
-
session.setdefault("variables", {}).update(variables)
|
167 |
-
|
168 |
-
is_valid, validation_errors = intent.validate_variable_formats(session["variables"], variable_format_map, data_formats)
|
169 |
-
if not is_valid:
|
170 |
-
session["awaiting_variable"] = list(validation_errors.keys())[0]
|
171 |
-
session_store[session_id] = session
|
172 |
-
app.state.session_store = session_store
|
173 |
-
return {"response": list(validation_errors.values())[0]}
|
174 |
-
|
175 |
-
expected_vars = list(variable_format_map.keys())
|
176 |
-
missing_vars = [v for v in expected_vars if v not in session["variables"]]
|
177 |
-
if missing_vars:
|
178 |
-
session["awaiting_variable"] = missing_vars[0]
|
179 |
-
session_store[session_id] = session
|
180 |
-
app.state.session_store = session_store
|
181 |
-
return {"response": f"Lütfen {missing_vars[0]} bilgisini belirtir misiniz?"}
|
182 |
-
|
183 |
-
result = intent.execute_intent(
|
184 |
-
detected_intent,
|
185 |
-
user_input,
|
186 |
-
session,
|
187 |
-
s_config.INTENT_DEFINITIONS,
|
188 |
-
s_config.DATA_FORMATS
|
189 |
-
)
|
190 |
-
if "reply" in result:
|
191 |
-
session_store[session_id] = result["session"]
|
192 |
-
app.state.session_store = session_store
|
193 |
-
return {"reply": result["reply"]}
|
194 |
-
elif "errors" in result:
|
195 |
-
session_store[session_id] = result["session"]
|
196 |
-
app.state.session_store = session_store
|
197 |
-
return {"response": list(result["errors"].values())[0]}
|
198 |
-
else:
|
199 |
-
return {"response": random.choice(s_config.FALLBACK_ANSWERS)}
|
200 |
-
|
201 |
-
session["awaiting_variable"] = None
|
202 |
-
session["variables"] = {}
|
203 |
-
response, response_conf = await llm_model.generate_response(user_input, s_config)
|
204 |
-
if response_conf is not None and response_conf < s_config.LLM_CONFIDENCE_THRESHOLD:
|
205 |
-
return {"response": random.choice(s_config.FALLBACK_ANSWERS)}
|
206 |
-
return {"response": response}
|
207 |
-
|
208 |
-
except Exception as e:
|
209 |
-
traceback.print_exc()
|
210 |
-
return JSONResponse(content={"error": str(e)}, status_code=500)
|
211 |
-
|
212 |
-
# 🧠 Model setup ve sunucu
|
213 |
threading.Thread(target=llm_model.setup_model, kwargs={"s_config": s_config}, daemon=True).start()
|
214 |
threading.Thread(target=lambda: uvicorn.run(app, host="0.0.0.0", port=7860), daemon=True).start()
|
215 |
-
|
216 |
-
# 🧘 Eğitim sonrası uygulama restart olmasın diye bekleme
|
217 |
-
log("⏸️ Eğitim tamamlandı. Servis bekleme modunda...")
|
218 |
while True:
|
219 |
-
time.sleep(60)
|
|
|
4 |
from transformers import AutoTokenizer, AutoModelForSequenceClassification
|
5 |
import intent_test_runner
|
6 |
from service_config import ServiceConfig
|
7 |
+
import intent, intent, llm_model
|
8 |
from log import log
|
9 |
+
from chat_handler import handle_chat
|
10 |
|
11 |
s_config = ServiceConfig()
|
12 |
s_config.setup_environment()
|
13 |
|
14 |
# === FastAPI
|
15 |
app = FastAPI()
|
16 |
+
chat_history = []
|
17 |
|
18 |
@app.get("/")
|
19 |
def health():
|
20 |
return {"status": "ok"}
|
21 |
|
22 |
+
import uuid # yukarıda zaten eklendiğini varsayıyoruz
|
23 |
+
|
24 |
@app.post("/run_tests", status_code=202)
|
25 |
def run_tests():
|
26 |
log("🚦 /run_tests çağrıldı. Testler başlatılıyor...")
|
|
|
29 |
|
30 |
@app.get("/start", response_class=HTMLResponse)
|
31 |
def root():
|
32 |
+
# Yeni session ID üret
|
33 |
session_id = str(uuid.uuid4())
|
34 |
session_info = {
|
35 |
"session_id": session_id,
|
|
|
38 |
"last_intent": None,
|
39 |
"awaiting_variable": None
|
40 |
}
|
41 |
+
|
42 |
+
# Session store başlatıldıysa ekle
|
43 |
if not hasattr(app.state, "session_store"):
|
44 |
app.state.session_store = {}
|
45 |
app.state.session_store[session_id] = session_info
|
46 |
+
|
47 |
log(f"🌐 /start ile yeni session başlatıldı: {session_id}")
|
48 |
+
|
49 |
+
# HTML + session_id gömülü
|
50 |
return f"""
|
51 |
<html><body>
|
52 |
<h2>Turkcell LLM Chat</h2>
|
|
|
78 |
def start_chat():
|
79 |
if llm_model.model is None or llm_model.tokenizer is None:
|
80 |
return {"error": "Model yüklenmedi."}
|
81 |
+
|
82 |
if not hasattr(app.state, "session_store"):
|
83 |
app.state.session_store = {}
|
84 |
+
|
85 |
session_id = str(uuid.uuid4())
|
86 |
session_info = {
|
87 |
"session_id": session_id,
|
|
|
115 |
|
116 |
@app.post("/chat")
|
117 |
async def chat(msg: llm_model.Message, request: Request):
|
118 |
+
return await handle_chat(msg, request, app, s_config)
|
|
|
119 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
120 |
threading.Thread(target=llm_model.setup_model, kwargs={"s_config": s_config}, daemon=True).start()
|
121 |
threading.Thread(target=lambda: uvicorn.run(app, host="0.0.0.0", port=7860), daemon=True).start()
|
|
|
|
|
|
|
122 |
while True:
|
123 |
+
time.sleep(60)
|