history_mistery / pages /📷 CritiSense.py
SaviAnna's picture
Added buttons (#2)
5707064
from sklearn.feature_extraction.text import CountVectorizer
from sklearn.linear_model import LogisticRegression
import re
import string
import pickle
import time
import streamlit as st
# Функция очистки текста
def clean(text):
text = text.lower() # нижний регистр
text = re.sub(r'http\S+', " ", text) # удаляем ссылки
text = re.sub(r'@\w+',' ',text) # удаляем упоминания пользователей
text = re.sub(r'#\w+', ' ', text) # удаляем хэштеги
text = re.sub(r'\d+', ' ', text) # удаляем числа
text = text.translate(str.maketrans('', '', string.punctuation))
return text
# Загрузка весов модели и векторизатора
def load_model_ml() : # return model
model_filename = 'model_weights.pkl'
with open(model_filename, 'rb') as file:
model = pickle.load(file)
vectorizer = CountVectorizer()
vectorizer_filename = 'vectorizer_weights.pkl'
with open(vectorizer_filename, 'rb') as file:
vectorizer = pickle.load(file)
return model, vectorizer
def predict_ml(model, vectorizer, user_review) :
user_review_clean = clean(user_review)
user_features = vectorizer.transform([user_review_clean])
start_ml=time.time()
prediction = model.predict(user_features)
end_ml=time.time()
st.write("Review:", user_review)
ml_time=end_ml-start_ml
return prediction, ml_time
#Placeholder for RNN
def load_model_rnn() : # return model
return # model
#Placeholder for RNN
def predict_rnn(model, user_review) :
prediction = 1
time = 0
return prediction, time
#Placeholder for BERT
def load_model_bert() : # return model
return # model
#Placeholder for BERT
def predict_bert(model, user_review) :
prediction = 1
time = 0
return prediction, time
# Само приложение
st.title("CritiSense")
st.subheader("Movie Review Sentiment Analyzer")
st.write("CritiSense is a powerful app that analyzes the sentiment of movie reviews.")
st.write("Whether you want to know if a review is positive or negative, CritiSense has got you covered.")
st.write("Just enter the review, and our app will provide you with instant sentiment analysis.")
st.write("Make informed decisions about movies with CritiSense!")
user_review = st.text_input("Enter your review:", "")
# Создаем пустой контейнер для отображения времени выполнения
execution_time_container = st.empty()
if st.button("Analyze Sentiment using ML"):
ml_model, ml_vectorizer = load_model_ml()
ml_prediction, ml_time = predict_ml(ml_model, ml_vectorizer, user_review)
if ml_prediction == 1:
st.markdown("<p style='color: green;'>Sentiment: Positive</p>", unsafe_allow_html=True)
else:
st.markdown("<p style='color: red;'>Sentiment: Negative</p>", unsafe_allow_html=True)
st.markdown(f"Execution Time: {ml_time:.5f} seconds")
execution_time_container.text(f"Execution Time: {ml_time:.5f} seconds")
st.divider()
if st.button("Analyze Sentiment using RNN"):
rnn_model = load_model_rnn()
rnn_prediction, rnn_time = predict_rnn(rnn_model, user_review)
if rnn_prediction == 1:
st.markdown("<p style='color: green;'>Sentiment: Positive</p>", unsafe_allow_html=True)
else:
st.markdown("<p style='color: red;'>Sentiment: Negative</p>", unsafe_allow_html=True)
st.markdown(f"Execution Time: {rnn_time:.5f} seconds")
execution_time_container.text(f"Execution Time: {rnn_time:.5f} seconds")
st.divider()
if st.button("Analyze Sentiment using Bert"):
bert_model = load_model_bert()
bert_prediction, bert_time = predict_bert(bert_model, user_review)
if bert_prediction == 1:
st.markdown("<p style='color: green;'>Sentiment: Positive</p>", unsafe_allow_html=True)
else:
st.markdown("<p style='color: red;'>Sentiment: Negative</p>", unsafe_allow_html=True)
st.markdown(f"Execution Time: {bert_time:.5f} seconds")
execution_time_container.text(f"Execution Time: {bert_time:.5f} seconds")