Spaces:
Paused
Paused
import streamlit as st | |
from functions_preprocess import LinguisticPreprocessor | |
import pickle | |
import nltk | |
nltk.download('stopwords') | |
download_if_non_existent('corpora/stopwords', 'stopwords') | |
download_if_non_existent('taggers/averaged_perceptron_tagger', 'averaged_perceptron_tagger') | |
download_if_non_existent('corpora/wordnet', 'wordnet') | |
#################################################################### 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 | |
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 π€") |