Spaces:
Runtime error
Runtime error
Create app.py
Browse files
app.py
ADDED
|
@@ -0,0 +1,67 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
from transformers import pipeline
|
| 2 |
+
import gradio as gr
|
| 3 |
+
|
| 4 |
+
# Initialize text classification pipeline
|
| 5 |
+
classifier = pipeline("text-classification", model="facebook/bart-large-mnli")
|
| 6 |
+
|
| 7 |
+
def classify_text(text):
|
| 8 |
+
if not text.strip():
|
| 9 |
+
return "Please enter some text to classify"
|
| 10 |
+
|
| 11 |
+
try:
|
| 12 |
+
# Get classification results
|
| 13 |
+
results = classifier(text)
|
| 14 |
+
|
| 15 |
+
# Format results
|
| 16 |
+
output = "## Classification Results:\n\n"
|
| 17 |
+
for result in results:
|
| 18 |
+
label = result['label']
|
| 19 |
+
score = result['score'] * 100
|
| 20 |
+
output += f"- **{label}**: {score:.2f}%\n"
|
| 21 |
+
|
| 22 |
+
return output
|
| 23 |
+
except Exception as e:
|
| 24 |
+
return f"Error during classification: {str(e)}"
|
| 25 |
+
|
| 26 |
+
# Gradio interface
|
| 27 |
+
with gr.Blocks(title="Text Classifier") as demo:
|
| 28 |
+
gr.Markdown("# 📝 Text Classification AI")
|
| 29 |
+
gr.Markdown("Classify text using Hugging Face's BART model")
|
| 30 |
+
|
| 31 |
+
with gr.Row():
|
| 32 |
+
with gr.Column():
|
| 33 |
+
input_text = gr.Textbox(
|
| 34 |
+
lines=8,
|
| 35 |
+
placeholder="Enter text to classify...",
|
| 36 |
+
label="Input Text"
|
| 37 |
+
)
|
| 38 |
+
classify_btn = gr.Button("Classify Text", variant="primary")
|
| 39 |
+
|
| 40 |
+
with gr.Column():
|
| 41 |
+
output_text = gr.Markdown(label="Classification Results")
|
| 42 |
+
|
| 43 |
+
classify_btn.click(
|
| 44 |
+
classify_text,
|
| 45 |
+
inputs=input_text,
|
| 46 |
+
outputs=output_text
|
| 47 |
+
)
|
| 48 |
+
|
| 49 |
+
gr.Examples(
|
| 50 |
+
[
|
| 51 |
+
["I love this movie, it's fantastic!"],
|
| 52 |
+
["This product is terrible and broke after one day"],
|
| 53 |
+
["The weather today is sunny and warm"],
|
| 54 |
+
["Machine learning is a subset of artificial intelligence"],
|
| 55 |
+
["I'm feeling sad and disappointed about the results"]
|
| 56 |
+
],
|
| 57 |
+
inputs=input_text
|
| 58 |
+
)
|
| 59 |
+
|
| 60 |
+
gr.Markdown("### About This Model")
|
| 61 |
+
gr.Markdown("- **Model**: [facebook/bart-large-mnli](https://huggingface.co/facebook/bart-large-mnli)")
|
| 62 |
+
gr.Markdown("- **Task**: Zero-shot text classification")
|
| 63 |
+
gr.Markdown("- **Capabilities**: Classifies text into various categories without specific training")
|
| 64 |
+
gr.Markdown("- **Note**: First classification may take 10-15 seconds (model loading)")
|
| 65 |
+
|
| 66 |
+
if __name__ == "__main__":
|
| 67 |
+
demo.launch()
|