Create app.py
Browse files
app.py
ADDED
|
@@ -0,0 +1,37 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import gradio as gr
|
| 2 |
+
from transformers import pipeline
|
| 3 |
+
|
| 4 |
+
# Create a zero-shot classification pipeline
|
| 5 |
+
classifier = pipeline("zero-shot-classification")
|
| 6 |
+
|
| 7 |
+
|
| 8 |
+
def classify_text(text, additional_labels):
|
| 9 |
+
# Default labels
|
| 10 |
+
labels = ["Education", "Business", "Sports", "Manufacturing"]
|
| 11 |
+
|
| 12 |
+
# Add custom labels if provided
|
| 13 |
+
if additional_labels:
|
| 14 |
+
custom_labels = additional_labels.split(',')
|
| 15 |
+
labels.extend(custom_labels)
|
| 16 |
+
|
| 17 |
+
# Perform classification
|
| 18 |
+
result = classifier(text, candidate_labels=labels)
|
| 19 |
+
|
| 20 |
+
# Formatting the output
|
| 21 |
+
output = []
|
| 22 |
+
for label, score in zip(result["labels"], result["scores"]):
|
| 23 |
+
output.append(f"Label: {label}, Score: {round(score, 4)}")
|
| 24 |
+
return "\n".join(output)
|
| 25 |
+
|
| 26 |
+
|
| 27 |
+
# Create a Gradio interface
|
| 28 |
+
interface = gr.Interface(
|
| 29 |
+
fn=classify_text,
|
| 30 |
+
inputs=["text", "text"],
|
| 31 |
+
outputs="text",
|
| 32 |
+
title="Text Classification",
|
| 33 |
+
description="Enter a text to classify into categories: Education, Business, Sports, Manufacturing. Optionally, add more categories separated by commas."
|
| 34 |
+
)
|
| 35 |
+
|
| 36 |
+
# Launch the interface
|
| 37 |
+
interface.launch()
|