Spaces:
Sleeping
Sleeping
| import os | |
| import pandas as pd | |
| import numpy as np | |
| import streamlit as st | |
| import re | |
| import pickle | |
| import nltk | |
| import string | |
| import contractions | |
| from nltk.corpus import stopwords | |
| nltk.download("stopwords") | |
| sw_list = stopwords.words('english') | |
| def remove_tags(text): | |
| return re.sub(re.compile('<.*?>'), '', text) | |
| def lwr(text): | |
| return text.lower() | |
| def stopword(text): | |
| return " ".join([word for word in text.split() if word not in sw_list]) | |
| def remove_punctuation(text): | |
| return text.translate(str.maketrans('', '', string.punctuation)) | |
| def remove_contractions(text): | |
| return contractions.fix(text) | |
| def dec_vector(doc): | |
| with open("Sentimental_Analysis_WV.pkl", 'rb') as file: | |
| model = pickle.load(file) | |
| doc = [word for word in doc.split() if word in model.wv.index_to_key] | |
| return np.mean(model.wv[doc], axis=0) | |
| def xvalue(text): | |
| X = [] | |
| X.append(dec_vector(text)) | |
| return X | |
| def preprocessed(text): | |
| text = remove_tags(text) | |
| text = lwr(text) | |
| text = stopword(text) | |
| text = remove_punctuation(text) | |
| text = remove_contractions(text) | |
| X = xvalue(text) | |
| X = np.array(X) | |
| return X | |
| def clear_text(): | |
| st.session_state["text"] = "" | |
| def main(): | |
| st.set_page_config(page_title="Sentiment Analysis AI", page_icon=":smiley:", layout="wide") | |
| with open("Sentimental_Analysis_Word2Vec.pkl", 'rb') as file1: | |
| rf = pickle.load(file1) | |
| st.title('Sentiment Analysis AI') | |
| st.markdown(""" | |
| <style> | |
| .main { | |
| background-color: #0a3d57; | |
| padding: 20px; | |
| border-radius: 10px; | |
| } | |
| .stButton > button { | |
| background-color: #4CAF50; | |
| color: white; | |
| padding: 10px 24px; | |
| border: none; | |
| border-radius: 4px; | |
| cursor: pointer; | |
| } | |
| .stButton > button:hover { | |
| background-color: #45a049; | |
| } | |
| .stTextInput > div > input { | |
| padding: 10px; | |
| border: 2px solid #ccc; | |
| border-radius: 4px; | |
| } | |
| </style> | |
| """, unsafe_allow_html=True) | |
| st.markdown("<div class='main'>", unsafe_allow_html=True) | |
| st.header('Welcome to Sentiment Analysis AI') | |
| st.subheader('Check if reviews or any text have a positive or negative sentiment instantly!') | |
| text = st.text_input( | |
| "Enter some text π", key="text", help="Type in any text to analyze its sentiment.") | |
| col1, col2 = st.columns(2) | |
| with col1: | |
| if st.button('Classify'): | |
| if text: | |
| z = preprocessed(text) | |
| if rf.predict(z)[0] == 1: | |
| st.success("Positive") | |
| else: | |
| st.success("Negative") | |
| else: | |
| st.warning("Please enter some text to analyze.") | |
| with col2: | |
| st.button("Clear", on_click=clear_text) | |
| st.markdown("</div>", unsafe_allow_html=True) | |
| if __name__ == '__main__': | |
| main() | |