Srikanth0804 commited on
Commit
2ea2157
·
verified ·
1 Parent(s): 10a6f7e

Upload app.py

Browse files
Files changed (1) hide show
  1. app.py +76 -0
app.py ADDED
@@ -0,0 +1,76 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import streamlit as st
2
+ import pickle
3
+ import numpy as np
4
+ import re
5
+ from gensim.models import Word2Vec
6
+ from nltk.corpus import stopwords
7
+ import nltk
8
+ nltk.download('stopwords')
9
+
10
+ # ===============================
11
+ # Word2VecVectorizer class (must be defined before loading pickle)
12
+ # ===============================
13
+ class Word2VecVectorizer:
14
+ def __init__(self, model):
15
+ self.model = model
16
+ self.size = model.vector_size
17
+
18
+ def fit(self, X, y=None):
19
+ return self
20
+
21
+ def transform(self, X):
22
+ vectors = []
23
+ for text in X:
24
+ tokens = text.split()
25
+ word_vecs = []
26
+ for w in tokens:
27
+ if w in self.model.wv:
28
+ vec = self.model.wv[w]
29
+ # normalize each vector
30
+ vec = vec / np.linalg.norm(vec)
31
+ word_vecs.append(vec)
32
+ if word_vecs:
33
+ vectors.append(np.mean(word_vecs, axis=0))
34
+ else:
35
+ vectors.append(np.zeros(self.size))
36
+ return np.array(vectors)
37
+
38
+ # ===============================
39
+ # Streamlit App Config
40
+ # ===============================
41
+ st.set_page_config(page_title="🎯 Word2Vec Sentiment Classifier")
42
+
43
+ st.title("🎯 Live Sentiment Classifier (Word2Vec + Logistic Regression)")
44
+ #st.write("This app predicts the sentiment of text using a Word2Vec-based Logistic Regression model trained on your dataset.")
45
+
46
+ # Stopwords
47
+ stop_words = set(stopwords.words('english'))
48
+
49
+ # ===============================
50
+ # Load Trained Pipeline
51
+ # ===============================
52
+ with open("text_classification_word2vec.pkl", "rb") as f:
53
+ pipeline = pickle.load(f)
54
+
55
+ # ===============================
56
+ # User Input
57
+ # ===============================
58
+ user_input = st.text_area("**Enter a sentence or YouTube comment to analyze:**")
59
+
60
+ # ===============================
61
+ # Prediction
62
+ # ===============================
63
+ if st.button("Predict Sentiment"):
64
+ if user_input.strip():
65
+ prediction = pipeline.predict([user_input])[0]
66
+
67
+ st.markdown(f"## ✅ Prediction: `{prediction.capitalize()}`")
68
+
69
+ if prediction.lower() == "positive":
70
+ st.success("😊 Positive Sentiment")
71
+ elif prediction.lower() == "negative":
72
+ st.error("😠 Negative Sentiment")
73
+ else:
74
+ st.info("😐 Neutral Sentiment")
75
+ else:
76
+ st.warning("⚠️ Please enter some text to analyze.")