Faisalaldwaish1's picture
Update app.py
1c2e70b verified
raw
history blame contribute delete
No virus
955 Bytes
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()