update app.py
Browse files
app.py
CHANGED
|
@@ -1,80 +1,64 @@
|
|
| 1 |
-
#
|
|
|
|
|
|
|
|
|
|
| 2 |
import streamlit as st
|
| 3 |
import joblib
|
| 4 |
from PIL import Image
|
| 5 |
-
|
| 6 |
-
|
| 7 |
-
|
| 8 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 9 |
model = joblib.load("sentiment_model.pkl")
|
| 10 |
vectorizer = joblib.load("tfidf_vectorizer.pkl")
|
| 11 |
|
| 12 |
-
# -------------------------------
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 13 |
# Streamlit Page Setup
|
| 14 |
-
# -------------------------------
|
| 15 |
st.set_page_config(page_title="TweetPulse AI 💬", page_icon="💫", layout="centered")
|
| 16 |
|
| 17 |
-
# Custom CSS for animations
|
| 18 |
-
st.markdown("""
|
| 19 |
-
<style>
|
| 20 |
-
body {
|
| 21 |
-
background-color: #f8f8ff;
|
| 22 |
-
}
|
| 23 |
-
.pulse {
|
| 24 |
-
animation: pulse 1.5s infinite;
|
| 25 |
-
}
|
| 26 |
-
@keyframes pulse {
|
| 27 |
-
0% { transform: scale(1); opacity: 0.8; }
|
| 28 |
-
50% { transform: scale(1.05); opacity: 1; }
|
| 29 |
-
100% { transform: scale(1); opacity: 0.8; }
|
| 30 |
-
}
|
| 31 |
-
.flash-green {
|
| 32 |
-
background: rgba(0,255,0,0.15);
|
| 33 |
-
animation: flash-green 1.5s;
|
| 34 |
-
}
|
| 35 |
-
@keyframes flash-green {
|
| 36 |
-
from {background-color: #d4edda;}
|
| 37 |
-
to {background-color: white;}
|
| 38 |
-
}
|
| 39 |
-
.flash-red {
|
| 40 |
-
background: rgba(255,0,0,0.15);
|
| 41 |
-
animation: flash-red 1.5s;
|
| 42 |
-
}
|
| 43 |
-
@keyframes flash-red {
|
| 44 |
-
from {background-color: #f8d7da;}
|
| 45 |
-
to {background-color: white;}
|
| 46 |
-
}
|
| 47 |
-
.flash-blue {
|
| 48 |
-
background: rgba(0,0,255,0.1);
|
| 49 |
-
animation: flash-blue 1.5s;
|
| 50 |
-
}
|
| 51 |
-
@keyframes flash-blue {
|
| 52 |
-
from {background-color: #d1ecf1;}
|
| 53 |
-
to {background-color: white;}
|
| 54 |
-
}
|
| 55 |
-
</style>
|
| 56 |
-
""", unsafe_allow_html=True)
|
| 57 |
-
|
| 58 |
-
# -------------------------------
|
| 59 |
-
# Header
|
| 60 |
-
# -------------------------------
|
| 61 |
st.markdown("""
|
| 62 |
<h1 style='text-align:center; color:#6a0dad;'>💫 TweetPulse AI - Sentiment Analyzer 💫</h1>
|
| 63 |
<h4 style='text-align:center; color:gray;'>Analyze tweet emotions instantly ⚡</h4>
|
| 64 |
""", unsafe_allow_html=True)
|
| 65 |
|
| 66 |
-
#
|
| 67 |
-
# Input Box
|
| 68 |
-
# -------------------------------
|
| 69 |
tweet = st.text_area(
|
| 70 |
"✍️ Type your tweet below:",
|
| 71 |
placeholder="e.g. I absolutely loved this movie! 🎬",
|
| 72 |
height=120,
|
|
|
|
| 73 |
)
|
| 74 |
|
| 75 |
-
# -------------------------------
|
| 76 |
# Predict Sentiment
|
| 77 |
-
# -------------------------------
|
| 78 |
if st.button("🔍 Analyze Sentiment"):
|
| 79 |
if tweet.strip() == "":
|
| 80 |
st.warning("⚠️ Please type something to analyze.")
|
|
@@ -83,31 +67,29 @@ if st.button("🔍 Analyze Sentiment"):
|
|
| 83 |
prediction = model.predict(tweet_vector)[0]
|
| 84 |
|
| 85 |
if prediction == "positive":
|
| 86 |
-
st.markdown("<div class='flash-green pulse'>", unsafe_allow_html=True)
|
| 87 |
img = Image.open("positive.png")
|
| 88 |
-
st.image(img, width=
|
| 89 |
st.success("🎉 Sentiment Detected: **Positive 😍**")
|
| 90 |
-
|
| 91 |
|
| 92 |
elif prediction == "negative":
|
| 93 |
-
st.markdown("<div class='flash-red pulse'>", unsafe_allow_html=True)
|
| 94 |
img = Image.open("negative.png")
|
| 95 |
-
st.image(img, width=
|
| 96 |
st.error("💢 Sentiment Detected: **Negative 😡**")
|
| 97 |
-
|
| 98 |
|
| 99 |
else:
|
| 100 |
-
st.markdown("<div class='flash-blue pulse'>", unsafe_allow_html=True)
|
| 101 |
img = Image.open("neutral.png")
|
| 102 |
-
st.image(img, width=
|
| 103 |
st.info("😐 Sentiment Detected: **Neutral 😐**")
|
| 104 |
-
|
| 105 |
|
| 106 |
-
# -------------------------------
|
| 107 |
# Footer
|
| 108 |
-
# -------------------------------
|
| 109 |
st.markdown("---")
|
| 110 |
-
st.caption("💜 Created by **Isneha Varshney** | Powered by TweetPulse AI
|
|
|
|
| 111 |
|
| 112 |
|
| 113 |
|
|
|
|
| 1 |
+
# ✅ Final Version – TweetPulse AI Sentiment Analyzer (Hugging Face Compatible)
|
| 2 |
+
# 🎯 Sound system automatically disabled for online run
|
| 3 |
+
# 💜 Created by Isneha Varshney
|
| 4 |
+
|
| 5 |
import streamlit as st
|
| 6 |
import joblib
|
| 7 |
from PIL import Image
|
| 8 |
+
import threading
|
| 9 |
+
import time
|
| 10 |
+
import os
|
| 11 |
+
|
| 12 |
+
# Try importing playsound (works only on local system)
|
| 13 |
+
try:
|
| 14 |
+
from playsound import playsound
|
| 15 |
+
SOUND_ENABLED = True
|
| 16 |
+
except Exception:
|
| 17 |
+
SOUND_ENABLED = False
|
| 18 |
+
|
| 19 |
+
# ------------------------------------------
|
| 20 |
+
# Load trained model and TF-IDF vectorizer
|
| 21 |
+
# ------------------------------------------
|
| 22 |
model = joblib.load("sentiment_model.pkl")
|
| 23 |
vectorizer = joblib.load("tfidf_vectorizer.pkl")
|
| 24 |
|
| 25 |
+
# ------------------------------------------
|
| 26 |
+
# Function: Play sound for few seconds (only for local)
|
| 27 |
+
# ------------------------------------------
|
| 28 |
+
def play_sound_limited(sound_file, duration=3):
|
| 29 |
+
if not SOUND_ENABLED:
|
| 30 |
+
return
|
| 31 |
+
def play():
|
| 32 |
+
try:
|
| 33 |
+
playsound(sound_file)
|
| 34 |
+
except Exception:
|
| 35 |
+
pass
|
| 36 |
+
t = threading.Thread(target=play)
|
| 37 |
+
t.start()
|
| 38 |
+
time.sleep(duration)
|
| 39 |
+
os.system("taskkill /IM wmplayer.exe /F >nul 2>&1")
|
| 40 |
+
|
| 41 |
+
# ------------------------------------------
|
| 42 |
# Streamlit Page Setup
|
| 43 |
+
# ------------------------------------------
|
| 44 |
st.set_page_config(page_title="TweetPulse AI 💬", page_icon="💫", layout="centered")
|
| 45 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 46 |
st.markdown("""
|
| 47 |
<h1 style='text-align:center; color:#6a0dad;'>💫 TweetPulse AI - Sentiment Analyzer 💫</h1>
|
| 48 |
<h4 style='text-align:center; color:gray;'>Analyze tweet emotions instantly ⚡</h4>
|
| 49 |
""", unsafe_allow_html=True)
|
| 50 |
|
| 51 |
+
# Stylish Input Box
|
|
|
|
|
|
|
| 52 |
tweet = st.text_area(
|
| 53 |
"✍️ Type your tweet below:",
|
| 54 |
placeholder="e.g. I absolutely loved this movie! 🎬",
|
| 55 |
height=120,
|
| 56 |
+
help="Type any sentence or tweet to analyze its emotion."
|
| 57 |
)
|
| 58 |
|
| 59 |
+
# ------------------------------------------
|
| 60 |
# Predict Sentiment
|
| 61 |
+
# ------------------------------------------
|
| 62 |
if st.button("🔍 Analyze Sentiment"):
|
| 63 |
if tweet.strip() == "":
|
| 64 |
st.warning("⚠️ Please type something to analyze.")
|
|
|
|
| 67 |
prediction = model.predict(tweet_vector)[0]
|
| 68 |
|
| 69 |
if prediction == "positive":
|
|
|
|
| 70 |
img = Image.open("positive.png")
|
| 71 |
+
st.image(img, width=180)
|
| 72 |
st.success("🎉 Sentiment Detected: **Positive 😍**")
|
| 73 |
+
play_sound_limited("positive.mp3", duration=3)
|
| 74 |
|
| 75 |
elif prediction == "negative":
|
|
|
|
| 76 |
img = Image.open("negative.png")
|
| 77 |
+
st.image(img, width=180)
|
| 78 |
st.error("💢 Sentiment Detected: **Negative 😡**")
|
| 79 |
+
play_sound_limited("negative.mp3", duration=3)
|
| 80 |
|
| 81 |
else:
|
|
|
|
| 82 |
img = Image.open("neutral.png")
|
| 83 |
+
st.image(img, width=180)
|
| 84 |
st.info("😐 Sentiment Detected: **Neutral 😐**")
|
| 85 |
+
play_sound_limited("neutral.mp3", duration=3)
|
| 86 |
|
| 87 |
+
# ------------------------------------------
|
| 88 |
# Footer
|
| 89 |
+
# ------------------------------------------
|
| 90 |
st.markdown("---")
|
| 91 |
+
st.caption("💜 Created by **Isneha Varshney** | Powered by TweetPulse AI")
|
| 92 |
+
|
| 93 |
|
| 94 |
|
| 95 |
|