Spaces:
Sleeping
Sleeping
Upload app.py
Browse files
app.py
ADDED
|
@@ -0,0 +1,55 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
|
| 2 |
+
import gradio as gr
|
| 3 |
+
from transformers import pipeline
|
| 4 |
+
import pandas as pd
|
| 5 |
+
import matplotlib.pyplot as plt
|
| 6 |
+
import tempfile
|
| 7 |
+
|
| 8 |
+
# Initialize the zero-shot classification pipeline
|
| 9 |
+
classifier = pipeline("zero-shot-classification", model="facebook/bart-large-mnli")
|
| 10 |
+
|
| 11 |
+
|
| 12 |
+
|
| 13 |
+
# Define the classification function
|
| 14 |
+
def classify_text(document, labels):
|
| 15 |
+
candidate_labels = labels.split(", ")
|
| 16 |
+
res = classifier(document, candidate_labels=candidate_labels, multi_label=False)
|
| 17 |
+
df = pd.DataFrame(res)
|
| 18 |
+
|
| 19 |
+
# Create a temporary file to save the plot
|
| 20 |
+
with tempfile.NamedTemporaryFile(delete=False, suffix=".png") as tmpfile:
|
| 21 |
+
df.plot.bar(x='labels', y='scores', legend=False)
|
| 22 |
+
plt.title("Classification Results")
|
| 23 |
+
plt.xlabel("Labels")
|
| 24 |
+
plt.ylabel("Scores")
|
| 25 |
+
plt.tight_layout()
|
| 26 |
+
plt.savefig(tmpfile.name)
|
| 27 |
+
plt.close()
|
| 28 |
+
|
| 29 |
+
return df, tmpfile.name
|
| 30 |
+
|
| 31 |
+
# Define the example inputs and outputs
|
| 32 |
+
examples = [
|
| 33 |
+
["It was about eleven o’clock in the morning, mid October, with the sun not shining and a look of hard wet rain in the clearness of the foothills. I was wearing my powder-blue suit, with dark blue shirt, tie and display handkerchief, black brogues, black wool socks with dark blue clocks on them. I was neat, clean, shaved and sober, and I didn’t care who knew it. I was everything the well-dressed private detective ought to be. I was calling on four million dollars.",
|
| 34 |
+
"history, crime, fantasy"],
|
| 35 |
+
]
|
| 36 |
+
|
| 37 |
+
# Create Gradio interface
|
| 38 |
+
interface = gr.Interface(
|
| 39 |
+
fn=classify_text,
|
| 40 |
+
inputs=[
|
| 41 |
+
gr.Textbox(lines=10, label="Document"),
|
| 42 |
+
gr.Textbox(lines=1, label="Candidate Labels (comma-separated)")
|
| 43 |
+
],
|
| 44 |
+
outputs=[
|
| 45 |
+
gr.Dataframe(type ="pandas",label="Classification Scores"),
|
| 46 |
+
gr.Image(type="numpy", label="Classification Bar Plot")
|
| 47 |
+
],
|
| 48 |
+
title="Text Genre Classification",
|
| 49 |
+
description="Classify text into specified labels using zero-shot classification. Provide a document and candidate labels separated by commas.",
|
| 50 |
+
examples=examples
|
| 51 |
+
)
|
| 52 |
+
|
| 53 |
+
# Launch the Gradio app
|
| 54 |
+
interface.launch(debug=False)
|
| 55 |
+
|