Spaces:
Running
Running
File size: 1,663 Bytes
93eddbd d139e9a 93eddbd d139e9a 93eddbd |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 |
import streamlit as st
from functions_preprocess import LinguisticPreprocessor, download_if_non_existent
import pickle
download_if_non_existent('corpora/stopwords', 'stopwords', 'taggers/averaged_perceptron_tagger',
'averaged_perceptron_tagger', 'corpora/wordnet', 'wordnet', 'punkt, omw-1.4')
#################################################################### Streamlit interface
st.title("Movie Reviews: An NLP Sentiment analysis")
st.markdown("### NLP Processing utilizing various ML approaches")
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.")
st.markdown("Give it a go by writing a positive or negative text, and analyze it!")
#################################################################### Cache the model loading
@st.cache_data()
def load_model():
model_pkl_file = "sentiment_model.pkl"
with open(model_pkl_file, 'rb') as file:
model = pickle.load(file)
return model
model = load_model()
processor = LinguisticPreprocessor()
def predict_sentiment(text, model):
processor.transform(text)
prediction = model.predict([text])
return prediction
############################################################# Text input
user_input = st.text_area("Enter text here...")
if st.button('Analyze'):
# Displaying output
result = predict_sentiment(user_input, model)
if result >= 0.5:
st.write('The sentiment is: Positive π')
else:
st.write('The sentiment is: Negative π')
st.caption("Por @efeperro con β€οΈ. Credits to π€") |