Ahmedhany1's picture
Create app.py
6b71b33 verified
raw
history blame contribute delete
958 Bytes
# app.py
from transformers import pipeline
import gradio as gr
# Load your fine-tuned model from Hugging Face
classifier = pipeline("text-classification", model="Ahmedhany1/my-finetuned-distilbert")
# Optional: map label IDs to readable names (if your model outputs LABEL_0, etc.)
id2label = {
"LABEL_0": "Negative",
"LABEL_1": "Neutral",
"LABEL_2": "Positive"
}
# Define the prediction function
def predict_sentiment(text):
result = classifier(text)[0]
label = id2label.get(result['label'], result['label'])
confidence = round(result['score'], 4)
return f"Sentiment: {label} (Confidence: {confidence})"
# Gradio interface
demo = gr.Interface(
fn=predict_sentiment,
inputs=gr.Textbox(lines=3, placeholder="Enter a tweet here..."),
outputs="text",
title="Twitter Sentiment Analyzer Lab Project",
description="Fine-tuned BERT model for classifying tweets as Positive, Neutral, or Negative."
)
demo.launch()