ananthakrishnan171's picture
Create app.py
5ba9099 verified
Raw History Blame Contribute Delete
3.96 kB
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"""
<div class="fade-card" style="background-color: {bg_color}; padding: 12px; margin-bottom: 10px;
border-radius: 8px; box-shadow: 0 0 5px rgba(0,0,0,0.1); color: #000;">
<strong style="color:#000;">Movie:</strong> <span style="color:#000;">{m_name}</span><br>
<strong style="color:#000;">Review:</strong><br><span style="color:#000;">{review_text}</span><br><br>
<strong style="color:#000;">Result:</strong><br><span style="color:#000;">{result_text}</span>
</div>
"""
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(
"""
<div style='
background: linear-gradient(to right, #FFF1EB, #ACE0F9);
padding: 30px;
border-radius: 12px;
box-shadow: 0 0 10px rgba(0,0,0,0.1);
'>
<h1 style='text-align: center; color: #6C63FF;'>πŸŽ₯ Movie Review Sentiment Analyzer</h1>
<p style='text-align: center; color: black;'>
Analyze your review below to see if it feels πŸ’” Negative or 🌟 Positive.<br>
Previous reviews appear as colorful cards below.
</p>
</div>
<hr>
"""
)
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()