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 # Загрузка весов модели 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) # Само приложение 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:", "") 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 execution_time_container = st.empty() # Создаем пустой контейнер для отображения времени выполнения if st.button("Analyze Sentiment"): if prediction == 1: st.markdown("
Sentiment: Positive
", unsafe_allow_html=True) else: st.markdown("Sentiment: Negative
", unsafe_allow_html=True) st.markdown(f"Execution Time: {ml_time:.5f} seconds") execution_time_container.text(f"Execution Time: {ml_time:.5f} seconds")