Spaces:
Runtime error
Runtime error
Create app.py
Browse files
app.py
ADDED
@@ -0,0 +1,32 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
import gradio as gr
|
2 |
+
|
3 |
+
from transformers import AutoTokenizer, AutoModelForSequenceClassification, pipeline
|
4 |
+
|
5 |
+
tokenizer = AutoTokenizer.from_pretrained("ua-l/topics-classifier")
|
6 |
+
model = AutoModelForSequenceClassification.from_pretrained(
|
7 |
+
"ua-l/topics-classifier"
|
8 |
+
)
|
9 |
+
|
10 |
+
topic_classifier = pipeline(
|
11 |
+
task="text-classification", model=model, tokenizer=tokenizer, device="cpu", top_k=5
|
12 |
+
)
|
13 |
+
|
14 |
+
def predict(question):
|
15 |
+
predictions = topic_classifier(question)
|
16 |
+
|
17 |
+
topics = []
|
18 |
+
for prediction in predictions[0]:
|
19 |
+
label = prediction["label"]
|
20 |
+
probability = round(prediction["score"] * 100, 2)
|
21 |
+
|
22 |
+
topics.append(
|
23 |
+
{
|
24 |
+
"label": label,
|
25 |
+
"probability": probability,
|
26 |
+
}
|
27 |
+
)
|
28 |
+
|
29 |
+
return str(topics)
|
30 |
+
|
31 |
+
demo = gr.Interface(fn=predict, inputs="text", outputs="text")
|
32 |
+
demo.launch()
|