File size: 3,964 Bytes
5ba9099
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
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()