Spaces:
Paused
Paused
Update app.py
Browse files
app.py
CHANGED
@@ -12,38 +12,58 @@ download_if_non_existent('corpora/wordnet', 'wordnet')
|
|
12 |
#################################################################### Streamlit interface
|
13 |
st.title("Movie Reviews: An NLP Sentiment analysis")
|
14 |
|
15 |
-
st.markdown("### NLP Processing utilizing various ML approaches")
|
16 |
-
st.markdown("##### This initial approach merges multiple datasets, processed through a TF-IDF vectorizer with 2 n-grams and fed into a Stochastic Gradient Descent model.")
|
17 |
-
st.markdown("Give it a go by writing a positive or negative text, and analyze it!")
|
18 |
-
|
19 |
-
|
20 |
#################################################################### Cache the model loading
|
|
|
21 |
@st.cache_data()
|
22 |
-
def load_model():
|
23 |
-
|
24 |
-
|
25 |
-
|
26 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
27 |
|
28 |
-
model = load_model()
|
29 |
-
processor = LinguisticPreprocessor()
|
30 |
def predict_sentiment(text, model):
|
31 |
processor.transform(text)
|
32 |
prediction = model.predict([text])
|
33 |
return prediction
|
34 |
|
35 |
|
|
|
|
|
|
|
|
|
|
|
36 |
############################################################# Text input
|
37 |
-
user_input = st.text_area("Enter text here...")
|
38 |
|
39 |
-
|
40 |
-
|
41 |
-
|
42 |
-
|
43 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
44 |
|
45 |
-
|
46 |
-
|
47 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
48 |
|
49 |
-
st.caption("Por @efeperro
|
|
|
12 |
#################################################################### Streamlit interface
|
13 |
st.title("Movie Reviews: An NLP Sentiment analysis")
|
14 |
|
|
|
|
|
|
|
|
|
|
|
15 |
#################################################################### Cache the model loading
|
16 |
+
|
17 |
@st.cache_data()
|
18 |
+
def load_model(model_path):
|
19 |
+
model_pkl_file = "sentiment_model.pkl"
|
20 |
+
with open(model_pkl_file, 'rb') as file:
|
21 |
+
model = pickle.load(file)
|
22 |
+
return model
|
23 |
+
|
24 |
+
def load_cnn():
|
25 |
+
model.load_state_dict(torch.load('model_cnn.pkl'))
|
26 |
+
model.eval()
|
27 |
+
|
28 |
+
return model
|
29 |
|
|
|
|
|
30 |
def predict_sentiment(text, model):
|
31 |
processor.transform(text)
|
32 |
prediction = model.predict([text])
|
33 |
return prediction
|
34 |
|
35 |
|
36 |
+
model_1 = load_model()
|
37 |
+
model_2 = load_cnn()
|
38 |
+
processor = LinguisticPreprocessor()
|
39 |
+
|
40 |
+
|
41 |
############################################################# Text input
|
|
|
42 |
|
43 |
+
with st.expander("Model 1: SGD Classifier"):
|
44 |
+
st.markdown("Give it a go by writing a positive or negative text, and analyze it!")
|
45 |
+
|
46 |
+
# Text input inside the expander
|
47 |
+
user_input = st.text_area("Enter text here...")
|
48 |
+
if st.button('Analyze'):
|
49 |
+
# Displaying output
|
50 |
+
result = predict_sentiment(user_input, model_1)
|
51 |
+
if result >= 0.5:
|
52 |
+
st.write('The sentiment is: Positive π')
|
53 |
+
else:
|
54 |
+
st.write('The sentiment is: Negative π')
|
55 |
|
56 |
+
with st.expander("Model 2: CNN Sentiment analysis"):
|
57 |
+
st.markdown("Give it a go by writing a positive or negative text, and analyze it!")
|
58 |
|
59 |
+
# Text input inside the expander
|
60 |
+
user_input = st.text_area("Enter text here...")
|
61 |
+
if st.button('Analyze'):
|
62 |
+
# Displaying output
|
63 |
+
result = predict_sentiment(user_input, model_2)
|
64 |
+
if result >= 0.5:
|
65 |
+
st.write('The sentiment is: Positive π')
|
66 |
+
else:
|
67 |
+
st.write('The sentiment is: Negative π')
|
68 |
|
69 |
+
st.caption("Por @efeperro.")
|