Create app.py
Browse files
app.py
ADDED
|
@@ -0,0 +1,121 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import gradio as gr
|
| 2 |
+
from transformers import pipeline
|
| 3 |
+
|
| 4 |
+
# β
Load Hugging Face Sentiment Analysis Pipeline
|
| 5 |
+
sentiment_pipeline = pipeline("sentiment-analysis")
|
| 6 |
+
|
| 7 |
+
|
| 8 |
+
# β
Function to predict sentiment
|
| 9 |
+
def predict_sentiment(movie_name, review, history):
|
| 10 |
+
result = sentiment_pipeline(review)[0]
|
| 11 |
+
label = result["label"]
|
| 12 |
+
score = result["score"]
|
| 13 |
+
|
| 14 |
+
sentiment_text = "π Positive Review" if label == "POSITIVE" else "π Negative Review"
|
| 15 |
+
emoji = "π" if label == "POSITIVE" else "π"
|
| 16 |
+
sentiment_result = f"{sentiment_text} {emoji} | Confidence: {score:.2f}"
|
| 17 |
+
|
| 18 |
+
# Append to history
|
| 19 |
+
history.append((movie_name, review, sentiment_result, label))
|
| 20 |
+
|
| 21 |
+
# Build animated colorful cards
|
| 22 |
+
history_display = ""
|
| 23 |
+
for m_name, review_text, result_text, sentiment_label in reversed(history):
|
| 24 |
+
bg_color = "#D1F2EB" if sentiment_label == "POSITIVE" else "#FADBD8"
|
| 25 |
+
history_display += f"""
|
| 26 |
+
<div class="fade-card" style="background-color: {bg_color}; padding: 12px; margin-bottom: 10px;
|
| 27 |
+
border-radius: 8px; box-shadow: 0 0 5px rgba(0,0,0,0.1); color: #000;">
|
| 28 |
+
<strong style="color:#000;">Movie:</strong> <span style="color:#000;">{m_name}</span><br>
|
| 29 |
+
<strong style="color:#000;">Review:</strong><br><span style="color:#000;">{review_text}</span><br><br>
|
| 30 |
+
<strong style="color:#000;">Result:</strong><br><span style="color:#000;">{result_text}</span>
|
| 31 |
+
</div>
|
| 32 |
+
"""
|
| 33 |
+
|
| 34 |
+
return sentiment_result, history_display, history
|
| 35 |
+
|
| 36 |
+
|
| 37 |
+
# β
Clear History Function
|
| 38 |
+
def clear_history():
|
| 39 |
+
return "", "", "", []
|
| 40 |
+
|
| 41 |
+
|
| 42 |
+
# β
Gradio App with Animation CSS
|
| 43 |
+
with gr.Blocks(css="""
|
| 44 |
+
body {
|
| 45 |
+
background-color: #FFF8E1;
|
| 46 |
+
}
|
| 47 |
+
.fade-card {
|
| 48 |
+
animation: fadeIn 1s ease-in-out;
|
| 49 |
+
}
|
| 50 |
+
@keyframes fadeIn {
|
| 51 |
+
0% { opacity: 0; transform: translateY(20px); }
|
| 52 |
+
100% { opacity: 1; transform: translateY(0); }
|
| 53 |
+
}
|
| 54 |
+
""", theme=gr.themes.Default(primary_hue="purple")) as demo:
|
| 55 |
+
gr.HTML(
|
| 56 |
+
"""
|
| 57 |
+
<div style='
|
| 58 |
+
background: linear-gradient(to right, #FFF1EB, #ACE0F9);
|
| 59 |
+
padding: 30px;
|
| 60 |
+
border-radius: 12px;
|
| 61 |
+
box-shadow: 0 0 10px rgba(0,0,0,0.1);
|
| 62 |
+
'>
|
| 63 |
+
<h1 style='text-align: center; color: #6C63FF;'>π₯ Movie Review Sentiment Analyzer</h1>
|
| 64 |
+
<p style='text-align: center; color: black;'>
|
| 65 |
+
Analyze your review below to see if it feels π Negative or π Positive.<br>
|
| 66 |
+
Previous reviews appear as colorful cards below.
|
| 67 |
+
</p>
|
| 68 |
+
</div>
|
| 69 |
+
<hr>
|
| 70 |
+
"""
|
| 71 |
+
)
|
| 72 |
+
|
| 73 |
+
review_history = gr.State([])
|
| 74 |
+
|
| 75 |
+
with gr.Row():
|
| 76 |
+
with gr.Column(scale=2):
|
| 77 |
+
movie_name = gr.Textbox(
|
| 78 |
+
label="π¬ Enter Your Movie Name",
|
| 79 |
+
placeholder="Ex: Leo, Don, Vikram, etc..."
|
| 80 |
+
)
|
| 81 |
+
review_input = gr.Textbox(
|
| 82 |
+
label="π¬ Your Movie Review",
|
| 83 |
+
placeholder="Type your movie review here...",
|
| 84 |
+
lines=5
|
| 85 |
+
)
|
| 86 |
+
with gr.Row():
|
| 87 |
+
clear_button = gr.Button("β Clear Input")
|
| 88 |
+
submit_button = gr.Button("β
Analyze Review")
|
| 89 |
+
clear_history_button = gr.Button("ποΈ Clear All History")
|
| 90 |
+
|
| 91 |
+
with gr.Column(scale=1):
|
| 92 |
+
sentiment_output = gr.Textbox(
|
| 93 |
+
label="π Sentiment Result",
|
| 94 |
+
interactive=False
|
| 95 |
+
)
|
| 96 |
+
|
| 97 |
+
history_box = gr.HTML(label="π Review History")
|
| 98 |
+
|
| 99 |
+
submit_button.click(
|
| 100 |
+
fn=predict_sentiment,
|
| 101 |
+
inputs=[movie_name, review_input, review_history],
|
| 102 |
+
outputs=[sentiment_output, history_box, review_history]
|
| 103 |
+
)
|
| 104 |
+
|
| 105 |
+
clear_button.click(
|
| 106 |
+
lambda: ("", "",),
|
| 107 |
+
None,
|
| 108 |
+
[movie_name, review_input],
|
| 109 |
+
queue=False
|
| 110 |
+
)
|
| 111 |
+
|
| 112 |
+
clear_history_button.click(
|
| 113 |
+
fn=clear_history,
|
| 114 |
+
inputs=None,
|
| 115 |
+
outputs=[sentiment_output, history_box, review_history],
|
| 116 |
+
queue=False
|
| 117 |
+
)
|
| 118 |
+
|
| 119 |
+
|
| 120 |
+
# β
Launch App
|
| 121 |
+
demo.launch()
|