HarshitaSuri's picture
Update app.py
57f4b6b verified
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()