Files changed (1) hide show
  1. pages/📷 CritiSense.py +78 -24
pages/📷 CritiSense.py CHANGED
@@ -16,20 +16,51 @@ def clean(text):
16
  text = text.translate(str.maketrans('', '', string.punctuation))
17
  return text
18
 
19
- # Загрузка весов модели
 
 
 
 
20
 
21
- model_filename = 'model_weights.pkl'
22
- with open(model_filename, 'rb') as file:
23
- model = pickle.load(file)
 
 
 
24
 
25
- # Загрузка весов векторизатора
26
- vectorizer = CountVectorizer()
27
- vectorizer_filename = 'vectorizer_weights.pkl'
28
- with open(vectorizer_filename, 'rb') as file:
29
- vectorizer = pickle.load(file)
 
 
 
30
 
31
- # Само приложение
 
 
 
 
 
 
 
 
 
 
32
 
 
 
 
 
 
 
 
 
 
 
 
33
  st.title("CritiSense")
34
  st.subheader("Movie Review Sentiment Analyzer")
35
  st.write("CritiSense is a powerful app that analyzes the sentiment of movie reviews.")
@@ -37,21 +68,44 @@ st.write("Whether you want to know if a review is positive or negative, CritiSen
37
  st.write("Just enter the review, and our app will provide you with instant sentiment analysis.")
38
  st.write("Make informed decisions about movies with CritiSense!")
39
  user_review = st.text_input("Enter your review:", "")
40
- user_review_clean = clean(user_review)
41
- user_features = vectorizer.transform([user_review_clean])
42
- start_ml=time.time()
43
- prediction = model.predict(user_features)
44
- end_ml=time.time()
45
- st.write("Review:", user_review)
46
- ml_time=end_ml-start_ml
47
-
48
- execution_time_container = st.empty() # Создаем пустой контейнер для отображения времени выполнения
49
-
50
- if st.button("Analyze Sentiment"):
51
- if prediction == 1:
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
52
  st.markdown("<p style='color: green;'>Sentiment: Positive</p>", unsafe_allow_html=True)
53
  else:
54
  st.markdown("<p style='color: red;'>Sentiment: Negative</p>", unsafe_allow_html=True)
55
- st.markdown(f"Execution Time: {ml_time:.5f} seconds")
56
- execution_time_container.text(f"Execution Time: {ml_time:.5f} seconds")
57
 
 
16
  text = text.translate(str.maketrans('', '', string.punctuation))
17
  return text
18
 
19
+ # Загрузка весов модели и векторизатора
20
+ def load_model_ml() : # return model
21
+ model_filename = 'model_weights.pkl'
22
+ with open(model_filename, 'rb') as file:
23
+ model = pickle.load(file)
24
 
25
+ vectorizer = CountVectorizer()
26
+ vectorizer_filename = 'vectorizer_weights.pkl'
27
+ with open(vectorizer_filename, 'rb') as file:
28
+ vectorizer = pickle.load(file)
29
+
30
+ return model, vectorizer
31
 
32
+ def predict_ml(model, vectorizer, user_review) :
33
+ user_review_clean = clean(user_review)
34
+ user_features = vectorizer.transform([user_review_clean])
35
+ start_ml=time.time()
36
+ prediction = model.predict(user_features)
37
+ end_ml=time.time()
38
+ st.write("Review:", user_review)
39
+ ml_time=end_ml-start_ml
40
 
41
+ return prediction, ml_time
42
+
43
+ #Placeholder for RNN
44
+ def load_model_rnn() : # return model
45
+ return # model
46
+
47
+ #Placeholder for RNN
48
+ def predict_rnn(model, user_review) :
49
+ prediction = 1
50
+ time = 0
51
+ return prediction, time
52
 
53
+ #Placeholder for BERT
54
+ def load_model_bert() : # return model
55
+ return # model
56
+
57
+ #Placeholder for BERT
58
+ def predict_bert(model, user_review) :
59
+ prediction = 1
60
+ time = 0
61
+ return prediction, time
62
+
63
+ # Само приложение
64
  st.title("CritiSense")
65
  st.subheader("Movie Review Sentiment Analyzer")
66
  st.write("CritiSense is a powerful app that analyzes the sentiment of movie reviews.")
 
68
  st.write("Just enter the review, and our app will provide you with instant sentiment analysis.")
69
  st.write("Make informed decisions about movies with CritiSense!")
70
  user_review = st.text_input("Enter your review:", "")
71
+
72
+ # Создаем пустой контейнер для отображения времени выполнения
73
+ execution_time_container = st.empty()
74
+ if st.button("Analyze Sentiment using ML"):
75
+ ml_model, ml_vectorizer = load_model_ml()
76
+ ml_prediction, ml_time = predict_ml(ml_model, ml_vectorizer, user_review)
77
+
78
+ if ml_prediction == 1:
79
+ st.markdown("<p style='color: green;'>Sentiment: Positive</p>", unsafe_allow_html=True)
80
+ else:
81
+ st.markdown("<p style='color: red;'>Sentiment: Negative</p>", unsafe_allow_html=True)
82
+
83
+ st.markdown(f"Execution Time: {ml_time:.5f} seconds")
84
+ execution_time_container.text(f"Execution Time: {ml_time:.5f} seconds")
85
+
86
+ st.divider()
87
+
88
+ if st.button("Analyze Sentiment using RNN"):
89
+ rnn_model = load_model_rnn()
90
+ rnn_prediction, rnn_time = predict_rnn(rnn_model, user_review)
91
+
92
+ if rnn_prediction == 1:
93
+ st.markdown("<p style='color: green;'>Sentiment: Positive</p>", unsafe_allow_html=True)
94
+ else:
95
+ st.markdown("<p style='color: red;'>Sentiment: Negative</p>", unsafe_allow_html=True)
96
+ st.markdown(f"Execution Time: {rnn_time:.5f} seconds")
97
+ execution_time_container.text(f"Execution Time: {rnn_time:.5f} seconds")
98
+
99
+ st.divider()
100
+
101
+ if st.button("Analyze Sentiment using Bert"):
102
+ bert_model = load_model_bert()
103
+ bert_prediction, bert_time = predict_bert(bert_model, user_review)
104
+
105
+ if bert_prediction == 1:
106
  st.markdown("<p style='color: green;'>Sentiment: Positive</p>", unsafe_allow_html=True)
107
  else:
108
  st.markdown("<p style='color: red;'>Sentiment: Negative</p>", unsafe_allow_html=True)
109
+ st.markdown(f"Execution Time: {bert_time:.5f} seconds")
110
+ execution_time_container.text(f"Execution Time: {bert_time:.5f} seconds")
111