File size: 2,429 Bytes
833d478
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
57f4b6b
 
 
833d478
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
import gradio as gr
import random
import re
import pickle

# ๐Ÿ” Load saved model components
with open("model.pkl", "rb") as f:
    model = pickle.load(f)

with open("vectorizer.pkl", "rb") as f:
    vectorizer = pickle.load(f)

with open("stopwords.pkl", "rb") as f:
    stop_words = pickle.load(f)

with open("stemmer.pkl", "rb") as f:
    stemmer = pickle.load(f)

# โœ‚๏ธ Text Preprocessing
def preprocess(text):
    text = re.sub(r'[^a-zA-Z ]', '', text)
    words = text.lower().split()
    words = [stemmer.stem(w) for w in words if w not in stop_words]
    return " ".join(words)

# ๐ŸŽˆ Emoji Animation HTML Generator
def generate_emoji_explosion(emojis):
    html = "<style>"
    html += """
    @keyframes fly {
        0% { transform: translateY(0) scale(1); opacity: 0; }
        10% { opacity: 1; }
        100% { transform: translateY(-120vh) scale(1.5); opacity: 0; }
    }
    .emoji-burst {
        position: fixed;
        font-size: 2.5rem;
        animation: fly 4s ease-out forwards;
        z-index: 9999;
        pointer-events: none;
    }
    """
    html += "</style>"

    for _ in range(75):
        emoji = random.choice(emojis)
        left = random.randint(0, 100)
        bottom = random.randint(0, 20)
        delay = round(random.uniform(0, 3), 2)
        html += f"<span class='emoji-burst' style='left:{left}%; bottom:{bottom}%; animation-delay:{delay}s'>{emoji}</span>\n"

    return html

# ๐Ÿ” Prediction + Emoji Burst
def predict_sentiment_burst(review_text):
    clean = preprocess(review_text)
    vec = vectorizer.transform([clean])
    pred = model.predict(vec)[0]

    emoji_dict = {
        "positive": ['๐ŸŽ‰', 'โœจ', '๐Ÿ˜', '๐Ÿ’–', '๐Ÿฅณ', '๐Ÿ’•', '๐Ÿ˜˜', 'โค๏ธ', '๐Ÿ˜Ž'],
        "neutral":  ['๐Ÿ˜', '๐Ÿค”', '๐Ÿซค', '๐Ÿ˜Š', '๐Ÿค“', '๐Ÿ‘'],
        "negative": ['๐Ÿ’ฉ', '๐Ÿ˜ก', '๐Ÿ’”', '๐Ÿคฎ', '๐Ÿ˜ญ', '๐Ÿ˜’', '๐Ÿคฆโ€โ™€๏ธ', '๐Ÿคก', '๐Ÿ’ข', '๐Ÿ˜ค']
    }

    flying_emojis = generate_emoji_explosion(emoji_dict.get(pred, ['โ“']))
    return flying_emojis + f"<h2 style='text-align:center;'>Sentiment: {pred}</h2>"

# ๐ŸŒ Gradio Interface
iface = gr.Interface(
    fn=predict_sentiment_burst,
    inputs=gr.Textbox(lines=3, placeholder="Type your product review here..."),
    outputs=gr.HTML(),
    title="๐Ÿง  Product Review Sentiment Analysis",
    description="Enter a product review to see its predicted sentiment and matching emoji animation!"
)

iface.launch()