-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathapp.py
54 lines (43 loc) · 1.45 KB
/
app.py
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
45
46
47
48
49
50
51
52
53
54
import nltk
nltk.download('stopwords')
nltk.download('punkt')
import numpy as np
import streamlit as st
import pickle
from nltk.corpus import stopwords
from nltk.tokenize import word_tokenize
from nltk.stem import PorterStemmer
sw = stopwords.words('english')
sw.remove('not')
sw = set(sw)
ps = PorterStemmer()
def cleaning_pipeline(review):
words = word_tokenize(review.lower())
words = [ps.stem(word) for word in words if word not in sw and word.isalpha()]
review = " ".join(words)
return review
def load_model(path):
with open(path, 'rb') as f:
cv, model = pickle.load(f)
return cv, model
def predict(text):
cv, model = load_model("./artifacts/MNBmodel2.pkl")
cleaned_text = cleaning_pipeline(text)
text_vect = cv.transform([cleaned_text])
return model.predict(text_vect)
def run():
st.set_page_config(layout='centered',
page_title="Sentiment Predicter")
st.title("Predicting Movie Review Sentiment")
st.header('This app is created to predict if the sentiment of a movie review is positive or negative')
text = st.text_area('Enter text')
output = ""
if st.button("Predict"):
output = predict(text)
output = str(output[0]) # since its a list, get the 1st item
if output == '1':
st.success("The predicted sentiment is positive")
else:
st.error("The predicted sentiment is negative")
st.balloons()
run()