Spaces:
Runtime error
Runtime error
Commit
·
7b73dfd
1
Parent(s):
c28c597
feat: updating visualization again
Browse files- .github/workflows/hgf-sync-main.yml +1 -1
- explanation/visualize.py +29 -1
- main.py +2 -2
- utils/formatting.py +1 -1
.github/workflows/hgf-sync-main.yml
CHANGED
|
@@ -54,4 +54,4 @@ jobs:
|
|
| 54 |
env:
|
| 55 |
HF_TOKEN: ${{ secrets.HF_TOKEN }}
|
| 56 |
# run git push
|
| 57 |
-
run: git push https://LennardZuendorf:[email protected]/spaces/LennardZuendorf/thesis
|
|
|
|
| 54 |
env:
|
| 55 |
HF_TOKEN: ${{ secrets.HF_TOKEN }}
|
| 56 |
# run git push
|
| 57 |
+
run: git push https://LennardZuendorf:[email protected]/spaces/LennardZuendorf/thesis main
|
explanation/visualize.py
CHANGED
|
@@ -1,6 +1,10 @@
|
|
| 1 |
# visualization module that creates an attention visualization using BERTViz
|
| 2 |
|
| 3 |
|
|
|
|
|
|
|
|
|
|
|
|
|
| 4 |
# internal imports
|
| 5 |
from utils import formatting as fmt
|
| 6 |
from .markup import markup_text
|
|
@@ -34,6 +38,30 @@ def chat_explained(model, prompt):
|
|
| 34 |
|
| 35 |
# create the response text and marked text for ui
|
| 36 |
response_text = fmt.format_output_text(decoder_text)
|
|
|
|
| 37 |
marked_text = markup_text(encoder_text, averaged_attention, variant="visualizer")
|
| 38 |
|
| 39 |
-
return response_text,
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
# visualization module that creates an attention visualization using BERTViz
|
| 2 |
|
| 3 |
|
| 4 |
+
# external imports
|
| 5 |
+
from bertviz import neuron_view as nv
|
| 6 |
+
|
| 7 |
+
|
| 8 |
# internal imports
|
| 9 |
from utils import formatting as fmt
|
| 10 |
from .markup import markup_text
|
|
|
|
| 38 |
|
| 39 |
# create the response text and marked text for ui
|
| 40 |
response_text = fmt.format_output_text(decoder_text)
|
| 41 |
+
xai_graphic = attention_graphic(encoder_text, decoder_text, model)
|
| 42 |
marked_text = markup_text(encoder_text, averaged_attention, variant="visualizer")
|
| 43 |
|
| 44 |
+
return response_text, xai_graphic, marked_text
|
| 45 |
+
|
| 46 |
+
|
| 47 |
+
def attention_graphic(encoder_text, decoder_text, model):
|
| 48 |
+
|
| 49 |
+
# set model type to BERT (to fake out BERTViz)
|
| 50 |
+
model_type = "bert"
|
| 51 |
+
|
| 52 |
+
# create sentence a and b from list of strings
|
| 53 |
+
sentence_a = " ".join(encoder_text)
|
| 54 |
+
sentence_b = " ".join(decoder_text)
|
| 55 |
+
|
| 56 |
+
# display neuron view
|
| 57 |
+
return nv.show(
|
| 58 |
+
model.MODEL,
|
| 59 |
+
model_type,
|
| 60 |
+
model.TOKENIZER,
|
| 61 |
+
sentence_a,
|
| 62 |
+
sentence_b,
|
| 63 |
+
display_mode="light",
|
| 64 |
+
layer=2,
|
| 65 |
+
head=0,
|
| 66 |
+
html_action="return",
|
| 67 |
+
)
|
main.py
CHANGED
|
@@ -200,10 +200,10 @@ with gr.Blocks(
|
|
| 200 |
xai_interactive = iFrame(
|
| 201 |
label="Interactive Explanation",
|
| 202 |
value=(
|
| 203 |
-
'<div style="text-align: center"><h4>No Graphic to Display'
|
| 204 |
" (Yet)</h4></div>"
|
| 205 |
),
|
| 206 |
-
height="
|
| 207 |
show_label=True,
|
| 208 |
)
|
| 209 |
|
|
|
|
| 200 |
xai_interactive = iFrame(
|
| 201 |
label="Interactive Explanation",
|
| 202 |
value=(
|
| 203 |
+
'<div style="text-align: center; font-family:arial;"><h4>No Graphic to Display'
|
| 204 |
" (Yet)</h4></div>"
|
| 205 |
),
|
| 206 |
+
height="1500px",
|
| 207 |
show_label=True,
|
| 208 |
)
|
| 209 |
|
utils/formatting.py
CHANGED
|
@@ -75,5 +75,5 @@ def flatten_attention(values: ndarray, axis: int = 0):
|
|
| 75 |
|
| 76 |
|
| 77 |
def avg_attention(attention_values):
|
| 78 |
-
attention = attention_values.
|
| 79 |
return np.mean(attention, axis=0)
|
|
|
|
| 75 |
|
| 76 |
|
| 77 |
def avg_attention(attention_values):
|
| 78 |
+
attention = attention_values.output_attentions[0][0].detach().numpy()
|
| 79 |
return np.mean(attention, axis=0)
|