Spaces:
Sleeping
Sleeping
albertmartinez
commited on
Commit
•
b8fd1ca
1
Parent(s):
7a175b1
Add application file
Browse files- app.py +75 -0
- requirements.txt +2 -0
app.py
ADDED
@@ -0,0 +1,75 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
import gradio as gr
|
2 |
+
from transformers import pipeline
|
3 |
+
|
4 |
+
# Define the models
|
5 |
+
model1 = pipeline("text-classification", model="albertmartinez/bert-sdg-classification")
|
6 |
+
model2 = pipeline("text-classification", model="albertmartinez/bert-multilingual-sdg-classification")
|
7 |
+
model3 = pipeline("text-classification", model="albertmartinez/distilbert-multilingual-sdg-classification")
|
8 |
+
model4 = pipeline("text-classification", model="albertmartinez/xlm-roberta-large-sdg-classification")
|
9 |
+
|
10 |
+
def classify_text(text, model):
|
11 |
+
result = model(text, top_k=16, truncation=True, max_length=512)
|
12 |
+
return {p["label"]: p["score"] for p in result}
|
13 |
+
|
14 |
+
def classify_all(text):
|
15 |
+
return [
|
16 |
+
{p["label"]: p["score"] for p in model1(text, top_k=16, truncation=True, max_length=512)},
|
17 |
+
{p["label"]: p["score"] for p in model2(text, top_k=16, truncation=True, max_length=512)},
|
18 |
+
{p["label"]: p["score"] for p in model3(text, top_k=16, truncation=True, max_length=512)},
|
19 |
+
{p["label"]: p["score"] for p in model4(text, top_k=16, truncation=True, max_length=512)}
|
20 |
+
]
|
21 |
+
|
22 |
+
|
23 |
+
ifaceall = gr.Interface(
|
24 |
+
fn=classify_all,
|
25 |
+
inputs=gr.Textbox(lines=2, label="Text", placeholder="Enter text here..."),
|
26 |
+
outputs=[gr.Label(label="bert"), gr.Label(label="bert-multilingual"), gr.Label(label="distilbert-multilingual"), gr.Label(label="xlm-roberta-large")],
|
27 |
+
title="",
|
28 |
+
description="Enter a text and see the text classification result!"
|
29 |
+
)
|
30 |
+
|
31 |
+
# Interface for the first model
|
32 |
+
iface1 = gr.Interface(
|
33 |
+
fn=lambda text: classify_text(text, model1),
|
34 |
+
inputs=gr.Textbox(lines=2, label="Text", placeholder="Enter text here..."),
|
35 |
+
outputs=gr.Label(label="Top SDG Predicted"),
|
36 |
+
title="albertmartinez/bert-sdg-classification",
|
37 |
+
description="Enter a text and see the text classification result!"
|
38 |
+
)
|
39 |
+
|
40 |
+
# Interface for the second model
|
41 |
+
iface2 = gr.Interface(
|
42 |
+
fn=lambda text: classify_text(text, model2),
|
43 |
+
inputs=gr.Textbox(lines=2, label="Text", placeholder="Enter text here..."),
|
44 |
+
outputs=gr.Label(label="Top SDG Predicted"),
|
45 |
+
title="albertmartinez/bert-multilingual-sdg-classification",
|
46 |
+
description="Enter a text and see the text classification result!"
|
47 |
+
)
|
48 |
+
|
49 |
+
# Interface for the three model
|
50 |
+
iface3 = gr.Interface(
|
51 |
+
fn=lambda text: classify_text(text, model3),
|
52 |
+
inputs=gr.Textbox(lines=2, label="Text", placeholder="Enter text here..."),
|
53 |
+
outputs=gr.Label(label="Top SDG Predicted"),
|
54 |
+
title="albertmartinez/distilbert-multilingual-sdg-classification",
|
55 |
+
description="Enter a text and see the text classification result!"
|
56 |
+
)
|
57 |
+
|
58 |
+
# Interface for the four model
|
59 |
+
iface4 = gr.Interface(
|
60 |
+
fn=lambda text: classify_text(text, model4),
|
61 |
+
inputs=gr.Textbox(lines=2, label="Text", placeholder="Enter text here..."),
|
62 |
+
outputs=gr.Label(label="Top SDG Predicted"),
|
63 |
+
title="albertmartinez/xlm-roberta-large-sdg-classification",
|
64 |
+
description="Enter a text and see the text classification result!"
|
65 |
+
)
|
66 |
+
|
67 |
+
# Combine both interfaces into a tabbed interface
|
68 |
+
io = gr.TabbedInterface(
|
69 |
+
interface_list=[ifaceall, iface1, iface2, iface3, iface4],
|
70 |
+
tab_names=["ALL", "bert-sdg-classification", "bert-multilingual-sdg-classification", "distilbert-multilingual-sdg-classification", "xlm-roberta-large-sdg-classification"],
|
71 |
+
title="Sustainable Development Goals (SDG) Text Classifier App",
|
72 |
+
theme='base'
|
73 |
+
)
|
74 |
+
|
75 |
+
io.launch()
|
requirements.txt
ADDED
@@ -0,0 +1,2 @@
|
|
|
|
|
|
|
1 |
+
transformers
|
2 |
+
torch
|