update app.py file
Browse filesapp.py file updated version .
app.py
CHANGED
|
@@ -1,95 +1,104 @@
|
|
| 1 |
-
# ✅ Final Version
|
| 2 |
-
#
|
| 3 |
-
|
| 4 |
-
|
| 5 |
-
import
|
| 6 |
-
|
| 7 |
-
import
|
| 8 |
-
import
|
| 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 |
-
st.
|
| 84 |
-
|
| 85 |
-
|
| 86 |
-
|
| 87 |
-
|
| 88 |
-
|
| 89 |
-
|
| 90 |
-
|
| 91 |
-
|
| 92 |
-
|
| 93 |
-
|
| 94 |
-
|
| 95 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 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.")
|
| 65 |
+
else:
|
| 66 |
+
tweet_vector = vectorizer.transform([tweet])
|
| 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 |
+
|
| 96 |
+
|
| 97 |
+
|
| 98 |
+
|
| 99 |
+
|
| 100 |
+
|
| 101 |
+
|
| 102 |
+
|
| 103 |
+
|
| 104 |
+
|