Redfire-1234 commited on
Commit
cdef703
·
verified ·
1 Parent(s): 9c0d2d8

Create app.py

Browse files
Files changed (1) hide show
  1. app.py +60 -0
app.py ADDED
@@ -0,0 +1,60 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import streamlit as st
2
+ import pickle
3
+ import string
4
+ from nltk.corpus import stopwords
5
+ from nltk.stem import WordNetLemmatizer
6
+ from nltk.tokenize import word_tokenize
7
+ import nltk
8
+
9
+ # Download NLTK data
10
+ nltk.download('punkt_tab') # Changed from 'punkt'
11
+ nltk.download('stopwords')
12
+ nltk.download('wordnet')
13
+ nltk.download('omw-1.4') # Added for better lemmatization
14
+
15
+ stop_words = set(stopwords.words('english'))
16
+ lemmatizer = WordNetLemmatizer()
17
+
18
+ # Function to preprocess text
19
+ def preprocess_text(text):
20
+ text = text.lower()
21
+ text = text.translate(str.maketrans('', '', string.punctuation))
22
+ tokens = word_tokenize(text)
23
+ tokens = [lemmatizer.lemmatize(word) for word in tokens if word not in stop_words]
24
+ return " ".join(tokens)
25
+
26
+ # --- Load models from local files in Space ---
27
+ @st.cache_resource
28
+ def load_models():
29
+ with open('rf_goboult_model.pkl', 'rb') as f:
30
+ goboult_model = pickle.load(f)
31
+ with open('tfidf_goboult.pkl', 'rb') as f:
32
+ goboult_tfidf = pickle.load(f)
33
+ with open('rf_flipflop_model.pkl', 'rb') as f:
34
+ flipflop_model = pickle.load(f)
35
+ with open('tfidf_flipflop.pkl', 'rb') as f:
36
+ flipflop_tfidf = pickle.load(f)
37
+ return goboult_model, goboult_tfidf, flipflop_model, flipflop_tfidf
38
+
39
+ goboult_model, goboult_tfidf, flipflop_model, flipflop_tfidf = load_models()
40
+
41
+ # --- Streamlit UI ---
42
+ st.title("Sentiment Analysis for Goboult & Flipflop")
43
+
44
+ dataset = st.selectbox("Select Dataset", ["Goboult", "Flipflop"])
45
+ review = st.text_area("Enter your review here:")
46
+
47
+ if st.button("Predict Sentiment"):
48
+ if review.strip() == "":
49
+ st.warning("Please enter a review!")
50
+ else:
51
+ cleaned = preprocess_text(review)
52
+
53
+ if dataset.lower() == "goboult":
54
+ vectorized = goboult_tfidf.transform([cleaned])
55
+ pred = goboult_model.predict(vectorized)[0]
56
+ else:
57
+ vectorized = flipflop_tfidf.transform([cleaned])
58
+ pred = flipflop_model.predict(vectorized)[0]
59
+
60
+ st.success(f"Predicted Sentiment: {pred}")