Spaces:
Running
Running
Update app.py
Browse files
app.py
CHANGED
@@ -5,6 +5,10 @@ from transformers import AutoTokenizer, AutoModelForCausalLM
|
|
5 |
# import torch
|
6 |
from transformers import StoppingCriteria, AutoModelForCausalLM, AutoTokenizer, StoppingCriteriaList
|
7 |
import numpy as np
|
|
|
|
|
|
|
|
|
8 |
|
9 |
ref_model = AutoModelForCausalLM.from_pretrained("w601sxs/b1ade-1b", torch_dtype=torch.bfloat16)
|
10 |
|
@@ -48,7 +52,7 @@ def get_tokens_and_labels(prompt):
|
|
48 |
"""
|
49 |
Given the prompt (text), return a list of tuples (decoded_token, label)
|
50 |
"""
|
51 |
-
inputs = tokenizer([prompt], return_tensors="pt")
|
52 |
outputs = ref_model.generate(
|
53 |
**inputs,
|
54 |
max_new_tokens=1000,
|
@@ -57,7 +61,7 @@ def get_tokens_and_labels(prompt):
|
|
57 |
stopping_criteria=StoppingCriteriaList([stop_criteria])
|
58 |
)
|
59 |
# Important: don't forget to set `normalize_logits=True` to obtain normalized probabilities (i.e. sum(p) = 1)
|
60 |
-
transition_scores =
|
61 |
transition_proba = np.exp(transition_scores.double().cpu())
|
62 |
|
63 |
# print(transition_proba)
|
@@ -89,47 +93,6 @@ def get_tokens_and_labels(prompt):
|
|
89 |
|
90 |
|
91 |
|
92 |
-
import spacy
|
93 |
-
from spacy import displacy
|
94 |
-
from spacy.tokens import Span
|
95 |
-
from spacy.tokens import Doc
|
96 |
-
|
97 |
-
|
98 |
-
|
99 |
-
def render_output(prompt):
|
100 |
-
output = get_tokens_and_labels(prompt)
|
101 |
-
nlp = spacy.blank("en")
|
102 |
-
doc = nlp(''.join([a[0] for a in output]).replace('Ġ',' ').replace('Ċ','\n'))
|
103 |
-
words = [a[0].replace('Ġ',' ').replace('Ċ','\n') for a in output]#[:indices[2]]
|
104 |
-
doc = Doc(nlp.vocab, words=words)
|
105 |
-
|
106 |
-
doc.spans["sc"]=[]
|
107 |
-
c = 0
|
108 |
-
|
109 |
-
for outs in output:
|
110 |
-
tmpouts = outs[0].replace('Ġ','').replace('Ċ','\n')
|
111 |
-
# print(c, "to", c+len(tmpouts)," : ", tmpouts)
|
112 |
-
|
113 |
-
if outs[1] is not None:
|
114 |
-
doc.spans["sc"].append(Span(doc, c, c+1, outs[1] ))
|
115 |
-
|
116 |
-
c+=1
|
117 |
-
|
118 |
-
# if c>indices[2]-1:
|
119 |
-
# break
|
120 |
-
|
121 |
-
|
122 |
-
options = {'colors' : {
|
123 |
-
'99%': '#44ce1b',
|
124 |
-
'95%': '#bbdb44',
|
125 |
-
'90%': '#f7e379',
|
126 |
-
'50%': '#fec12a',
|
127 |
-
'10%': '#f2a134',
|
128 |
-
'1%': '#e51f1f',
|
129 |
-
'': '#e51f1f',
|
130 |
-
}}
|
131 |
-
|
132 |
-
return displacy.render(doc, style="span", options = options)
|
133 |
|
134 |
|
135 |
|
@@ -144,7 +107,7 @@ def predict(text):
|
|
144 |
|
145 |
|
146 |
demo = gr.Interface(
|
147 |
-
fn=
|
148 |
inputs='text',
|
149 |
outputs='text',
|
150 |
)
|
|
|
5 |
# import torch
|
6 |
from transformers import StoppingCriteria, AutoModelForCausalLM, AutoTokenizer, StoppingCriteriaList
|
7 |
import numpy as np
|
8 |
+
import spacy
|
9 |
+
from spacy import displacy
|
10 |
+
from spacy.tokens import Span
|
11 |
+
from spacy.tokens import Doc
|
12 |
|
13 |
ref_model = AutoModelForCausalLM.from_pretrained("w601sxs/b1ade-1b", torch_dtype=torch.bfloat16)
|
14 |
|
|
|
52 |
"""
|
53 |
Given the prompt (text), return a list of tuples (decoded_token, label)
|
54 |
"""
|
55 |
+
inputs = tokenizer([prompt], return_tensors="pt")
|
56 |
outputs = ref_model.generate(
|
57 |
**inputs,
|
58 |
max_new_tokens=1000,
|
|
|
61 |
stopping_criteria=StoppingCriteriaList([stop_criteria])
|
62 |
)
|
63 |
# Important: don't forget to set `normalize_logits=True` to obtain normalized probabilities (i.e. sum(p) = 1)
|
64 |
+
transition_scores = ref_model.compute_transition_scores(outputs.sequences, outputs.scores, normalize_logits=True)
|
65 |
transition_proba = np.exp(transition_scores.double().cpu())
|
66 |
|
67 |
# print(transition_proba)
|
|
|
93 |
|
94 |
|
95 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
96 |
|
97 |
|
98 |
|
|
|
107 |
|
108 |
|
109 |
demo = gr.Interface(
|
110 |
+
fn=get_tokens_and_labels,
|
111 |
inputs='text',
|
112 |
outputs='text',
|
113 |
)
|