Spaces:
Sleeping
Sleeping
Create App.py
Browse files
App.py
ADDED
@@ -0,0 +1,38 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
from transformers import pipeline
|
2 |
+
import torch
|
3 |
+
import gradio as gr
|
4 |
+
|
5 |
+
# Initialize zero-shot classification pipeline
|
6 |
+
classifier = pipeline("zero-shot-classification",
|
7 |
+
model="MoritzLaurer/mDeBERTa-v3-base-xnli-multilingual-nli-2mil7",
|
8 |
+
device=0 if torch.cuda.is_available() else -1)
|
9 |
+
|
10 |
+
def classify_text(text, labels):
|
11 |
+
# Split labels into a list
|
12 |
+
candidate_labels = [label.strip() for label in labels.split(",")]
|
13 |
+
|
14 |
+
# Perform zero-shot classification
|
15 |
+
result = classifier(text, candidate_labels, multi_label=False)
|
16 |
+
|
17 |
+
# Format output
|
18 |
+
output = ""
|
19 |
+
for label, score in zip(result['labels'], result['scores']):
|
20 |
+
percentage = score * 100
|
21 |
+
output += f"{label}: {percentage:.2f}%\n"
|
22 |
+
|
23 |
+
return output
|
24 |
+
|
25 |
+
# Create Gradio interface
|
26 |
+
iface = gr.Interface(
|
27 |
+
fn=classify_text,
|
28 |
+
inputs=[
|
29 |
+
gr.Textbox(label="Enter text to classify", lines=3),
|
30 |
+
gr.Textbox(label="Enter labels (comma-separated)", value="politics, sports, technology, entertainment")
|
31 |
+
],
|
32 |
+
outputs=gr.Textbox(label="Classification Results"),
|
33 |
+
title="Zero-Shot Text Classification",
|
34 |
+
description="Enter text and labels to classify the text into different categories."
|
35 |
+
)
|
36 |
+
|
37 |
+
# Launch the app
|
38 |
+
iface.launch()
|