File size: 4,152 Bytes
e3a9602
d8146a1
 
 
 
50bb034
7a5f863
 
d8146a1
 
 
 
 
 
 
 
 
 
5707064
 
 
 
 
d8146a1
5707064
 
 
 
 
 
d8146a1
5707064
 
 
 
 
 
 
 
d8146a1
5707064
 
 
 
 
 
 
 
 
 
 
d8146a1
5707064
 
 
 
 
 
 
 
 
 
 
d8146a1
 
 
 
 
 
 
5707064
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
6432174
 
 
5707064
 
ac97786
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
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")