Spaces:
Runtime error
Runtime error
dashasabirova
commited on
Commit
•
4f2093f
1
Parent(s):
f763794
Update app.py
Browse files
app.py
CHANGED
@@ -1 +1,47 @@
|
|
1 |
-
import torch
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
import torch
|
2 |
+
import numpy as np
|
3 |
+
from transformers import AutoTokenizer, AutoModel
|
4 |
+
import torch.nn as nn
|
5 |
+
|
6 |
+
labels_articles = {1: 'Computer Science', 2: 'Economics', 3: "Electrical Engineering And Systems Science",
|
7 |
+
4: "Mathematics", 5: "Physics", 6: "Quantitative Biology", 7: "Quantitative Finance",
|
8 |
+
8: "Statistics"
|
9 |
+
}
|
10 |
+
|
11 |
+
class Net(nn.Module):
|
12 |
+
def __init__(self):
|
13 |
+
super(Net,self).__init__()
|
14 |
+
self.layer = nn.Sequential(
|
15 |
+
nn.Linear(768, 256),
|
16 |
+
nn.ReLU(),
|
17 |
+
nn.Linear(256, 128),
|
18 |
+
nn.ReLU(),
|
19 |
+
nn.Linear(128, 8),
|
20 |
+
)
|
21 |
+
|
22 |
+
def forward(self,x):
|
23 |
+
return self.layer(x)
|
24 |
+
|
25 |
+
model_second = Net()
|
26 |
+
model_second.load_state_dict(torch.load('model.txt'))
|
27 |
+
model_second.eval()
|
28 |
+
|
29 |
+
model_name = 'bert-base-uncased'
|
30 |
+
tokenizer = AutoTokenizer.from_pretrained(model_name)
|
31 |
+
model_first = AutoModel.from_pretrained(model_name)
|
32 |
+
|
33 |
+
title = st.text_area("Write the title of your article, please")
|
34 |
+
abstract = st.text_area("Write the abstract")
|
35 |
+
text = title + '. ' + abstract
|
36 |
+
|
37 |
+
tokens_info = tokenizer(text, padding=True, truncation=True, return_tensors="pt")
|
38 |
+
out_first = model_first(**tokens_info).pooler_output
|
39 |
+
out_second = model_second(out_first).detach().numpy()
|
40 |
+
out_second = softmax(out_second)
|
41 |
+
indices = np.argsort(out_second)[0][::-1]
|
42 |
+
sum_prob = 0
|
43 |
+
for i in indices:
|
44 |
+
print(labels_articles[i+1])
|
45 |
+
sum_prob += out_second[i]
|
46 |
+
if sum_prob > 0.95:
|
47 |
+
break
|