File size: 3,077 Bytes
579de7d
 
 
 
9f1a075
 
 
579de7d
 
 
 
 
 
 
 
 
 
 
 
 
 
9f1a075
 
 
579de7d
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
9f1a075
579de7d
9f1a075
 
 
 
 
 
 
579de7d
9f1a075
 
 
 
579de7d
9f1a075
 
579de7d
9f1a075
579de7d
9f1a075
 
 
 
 
 
 
 
 
579de7d
9f1a075
579de7d
9f1a075
 
 
579de7d
9f1a075
579de7d
9f1a075
 
 
579de7d
9f1a075
579de7d
9f1a075
579de7d
9f1a075
579de7d
9f1a075
579de7d
 
9f1a075
 
 
 
 
 
 
 
 
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
# ✅ Final Version – TweetPulse AI Sentiment Analyzer (Hugging Face Compatible)
# 🎯 Sound system automatically disabled for online run
# 💜 Created by Isneha Varshney

import streamlit as st
import joblib
from PIL import Image
import threading
import time
import os

# Try importing playsound (works only on local system)
try:
    from playsound import playsound
    SOUND_ENABLED = True
except Exception:
    SOUND_ENABLED = False

# ------------------------------------------
# Load trained model and TF-IDF vectorizer
# ------------------------------------------
model = joblib.load("sentiment_model.pkl")
vectorizer = joblib.load("tfidf_vectorizer.pkl")

# ------------------------------------------
# Function: Play sound for few seconds (only for local)
# ------------------------------------------
def play_sound_limited(sound_file, duration=3):
    if not SOUND_ENABLED:
        return
    def play():
        try:
            playsound(sound_file)
        except Exception:
            pass
    t = threading.Thread(target=play)
    t.start()
    time.sleep(duration)
    os.system("taskkill /IM wmplayer.exe /F >nul 2>&1")

# ------------------------------------------
# Streamlit Page Setup
# ------------------------------------------
st.set_page_config(page_title="TweetPulse AI 💬", page_icon="💫", layout="centered")

st.markdown("""
    <h1 style='text-align:center; color:#6a0dad;'>💫 TweetPulse AI - Sentiment Analyzer 💫</h1>
    <h4 style='text-align:center; color:gray;'>Analyze tweet emotions instantly ⚡</h4>
""", unsafe_allow_html=True)

# Stylish Input Box
tweet = st.text_area(
    "✍️ Type your tweet below:",
    placeholder="e.g. I absolutely loved this movie! 🎬",
    height=120,
    help="Type any sentence or tweet to analyze its emotion."
)

# ------------------------------------------
# Predict Sentiment
# ------------------------------------------
if st.button("🔍 Analyze Sentiment"):
    if tweet.strip() == "":
        st.warning("⚠️ Please type something to analyze.")
    else:
        tweet_vector = vectorizer.transform([tweet])
        prediction = model.predict(tweet_vector)[0]

        if prediction == "positive":
            img = Image.open("positive.png")
            st.image(img, width=180)
            st.success("🎉 Sentiment Detected: **Positive 😍**")
            play_sound_limited("positive.mp3", duration=3)

        elif prediction == "negative":
            img = Image.open("negative.png")
            st.image(img, width=180)
            st.error("💢 Sentiment Detected: **Negative 😡**")
            play_sound_limited("negative.mp3", duration=3)

        else:
            img = Image.open("neutral.png")
            st.image(img, width=180)
            st.info("😐 Sentiment Detected: **Neutral 😐**")
            play_sound_limited("neutral.mp3", duration=3)

# ------------------------------------------
# Footer
# ------------------------------------------
st.markdown("---")
st.caption("💜 Created by **Isneha Varshney** | Powered by TweetPulse AI")