Poe Dator commited on
Commit
2ffe758
·
1 Parent(s): ec34e2f

formatting, timing added

Browse files
Files changed (1) hide show
  1. app.py +19 -23
app.py CHANGED
@@ -2,6 +2,7 @@ import streamlit as st
2
  import torch
3
  from torch import nn
4
  from transformers import BertModel, AutoTokenizer, AutoModel, pipeline
 
5
  # device = torch.device("cuda" if torch.cuda.is_available() else "cpu")
6
  device = 'cpu'
7
 
@@ -9,6 +10,7 @@ st.markdown("### Privet, mir!")
9
  st.markdown("<img width=200px src='https://i.pinimg.com/736x/11/33/19/113319f0ffe91f4bb0f468914b9916da.jpg'>", unsafe_allow_html=True)
10
 
11
  text = st.text_area("ENTER TEXT HERE")
 
12
  st.markdown("INFERENCE STARTS ...")
13
 
14
  # dict for decoding / enclding labels
@@ -34,20 +36,21 @@ class BertClassifier(nn.Module):
34
  linear_output = self.linear(dropout_output)
35
  final_layer = self.relu(linear_output)
36
  return final_layer
37
-
38
- model = BertClassifier(n_classes=len(labels))
39
- st.markdown("Model created")
40
- model.load_state_dict(torch.load('model_weights_1.pt', map_location=torch.device('cpu')))
41
- model.eval()
42
- st.markdown("Model weights loaded")
43
 
 
 
 
 
 
 
 
 
 
 
44
  def inference(txt, mode=None):
45
  # infers classes for text topic based on the trained model from above
46
  # has separate mode 'print' for just output
47
-
48
- txt = txt.lower().replace('\n', '')
49
-
50
- t2 = tokenizer(txt,
51
  padding='max_length', max_length = 512, truncation=True,
52
  return_tensors="pt")
53
 
@@ -58,19 +61,12 @@ def inference(txt, mode=None):
58
  out = out.cpu().detach().numpy().reshape(-1)
59
  out = out/out.sum() * 100
60
  res = [(l, o) for l, o in zip (list(labels.keys()), out.tolist())]
61
-
62
- if mode == 'print':
63
- res.sort(key = lambda x : - x[1])
64
- for lbl, score in res:
65
- if score >=1:
66
- print(f"[{lbl:<7}] {labels_decoder[lbl]:<35} {score:.1f}%")
67
-
68
- elif mode == 'debug':
69
- return out, res
70
-
71
- else:
72
- return res
73
 
74
  res = inference(text, mode=None)
75
  st.markdown("INFERENCE RESULT:")
76
- st.markdown(f"{res}")
 
 
 
 
 
2
  import torch
3
  from torch import nn
4
  from transformers import BertModel, AutoTokenizer, AutoModel, pipeline
5
+ from time import time
6
  # device = torch.device("cuda" if torch.cuda.is_available() else "cpu")
7
  device = 'cpu'
8
 
 
10
  st.markdown("<img width=200px src='https://i.pinimg.com/736x/11/33/19/113319f0ffe91f4bb0f468914b9916da.jpg'>", unsafe_allow_html=True)
11
 
12
  text = st.text_area("ENTER TEXT HERE")
13
+ start_time = time()
14
  st.markdown("INFERENCE STARTS ...")
15
 
16
  # dict for decoding / enclding labels
 
36
  linear_output = self.linear(dropout_output)
37
  final_layer = self.relu(linear_output)
38
  return final_layer
 
 
 
 
 
 
39
 
40
+ def build_model():
41
+ model = BertClassifier(n_classes=len(labels))
42
+ st.markdown("Model created")
43
+ model.load_state_dict(torch.load('model_weights_1.pt', map_location=torch.device('cpu')))
44
+ model.eval()
45
+ st.markdown("Model weights loaded")
46
+ return model
47
+
48
+ model = build_model()
49
+
50
  def inference(txt, mode=None):
51
  # infers classes for text topic based on the trained model from above
52
  # has separate mode 'print' for just output
53
+ t2 = tokenizer(txt.lower().replace('\n', ''),
 
 
 
54
  padding='max_length', max_length = 512, truncation=True,
55
  return_tensors="pt")
56
 
 
61
  out = out.cpu().detach().numpy().reshape(-1)
62
  out = out/out.sum() * 100
63
  res = [(l, o) for l, o in zip (list(labels.keys()), out.tolist())]
64
+ return res
 
 
 
 
 
 
 
 
 
 
 
65
 
66
  res = inference(text, mode=None)
67
  st.markdown("INFERENCE RESULT:")
68
+ for lbl, score in res:
69
+ if score >=1:
70
+ st.markdown(f"[{lbl:<7}] {labels_decoder[lbl]:<35} {score:.1f}%")
71
+
72
+ st.markdown(f"cycle time = {time() - start_time:.2f} s.")