Download app.py from TweetPulseAISentiment/TweetPulse_AI_Sentiment: direct link, hf CLI and curl.
- Browser
- Download file 3.08 kB
-
https://huggingface.co/spaces/TweetPulseAISentiment/TweetPulse_AI_Sentiment/resolve/main/app.py
- Command line
-
hf download hf://spaces/TweetPulseAISentiment/TweetPulse_AI_Sentiment/app.py
-
curl -L -o app.py https://huggingface.co/spaces/TweetPulseAISentiment/TweetPulse_AI_Sentiment/resolve/main/app.py
3.08 kB
| # ✅ 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") | |