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 |
-
|
| 82 |
-
|
| 83 |
-
|
| 84 |
-
|
| 85 |
-
|
| 86 |
-
|
| 87 |
-
|
| 88 |
-
|
| 89 |
-
|
| 90 |
-
|
| 91 |
-
|
| 92 |
-
|
| 93 |
-
|
| 94 |
-
|
| 95 |
-
|
| 96 |
-
|
| 97 |
-
|
| 98 |
-
|
| 99 |
-
|
| 100 |
-
|
| 101 |
-
|
| 102 |
-
|
| 103 |
-
|
| 104 |
-
|
| 105 |
-
|
| 106 |
-
|
| 107 |
-
|
| 108 |
-
|
| 109 |
-
|
| 110 |
-
|
| 111 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 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():
|