Spaces:
Sleeping
Sleeping
BramVanroy
commited on
Update app.py
Browse files
app.py
CHANGED
@@ -0,0 +1,58 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
from transformers import pipeline
|
2 |
+
import gradio as gr
|
3 |
+
import spaces
|
4 |
+
import plotly.express as px
|
5 |
+
|
6 |
+
model_id = "BramVanroy/sonar-coarse-classification"
|
7 |
+
pipeline = pipeline("text-classification", model=model_id)
|
8 |
+
|
9 |
+
|
10 |
+
@spaces.GPU
|
11 |
+
def predict(text):
|
12 |
+
"""Perform text classification and generate results."""
|
13 |
+
outputs = pipeline(text, top_k=None)
|
14 |
+
# Sort outputs by label name
|
15 |
+
outputs = sorted(outputs, key=lambda item: item["label"])
|
16 |
+
print(outputs)
|
17 |
+
df = {
|
18 |
+
"label": [item["label"] for item in outputs],
|
19 |
+
"score": [item["score"] for item in outputs],
|
20 |
+
}
|
21 |
+
|
22 |
+
fig = px.bar(df, x="score", y="label", orientation='h')
|
23 |
+
fig.update_layout(bargap=0.9)
|
24 |
+
|
25 |
+
return fig
|
26 |
+
|
27 |
+
|
28 |
+
def create_app():
|
29 |
+
"""Create the Gradio app."""
|
30 |
+
|
31 |
+
with gr.Blocks() as demo:
|
32 |
+
gr.Markdown("# Text Classification App")
|
33 |
+
gr.Markdown(
|
34 |
+
"Paste your text below, and the app will classify it using the specified model."
|
35 |
+
)
|
36 |
+
|
37 |
+
with gr.Row():
|
38 |
+
input_text = gr.Textbox(
|
39 |
+
label="Input Text",
|
40 |
+
lines=5,
|
41 |
+
placeholder="Paste your text here...",
|
42 |
+
)
|
43 |
+
prediction_output = gr.Plot(label="Prediction Probabilities")
|
44 |
+
|
45 |
+
submit_button = gr.Button("Classify Text")
|
46 |
+
|
47 |
+
submit_button.click(
|
48 |
+
predict,
|
49 |
+
inputs=[input_text],
|
50 |
+
outputs=[prediction_output],
|
51 |
+
)
|
52 |
+
|
53 |
+
return demo
|
54 |
+
|
55 |
+
|
56 |
+
if __name__ == "__main__":
|
57 |
+
app = create_app()
|
58 |
+
app.launch()
|