ciyidogan commited on
Commit
8ecf4a5
·
verified ·
1 Parent(s): b723b80

Update inference_test.py

Browse files
Files changed (1) hide show
  1. inference_test.py +17 -17
inference_test.py CHANGED
@@ -3,11 +3,11 @@ from fastapi import FastAPI, Request
3
  from fastapi.responses import HTMLResponse, JSONResponse
4
  from transformers import AutoTokenizer, AutoModelForSequenceClassification
5
  import intent_test_runner
6
- from app_config import AppConfig
7
  import intent, log, intent, llm_model
8
 
9
- app_config = AppConfig()
10
- app_config.setup_environment()
11
 
12
  # === FastAPI
13
  app = FastAPI()
@@ -95,15 +95,15 @@ def train_intents(train_input: intent.TrainInput):
95
  log("📥 POST /train_intents çağrıldı.")
96
  intents = train_input.intents
97
  intent.INTENT_DEFINITIONS = {intent["name"]: intent for intent in intents}
98
- threading.Thread(target=lambda: intent.background_training(intents, app_config), daemon=True).start()
99
  return {"status": "accepted", "message": "Intent eğitimi arka planda başlatıldı."}
100
 
101
  @app.post("/load_intent_model")
102
  def load_intent_model():
103
  try:
104
- intent.INTENT_TOKENIZER = AutoTokenizer.from_pretrained(app_config.INTENT_MODEL_PATH)
105
- intent.INTENT_MODEL = AutoModelForSequenceClassification.from_pretrained(app_config.INTENT_MODEL_PATH)
106
- with open(os.path.join(app_config.INTENT_MODEL_PATH, "label2id.json")) as f:
107
  intent.LABEL2ID = json.load(f)
108
  return {"status": "ok", "message": "Intent modeli yüklendi."}
109
  except Exception as e:
@@ -129,12 +129,12 @@ async def chat(msg: llm_model.Message, request: Request):
129
  if llm_model.model is None or llm_model.tokenizer is None:
130
  return {"error": "Model yüklenmedi."}
131
 
132
- if app_config.INTENT_MODEL:
133
  intent_task = asyncio.create_task(intent.detect_intent(user_input))
134
- response_task = asyncio.create_task(llm_model.generate_response(user_input, app_config))
135
  intent, intent_conf = await intent_task
136
  log(f"🎯 Intent: {intent} (conf={intent_conf:.2f})")
137
- if intent_conf > app_config.INTENT_CONFIDENCE_THRESHOLD and intent in app_config.INTENT_DEFINITIONS:
138
  result = intent.execute_intent(intent, user_input, session)
139
  if "reply" in result:
140
  session_store[session_id] = result["session"]
@@ -145,22 +145,22 @@ async def chat(msg: llm_model.Message, request: Request):
145
  app.state.session_store = session_store
146
  return {"response": list(result["errors"].values())[0]}
147
  else:
148
- return {"response": random.choice(app_config.FALLBACK_ANSWERS)}
149
  else:
150
  response, response_conf = await response_task
151
- if response_conf is not None and response_conf < app_config.LLM_CONFIDENCE_THRESHOLD:
152
- return {"response": random.choice(app_config.FALLBACK_ANSWERS)}
153
  return {"response": response}
154
  else:
155
- response, response_conf = await llm_model.generate_response(user_input, app_config)
156
- if response_conf is not None and response_conf < app_config.LLM_CONFIDENCE_THRESHOLD:
157
- return {"response": random.choice(app_config.FALLBACK_ANSWERS)}
158
  return {"response": response}
159
  except Exception as e:
160
  traceback.print_exc()
161
  return JSONResponse(content={"error": str(e)}, status_code=500)
162
 
163
- threading.Thread(target=llm_model.setup_model, kwargs={"app_config": app_config}, daemon=True).start()
164
  threading.Thread(target=lambda: uvicorn.run(app, host="0.0.0.0", port=7860), daemon=True).start()
165
  while True:
166
  time.sleep(60)
 
3
  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, log, intent, llm_model
8
 
9
+ s_config = ServiceConfig()
10
+ s_config.setup_environment()
11
 
12
  # === FastAPI
13
  app = FastAPI()
 
95
  log("📥 POST /train_intents çağrıldı.")
96
  intents = train_input.intents
97
  intent.INTENT_DEFINITIONS = {intent["name"]: intent for intent in intents}
98
+ threading.Thread(target=lambda: intent.background_training(intents, s_config), daemon=True).start()
99
  return {"status": "accepted", "message": "Intent eğitimi arka planda başlatıldı."}
100
 
101
  @app.post("/load_intent_model")
102
  def load_intent_model():
103
  try:
104
+ intent.INTENT_TOKENIZER = AutoTokenizer.from_pretrained(s_config.INTENT_MODEL_PATH)
105
+ intent.INTENT_MODEL = AutoModelForSequenceClassification.from_pretrained(s_config.INTENT_MODEL_PATH)
106
+ with open(os.path.join(s_config.INTENT_MODEL_PATH, "label2id.json")) as f:
107
  intent.LABEL2ID = json.load(f)
108
  return {"status": "ok", "message": "Intent modeli yüklendi."}
109
  except Exception as e:
 
129
  if llm_model.model is None or llm_model.tokenizer is None:
130
  return {"error": "Model yüklenmedi."}
131
 
132
+ if s_config.INTENT_MODEL:
133
  intent_task = asyncio.create_task(intent.detect_intent(user_input))
134
+ response_task = asyncio.create_task(llm_model.generate_response(user_input, s_config))
135
  intent, intent_conf = await intent_task
136
  log(f"🎯 Intent: {intent} (conf={intent_conf:.2f})")
137
+ if intent_conf > s_config.INTENT_CONFIDENCE_THRESHOLD and intent in s_config.INTENT_DEFINITIONS:
138
  result = intent.execute_intent(intent, user_input, session)
139
  if "reply" in result:
140
  session_store[session_id] = result["session"]
 
145
  app.state.session_store = session_store
146
  return {"response": list(result["errors"].values())[0]}
147
  else:
148
+ return {"response": random.choice(s_config.FALLBACK_ANSWERS)}
149
  else:
150
  response, response_conf = await response_task
151
+ if response_conf is not None and response_conf < s_config.LLM_CONFIDENCE_THRESHOLD:
152
+ return {"response": random.choice(s_config.FALLBACK_ANSWERS)}
153
  return {"response": response}
154
  else:
155
+ response, response_conf = await llm_model.generate_response(user_input, s_config)
156
+ if response_conf is not None and response_conf < s_config.LLM_CONFIDENCE_THRESHOLD:
157
+ return {"response": random.choice(s_config.FALLBACK_ANSWERS)}
158
  return {"response": response}
159
  except Exception as e:
160
  traceback.print_exc()
161
  return JSONResponse(content={"error": str(e)}, status_code=500)
162
 
163
+ threading.Thread(target=llm_model.setup_model, kwargs={"service_config": s_config}, daemon=True).start()
164
  threading.Thread(target=lambda: uvicorn.run(app, host="0.0.0.0", port=7860), daemon=True).start()
165
  while True:
166
  time.sleep(60)