Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
|
@@ -2,6 +2,7 @@ import streamlit as st
|
|
| 2 |
from functions_preprocess import LinguisticPreprocessor, download_if_non_existent, CNN
|
| 3 |
import pickle
|
| 4 |
import nltk
|
|
|
|
| 5 |
import torch
|
| 6 |
nltk.download('stopwords')
|
| 7 |
nltk.download('punkt')
|
|
@@ -16,7 +17,7 @@ st.title("Movie Reviews: An NLP Sentiment analysis")
|
|
| 16 |
|
| 17 |
#################################################################### Cache the model loading
|
| 18 |
|
| 19 |
-
@st.
|
| 20 |
def load_model():
|
| 21 |
model_pkl_file = "sentiment_model.pkl"
|
| 22 |
with open(model_pkl_file, 'rb') as file:
|
|
@@ -29,7 +30,7 @@ def load_cnn():
|
|
| 29 |
model.eval()
|
| 30 |
return model
|
| 31 |
|
| 32 |
-
def predict_sentiment(text, model, vocab
|
| 33 |
tokenizer = get_tokenizer("basic_english")
|
| 34 |
if torch_text == True:
|
| 35 |
processor.transform(text)
|
|
@@ -53,6 +54,8 @@ def predict_sentiment(text, model, vocab=16236, torch_text = False):
|
|
| 53 |
model_1 = load_model()
|
| 54 |
model_2 = load_cnn()
|
| 55 |
processor = LinguisticPreprocessor()
|
|
|
|
|
|
|
| 56 |
|
| 57 |
|
| 58 |
############################################################# Text input
|
|
@@ -77,10 +80,11 @@ with st.expander("Model 2: CNN Sentiment analysis"):
|
|
| 77 |
user_input = st.text_area("Enter text here...", key='model2_input')
|
| 78 |
if st.button('Analyze', key='model2_button'):
|
| 79 |
# Displaying output
|
| 80 |
-
result = predict_sentiment(user_input, model_2,
|
| 81 |
if result >= 0.5:
|
| 82 |
st.write('The sentiment is: Positive π', key='model2_poswrite')
|
| 83 |
else:
|
| 84 |
st.write('The sentiment is: Negative π', key='model2_negwrite')
|
| 85 |
|
| 86 |
-
st.caption("Por @efeperro.")
|
|
|
|
|
|
| 2 |
from functions_preprocess import LinguisticPreprocessor, download_if_non_existent, CNN
|
| 3 |
import pickle
|
| 4 |
import nltk
|
| 5 |
+
from datasets import load_dataset
|
| 6 |
import torch
|
| 7 |
nltk.download('stopwords')
|
| 8 |
nltk.download('punkt')
|
|
|
|
| 17 |
|
| 18 |
#################################################################### Cache the model loading
|
| 19 |
|
| 20 |
+
@st.cache_data()
|
| 21 |
def load_model():
|
| 22 |
model_pkl_file = "sentiment_model.pkl"
|
| 23 |
with open(model_pkl_file, 'rb') as file:
|
|
|
|
| 30 |
model.eval()
|
| 31 |
return model
|
| 32 |
|
| 33 |
+
def predict_sentiment(text, model, vocab, torch_text = False):
|
| 34 |
tokenizer = get_tokenizer("basic_english")
|
| 35 |
if torch_text == True:
|
| 36 |
processor.transform(text)
|
|
|
|
| 54 |
model_1 = load_model()
|
| 55 |
model_2 = load_cnn()
|
| 56 |
processor = LinguisticPreprocessor()
|
| 57 |
+
train_data = load_dataset('rotten_tomatoes', split='train')
|
| 58 |
+
vocab, tokenizer = build_vocab(train_data)
|
| 59 |
|
| 60 |
|
| 61 |
############################################################# Text input
|
|
|
|
| 80 |
user_input = st.text_area("Enter text here...", key='model2_input')
|
| 81 |
if st.button('Analyze', key='model2_button'):
|
| 82 |
# Displaying output
|
| 83 |
+
result = predict_sentiment(user_input, model_2, vocab, torch_text=True)
|
| 84 |
if result >= 0.5:
|
| 85 |
st.write('The sentiment is: Positive π', key='model2_poswrite')
|
| 86 |
else:
|
| 87 |
st.write('The sentiment is: Negative π', key='model2_negwrite')
|
| 88 |
|
| 89 |
+
st.caption("Por @efeperro.")
|
| 90 |
+
stop_words = set(stopwords.words('english'))
|