File size: 955 Bytes
e9e8255
 
 
1c2e70b
e9e8255
 
1c2e70b
e9e8255
1c2e70b
e9e8255
 
 
 
1c2e70b
e9e8255
1c2e70b
e9e8255
 
 
 
1c2e70b
 
e9e8255
 
1c2e70b
e9e8255
 
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
import gradio as gr
from transformers import pipeline

# Load the pre-trained text classification model from Hugging Face
classifier = pipeline("zero-shot-classification", model="facebook/bart-large-mnli")

# Define a function to classify the text by topic
def classify_text(text):
    candidate_labels = ["Technology", "Health", "Sports", "Politics", "Education", "Art", "Economy"]
    result = classifier(text, candidate_labels=candidate_labels)
    highest_confidence_label = result['labels'][0]
    confidence = result['scores'][0]
    
    return f"Topic: {highest_confidence_label}, Confidence: {confidence:.2f}"

# Create a Gradio interface
iface = gr.Interface(
    fn=classify_text, 
    inputs="text", 
    outputs="text",
    title="Text Classification by Topic",
    description="Enter a text, and we'll classify it by topic (Technology, Health, Sports, Politics, etc.)."
)

# Run the application
if __name__ == "__main__":
    iface.launch()