ciyidogan commited on
Commit
07ed05b
·
verified ·
1 Parent(s): 8198cca

Update inference_test_turkcell_with_intents.py

Browse files
inference_test_turkcell_with_intents.py CHANGED
@@ -75,40 +75,48 @@ def root():
75
  </body></html>
76
  """
77
 
78
- @app.post("/train_intents")
79
  def train_intents(train_input: TrainInput):
80
  global INTENT_DEFINITIONS
81
- try:
82
- intents = train_input.intents
83
- INTENT_DEFINITIONS = {intent["name"]: intent for intent in intents}
84
- texts, labels, label2id = [], [], {}
85
- for idx, intent in enumerate(intents):
86
- label2id[intent["name"]] = idx
87
- for ex in intent["examples"]:
88
- texts.append(ex)
89
- labels.append(idx)
90
-
91
- dataset = Dataset.from_dict({"text": texts, "label": labels})
92
- tokenizer = AutoTokenizer.from_pretrained(INTENT_MODEL_ID)
93
- model = AutoModelForSequenceClassification.from_pretrained(INTENT_MODEL_ID, num_labels=len(label2id))
94
-
95
- def tokenize(batch):
96
- return tokenizer(batch["text"], truncation=True, padding=True)
97
-
98
- tokenized = dataset.map(tokenize, batched=True)
99
- args = TrainingArguments("./intent_train_output", per_device_train_batch_size=4, num_train_epochs=3, logging_steps=10, save_strategy="no", report_to=[])
100
- trainer = Trainer(model=model, args=args, train_dataset=tokenized)
101
- trainer.train()
102
-
103
- if os.path.exists(INTENT_MODEL_PATH): shutil.rmtree(INTENT_MODEL_PATH)
104
- model.save_pretrained(INTENT_MODEL_PATH)
105
- tokenizer.save_pretrained(INTENT_MODEL_PATH)
106
- with open(os.path.join(INTENT_MODEL_PATH, "label2id.json"), "w") as f:
107
- json.dump(label2id, f)
108
-
109
- return {"status": "ok", "message": "Intent modeli eğitildi."}
110
- except Exception as e:
111
- return JSONResponse(content={"error": str(e)}, status_code=500)
 
 
 
 
 
 
 
 
112
 
113
  @app.post("/load_intent_model")
114
  def load_intent_model():
 
75
  </body></html>
76
  """
77
 
78
+ @app.post("/train_intents", status_code=202)
79
  def train_intents(train_input: TrainInput):
80
  global INTENT_DEFINITIONS
81
+ log("📥 POST /train_intents çağrıldı.")
82
+ intents = train_input.intents
83
+ INTENT_DEFINITIONS = {intent["name"]: intent for intent in intents}
84
+
85
+ def background_training():
86
+ try:
87
+ log("🔧 Intent eğitimi başlatıldı...")
88
+ texts, labels, label2id = [], [], {}
89
+ for idx, intent in enumerate(intents):
90
+ label2id[intent["name"]] = idx
91
+ for ex in intent["examples"]:
92
+ texts.append(ex)
93
+ labels.append(idx)
94
+
95
+ dataset = Dataset.from_dict({"text": texts, "label": labels})
96
+ tokenizer = AutoTokenizer.from_pretrained(INTENT_MODEL_ID)
97
+ model = AutoModelForSequenceClassification.from_pretrained(INTENT_MODEL_ID, num_labels=len(label2id))
98
+
99
+ def tokenize(batch):
100
+ return tokenizer(batch["text"], truncation=True, padding=True)
101
+
102
+ tokenized = dataset.map(tokenize, batched=True)
103
+ args = TrainingArguments("./intent_train_output", per_device_train_batch_size=4, num_train_epochs=3, logging_steps=10, save_strategy="no", report_to=[])
104
+ trainer = Trainer(model=model, args=args, train_dataset=tokenized)
105
+ trainer.train()
106
+
107
+ if os.path.exists(INTENT_MODEL_PATH): shutil.rmtree(INTENT_MODEL_PATH)
108
+ model.save_pretrained(INTENT_MODEL_PATH)
109
+ tokenizer.save_pretrained(INTENT_MODEL_PATH)
110
+ with open(os.path.join(INTENT_MODEL_PATH, "label2id.json"), "w") as f:
111
+ json.dump(label2id, f)
112
+
113
+ log("✅ Intent eğitimi tamamlandı ve model kaydedildi.")
114
+ except Exception as e:
115
+ log(f"❌ Intent eğitimi hatası: {e}")
116
+ traceback.print_exc()
117
+
118
+ threading.Thread(target=background_training, daemon=True).start()
119
+ return {"status": "accepted", "message": "Intent eğitimi arka planda başlatıldı."}
120
 
121
  @app.post("/load_intent_model")
122
  def load_intent_model():