-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathStepikEval.py
More file actions
297 lines (253 loc) · 11.7 KB
/
StepikEval.py
File metadata and controls
297 lines (253 loc) · 11.7 KB
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
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
import time
from time import sleep
from datetime import datetime
import torch
from bs4 import BeautifulSoup
from selenium import webdriver
from selenium.webdriver.common.by import By
from selenium.webdriver.firefox.options import Options
from transformers import AutoTokenizer, AutoModelForSequenceClassification
from translators import translate_text
class Review:
def __init__(self):
self.scores = []
self.emotions = []
self.timestamp = None
self.stars = None
class Course:
def __init__(self, classifier):
self.reviews = []
self.classifier = classifier
self.average_stars = None
self.total_score = None
self.detail_score = None
self.deep_score = None
self.material_quality_score = None
self.problems_quality_score = None
self.coach_skills_score = None
self.practice_experience_score = None
self.feedback_score = None
self.positive = None
self.negative = None
self.positive_part = None
self.negative_part = None
self.important_positive_part = None
self.important_negative_part = None
self.important_rews_amount = None
self.important_rews_part = None
def get_info(self, url: str):
self.reviews.clear()
self.average_stars = 0
self.total_score = 0
self.detail_score = 0
self.deep_score = 0
self.material_quality_score = 0
self.problems_quality_score = 0
self.coach_skills_score = 0
self.practice_experience_score = 0
self.feedback_score = 0
self.positive = 0
self.negative = 0
self.positive_part = 0
self.negative_part = 0
self.important_positive_part = 0
self.important_negative_part = 0
self.important_rews_amount = 0
self.important_rews_part = 0
options = Options()
options.add_argument("--headless")
browser = webdriver.Firefox(options=options)
browser.get(url)
sleep(3)
print('Сбор данных...')
try:
button = browser.find_elements(By.CSS_SELECTOR,
"button:not(.st-button_style_none).btn-details")[-1]
while button.text == 'Показать ещё':
try:
sleep(0.02)
button = browser.find_elements(By.CSS_SELECTOR,
"button:not(.st-button_style_none).btn-details")[-1]
if button.text == 'Показать ещё':
button.click()
else:
break
except Exception as exp:
print(exp)
break
except Exception as exp:
print(exp)
html = browser.page_source
browser.close()
bs = BeautifulSoup(html, 'html.parser')
rews = bs.find_all('div', {'class': 'show-more__content'})
dates = bs.find_all('time', {'class': 'course-review-card__date'})
global_exeption = None
stamps = []
vect_7_ids = []
vect_7_masks = []
vect_emotions_ids = []
vect_emotions_masks = []
x = time.time()
text = []
for rev, date in zip(rews, dates):
try:
dt = datetime.strptime(date['datetime'], '%Y-%m-%dT%H:%M:%S.%fZ')
stamps.append(datetime.timestamp(dt))
text.append(rev.contents[0])
self.reviews.append(Review())
except Exception as exp:
print(exp)
global_exeption = exp
continue
if len(text) > 0:
vect_7_ids, vect_7_masks = self.classifier.get_ids_and_mask(text)
vect_emotions_ids, vect_emotions_masks = self.classifier.get_ids_and_mask(text)
output_data_for7 = self.classifier.predict_stack(vect_7_ids, vect_7_masks)
output_data_for_emotions = self.classifier.predict_stack_emotions(vect_emotions_ids, vect_emotions_masks)
for scores, emotions, review, stamp in zip(output_data_for7, output_data_for_emotions, self.reviews, stamps):
review.scores = scores
review.emotions = emotions
review.timestamp = stamp
if scores[0] >= 0.5:
self.important_rews_amount = self.important_rews_amount + 1
self.detail_score = self.detail_score + scores[0]
self.deep_score = self.deep_score + scores[1]
self.material_quality_score = self.material_quality_score + scores[2]
self.problems_quality_score = self.problems_quality_score + scores[3]
self.coach_skills_score = self.coach_skills_score + scores[4]
self.practice_experience_score = self.practice_experience_score + scores[5]
self.feedback_score = self.feedback_score + scores[6]
self.negative = self.negative + emotions[0]
self.positive = self.positive + emotions[1]
if emotions[0] > emotions[1]:
self.negative_part = self.negative_part + 1
if scores[0] >= 0.5:
self.important_negative_part = self.important_negative_part + 1
else:
self.positive_part = self.positive_part + 1
if scores[0] >= 0.5:
self.important_positive_part = self.important_positive_part + 1
try:
lenth = len(self.reviews)
self.detail_score = self.detail_score / lenth
self.deep_score = self.deep_score / lenth
self.material_quality_score = self.material_quality_score / lenth
self.problems_quality_score = self.problems_quality_score / lenth
self.coach_skills_score = self.coach_skills_score / lenth
self.practice_experience_score = self.practice_experience_score / lenth
self.feedback_score = self.feedback_score / lenth
self.negative = self.negative / lenth
self.positive = self.positive / lenth
self.negative_part = self.negative_part / lenth
self.positive_part = self.positive_part / lenth
self.important_positive_part = self.important_positive_part / self.important_rews_amount
self.important_negative_part = self.important_negative_part / self.important_rews_amount
self.important_rews_part = self.important_rews_amount / lenth
stars_parsed = []
summ = 0
i = 0
sections = bs.find_all('section', {'class': 'course-promo__main'})
for section in sections:
for div in section.find_all('div', {'class': 'course-review-card course-promo-reviews__item'}):
for sp in div.find_all('span'):
for span in sp.find_all('span'):
try:
span_cl = span['class'][1]
if 'colored-star' in span_cl:
summ = summ + 1
i = i + 1
elif 'uncolored-star' in span_cl:
i = i + 1
if i == 5:
stars_parsed.append(summ)
summ = 0
i = 0
except Exception as exp:
print('У span нет класса', exp)
self.average_stars = sum(stars_parsed) / len(stars_parsed)
tech = 0
for rew, star in zip(self.reviews, stars_parsed):
rew.stars = star
for i in range(1, 7):
tech = rew.scores[i] + tech
self.total_score = self.total_score + tech * (rew.emotions[1] - rew.emotions[0]) * rew.scores[0]
tech = 0
self.total_score = 100 * ((((self.total_score / (self.detail_score * lenth)) / 6) + 1) / 2)
print(f"Время оценки: {time.time()-x}")
return self.total_score
except Exception as exp:
print('Ошибка.', exp)
return global_exeption
else:
raise Exception('Course has no reviews')
class CourseClassifier:
def __init__(self):
model_name = "cointegrated/rubert-tiny2"
num_labels = 7
device = 'cpu'
self.model = AutoModelForSequenceClassification.from_pretrained(model_name, num_labels=num_labels)
self.model.load_state_dict(
torch.load('models/categories.pth', weights_only=False, map_location=torch.device(device)))
self.model.eval()
self.binary_model = AutoModelForSequenceClassification.from_pretrained(model_name, num_labels=2)
self.binary_model.load_state_dict(
torch.load('models/binary.pth', weights_only=False, map_location=torch.device(device)))
self.binary_model.eval()
self.tokenizer = AutoTokenizer.from_pretrained(model_name)
def predict_stack(self, ids, mask):
with torch.no_grad():
output = self.model(ids, mask)
if isinstance(output, tuple):
output = output[0]
return torch.sigmoid(output.logits).tolist()
def predict_stack_emotions(self, ids, mask):
with torch.no_grad():
output = self.binary_model(ids, mask)
if isinstance(output, tuple):
output = output[0]
return torch.sigmoid(output.logits).tolist()
def get_ids_and_mask(self, text: str):
encodings = self.tokenizer(
text,
truncation=True,
padding=True,
max_length=512,
return_tensors='pt'
)
input_ids = encodings["input_ids"].clone().detach()
attention_mask = encodings['attention_mask']
return input_ids, attention_mask
def predict_scores(self, text: str):
encodings = self.tokenizer(
text,
truncation=True,
padding=True,
max_length=512,
return_tensors='pt'
)
input_ids = encodings["input_ids"].clone().detach()
attention_mask = encodings['attention_mask']
output = None
with torch.no_grad():
output = self.model(input_ids, attention_mask)
if isinstance(output, tuple):
output = output[0]
return torch.sigmoid(output.logits).tolist()
def predict_emotions(self, text: str):
text = translate_text(text, to_language='en')
encodings = self.tokenizer(
text,
truncation=True,
padding=True,
max_length=512,
return_tensors='pt'
)
input_ids = encodings["input_ids"].clone().detach()
attention_mask = encodings['attention_mask']
output = None
with torch.no_grad():
output = self.binary_model(input_ids, attention_mask)
if isinstance(output, tuple):
output = output[0]
return torch.sigmoid(output.logits).tolist()