Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
|
@@ -4,31 +4,34 @@ import gradio as gr
|
|
| 4 |
# Load trained topic model
|
| 5 |
topic_model = BERTopic.load("topic_model")
|
| 6 |
|
| 7 |
-
|
| 8 |
-
|
| 9 |
-
|
| 10 |
-
|
| 11 |
|
| 12 |
if topic_id == -1:
|
| 13 |
return {
|
| 14 |
"topic_id": -1,
|
| 15 |
"topic_name": "Other",
|
| 16 |
-
"
|
| 17 |
-
"confidence": confidence
|
| 18 |
}
|
| 19 |
|
| 20 |
-
topic_info =
|
| 21 |
topic_name = topic_info["Name"].values[0]
|
| 22 |
|
| 23 |
-
|
|
|
|
|
|
|
|
|
|
| 24 |
|
| 25 |
return {
|
| 26 |
"topic_id": topic_id,
|
| 27 |
"topic_name": topic_name,
|
| 28 |
-
"keywords": keywords,
|
| 29 |
"confidence": confidence
|
| 30 |
}
|
| 31 |
|
|
|
|
| 32 |
# Gradio UI
|
| 33 |
iface = gr.Interface(
|
| 34 |
fn=predict_topic,
|
|
|
|
| 4 |
# Load trained topic model
|
| 5 |
topic_model = BERTopic.load("topic_model")
|
| 6 |
|
| 7 |
+
@app.post("/predict")
|
| 8 |
+
def predict_topic(input: TextInput):
|
| 9 |
+
topic, probs = model.transform([input.text], calculate_probabilities=True)
|
| 10 |
+
topic_id = int(topic[0])
|
| 11 |
|
| 12 |
if topic_id == -1:
|
| 13 |
return {
|
| 14 |
"topic_id": -1,
|
| 15 |
"topic_name": "Other",
|
| 16 |
+
"confidence": 0.0
|
|
|
|
| 17 |
}
|
| 18 |
|
| 19 |
+
topic_info = model.get_topic_info(topic_id)
|
| 20 |
topic_name = topic_info["Name"].values[0]
|
| 21 |
|
| 22 |
+
# Probability for the predicted topic
|
| 23 |
+
confidence = float(probs[0][topic_id])
|
| 24 |
+
|
| 25 |
+
keywords = [w for w, _ in model.get_topic(topic_id)]
|
| 26 |
|
| 27 |
return {
|
| 28 |
"topic_id": topic_id,
|
| 29 |
"topic_name": topic_name,
|
| 30 |
+
"keywords": keywords[:5],
|
| 31 |
"confidence": confidence
|
| 32 |
}
|
| 33 |
|
| 34 |
+
|
| 35 |
# Gradio UI
|
| 36 |
iface = gr.Interface(
|
| 37 |
fn=predict_topic,
|