Commit
·
5c0db7c
1
Parent(s):
7a175b1
Add application file
Browse files- README.md +1 -1
- app.py +92 -0
- requirements.txt +3 -0
README.md
CHANGED
|
@@ -4,7 +4,7 @@ emoji: ⚡
|
|
| 4 |
colorFrom: yellow
|
| 5 |
colorTo: indigo
|
| 6 |
sdk: gradio
|
| 7 |
-
sdk_version:
|
| 8 |
app_file: app.py
|
| 9 |
pinned: false
|
| 10 |
license: mit
|
|
|
|
| 4 |
colorFrom: yellow
|
| 5 |
colorTo: indigo
|
| 6 |
sdk: gradio
|
| 7 |
+
sdk_version: 5.3.0
|
| 8 |
app_file: app.py
|
| 9 |
pinned: false
|
| 10 |
license: mit
|
app.py
ADDED
|
@@ -0,0 +1,92 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 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 |
+
|
| 11 |
+
def classify_text(text, model):
|
| 12 |
+
result = model(text, top_k=16, truncation=True, max_length=512)
|
| 13 |
+
return {p["label"]: p["score"] for p in result}
|
| 14 |
+
|
| 15 |
+
|
| 16 |
+
def classify_all(text):
|
| 17 |
+
return [
|
| 18 |
+
{p["label"]: p["score"] for p in model1(text, top_k=16, truncation=True, max_length=512)},
|
| 19 |
+
{p["label"]: p["score"] for p in model2(text, top_k=16, truncation=True, max_length=512)},
|
| 20 |
+
{p["label"]: p["score"] for p in model3(text, top_k=16, truncation=True, max_length=512)},
|
| 21 |
+
{p["label"]: p["score"] for p in model4(text, top_k=16, truncation=True, max_length=512)}
|
| 22 |
+
]
|
| 23 |
+
|
| 24 |
+
|
| 25 |
+
ifaceall = gr.Interface(
|
| 26 |
+
fn=classify_all,
|
| 27 |
+
inputs=gr.Textbox(lines=2, label="Text", placeholder="Enter text here..."),
|
| 28 |
+
outputs=[gr.Label(label="bert"), gr.Label(label="bert-multilingual"), gr.Label(label="distilbert-multilingual"),
|
| 29 |
+
gr.Label(label="xlm-roberta-large")],
|
| 30 |
+
title="SDG text classification",
|
| 31 |
+
description="Enter a text and see the text classification result!",
|
| 32 |
+
flagging_mode="never",
|
| 33 |
+
api_name="classify_all"
|
| 34 |
+
)
|
| 35 |
+
|
| 36 |
+
# Interface for the first model
|
| 37 |
+
iface1 = gr.Interface(
|
| 38 |
+
fn=lambda text: classify_text(text, model1),
|
| 39 |
+
inputs=gr.Textbox(lines=2, label="Text", placeholder="Enter text here..."),
|
| 40 |
+
outputs=gr.Label(label="Top SDG Predicted"),
|
| 41 |
+
title="BERT SDG classification",
|
| 42 |
+
description="Enter a text and see the text classification result!",
|
| 43 |
+
flagging_mode="never",
|
| 44 |
+
api_name="classify_bert"
|
| 45 |
+
)
|
| 46 |
+
|
| 47 |
+
# Interface for the second model
|
| 48 |
+
iface2 = gr.Interface(
|
| 49 |
+
fn=lambda text: classify_text(text, model2),
|
| 50 |
+
inputs=gr.Textbox(lines=2, label="Text", placeholder="Enter text here..."),
|
| 51 |
+
outputs=gr.Label(label="Top SDG Predicted"),
|
| 52 |
+
title="BERT multilingual SDG classification",
|
| 53 |
+
description="Enter a text and see the text classification result!",
|
| 54 |
+
flagging_mode="never",
|
| 55 |
+
api_name="classify_bert-multilingual"
|
| 56 |
+
)
|
| 57 |
+
|
| 58 |
+
# Interface for the three model
|
| 59 |
+
iface3 = gr.Interface(
|
| 60 |
+
fn=lambda text: classify_text(text, model3),
|
| 61 |
+
inputs=gr.Textbox(lines=2, label="Text", placeholder="Enter text here..."),
|
| 62 |
+
outputs=gr.Label(label="Top SDG Predicted"),
|
| 63 |
+
title="DISTILBERT multilingual SDG classification",
|
| 64 |
+
description="Enter a text and see the text classification result!",
|
| 65 |
+
flagging_mode="never",
|
| 66 |
+
api_name="classify_distilbert-multilingual"
|
| 67 |
+
)
|
| 68 |
+
|
| 69 |
+
# Interface for the four model
|
| 70 |
+
iface4 = gr.Interface(
|
| 71 |
+
fn=lambda text: classify_text(text, model4),
|
| 72 |
+
inputs=gr.Textbox(lines=2, label="Text", placeholder="Enter text here..."),
|
| 73 |
+
outputs=gr.Label(label="Top SDG Predicted"),
|
| 74 |
+
title="XLM-ROBERTA-LARGE SDG classification",
|
| 75 |
+
description="Enter a text and see the text classification result!",
|
| 76 |
+
flagging_mode="never",
|
| 77 |
+
api_name="classify_xlm-roberta-large"
|
| 78 |
+
)
|
| 79 |
+
|
| 80 |
+
with gr.Blocks() as demo:
|
| 81 |
+
# Combine both interfaces into a tabbed interface
|
| 82 |
+
gr.TabbedInterface(
|
| 83 |
+
interface_list=[ifaceall, iface1, iface2, iface3, iface4],
|
| 84 |
+
tab_names=["ALL", "bert-sdg-classification", "bert-multilingual-sdg-classification",
|
| 85 |
+
"distilbert-multilingual-sdg-classification", "xlm-roberta-large-sdg-classification"],
|
| 86 |
+
title="Sustainable Development Goals (SDG) Text Classifier App",
|
| 87 |
+
theme='base'
|
| 88 |
+
)
|
| 89 |
+
|
| 90 |
+
if __name__ == "__main__":
|
| 91 |
+
print(gr.__version__)
|
| 92 |
+
demo.launch()
|
requirements.txt
ADDED
|
@@ -0,0 +1,3 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
transformers
|
| 2 |
+
torch
|
| 3 |
+
gradio
|