ItsNikolor commited on
Commit
e05ddac
·
1 Parent(s): 25de76d

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +39 -39
app.py CHANGED
@@ -1,56 +1,56 @@
1
  import streamlit as st
2
- import torch
3
- from transformers import AutoModelForSequenceClassification, AutoTokenizer
4
 
5
 
6
- def combine_title_summary(title, summary):
7
- return "title: " + title + " summary: " + summary
8
 
9
 
10
- tag2ind = {
11
- "bio": 0,
12
- "physics": 1,
13
- "math": 2,
14
- "cs": 3,
15
- }
16
 
17
 
18
- @st.cache_resource
19
- def load_model():
20
- device = torch.device("cuda" if torch.cuda.is_available() else "cpu")
21
 
22
- # assert torch.cuda.is_available()
23
- tokenizer = AutoTokenizer.from_pretrained("distilbert/distilbert-base-cased")
24
- model = AutoModelForSequenceClassification.from_pretrained(
25
- "./my_model/checkpoint-513"
26
- ).to(device)
27
 
28
- return tokenizer, model
29
 
30
 
31
- tokenizer, model = load_model()
32
 
33
 
34
- def run_model(model, tokenizer, title, summary):
35
- text = combine_title_summary(title, summary)
36
 
37
- tokens_info = tokenizer(
38
- text,
39
- padding=False,
40
- truncation=True,
41
- return_tensors="pt",
42
- )
43
 
44
- model.eval()
45
- model.cpu()
46
- with torch.no_grad():
47
- out = model(**tokens_info)
48
- probs = torch.nn.functional.softmax(out.logits, dim=-1)[0]
49
 
50
- result = f"Text: `{text}`\nPrediction (prob): \n" + "\n".join(
51
- [f"{tag}={tag_prob}" for tag, tag_prob in zip(tag2ind, probs)]
52
- )
53
- return result
54
 
55
 
56
  title = st.text_input(label="Title", value="")
@@ -62,5 +62,5 @@ if st.button("Submit"):
62
  result = combine_title_summary(title, abstract)
63
  st.success(result)
64
 
65
- result = run_model(model, tokenizer, title, abstract)
66
- st.success(result)
 
1
  import streamlit as st
2
+ # import torch
3
+ # from transformers import AutoModelForSequenceClassification, AutoTokenizer
4
 
5
 
6
+ # def combine_title_summary(title, summary):
7
+ # return "title: " + title + " summary: " + summary
8
 
9
 
10
+ # tag2ind = {
11
+ # "bio": 0,
12
+ # "physics": 1,
13
+ # "math": 2,
14
+ # "cs": 3,
15
+ # }
16
 
17
 
18
+ # @st.cache_resource
19
+ # def load_model():
20
+ # device = torch.device("cuda" if torch.cuda.is_available() else "cpu")
21
 
22
+ # # assert torch.cuda.is_available()
23
+ # tokenizer = AutoTokenizer.from_pretrained("distilbert/distilbert-base-cased")
24
+ # model = AutoModelForSequenceClassification.from_pretrained(
25
+ # "./my_model/checkpoint-513"
26
+ # ).to(device)
27
 
28
+ # return tokenizer, model
29
 
30
 
31
+ # tokenizer, model = load_model()
32
 
33
 
34
+ # def run_model(model, tokenizer, title, summary):
35
+ # text = combine_title_summary(title, summary)
36
 
37
+ # tokens_info = tokenizer(
38
+ # text,
39
+ # padding=False,
40
+ # truncation=True,
41
+ # return_tensors="pt",
42
+ # )
43
 
44
+ # model.eval()
45
+ # model.cpu()
46
+ # with torch.no_grad():
47
+ # out = model(**tokens_info)
48
+ # probs = torch.nn.functional.softmax(out.logits, dim=-1)[0]
49
 
50
+ # result = f"Text: `{text}`\nPrediction (prob): \n" + "\n".join(
51
+ # [f"{tag}={tag_prob}" for tag, tag_prob in zip(tag2ind, probs)]
52
+ # )
53
+ # return result
54
 
55
 
56
  title = st.text_input(label="Title", value="")
 
62
  result = combine_title_summary(title, abstract)
63
  st.success(result)
64
 
65
+ # result = run_model(model, tokenizer, title, abstract)
66
+ # st.success(result)