import gradio as gr
from transformers import pipeline
# ✅ Load Hugging Face Sentiment Analysis Pipeline
sentiment_pipeline = pipeline("sentiment-analysis")
# ✅ Function to predict sentiment
def predict_sentiment(movie_name, review, history):
result = sentiment_pipeline(review)[0]
label = result["label"]
score = result["score"]
sentiment_text = "🌟 Positive Review" if label == "POSITIVE" else "💔 Negative Review"
emoji = "😊" if label == "POSITIVE" else "😞"
sentiment_result = f"{sentiment_text} {emoji} | Confidence: {score:.2f}"
# Append to history
history.append((movie_name, review, sentiment_result, label))
# Build animated colorful cards
history_display = ""
for m_name, review_text, result_text, sentiment_label in reversed(history):
bg_color = "#D1F2EB" if sentiment_label == "POSITIVE" else "#FADBD8"
history_display += f"""
Movie: {m_name}
Review:
{review_text}
Result:
{result_text}
"""
return sentiment_result, history_display, history
# ✅ Clear History Function
def clear_history():
return "", "", "", []
# ✅ Gradio App with Animation CSS
with gr.Blocks(css="""
body {
background-color: #FFF8E1;
}
.fade-card {
animation: fadeIn 1s ease-in-out;
}
@keyframes fadeIn {
0% { opacity: 0; transform: translateY(20px); }
100% { opacity: 1; transform: translateY(0); }
}
""", theme=gr.themes.Default(primary_hue="purple")) as demo:
gr.HTML(
"""
🎥 Movie Review Sentiment Analyzer
Analyze your review below to see if it feels 💔 Negative or 🌟 Positive.
Previous reviews appear as colorful cards below.
"""
)
review_history = gr.State([])
with gr.Row():
with gr.Column(scale=2):
movie_name = gr.Textbox(
label="🎬 Enter Your Movie Name",
placeholder="Ex: Leo, Don, Vikram, etc..."
)
review_input = gr.Textbox(
label="🎬 Your Movie Review",
placeholder="Type your movie review here...",
lines=5
)
with gr.Row():
clear_button = gr.Button("❌ Clear Input")
submit_button = gr.Button("✅ Analyze Review")
clear_history_button = gr.Button("🗑️ Clear All History")
with gr.Column(scale=1):
sentiment_output = gr.Textbox(
label="🔍 Sentiment Result",
interactive=False
)
history_box = gr.HTML(label="📝 Review History")
submit_button.click(
fn=predict_sentiment,
inputs=[movie_name, review_input, review_history],
outputs=[sentiment_output, history_box, review_history]
)
clear_button.click(
lambda: ("", "",),
None,
[movie_name, review_input],
queue=False
)
clear_history_button.click(
fn=clear_history,
inputs=None,
outputs=[sentiment_output, history_box, review_history],
queue=False
)
# ✅ Launch App
demo.launch()