RawadAlghamdi's picture
Create app.py
75814ec verified
import gradio as gr
from transformers import pipeline
# Load the sentiment analysis pipeline with the specified model
sentiment_analyzer = pipeline("sentiment-analysis", model="nlptown/bert-base-multilingual-uncased-sentiment")
# Define the sentiment analysis function
def analyze_sentiment(text):
# Perform sentiment analysis
result = sentiment_analyzer(text)[0]
# Extract label (e.g., "1 star", "2 stars", etc.) and return it
return f"Predicted Sentiment: {result['label']}"
# Define input and output components with clear labels
input_text = gr.Textbox(lines=5, label="Enter Your Text", placeholder="Type a sentence or paragraph here...")
output_sentiment = gr.Textbox(label="Sentiment Result")
# Define example inputs
examples = [
"I love this product! It's amazing!",
"This was the worst experience I've ever had.",
"The movie was okay, not great but not bad either.",
"Absolutely fantastic! I would recommend it to everyone."
]
# Create the Gradio interface
interface = gr.Interface(
fn=analyze_sentiment,
inputs=input_text,
outputs=output_sentiment,
title="Sentiment Analyzer",
description="Enter text to analyze its sentiment (1 to 5 stars) using a BERT-based model.",
examples=examples,
theme="default" # Ensures a clean, responsive design
)
# Launch the interface
interface.launch()