heeba001 commited on
Commit
5452188
·
verified ·
1 Parent(s): d896373

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +12 -9
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
- def predict_topic(text):
8
- topics, probs = topic_model.transform([text])
9
- topic_id = int(topics[0])
10
- confidence = float(probs[0]) if probs is not None else 0.0
11
 
12
  if topic_id == -1:
13
  return {
14
  "topic_id": -1,
15
  "topic_name": "Other",
16
- "keywords": [],
17
- "confidence": confidence
18
  }
19
 
20
- topic_info = topic_model.get_topic_info(topic_id)
21
  topic_name = topic_info["Name"].values[0]
22
 
23
- keywords = [word for word, _ in topic_model.get_topic(topic_id)][:5]
 
 
 
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,