-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathui_streamlit.py
More file actions
83 lines (64 loc) · 2.15 KB
/
ui_streamlit.py
File metadata and controls
83 lines (64 loc) · 2.15 KB
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
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
import streamlit as st
import pandas as pd
import numpy as np
import matplotlib.pyplot as plt
import seaborn as sns
from sklearn.metrics import classification_report, confusion_matrix
from sklearn.preprocessing import MinMaxScaler
from sklearn.naive_bayes import MultinomialNB
from joblib import load
import spacy
import time
from wordcloud import WordCloud
nlp = spacy.load("en_core_web_lg")
__model = None
# for preprocessing
def preprocess(text):
doc = nlp(text)
filtered_tokens = []
for token in doc:
if token.is_stop or token.is_punct:
continue
filtered_tokens.append(token.lemma_)
return " ".join(filtered_tokens)
# vectorise
def vectorize(text):
return nlp(text).vector.reshape(1, -1)
# for finding the bias
def get_bias(text):
clean_text = preprocess(text)
embeddings = vectorize(clean_text)
prediction = __model.predict(embeddings)[0]
if prediction == -1:
return "Negative"
elif prediction == 1:
return "Positive"
else:
return "Neutral"
# for loading the model
def load_saved_model():
global __model
with open("model.joblib", "rb") as f:
__model = load(f)
# Main function to run the Streamlit app
def main():
load_saved_model()
st.title('Sentiment Analysis Web App')
st.sidebar.title('Navigation')
page = st.sidebar.radio('Go to', ('Home', 'Predict', 'About'))
if page == 'Home':
st.header('Home Page')
st.write('Welcome to Sentiment Analysis Web App!')
st.write('This app predicts sentiment (Positive, Negative, Neutral) based on user input.')
elif page == 'Predict':
st.header('Predict Sentiment')
text = st.text_area('Enter your comment here:', height=200)
if st.button('Predict'):
prediction = get_bias(text)
st.write(f'Predicted Sentiment: {prediction}')
elif page == 'About':
st.header('About')
st.write('This is a sentiment analysis web application.')
st.write('Created by Aman Kumar Singh.')
if __name__ == '__main__':
main()