Download app.py from Srikanth0804/Sentiment_Analysis_Word2Vec: direct link, hf CLI and curl.
- Browser
- Download file 2.49 kB
-
https://huggingface.co/spaces/Srikanth0804/Sentiment_Analysis_Word2Vec/resolve/main/app.py
- Command line
-
hf download hf://spaces/Srikanth0804/Sentiment_Analysis_Word2Vec/app.py
-
curl -L -o app.py https://huggingface.co/spaces/Srikanth0804/Sentiment_Analysis_Word2Vec/resolve/main/app.py
2.49 kB
| import streamlit as st | |
| import pickle | |
| import numpy as np | |
| import re | |
| from gensim.models import Word2Vec | |
| from nltk.corpus import stopwords | |
| import nltk | |
| nltk.download('stopwords') | |
| # =============================== | |
| # Word2VecVectorizer class (must be defined before loading pickle) | |
| # =============================== | |
| class Word2VecVectorizer: | |
| def __init__(self, model): | |
| self.model = model | |
| self.size = model.vector_size | |
| def fit(self, X, y=None): | |
| return self | |
| def transform(self, X): | |
| vectors = [] | |
| for text in X: | |
| tokens = text.split() | |
| word_vecs = [] | |
| for w in tokens: | |
| if w in self.model.wv: | |
| vec = self.model.wv[w] | |
| # normalize each vector | |
| vec = vec / np.linalg.norm(vec) | |
| word_vecs.append(vec) | |
| if word_vecs: | |
| vectors.append(np.mean(word_vecs, axis=0)) | |
| else: | |
| vectors.append(np.zeros(self.size)) | |
| return np.array(vectors) | |
| # =============================== | |
| # Streamlit App Config | |
| # =============================== | |
| st.set_page_config(page_title="π― Word2Vec Sentiment Classifier") | |
| st.title("π― Live Sentiment Classifier (Word2Vec + Logistic Regression)") | |
| #st.write("This app predicts the sentiment of text using a Word2Vec-based Logistic Regression model trained on your dataset.") | |
| # Stopwords | |
| stop_words = set(stopwords.words('english')) | |
| # =============================== | |
| # Load Trained Pipeline | |
| # =============================== | |
| with open("text_classification_word2vec.pkl", "rb") as f: | |
| pipeline = pickle.load(f) | |
| # =============================== | |
| # User Input | |
| # =============================== | |
| user_input = st.text_area("**Enter a sentence or YouTube comment to analyze:**") | |
| # =============================== | |
| # Prediction | |
| # =============================== | |
| if st.button("Predict Sentiment"): | |
| if user_input.strip(): | |
| prediction = pipeline.predict([user_input])[0] | |
| st.markdown(f"## β Prediction: `{prediction.capitalize()}`") | |
| if prediction.lower() == "positive": | |
| st.success("π Positive Sentiment") | |
| elif prediction.lower() == "negative": | |
| st.error("π Negative Sentiment") | |
| else: | |
| st.info("π Neutral Sentiment") | |
| else: | |
| st.warning("β οΈ Please enter some text to analyze.") |