Commit
·
f7a530f
1
Parent(s):
64f6827
fix: predict intent
Browse files
main.py
CHANGED
@@ -26,23 +26,26 @@ except Exception as e:
|
|
26 |
text_classifier = None
|
27 |
|
28 |
|
29 |
-
|
30 |
-
|
31 |
@app.get("/")
|
32 |
def hello():
|
33 |
-
return {"Message":"Space is running Good.",
|
34 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
35 |
# Prediction Endpoint
|
36 |
@app.post("/predict")
|
37 |
-
def predict_intent(request):
|
38 |
if text_classifier is None:
|
39 |
return {"error": "Model not found"}
|
40 |
-
sentence=request.data.get("sentence", "").strip()
|
41 |
-
preds_list = text_classifier(sentence)
|
42 |
-
print(preds_list)
|
43 |
-
best_pred = max(preds_list[0], key=lambda x: x["score"]) # Get highest-scoring intent
|
44 |
-
return {"intent": best_pred["label"], "confidence": best_pred["score"]}
|
45 |
-
|
46 |
|
|
|
47 |
|
|
|
|
|
48 |
|
|
|
|
26 |
text_classifier = None
|
27 |
|
28 |
|
29 |
+
# Health Check Endpoint
|
|
|
30 |
@app.get("/")
|
31 |
def hello():
|
32 |
+
return {"Message": "Space is running Good.", "Status": "Healthy"}
|
33 |
+
|
34 |
+
|
35 |
+
# Define Pydantic Model for Input Validation
|
36 |
+
class PredictionRequest(BaseModel):
|
37 |
+
sentence: str
|
38 |
+
|
39 |
+
|
40 |
# Prediction Endpoint
|
41 |
@app.post("/predict")
|
42 |
+
def predict_intent(request: PredictionRequest):
|
43 |
if text_classifier is None:
|
44 |
return {"error": "Model not found"}
|
|
|
|
|
|
|
|
|
|
|
|
|
45 |
|
46 |
+
sentence = request.sentence.strip() # Correct way to get JSON input
|
47 |
|
48 |
+
preds_list = text_classifier(sentence)
|
49 |
+
best_pred = max(preds_list[0], key=lambda x: x["score"]) # Get highest-scoring intent
|
50 |
|
51 |
+
return {"intent": best_pred["label"], "confidence": best_pred["score"]}
|