Download app.py from ananthakrishnan171/Sentiment-Classification-using-LSTM: direct link, hf CLI and curl.
- Browser
- Download file 3.96 kB
-
https://huggingface.co/spaces/ananthakrishnan171/Sentiment-Classification-using-LSTM/resolve/main/app.py
- Command line
-
hf download hf://spaces/ananthakrishnan171/Sentiment-Classification-using-LSTM/app.py
-
curl -L -o app.py https://huggingface.co/spaces/ananthakrishnan171/Sentiment-Classification-using-LSTM/resolve/main/app.py
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() | |