Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
|
@@ -1,77 +1,115 @@
|
|
| 1 |
-
|
| 2 |
import os
|
| 3 |
import pandas as pd
|
| 4 |
import numpy as np
|
| 5 |
import streamlit as st
|
| 6 |
import re
|
| 7 |
import pickle
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 8 |
def remove_tags(text):
|
| 9 |
-
return re.sub(re.compile('<.*?>'),'',text)
|
| 10 |
|
| 11 |
def lwr(text):
|
| 12 |
return text.lower()
|
| 13 |
-
|
| 14 |
-
import nltk
|
| 15 |
-
|
| 16 |
-
nltk.download("stopwords")
|
| 17 |
-
from nltk.corpus import stopwords
|
| 18 |
-
sw_list=stopwords.words('english')
|
| 19 |
|
| 20 |
def stopword(text):
|
| 21 |
return " ".join([word for word in text.split() if word not in sw_list])
|
| 22 |
|
| 23 |
-
import string
|
| 24 |
def remove_punctuation(text):
|
| 25 |
return text.translate(str.maketrans('', '', string.punctuation))
|
| 26 |
|
| 27 |
-
import contractions
|
| 28 |
def remove_contractions(text):
|
| 29 |
return contractions.fix(text)
|
| 30 |
|
| 31 |
def dec_vector(doc):
|
| 32 |
with open("Sentimental_Analysis_WV.pkl", 'rb') as file:
|
| 33 |
model = pickle.load(file)
|
| 34 |
-
doc=[word for word in doc.split() if word in model.wv.index_to_key]
|
| 35 |
-
return np.mean(model.wv[doc],axis=0)
|
| 36 |
|
| 37 |
def xvalue(text):
|
| 38 |
-
X=[]
|
| 39 |
X.append(dec_vector(text))
|
| 40 |
return X
|
| 41 |
|
| 42 |
def preprocessed(text):
|
| 43 |
-
|
| 44 |
-
text=
|
| 45 |
-
text=
|
| 46 |
-
text=
|
| 47 |
-
text=
|
| 48 |
-
|
| 49 |
-
X=
|
| 50 |
-
X=np.array(X)
|
| 51 |
return X
|
| 52 |
|
| 53 |
def clear_text():
|
| 54 |
st.session_state["text"] = ""
|
| 55 |
|
| 56 |
-
|
| 57 |
def main():
|
| 58 |
-
|
| 59 |
|
| 60 |
with open("Sentimental_Analysis_Word2Vec.pkl", 'rb') as file1:
|
| 61 |
rf = pickle.load(file1)
|
| 62 |
-
st.title('Sentiment Analysis')
|
| 63 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 64 |
text = st.text_input(
|
| 65 |
-
"Enter some text π", key="text")
|
|
|
|
|
|
|
| 66 |
|
| 67 |
-
|
| 68 |
-
|
| 69 |
-
|
| 70 |
-
|
| 71 |
-
|
| 72 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 73 |
st.button("Clear", on_click=clear_text)
|
|
|
|
|
|
|
| 74 |
|
| 75 |
-
|
| 76 |
-
if __name__=='__main__':
|
| 77 |
main()
|
|
|
|
|
|
|
| 1 |
import os
|
| 2 |
import pandas as pd
|
| 3 |
import numpy as np
|
| 4 |
import streamlit as st
|
| 5 |
import re
|
| 6 |
import pickle
|
| 7 |
+
import nltk
|
| 8 |
+
import string
|
| 9 |
+
import contractions
|
| 10 |
+
|
| 11 |
+
from nltk.corpus import stopwords
|
| 12 |
+
|
| 13 |
+
nltk.download("stopwords")
|
| 14 |
+
sw_list = stopwords.words('english')
|
| 15 |
+
|
| 16 |
def remove_tags(text):
|
| 17 |
+
return re.sub(re.compile('<.*?>'), '', text)
|
| 18 |
|
| 19 |
def lwr(text):
|
| 20 |
return text.lower()
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 21 |
|
| 22 |
def stopword(text):
|
| 23 |
return " ".join([word for word in text.split() if word not in sw_list])
|
| 24 |
|
|
|
|
| 25 |
def remove_punctuation(text):
|
| 26 |
return text.translate(str.maketrans('', '', string.punctuation))
|
| 27 |
|
|
|
|
| 28 |
def remove_contractions(text):
|
| 29 |
return contractions.fix(text)
|
| 30 |
|
| 31 |
def dec_vector(doc):
|
| 32 |
with open("Sentimental_Analysis_WV.pkl", 'rb') as file:
|
| 33 |
model = pickle.load(file)
|
| 34 |
+
doc = [word for word in doc.split() if word in model.wv.index_to_key]
|
| 35 |
+
return np.mean(model.wv[doc], axis=0)
|
| 36 |
|
| 37 |
def xvalue(text):
|
| 38 |
+
X = []
|
| 39 |
X.append(dec_vector(text))
|
| 40 |
return X
|
| 41 |
|
| 42 |
def preprocessed(text):
|
| 43 |
+
text = remove_tags(text)
|
| 44 |
+
text = lwr(text)
|
| 45 |
+
text = stopword(text)
|
| 46 |
+
text = remove_punctuation(text)
|
| 47 |
+
text = remove_contractions(text)
|
| 48 |
+
X = xvalue(text)
|
| 49 |
+
X = np.array(X)
|
|
|
|
| 50 |
return X
|
| 51 |
|
| 52 |
def clear_text():
|
| 53 |
st.session_state["text"] = ""
|
| 54 |
|
|
|
|
| 55 |
def main():
|
| 56 |
+
st.set_page_config(page_title="Sentiment Analysis AI", page_icon=":smiley:", layout="wide")
|
| 57 |
|
| 58 |
with open("Sentimental_Analysis_Word2Vec.pkl", 'rb') as file1:
|
| 59 |
rf = pickle.load(file1)
|
|
|
|
| 60 |
|
| 61 |
+
st.title('Sentiment Analysis AI')
|
| 62 |
+
st.markdown("""
|
| 63 |
+
<style>
|
| 64 |
+
.main {
|
| 65 |
+
background-color: #f5f5f5;
|
| 66 |
+
padding: 20px;
|
| 67 |
+
border-radius: 10px;
|
| 68 |
+
}
|
| 69 |
+
.stButton > button {
|
| 70 |
+
background-color: #4CAF50;
|
| 71 |
+
color: white;
|
| 72 |
+
padding: 10px 24px;
|
| 73 |
+
border: none;
|
| 74 |
+
border-radius: 4px;
|
| 75 |
+
cursor: pointer;
|
| 76 |
+
}
|
| 77 |
+
.stButton > button:hover {
|
| 78 |
+
background-color: #45a049;
|
| 79 |
+
}
|
| 80 |
+
.stTextInput > div > input {
|
| 81 |
+
padding: 10px;
|
| 82 |
+
border: 2px solid #ccc;
|
| 83 |
+
border-radius: 4px;
|
| 84 |
+
}
|
| 85 |
+
</style>
|
| 86 |
+
""", unsafe_allow_html=True)
|
| 87 |
+
|
| 88 |
+
st.markdown("<div class='main'>", unsafe_allow_html=True)
|
| 89 |
+
|
| 90 |
+
st.header('Welcome to Sentiment Analysis AI')
|
| 91 |
+
st.subheader('Analyze the sentiment of your text in an instant!')
|
| 92 |
+
|
| 93 |
text = st.text_input(
|
| 94 |
+
"Enter some text π", key="text", help="Type in any text to analyze its sentiment.")
|
| 95 |
+
|
| 96 |
+
col1, col2 = st.columns(2)
|
| 97 |
|
| 98 |
+
with col1:
|
| 99 |
+
if st.button('Classify'):
|
| 100 |
+
if text:
|
| 101 |
+
z = preprocessed(text)
|
| 102 |
+
if rf.predict(z)[0] == 1:
|
| 103 |
+
st.success("Positive")
|
| 104 |
+
else:
|
| 105 |
+
st.success("Negative")
|
| 106 |
+
else:
|
| 107 |
+
st.warning("Please enter some text to analyze.")
|
| 108 |
+
|
| 109 |
+
with col2:
|
| 110 |
st.button("Clear", on_click=clear_text)
|
| 111 |
+
|
| 112 |
+
st.markdown("</div>", unsafe_allow_html=True)
|
| 113 |
|
| 114 |
+
if __name__ == '__main__':
|
|
|
|
| 115 |
main()
|