Kotann commited on
Commit
076c251
·
verified ·
1 Parent(s): 9f60e14

Upload 3 files

Browse files
Files changed (3) hide show
  1. app.py +109 -0
  2. model.pth +3 -0
  3. vocab.tsv +562 -0
app.py ADDED
@@ -0,0 +1,109 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import streamlit as st
2
+ import os
3
+ import torch
4
+ from transformers import BertTokenizer
5
+ from transformers import BertForSequenceClassification
6
+
7
+
8
+ @st.cache_resource # кэширование
9
+ def get_model(unzip_root: str='./'):
10
+ """
11
+ unzip_root ~ в тестовой среде будет произведена операция `unzip archive.zip` с переданным архивом и в эту функцию будет передан путь до `realpath .`
12
+ """
13
+ checkpoint_path = os.path.join(unzip_root, "model.pth")
14
+ checkpoint = torch.load(checkpoint_path, map_location="cpu")
15
+
16
+ model_path = 'cointegrated/rubert-tiny'
17
+ model = BertForSequenceClassification.from_pretrained(model_path)
18
+ out_features = model.bert.encoder.layer[1].output.dense.out_features
19
+ model.classifier = torch.nn.Linear(out_features, len(dict_tetm_to_int))
20
+
21
+ model.load_state_dict(checkpoint)
22
+ return model
23
+
24
+
25
+ @st.cache_resource # кэширование
26
+ def get_tokenizer():
27
+ tokenizer_path = 'cointegrated/rubert-tiny'
28
+ tokenizer = BertTokenizer.from_pretrained(tokenizer_path)
29
+ return tokenizer
30
+
31
+
32
+ @st.cache_resource # кэширование
33
+ def get_vocab(unzip_root: str='./'):
34
+ """
35
+ unzip_root ~ в тестовой среде будет произведена операция `unzip archive.zip` с переданным архивом и в эту функцию будет передан путь до `realpath .`
36
+ """
37
+ path = os.path.join(unzip_root, "vocab.tsv")
38
+
39
+ with open(path, 'r') as f:
40
+ size_dict = int(f.readline())
41
+ dict_tetm_to_int = dict()
42
+ for _ in range(size_dict):
43
+ key = f.readline()[:-1]
44
+ dict_tetm_to_int[key] = int(f.readline())
45
+
46
+ size_dict = int(f.readline())
47
+ dict_int_to_term = dict()
48
+ for _ in range(size_dict):
49
+ key = int(f.readline())
50
+ dict_int_to_term[key] = f.readline()[:-1]
51
+
52
+ return dict_tetm_to_int, dict_int_to_term
53
+
54
+
55
+ softmax = torch.nn.Softmax(dim=1)
56
+ dict_tetm_to_int, dict_int_to_term = get_vocab()
57
+ model = get_model()
58
+ tokenizer = get_tokenizer()
59
+
60
+
61
+ def predict(text, device='cpu'):
62
+ encoding = tokenizer.encode_plus(
63
+ text,
64
+ add_special_tokens=True,
65
+ max_length=512,
66
+ return_token_type_ids=False,
67
+ truncation=True,
68
+ padding='max_length',
69
+ return_attention_mask=True,
70
+ return_tensors='pt',
71
+ )
72
+
73
+ out = {
74
+ 'text': text,
75
+ 'input_ids': encoding['input_ids'].flatten(),
76
+ 'attention_mask': encoding['attention_mask'].flatten()
77
+ }
78
+
79
+ input_ids = out["input_ids"].to(device)
80
+ attention_mask = out["attention_mask"].to(device)
81
+
82
+ outputs = model(
83
+ input_ids=input_ids.unsqueeze(0),
84
+ attention_mask=attention_mask.unsqueeze(0)
85
+ )
86
+
87
+ out = softmax(outputs.logits)
88
+ prediction = torch.argsort(outputs.logits, dim=1, descending=True).cpu()[0]
89
+ sum_answer = 0
90
+ answer = []
91
+ idx = 0
92
+ while sum_answer < 0.95:
93
+ sum_answer += out[0][idx].item()
94
+ answer.append(dict_int_to_term[prediction[idx].item()])
95
+ idx += 1
96
+
97
+ return answer
98
+
99
+
100
+ st.title("We will help you determine what topic your article belongs to:)")
101
+ st.header("Please enter a title and/or introduction")
102
+
103
+
104
+ title = st.text_input(label="Title", value="")
105
+ abstract = st.text_input(label="Abstract", value="")
106
+ if(st.button('Show result')):
107
+ predict = ' '.join(predict(title.title() + ' ' + abstract.title()))
108
+ result = 'Suggested answer:\n' + predict
109
+ st.success(result)
model.pth ADDED
@@ -0,0 +1,3 @@
 
 
 
 
1
+ version https://git-lfs.github.com/spec/v1
2
+ oid sha256:0b719d3530078cfa62d0f0bb8f203a117cfda27c808e236800614a0d53b619df
3
+ size 47331569
vocab.tsv ADDED
@@ -0,0 +1,562 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ 140
2
+ cs.AI
3
+ 0
4
+ cs.CL
5
+ 1
6
+ cs.CV
7
+ 2
8
+ cs.NE
9
+ 3
10
+ stat.ML
11
+ 4
12
+ cs.LG
13
+ 5
14
+ physics.soc-ph
15
+ 6
16
+ stat.AP
17
+ 7
18
+ cs.RO
19
+ 8
20
+ cs.SE
21
+ 9
22
+ cs.MA
23
+ 10
24
+ math.OC
25
+ 11
26
+ cs.IR
27
+ 12
28
+ cond-mat.dis-nn
29
+ 13
30
+ stat.ME
31
+ 14
32
+ physics.chem-ph
33
+ 15
34
+ cs.DC
35
+ 16
36
+ stat.CO
37
+ 17
38
+ q-bio.NC
39
+ 18
40
+ cs.GT
41
+ 19
42
+ cs.MM
43
+ 20
44
+ cs.CG
45
+ 21
46
+ cs.CR
47
+ 22
48
+ cs.HC
49
+ 23
50
+ cs.GL
51
+ 24
52
+ eess.AS
53
+ 25
54
+ cs.SD
55
+ 26
56
+ math.DS
57
+ 27
58
+ cs.GR
59
+ 28
60
+ math.NA
61
+ 29
62
+ cs.CY
63
+ 30
64
+ physics.data-an
65
+ 31
66
+ math.ST
67
+ 32
68
+ stat.TH
69
+ 33
70
+ cs.IT
71
+ 34
72
+ math.IT
73
+ 35
74
+ astro-ph.IM
75
+ 36
76
+ astro-ph.GA
77
+ 37
78
+ cs.SI
79
+ 38
80
+ cs.DB
81
+ 39
82
+ cs.LO
83
+ 40
84
+ nlin.AO
85
+ 41
86
+ cs.PF
87
+ 42
88
+ cs.ET
89
+ 43
90
+ eess.IV
91
+ 44
92
+ cs.AR
93
+ 45
94
+ cs.SY
95
+ 46
96
+ cs.CC
97
+ 47
98
+ q-bio.BM
99
+ 48
100
+ q-bio.QM
101
+ 49
102
+ cs.NI
103
+ 50
104
+ cs.DS
105
+ 51
106
+ cond-mat.stat-mech
107
+ 52
108
+ cs.NA
109
+ 53
110
+ cs.DM
111
+ 54
112
+ eess.SP
113
+ 55
114
+ cs.MS
115
+ 56
116
+ physics.med-ph
117
+ 57
118
+ physics.optics
119
+ 58
120
+ q-fin.CP
121
+ 59
122
+ cs.FL
123
+ 60
124
+ cs.SC
125
+ 61
126
+ q-fin.EC
127
+ 62
128
+ q-fin.TR
129
+ 63
130
+ cond-mat.mes-hall
131
+ 64
132
+ math.PR
133
+ 65
134
+ q-fin.RM
135
+ 66
136
+ nlin.CD
137
+ 67
138
+ cs.CE
139
+ 68
140
+ math.AT
141
+ 69
142
+ stat.OT
143
+ 70
144
+ physics.ao-ph
145
+ 71
146
+ math.SP
147
+ 72
148
+ cs.PL
149
+ 73
150
+ math.AP
151
+ 74
152
+ math.FA
153
+ 75
154
+ physics.geo-ph
155
+ 76
156
+ q-bio.TO
157
+ 77
158
+ physics.comp-ph
159
+ 78
160
+ cs.DL
161
+ 79
162
+ math.CO
163
+ 80
164
+ physics.flu-dyn
165
+ 81
166
+ math.MG
167
+ 82
168
+ astro-ph.EP
169
+ 83
170
+ q-bio.CB
171
+ 84
172
+ math.RA
173
+ 85
174
+ astro-ph.CO
175
+ 86
176
+ cond-mat.mtrl-sci
177
+ 87
178
+ q-fin.ST
179
+ 88
180
+ q-bio.GN
181
+ 89
182
+ nlin.CG
183
+ 90
184
+ nlin.PS
185
+ 91
186
+ math.HO
187
+ 92
188
+ q-fin.GN
189
+ 93
190
+ math.LO
191
+ 94
192
+ math.CT
193
+ 95
194
+ cs.CL, cs.AI, math.CT
195
+ 96
196
+ q-bio.PE
197
+ 97
198
+ astro-ph.SR
199
+ 98
200
+ q-fin.PM
201
+ 99
202
+ physics.bio-ph
203
+ 100
204
+ math.AG
205
+ 101
206
+ cs.OH
207
+ 102
208
+ math.DG
209
+ 103
210
+ astro-ph.HE
211
+ 104
212
+ econ.EM
213
+ 105
214
+ math.QA
215
+ 106
216
+ q-bio.SC
217
+ 107
218
+ math.GM
219
+ 108
220
+ q-bio.MN
221
+ 109
222
+ math.GT
223
+ 110
224
+ math.AC
225
+ 111
226
+ math.CA
227
+ 112
228
+ cond-mat.str-el
229
+ 113
230
+ math.GN
231
+ 114
232
+ aaai.org
233
+ 115
234
+ cond-mat.supr-con
235
+ 116
236
+ q-bio.OT
237
+ 117
238
+ physics.ins-det
239
+ 118
240
+ physics.app-ph
241
+ 119
242
+ math.RT
243
+ 120
244
+ math.MP
245
+ 121
246
+ A.m
247
+ 122
248
+ H.m
249
+ 123
250
+ physics.class-ph
251
+ 124
252
+ q-fin.PR
253
+ 125
254
+ physics.space-ph
255
+ 126
256
+ physics.gen-ph
257
+ 127
258
+ cond-mat.other
259
+ 128
260
+ math.GR
261
+ 129
262
+ cond-mat.quant-gas
263
+ 130
264
+ math.OA
265
+ 131
266
+ physics.hist-ph
267
+ 132
268
+ math.NT
269
+ 133
270
+ cs.OS
271
+ 134
272
+ cond-mat.soft
273
+ 135
274
+ q-bio.BM, q-bio.MN, q-bio.NC, nlin.AO, nlin.CD
275
+ 136
276
+ nlin.AO, nlin.CD, q-bio.NC, physics.bio-ph, cond-mat.dis-nn
277
+ 137
278
+ physics.pop-ph
279
+ 138
280
+ math.CV
281
+ 139
282
+ 140
283
+ 0
284
+ cs.AI
285
+ 1
286
+ cs.CL
287
+ 2
288
+ cs.CV
289
+ 3
290
+ cs.NE
291
+ 4
292
+ stat.ML
293
+ 5
294
+ cs.LG
295
+ 6
296
+ physics.soc-ph
297
+ 7
298
+ stat.AP
299
+ 8
300
+ cs.RO
301
+ 9
302
+ cs.SE
303
+ 10
304
+ cs.MA
305
+ 11
306
+ math.OC
307
+ 12
308
+ cs.IR
309
+ 13
310
+ cond-mat.dis-nn
311
+ 14
312
+ stat.ME
313
+ 15
314
+ physics.chem-ph
315
+ 16
316
+ cs.DC
317
+ 17
318
+ stat.CO
319
+ 18
320
+ q-bio.NC
321
+ 19
322
+ cs.GT
323
+ 20
324
+ cs.MM
325
+ 21
326
+ cs.CG
327
+ 22
328
+ cs.CR
329
+ 23
330
+ cs.HC
331
+ 24
332
+ cs.GL
333
+ 25
334
+ eess.AS
335
+ 26
336
+ cs.SD
337
+ 27
338
+ math.DS
339
+ 28
340
+ cs.GR
341
+ 29
342
+ math.NA
343
+ 30
344
+ cs.CY
345
+ 31
346
+ physics.data-an
347
+ 32
348
+ math.ST
349
+ 33
350
+ stat.TH
351
+ 34
352
+ cs.IT
353
+ 35
354
+ math.IT
355
+ 36
356
+ astro-ph.IM
357
+ 37
358
+ astro-ph.GA
359
+ 38
360
+ cs.SI
361
+ 39
362
+ cs.DB
363
+ 40
364
+ cs.LO
365
+ 41
366
+ nlin.AO
367
+ 42
368
+ cs.PF
369
+ 43
370
+ cs.ET
371
+ 44
372
+ eess.IV
373
+ 45
374
+ cs.AR
375
+ 46
376
+ cs.SY
377
+ 47
378
+ cs.CC
379
+ 48
380
+ q-bio.BM
381
+ 49
382
+ q-bio.QM
383
+ 50
384
+ cs.NI
385
+ 51
386
+ cs.DS
387
+ 52
388
+ cond-mat.stat-mech
389
+ 53
390
+ cs.NA
391
+ 54
392
+ cs.DM
393
+ 55
394
+ eess.SP
395
+ 56
396
+ cs.MS
397
+ 57
398
+ physics.med-ph
399
+ 58
400
+ physics.optics
401
+ 59
402
+ q-fin.CP
403
+ 60
404
+ cs.FL
405
+ 61
406
+ cs.SC
407
+ 62
408
+ q-fin.EC
409
+ 63
410
+ q-fin.TR
411
+ 64
412
+ cond-mat.mes-hall
413
+ 65
414
+ math.PR
415
+ 66
416
+ q-fin.RM
417
+ 67
418
+ nlin.CD
419
+ 68
420
+ cs.CE
421
+ 69
422
+ math.AT
423
+ 70
424
+ stat.OT
425
+ 71
426
+ physics.ao-ph
427
+ 72
428
+ math.SP
429
+ 73
430
+ cs.PL
431
+ 74
432
+ math.AP
433
+ 75
434
+ math.FA
435
+ 76
436
+ physics.geo-ph
437
+ 77
438
+ q-bio.TO
439
+ 78
440
+ physics.comp-ph
441
+ 79
442
+ cs.DL
443
+ 80
444
+ math.CO
445
+ 81
446
+ physics.flu-dyn
447
+ 82
448
+ math.MG
449
+ 83
450
+ astro-ph.EP
451
+ 84
452
+ q-bio.CB
453
+ 85
454
+ math.RA
455
+ 86
456
+ astro-ph.CO
457
+ 87
458
+ cond-mat.mtrl-sci
459
+ 88
460
+ q-fin.ST
461
+ 89
462
+ q-bio.GN
463
+ 90
464
+ nlin.CG
465
+ 91
466
+ nlin.PS
467
+ 92
468
+ math.HO
469
+ 93
470
+ q-fin.GN
471
+ 94
472
+ math.LO
473
+ 95
474
+ math.CT
475
+ 96
476
+ cs.CL, cs.AI, math.CT
477
+ 97
478
+ q-bio.PE
479
+ 98
480
+ astro-ph.SR
481
+ 99
482
+ q-fin.PM
483
+ 100
484
+ physics.bio-ph
485
+ 101
486
+ math.AG
487
+ 102
488
+ cs.OH
489
+ 103
490
+ math.DG
491
+ 104
492
+ astro-ph.HE
493
+ 105
494
+ econ.EM
495
+ 106
496
+ math.QA
497
+ 107
498
+ q-bio.SC
499
+ 108
500
+ math.GM
501
+ 109
502
+ q-bio.MN
503
+ 110
504
+ math.GT
505
+ 111
506
+ math.AC
507
+ 112
508
+ math.CA
509
+ 113
510
+ cond-mat.str-el
511
+ 114
512
+ math.GN
513
+ 115
514
+ aaai.org
515
+ 116
516
+ cond-mat.supr-con
517
+ 117
518
+ q-bio.OT
519
+ 118
520
+ physics.ins-det
521
+ 119
522
+ physics.app-ph
523
+ 120
524
+ math.RT
525
+ 121
526
+ math.MP
527
+ 122
528
+ A.m
529
+ 123
530
+ H.m
531
+ 124
532
+ physics.class-ph
533
+ 125
534
+ q-fin.PR
535
+ 126
536
+ physics.space-ph
537
+ 127
538
+ physics.gen-ph
539
+ 128
540
+ cond-mat.other
541
+ 129
542
+ math.GR
543
+ 130
544
+ cond-mat.quant-gas
545
+ 131
546
+ math.OA
547
+ 132
548
+ physics.hist-ph
549
+ 133
550
+ math.NT
551
+ 134
552
+ cs.OS
553
+ 135
554
+ cond-mat.soft
555
+ 136
556
+ q-bio.BM, q-bio.MN, q-bio.NC, nlin.AO, nlin.CD
557
+ 137
558
+ nlin.AO, nlin.CD, q-bio.NC, physics.bio-ph, cond-mat.dis-nn
559
+ 138
560
+ physics.pop-ph
561
+ 139
562
+ math.CV