Create app.py
Browse files
app.py
ADDED
|
@@ -0,0 +1,32 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import gradio as gr
|
| 2 |
+
from transformers import pipeline
|
| 3 |
+
|
| 4 |
+
# Load multi-class topic classification pipeline
|
| 5 |
+
topic_pipeline = pipeline(
|
| 6 |
+
"text-classification",
|
| 7 |
+
model="AfroLogicInsect/topic-model-analysis-model",
|
| 8 |
+
tokenizer="AfroLogicInsect/topic-model-analysis-model",
|
| 9 |
+
return_all_scores=True
|
| 10 |
+
)
|
| 11 |
+
|
| 12 |
+
def predict_topics(text):
|
| 13 |
+
if not text.strip():
|
| 14 |
+
return [["Please enter some text", 0.0]]
|
| 15 |
+
|
| 16 |
+
results = topic_pipeline(text)
|
| 17 |
+
sorted_results = sorted(results[0], key=lambda x: x['score'], reverse=True)[:5]
|
| 18 |
+
|
| 19 |
+
# Format for Gradio output: list of [label, score]
|
| 20 |
+
return [[res['label'], round(res['score'], 3)] for res in sorted_results]
|
| 21 |
+
|
| 22 |
+
iface = gr.Interface(
|
| 23 |
+
fn=predict_topics,
|
| 24 |
+
inputs=gr.Textbox(label="Enter text"),
|
| 25 |
+
outputs=gr.Dataframe(
|
| 26 |
+
headers=["Topic", "Confidence"],
|
| 27 |
+
label="Top 5 Predicted Topics",
|
| 28 |
+
type="array"
|
| 29 |
+
)
|
| 30 |
+
)
|
| 31 |
+
|
| 32 |
+
iface.launch(share=True)
|