m-ric HF staff commited on
Commit
c7c30cc
Β·
1 Parent(s): 098d586

Add exmamples

Browse files
Files changed (2) hide show
  1. README.md +4 -4
  2. app.py +51 -10
README.md CHANGED
@@ -1,14 +1,14 @@
1
  ---
2
- title: Attention Visualizer
3
  emoji: πŸ”ŽπŸ‘€
4
- colorFrom: gray
5
  colorTo: red
6
  sdk: gradio
7
- sdk_version: 4.21.0
8
  app_file: app.py
9
  pinned: false
10
  license: apache-2.0
11
- short_description: Discover how attention works in LLMs!
12
  ---
13
 
14
  Check out the configuration reference at https://huggingface.co/docs/hub/spaces-config-reference
 
1
  ---
2
+ title: Attribution Visualizer
3
  emoji: πŸ”ŽπŸ‘€
4
+ colorFrom: yellow
5
  colorTo: red
6
  sdk: gradio
7
+ sdk_version: 4.44.0
8
  app_file: app.py
9
  pinned: false
10
  license: apache-2.0
11
+ short_description: Find which input tokens influence the output of your LLM.
12
  ---
13
 
14
  Check out the configuration reference at https://huggingface.co/docs/hub/spaces-config-reference
app.py CHANGED
@@ -1,13 +1,13 @@
 
1
  import torch
2
- from transformers import AutoTokenizer
3
- from lxt.models.llama import LlamaForCausalLM, attnlrp
4
- from lxt.utils import clean_tokens
5
  import gradio as gr
6
  import matplotlib.pyplot as plt
7
  import seaborn as sns
8
  import numpy as np
9
- import io
10
  from PIL import Image
 
 
 
11
 
12
  # Load model and tokenizer
13
  model = LlamaForCausalLM.from_pretrained("TinyLlama/TinyLlama-1.1B-Chat-v1.0", torch_dtype=torch.bfloat16, device_map="cuda")
@@ -77,7 +77,6 @@ def generate_heatmap(input_tokens, all_relevances, output_tokens):
77
  non_zero_cols = np.sort(non_zero_cols)
78
  non_zero_cols = non_zero_cols[(non_zero_cols >= 0) & (non_zero_cols < attention_matrix.shape[1])]
79
 
80
-
81
  # Filter the matrix and input tokens
82
  filtered_matrix = attention_matrix[:, non_zero_cols]
83
  filtered_input_tokens = [input_tokens[i] for i in non_zero_cols]
@@ -114,15 +113,49 @@ def on_generate(input_text, num_tokens):
114
  heatmap
115
  )
116
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
117
  # Define Gradio interface
118
  with gr.Blocks() as demo:
119
- gr.Markdown("""# Extended Attention Visualization Demo
120
 
121
- This demo uses the library [LXT](https://lxt.readthedocs.io/en/latest/quickstart.html#tinyllama) under the hood.""")
122
 
123
- input_text = gr.Textbox(label="Input Prompt", lines=5, value="""\
124
- Context: Mount Everest attracts many climbers, including highly experienced mountaineers. There are two main climbing routes, one approaching the summit from the southeast in Nepal (known as the standard route) and the other from the north in Tibet. While not posing substantial technical climbing challenges on the standard route, Everest presents dangers such as altitude sickness, weather, and wind, as well as hazards from avalanches and the Khumbu Icefall. As of November 2022, 310 people have died on Everest. Over 200 bodies remain on the mountain and have not been removed due to the dangerous conditions. The first recorded efforts to reach Everest's summit were made by British mountaineers. As Nepal did not allow foreigners to enter the country at the time, the British made several attempts on the north ridge route from the Tibetan side. After the first reconnaissance expedition by the British in 1921 reached 7,000 m (22,970 ft) on the North Col, the 1922 expedition pushed the north ridge route up to 8,320 m (27,300 ft), marking the first time a human had climbed above 8,000 m (26,247 ft). The 1924 expedition resulted in one of the greatest mysteries on Everest to this day: George Mallory and Andrew Irvine made a final summit attempt on 8 June but never returned, sparking debate as to whether they were the first to reach the top. Tenzing Norgay and Edmund Hillary made the first documented ascent of Everest in 1953, using the southeast ridge route. Norgay had reached 8,595 m (28,199 ft) the previous year as a member of the 1952 Swiss expedition. The Chinese mountaineering team of Wang Fuzhou, Gonpo, and Qu Yinhua made the first reported ascent of the peak from the north ridge on 25 May 1960. \
125
- Question: How high did they climb in 1922? According to the text, the 1922 expedition reached 8,""")
126
  num_tokens = gr.Slider(minimum=1, maximum=50, step=1, value=10, label="Number of tokens to generate")
127
  generate_button = gr.Button("Generate and Visualize")
128
 
@@ -137,6 +170,14 @@ Question: How high did they climb in 1922? According to the text, the 1922 exped
137
  output_tokens_state = gr.State([])
138
  relevances_state = gr.State([])
139
 
 
 
 
 
 
 
 
 
140
  generate_button.click(
141
  on_generate,
142
  inputs=[input_text, num_tokens],
 
1
+ import io
2
  import torch
 
 
 
3
  import gradio as gr
4
  import matplotlib.pyplot as plt
5
  import seaborn as sns
6
  import numpy as np
 
7
  from PIL import Image
8
+ from transformers import AutoTokenizer
9
+ from lxt.models.llama import LlamaForCausalLM, attnlrp
10
+ from lxt.utils import clean_tokens
11
 
12
  # Load model and tokenizer
13
  model = LlamaForCausalLM.from_pretrained("TinyLlama/TinyLlama-1.1B-Chat-v1.0", torch_dtype=torch.bfloat16, device_map="cuda")
 
77
  non_zero_cols = np.sort(non_zero_cols)
78
  non_zero_cols = non_zero_cols[(non_zero_cols >= 0) & (non_zero_cols < attention_matrix.shape[1])]
79
 
 
80
  # Filter the matrix and input tokens
81
  filtered_matrix = attention_matrix[:, non_zero_cols]
82
  filtered_input_tokens = [input_tokens[i] for i in non_zero_cols]
 
113
  heatmap
114
  )
115
 
116
+ # Define example inputs
117
+ examples = [
118
+ [
119
+ """Context: Mount Everest attracts many climbers, including highly experienced mountaineers.
120
+ There are two main climbing routes, one approaching the summit from the southeast in Nepal (known as the standard route) and the other from the north in Tibet. While not posing substantial technical climbing challenges on the standard route, Everest presents dangers such as altitude sickness, weather, and wind, as well as hazards from avalanches and the Khumbu Icefall. As of November 2022, 310 people have died on Everest.
121
+ Over 200 bodies remain on the mountain and have not been removed due to the dangerous conditions. The first recorded efforts to reach Everest's summit were made by British mountaineers.
122
+ As Nepal did not allow foreigners to enter the country at the time, the British made several attempts on the north ridge route from the Tibetan side. After the first reconnaissance expedition by the British in 1921 reached 7,000 m (22,970 ft) on the North Col, the 1922 expedition pushed the north ridge route up to 8,320 m (27,300 ft), marking the first time a human had climbed above 8,000 m (26,247 ft).
123
+ The 1924 expedition resulted in one of the greatest mysteries on Everest to this day: George Mallory and Andrew Irvine made a final summit attempt on 8 June but never returned, sparking debate as to whether they were the first to reach the top.
124
+ Tenzing Norgay and Edmund Hillary made the first documented ascent of Everest in 1953, using the southeast ridge route. Norgay had reached 8,595 m (28,199 ft) the previous year as a member of the 1952 Swiss expedition. The Chinese mountaineering team of Wang Fuzhou, Gonpo, and Qu Yinhua made the first reported ascent of the peak from the north ridge on 25 May 1960. \nQuestion: How high did they climb in 1922? According to the text, the 1922 expedition reached 8,""",
125
+ 10
126
+ ],
127
+ [
128
+ """Hurricane Katrina killed hundreds of people as it made landfall on New Orleans in 2005 - many of these deaths could have been avoided if alerts had been given one day earlier. Accurate weather forecasts are really life-saving.
129
+
130
+ πŸ”₯ Now, NASA and IBM just dropped a game-changing new model: the first ever foundation model for weather! This means, it's the first time we have a generalist model not restricted to one task, but able to predict 160 weather variables!
131
+
132
+ Prithvi WxC (Prithvi, β€œΰ€ͺΰ₯ƒΰ€₯ΰ₯ΰ€΅ΰ₯€β€, is the Sanskrit name for Earth) - is a 2.3 billion parameter model, with an architecture close to previous vision transformers like Hiera.
133
+
134
+ πŸ’‘ But it comes with some important tweaks: under the hood, Prithvi WxC uses a clever transformer-based architecture with 25 encoder and 5 decoder blocks. It alternates between "local" and "global" attention to capture both regional and global weather patterns. How many weather variables can Prithvi predict? Prithvi can""",
135
+ 15
136
+ ],
137
+ [
138
+ """Transformers v4.45.0 released: includes a lightning-fast method to build tools! ⚑️
139
+
140
+ During user research with colleagues @MoritzLaurer and @Jofthomas , we discovered that the class definition currently in used to define a Tool in
141
+ transformers.agents is a bit tedious to use, because it goes in great detail.
142
+
143
+ ➑️ So I’ve made an easier way to build tools: just make a function with type hints + a docstring, and add a @tool decorator in front.
144
+
145
+ βœ… VoilΓ , you’re good to go!
146
+
147
+ How can you build tools simply in transformers? Just use the decorator""",
148
+ 20
149
+ ]
150
+ ]
151
+
152
  # Define Gradio interface
153
  with gr.Blocks() as demo:
154
+ gr.Markdown("""# Attribution Visualization Demo
155
 
156
+ This demo uses the library [LXT](https://lxt.readthedocs.io/en/latest/quickstart.html#tinyllama) to attribute the output tokens to some input tokens.""")
157
 
158
+ input_text = gr.Textbox(label="Input Prompt", lines=5, value=examples[0][0])
 
 
159
  num_tokens = gr.Slider(minimum=1, maximum=50, step=1, value=10, label="Number of tokens to generate")
160
  generate_button = gr.Button("Generate and Visualize")
161
 
 
170
  output_tokens_state = gr.State([])
171
  relevances_state = gr.State([])
172
 
173
+ gr.Examples(
174
+ examples=examples,
175
+ inputs=[input_text, num_tokens],
176
+ outputs=[input_tokens_state, relevances_state, attention_on_inputs, output_tokens_state, current_tokens, generated_output, step_slider, heatmap_output],
177
+ fn=on_generate,
178
+ cache_examples=True
179
+ )
180
+
181
  generate_button.click(
182
  on_generate,
183
  inputs=[input_text, num_tokens],