Sneha2134 commited on
Commit
9f1a075
·
verified ·
1 Parent(s): bbeb69e

update app.py file

Browse files

app.py file updated version .

Files changed (1) hide show
  1. app.py +104 -95
app.py CHANGED
@@ -1,95 +1,104 @@
1
- # ✅ Final Version - TweetPulse AI with Proper Sound Control
2
- # 💜 Created by Isneha Varshney
3
-
4
- import streamlit as st
5
- import joblib
6
- from PIL import Image
7
- import time
8
- import pygame # New module for stable sound playback
9
-
10
- # -------------------------------
11
- # Load trained model & vectorizer
12
- # -------------------------------
13
- model = joblib.load("sentiment_model.pkl")
14
- vectorizer = joblib.load("tfidf_vectorizer.pkl")
15
-
16
- # -------------------------------
17
- # Function: Play sound for limited duration safely
18
- # -------------------------------
19
- def play_sound_limited(sound_file, duration=5):
20
- try:
21
- pygame.mixer.init()
22
- pygame.mixer.music.load(sound_file)
23
- pygame.mixer.music.play()
24
- time.sleep(duration)
25
- pygame.mixer.music.stop()
26
- pygame.mixer.quit()
27
- except Exception as e:
28
- print(f"⚠️ Sound error: {e}")
29
-
30
- # -------------------------------
31
- # Streamlit Page Setup
32
- # -------------------------------
33
- st.set_page_config(page_title="TweetPulse AI 💬", page_icon="💫", layout="centered")
34
-
35
- st.markdown("""
36
- <h1 style='text-align:center; color:#6a0dad;'>💫 TweetPulse AI - Sentiment Analyzer 💫</h1>
37
- <h4 style='text-align:center; color:gray;'>Analyze tweet emotions instantly ⚡</h4>
38
- """, unsafe_allow_html=True)
39
-
40
- # Stylish input box
41
- tweet = st.text_area(
42
- "✍️ Type your tweet below:",
43
- placeholder="e.g. I absolutely loved this movie! 🎬",
44
- height=120,
45
- help="Type any sentence or tweet to analyze its emotion."
46
- )
47
-
48
- # -------------------------------
49
- # Predict sentiment
50
- # -------------------------------
51
- if st.button("🔍 Analyze Sentiment"):
52
- if tweet.strip() == "":
53
- st.warning("Please type something to analyze.")
54
- else:
55
- tweet_vector = vectorizer.transform([tweet])
56
- prediction = model.predict(tweet_vector)[0]
57
-
58
- # 🎵 Stop any previous sound (safety)
59
- pygame.mixer.quit()
60
-
61
- if prediction == "positive":
62
- img = Image.open("positive.png")
63
- st.image(img, width=180)
64
- st.success("🌞 Sentiment Detected: **Positive 😍**")
65
- play_sound_limited("positive.mp3", duration=3)
66
-
67
- elif prediction == "negative":
68
- img = Image.open("negative.png")
69
- st.image(img, width=180)
70
- st.error("💢 Sentiment Detected: **Negative 😡**")
71
- play_sound_limited("negative.mp3", duration=4)
72
-
73
- else:
74
- img = Image.open("neutral.png")
75
- st.image(img, width=180)
76
- st.info("😐 Sentiment Detected: **Neutral 😐**")
77
- play_sound_limited("neutral.mp3", duration=4)
78
-
79
- # -------------------------------
80
- # Footer
81
- # -------------------------------
82
- st.markdown("---")
83
- st.caption("💜 Created by **Isneha Varshney** | Powered by TweetPulse AI")
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
+