File size: 1,437 Bytes
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
import streamlit as st 
from functions_preprocess import LinguisticPreprocessor
import pickle


#################################################################### 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 πŸ€—")