Update handler.py
Browse files- handler.py +6 -12
handler.py
CHANGED
|
@@ -33,17 +33,15 @@ class EndpointHandler:
|
|
| 33 |
# Perform topic inference
|
| 34 |
topics, probabilities = self.topic_model.transform(docs)
|
| 35 |
|
| 36 |
-
# Get topic information
|
| 37 |
-
topics_info = self.topic_model.get_topics()
|
| 38 |
-
|
| 39 |
# Prepare the results
|
| 40 |
results = []
|
| 41 |
for topic, prob in zip(topics, probabilities):
|
| 42 |
-
|
|
|
|
| 43 |
results.append({
|
| 44 |
"topic": int(topic),
|
| 45 |
"probability": float(prob),
|
| 46 |
-
"top_words":
|
| 47 |
})
|
| 48 |
|
| 49 |
return results
|
|
@@ -52,11 +50,9 @@ class EndpointHandler:
|
|
| 52 |
|
| 53 |
def postprocess(self, results):
|
| 54 |
"""
|
| 55 |
-
Postprocess the inference results into a JSON
|
| 56 |
"""
|
| 57 |
-
return
|
| 58 |
-
"results": results
|
| 59 |
-
}
|
| 60 |
|
| 61 |
def __call__(self, data):
|
| 62 |
"""
|
|
@@ -74,6 +70,4 @@ class EndpointHandler:
|
|
| 74 |
|
| 75 |
return response
|
| 76 |
except Exception as e:
|
| 77 |
-
return {
|
| 78 |
-
"error": str(e)
|
| 79 |
-
}
|
|
|
|
| 33 |
# Perform topic inference
|
| 34 |
topics, probabilities = self.topic_model.transform(docs)
|
| 35 |
|
|
|
|
|
|
|
|
|
|
| 36 |
# Prepare the results
|
| 37 |
results = []
|
| 38 |
for topic, prob in zip(topics, probabilities):
|
| 39 |
+
topic_info = self.topic_model.get_topic(topic)
|
| 40 |
+
topic_words = [word for word, _ in topic_info] if topic_info else []
|
| 41 |
results.append({
|
| 42 |
"topic": int(topic),
|
| 43 |
"probability": float(prob),
|
| 44 |
+
"top_words": topic_words[:5] # Top 5 words
|
| 45 |
})
|
| 46 |
|
| 47 |
return results
|
|
|
|
| 50 |
|
| 51 |
def postprocess(self, results):
|
| 52 |
"""
|
| 53 |
+
Postprocess the inference results into a JSON-serializable list.
|
| 54 |
"""
|
| 55 |
+
return results # Directly returning the list of results
|
|
|
|
|
|
|
| 56 |
|
| 57 |
def __call__(self, data):
|
| 58 |
"""
|
|
|
|
| 70 |
|
| 71 |
return response
|
| 72 |
except Exception as e:
|
| 73 |
+
return [{"error": str(e)}] # Returning error as a list with a dictionary
|
|
|
|
|
|