ItsNikolor commited on
Commit
b8ff6ff
·
1 Parent(s): fffa35e

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +29 -29
app.py CHANGED
@@ -15,42 +15,42 @@ tag2ind = {
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)
 
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)