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.")