ilushado commited on
Commit
d4ef182
·
1 Parent(s): 279d5aa

added model

Browse files
app.py CHANGED
@@ -1,14 +1,116 @@
1
  import streamlit as st
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
2
 
3
  st.markdown("### Hello, world!")
4
- st.markdown("<img width=200px src='https://rozetked.me/images/uploads/dwoilp3BVjlE.jpg'>", unsafe_allow_html=True)
5
  # ^-- можно показывать пользователю текст, картинки, ограниченное подмножество html - всё как в jupyter
6
 
7
- text = st.text_area("TEXT HERE")
8
- # ^-- показать текстовое поле. В поле text лежит строка, которая находится там в данный момент
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
9
 
10
  from transformers import pipeline
11
  # тут уже знакомый вам код с huggingface.transformers -- его можно заменить на что угодно от fairseq до catboost
12
 
13
- st.markdown("12456")
 
14
  # выводим результаты модели в текстовое поле, на потеху пользователю
 
1
  import streamlit as st
2
+ import torch
3
+ import pandas as pd
4
+ import numpy as np
5
+ import torch
6
+ import transformers
7
+ import json
8
+ from torch.utils.data import Dataset, DataLoader
9
+ from transformers import RobertaModel, RobertaTokenizer
10
+ import transformers
11
+
12
+
13
+ idx_to_tag = {0: 'cs',
14
+ 1: 'stat',
15
+ 2: 'physics',
16
+ 3: 'math',
17
+ 4: 'cond-mat',
18
+ 5: 'q-bio',
19
+ 6: 'eess',
20
+ 7: 'quant-ph',
21
+ 8: 'astro-ph',
22
+ 9: 'nlin',
23
+ 10: 'q-fin',
24
+ 11: 'gr-qc',
25
+ 12: 'hep-th',
26
+ 13: 'hep-ex',
27
+ 14: 'econ',
28
+ 15: 'hep-ph',
29
+ 16: 'nucl-th',
30
+ 17: 'hep-lat',
31
+ 18: 'math-ph',
32
+ 19: 'nucl-ex'}
33
+
34
+
35
+ tag_to_idx = {'cs': 0,
36
+ 'stat': 1,
37
+ 'physics': 2,
38
+ 'math': 3,
39
+ 'cond-mat': 4,
40
+ 'q-bio': 5,
41
+ 'eess': 6,
42
+ 'quant-ph': 7,
43
+ 'astro-ph': 8,
44
+ 'nlin': 9,
45
+ 'q-fin': 10,
46
+ 'gr-qc': 11,
47
+ 'hep-th': 12,
48
+ 'hep-ex': 13,
49
+ 'econ': 14,
50
+ 'hep-ph': 15,
51
+ 'nucl-th': 16,
52
+ 'hep-lat': 17,
53
+ 'math-ph': 18,
54
+ 'nucl-ex': 19}
55
+
56
+ class RobertaClass(torch.nn.Module):
57
+ def __init__(self):
58
+ super(RobertaClass, self).__init__()
59
+ self.l1 = RobertaModel.from_pretrained("roberta-base")
60
+ self.pre_classifier = torch.nn.Linear(768, 768)
61
+ self.dropout = torch.nn.Dropout(0.3)
62
+ self.classifier = torch.nn.Linear(768, 5)
63
+
64
+ def forward(self, input_ids, attention_mask, token_type_ids):
65
+ output_1 = self.l1(input_ids=input_ids, attention_mask=attention_mask, token_type_ids=token_type_ids)
66
+ hidden_state = output_1[0]
67
+ pooler = hidden_state[:, 0]
68
+ pooler = self.pre_classifier(pooler)
69
+ pooler = torch.nn.ReLU()(pooler)
70
+ pooler = self.dropout(pooler)
71
+ output = self.classifier(pooler)
72
+ return output
73
+
74
+ tokenizer = RobertaTokenizer.from_pretrained('roberta-base', truncation=True, do_lower_case=True,
75
+ vocab_file='model/vocab.json',
76
+ merges_file='model/merges.txt')
77
+
78
+
79
+
80
+ model = torch.load('model/pytorch_roberta_sentiment.bin', map_location=torch.device('cpu'))
81
+
82
 
83
  st.markdown("### Hello, world!")
84
+ # st.markdown("<img width=200px src='https://rozetked.me/images/uploads/dwoilp3BVjlE.jpg'>", unsafe_allow_html=True)
85
  # ^-- можно показывать пользователю текст, картинки, ограниченное подмножество html - всё как в jupyter
86
 
87
+ title = st.text_area("Title HERE")
88
+ abstract = st.text_area("Abstract HERE")
89
+
90
+
91
+
92
+ text = title + " : " + abstract
93
+
94
+ inputs = tokenizer.encode_plus(
95
+ text,
96
+ None,
97
+ add_special_tokens=True,
98
+ max_length=256,
99
+ pad_to_max_length=True,
100
+ return_token_type_ids=True
101
+ )
102
+
103
+
104
+ ids = torch.Tensor(inputs['input_ids']).long()
105
+ mask = torch.Tensor(inputs['attention_mask']).long()
106
+ token_type_ids = torch.Tensor(inputs['token_type_ids']).long()
107
+
108
+ ans = model(ids.unsqueeze(0), mask.unsqueeze(0), token_type_ids.unsqueeze(0))
109
+
110
 
111
  from transformers import pipeline
112
  # тут уже знакомый вам код с huggingface.transformers -- его можно заменить на что угодно от fairseq до catboost
113
 
114
+ idx = torch.nn.functional.softmax(ans[0], dim=0).argmax().item()
115
+ st.markdown(f'{idx_to_tag[idx]}')
116
  # выводим результаты модели в текстовое поле, на потеху пользователю
model/merges.txt ADDED
The diff for this file is too large to render. See raw diff
 
model/pytorch_roberta_sentiment.bin ADDED
@@ -0,0 +1,3 @@
 
 
 
 
1
+ version https://git-lfs.github.com/spec/v1
2
+ oid sha256:d981012dade5ff2425eff3ccfb9bdbdc2938b1785009fa969acca60916a75ff0
3
+ size 501514997
model/vocab.json ADDED
The diff for this file is too large to render. See raw diff
 
requirements.txt CHANGED
@@ -1,3 +1,5 @@
1
  torch
2
  streamlit
3
- transformers
 
 
 
1
  torch
2
  streamlit
3
+ transformers
4
+ pandas
5
+ numpy