Spaces:
				
			
			
	
			
			
		Runtime error
		
	
	
	
			
			
	
	
	
	
		
		
		Runtime error
		
	| import streamlit as st | |
| import pandas as pd | |
| import re | |
| import joblib | |
| from sklearn.feature_extraction.text import CountVectorizer | |
| from keras.preprocessing.sequence import pad_sequences | |
| from keras.preprocessing.text import Tokenizer | |
| from keras.models import load_model | |
| from sklearn.metrics import accuracy_score | |
| # Function to clean text | |
| def clean_text(text): | |
| text = re.sub(r'<.*?>', '', text) # Remove HTML tags | |
| text = re.sub(r'[^a-zA-Z\s]', '', text) # Remove special characters and digits | |
| text = text.lower() # Convert to lowercase | |
| text = re.sub(r'\s+', ' ', text).strip() # Remove extra spaces | |
| return text | |
| # Load the pre-trained Naive Bayes model and CountVectorizer | |
| nb_model = joblib.load('nb_model.h5') | |
| count_vectorizer = joblib.load('vectorizer.joblib') | |
| # Load the pre-trained RNN model and Tokenizer | |
| rnn_model = load_model('RNN_Model.h5') | |
| tokenizer = joblib.load('tokenizer.joblib') | |
| # Define max length for padding | |
| max_length = 15 | |
| # Streamlit UI | |
| st.title(":green[Sentiment Analysis of Reviews]") | |
| st.write(""" | |
| This app predicts the sentiment of product reviews using two machine learning models: | |
| - Naive Bayes | |
| - Recurrent Neural Network (RNN) | |
| """) | |
| # Text input | |
| review_text = st.text_area("Enter a review text:", "") | |
| if st.button("Predict"): | |
| if review_text: | |
| cleaned_text = clean_text(review_text) | |
| # Naive Bayes Prediction | |
| review_cv = count_vectorizer.transform([cleaned_text]) | |
| nb_prediction = nb_model.predict(review_cv) | |
| # RNN Prediction | |
| review_seq = tokenizer.texts_to_sequences([cleaned_text]) | |
| review_pad = pad_sequences(review_seq, maxlen=max_length) | |
| rnn_prediction_prob = rnn_model.predict(review_pad) | |
| rnn_prediction = rnn_prediction_prob.argmax(axis=-1)[0] | |
| sentiment_mapping = {0: 'Negative Review', 1: 'Neutral Review', 2: 'Positive Review'} | |
| st.write("### Predictions") | |
| if nb_prediction[0] =="negative": | |
| st.success(f"**Naive Bayes Prediction: Negative Review With an Accuracy of 0.95**") | |
| elif nb_prediction[0] =="positive": | |
| st.success(f"**Naive Bayes Prediction: Positive Review With an Accuracy of 0.95**") | |
| else: | |
| st.success(f"**Naive Bayes Prediction: Neutral Review With an Accuracy of 0.95**") | |
| st.success(f"**RNN Prediction: {sentiment_mapping[rnn_prediction]} With an Accuracy of {round(rnn_prediction_prob[0][rnn_prediction],2)}**") | |
| # Display probabilities for RNN | |
| # st.write(f"**RNN Prediction Probabilities:**") | |
| # st.write(f"Negative: {rnn_prediction_prob[0][0]:.2f}") | |
| # st.write(f"Neutral: {rnn_prediction_prob[0][1]:.2f}") | |
| # st.write(f"Positive: {rnn_prediction_prob[0][2]:.2f}") | |
| else: | |
| st.write("Please enter a review text to get predictions.") | |
| # Add some style to the UI | |
| st.markdown(""" | |
| <style> | |
| .reportview-container { | |
| background: #f0f2f6; | |
| } | |
| .sidebar .sidebar-content { | |
| background: #f0f2f6; | |
| } | |
| .stButton>button { | |
| color: #ffffff; | |
| background-color: #4CAF50; | |
| border-radius: 8px; | |
| padding: 10px; | |
| border: none; | |
| cursor: pointer; | |
| } | |
| .stButton>button:hover { | |
| background-color: #red; | |
| } | |
| .stTextArea>label { | |
| font-size: 20px; | |
| color: #4CAF50; | |
| } | |
| </style> | |
| """, unsafe_allow_html=True) | |
