SCANSKY commited on
Commit
a963050
·
verified ·
1 Parent(s): b8f2fb6

Update handler.py

Browse files
Files changed (1) hide show
  1. handler.py +8 -3
handler.py CHANGED
@@ -43,7 +43,12 @@ class EndpointHandler:
43
 
44
  # Rank topics by their contribution in descending order
45
  ranked_topics = sorted(enumerate(doc_topic_distribution), key=lambda x: x[1], reverse=True)[:10]
46
-
 
 
 
 
 
47
  # Prepare the results
48
  results = []
49
  for topic, prob in zip(topics, probabilities):
@@ -57,14 +62,14 @@ class EndpointHandler:
57
  custom_label = f"Topic {topic}" # Fallback label
58
 
59
  # Get the contribution from approximate distribution
60
- contribution = next((contribution for idx, contribution in ranked_topics if idx == topic), 0.0)
61
 
62
  results.append({
63
  "topic": int(topic),
64
  "probability": float(prob),
65
  "top_words": topic_words[:5], # Top 5 words
66
  "customLabel": custom_label, # Add custom label
67
- "contribution": ranked_topics
68
  })
69
 
70
  return results
 
43
 
44
  # Rank topics by their contribution in descending order
45
  ranked_topics = sorted(enumerate(doc_topic_distribution), key=lambda x: x[1], reverse=True)[:10]
46
+ customlabels = self.topic_model.custom_labels_
47
+
48
+ distributedtopics = [
49
+ [f"Topic {topic_idx}", customlabels[topic_idx + 1], f"{contribution * 100}%"]
50
+ for topic_idx, contribution in ranked_topics
51
+ ]
52
  # Prepare the results
53
  results = []
54
  for topic, prob in zip(topics, probabilities):
 
62
  custom_label = f"Topic {topic}" # Fallback label
63
 
64
  # Get the contribution from approximate distribution
65
+ # contribution = next((contribution for idx, contribution in ranked_topics if idx == topic), 0.0)
66
 
67
  results.append({
68
  "topic": int(topic),
69
  "probability": float(prob),
70
  "top_words": topic_words[:5], # Top 5 words
71
  "customLabel": custom_label, # Add custom label
72
+ "contribution": distributedtopics
73
  })
74
 
75
  return results