MossyHead commited on
Commit
c8f4172
·
1 Parent(s): 377ca3f
pages/__pycache__/film_review.cpython-312.pyc ADDED
Binary file (5.25 kB). View file
 
pages/film_review.py CHANGED
@@ -1,4 +1,87 @@
1
  import streamlit as st
 
 
 
 
 
 
 
2
 
3
- x = st.slider('Select a value')
4
- st.write(x, 'squared is', x * x)
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
  import streamlit as st
2
+ import torch
3
+ from models.p_logreg import predict_tfidf
4
+ from models.p_bert import predict_bert
5
+ from models.p_lstm import *
6
+ import time
7
+ import os
8
+ import pandas as pd
9
 
10
+
11
+ device = torch.device("cuda" if torch.cuda.is_available() else "cpu")
12
+
13
+ def classify_reviews():
14
+ st.title("Классификация отзывов к фильмам")
15
+ input_text = st.text_area("Напишите свой отзыв", height=100)
16
+
17
+ if st.button("Классифицировать отзыв"):
18
+ if input_text:
19
+ with st.spinner('Идет классификация...'):
20
+ model_lstm = MyModel()
21
+ model_lstm.load_state_dict(torch.load(get_model_path(), map_location=device).state_dict())
22
+ model_lstm.to(device)
23
+ model_lstm.eval()
24
+
25
+ # TF-IDF
26
+ start_time_tfidf = time.time()
27
+ prediction_tfidf = predict_tfidf(input_text)
28
+ time_tfidf = time.time() - start_time_tfidf
29
+
30
+ # LSTM with Attention
31
+ start_time_lstm = time.time()
32
+ with torch.no_grad():
33
+ # input_ids = tokenize(input_text)
34
+ # input_ids = torch.tensor([tokenize(input_text)]).to(device)
35
+ input_ids = torch.tensor([tokenize(input_text)], device=device)
36
+ prediction_lstm = torch.argmax(torch.nn.functional.softmax(model_lstm(input_ids.to(device)), dim=1), dim=1).item()
37
+
38
+ time_lstm = time.time() - start_time_lstm
39
+
40
+ # BERT
41
+ start_time_bert = time.time()
42
+ prediction_bert = predict_bert(input_text)
43
+ time_bert = time.time() - start_time_bert
44
+
45
+ # st.write("TF-IDF - отзыв:", "нейтральный" if prediction_tfidf == 1 else ("положительный" if prediction_tfidf == 2 else "отрицательный"), ", время:", round(time_tfidf * 1000, 2), "мс")
46
+ # st.write("LSTM - отзыв:", "нейтральный" if prediction_lstm == 1 else ("положительный" if prediction_lstm == 2 else "отрицательный"), ", время:", round(time_lstm * 1000, 2), "мс")
47
+ # st.write("BERT - отзыв:", "нейтральный" if prediction_bert == 1 else ("положительный" if prediction_bert == 2 else "отрицательный"), ", время:", round(time_bert * 1000, 2), "мс")
48
+
49
+
50
+ # Define colors based on predictions
51
+ color_tfidf = "blue" if prediction_tfidf == 1 else ("green" if prediction_tfidf == 2 else "red")
52
+ color_lstm = "blue" if prediction_lstm == 1 else ("green" if prediction_lstm == 2 else "red")
53
+ color_bert = "blue" if prediction_bert == 1 else ("green" if prediction_bert == 2 else "red")
54
+
55
+ # Write predictions with colored text
56
+ st.markdown(
57
+ f"TF-IDF - отзыв: <span style='color:{color_tfidf}'>{ 'нейтральный' if prediction_tfidf == 1 else ('положительный' if prediction_tfidf == 2 else 'отрицательный')}</span>, время: {round(time_tfidf * 1000, 2)} мс",
58
+ unsafe_allow_html=True
59
+ )
60
+ st.markdown(
61
+ f"LSTM - отзыв: <span style='color:{color_lstm}'>{ 'нейтральный' if prediction_lstm == 1 else ('положительный' if prediction_lstm == 2 else 'отрицательный')}</span>, время: {round(time_lstm * 1000, 2)} мс",
62
+ unsafe_allow_html=True
63
+ )
64
+ st.markdown(
65
+ f"BERT - отзыв: <span style='color:{color_bert}'>{ 'нейтральный' if prediction_bert == 1 else ('положительный' if prediction_bert == 2 else 'отрицательный')}</span>, время: {round(time_bert * 1000, 2)} мс",
66
+ unsafe_allow_html=True
67
+ )
68
+
69
+
70
+
71
+
72
+ st.write("------------")
73
+
74
+ metrics = {
75
+ "Models": ["TF-IDF+LogReg", "LSTM + attention", "ruBERTtiny2"],
76
+ "f1-macro score": [0.6982, 0.6977, 0.6957],
77
+ }
78
+ df = pd.DataFrame(metrics)
79
+ df.set_index("Models", inplace=True)
80
+ df.style.set_caption("Model Performance")
81
+ df.index.name = "Модель"
82
+ st.write(df)
83
+
84
+ def get_model_path():
85
+ current_dir = os.path.dirname(__file__)
86
+ return os.path.join(current_dir, "..", "models", "model_lstm.pt")
87
+ classify_reviews()
pages/pages_Токсичность.py ADDED
@@ -0,0 +1,16 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import streamlit as st
2
+ import time
3
+ from models.models_predict_toxicity import predict_toxicity
4
+
5
+ def toxicity_page():
6
+ st.title("Оценка степени токсичности сообщения")
7
+ message_input = st.text_area("Введите ваше сообщение:")
8
+
9
+ if st.button("Оценить токсичность"):
10
+ if message_input:
11
+ start_time = time.time()
12
+ prediction, probability = predict_toxicity(message_input)
13
+ predict_time = time.time() - start_time
14
+ st.write(f"Степень токсичности: {prediction} (вероятность: {probability:.4f}, время: {predict_time:.4f} секунд)")
15
+
16
+ toxicity_page()
pages/toxic_massages.py DELETED
@@ -1,4 +0,0 @@
1
- import streamlit as st
2
-
3
- x = st.slider('Select a value')
4
- st.write(x, 'squared is', x * x)