File size: 1,568 Bytes
0dab116
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
from transformers import pipeline
import torch
import gradio as gr

# Initialize zero-shot classification pipeline
classifier = pipeline("zero-shot-classification",
                                      model="MoritzLaurer/mDeBERTa-v3-base-xnli-multilingual-nli-2mil7",
                                      device=0 if torch.cuda.is_available() else -1)

def classify_text(text, labels):
                   # Split labels into a list
                   candidate_labels = [label.strip() for label in labels.split(",")]
    
                   # Perform zero-shot classification
                   result = classifier(text, candidate_labels, multi_label=False)
    
                   # Format output
                   output = ""
                   for label, score in zip(result['labels'], result['scores']):
                       percentage = score * 100
                       output += f"{label}: {percentage:.2f}%\n"
    
                   return output

# Create Gradio interface
iface = gr.Interface(
                   fn=classify_text,
                   inputs=[
                       gr.Textbox(label="Enter text to classify", lines=3),
                       gr.Textbox(label="Enter labels (comma-separated)", value="politics, sports, technology, entertainment")
                   ],
                   outputs=gr.Textbox(label="Classification Results"),
                   title="Zero-Shot Text Classification",
                   description="Enter text and labels to classify the text into different categories."
)

# Launch the app
iface.launch()