Spaces:
Runtime error
Runtime error
Nearly appearing notes
Browse files- app.py +45 -40
- requirements.txt +2 -1
app.py
CHANGED
@@ -5,16 +5,12 @@ from lxt.utils import clean_tokens
|
|
5 |
import gradio as gr
|
6 |
import numpy as np
|
7 |
import spaces
|
|
|
8 |
|
9 |
-
|
10 |
-
|
11 |
-
|
12 |
-
model = LlamaForCausalLM.from_pretrained("TinyLlama/TinyLlama-1.1B-Chat-v1.0", torch_dtype=torch.float16, device_map="auto")
|
13 |
-
tokenizer = AutoTokenizer.from_pretrained("TinyLlama/TinyLlama-1.1B-Chat-v1.0")
|
14 |
-
attnlrp.register(model)
|
15 |
-
return model, tokenizer
|
16 |
|
17 |
-
model, tokenizer = load_model_and_tokenizer()
|
18 |
|
19 |
def really_clean_tokens(tokens):
|
20 |
tokens = clean_tokens(tokens)
|
@@ -29,8 +25,6 @@ def generate_and_visualize(prompt, num_tokens=10):
|
|
29 |
generated_tokens_ids = []
|
30 |
all_relevances = []
|
31 |
|
32 |
-
print("OKKK let's gooo")
|
33 |
-
|
34 |
for _ in range(num_tokens):
|
35 |
output_logits = model(inputs_embeds=input_embeds.requires_grad_(), use_cache=False).logits
|
36 |
max_logits, max_indices = torch.max(output_logits[0, -1, :], dim=-1)
|
@@ -53,34 +47,45 @@ def generate_and_visualize(prompt, num_tokens=10):
|
|
53 |
|
54 |
def process_relevances(input_tokens, all_relevances, generated_tokens):
|
55 |
attention_matrix = np.array([el[:len(all_relevances[0])] for el in all_relevances])
|
56 |
-
|
57 |
-
|
58 |
-
|
59 |
-
|
60 |
-
|
61 |
-
non_zero_cols = non_zero_cols[(non_zero_cols >= 0) & (non_zero_cols < attention_matrix.shape[1])]
|
62 |
-
|
63 |
-
important_input_tokens = [input_tokens[i] for i in non_zero_cols]
|
64 |
-
|
65 |
-
output_with_notes = []
|
66 |
-
current_group = []
|
67 |
-
current_note = set()
|
68 |
-
|
69 |
-
for i, (token, relevance) in enumerate(zip(generated_tokens, attention_matrix)):
|
70 |
-
important_indices = np.where(relevance[non_zero_cols] > 0.1)[0]
|
71 |
-
if len(important_indices) > 0:
|
72 |
-
current_group.append(token)
|
73 |
-
current_note.update([important_input_tokens[j] for j in important_indices])
|
74 |
-
else:
|
75 |
-
if current_group:
|
76 |
-
output_with_notes.append((" ".join(current_group), list(current_note)))
|
77 |
-
current_group = []
|
78 |
-
current_note = set()
|
79 |
-
output_with_notes.append((token, []))
|
80 |
|
81 |
-
|
82 |
-
|
83 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
84 |
return output_with_notes
|
85 |
|
86 |
def create_html_with_hover(output_with_notes):
|
@@ -124,7 +129,7 @@ examples = [
|
|
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 |
|
126 |
Question: How high did they climb in 1922? According to the text, the 1922 expedition reached 8,""",
|
127 |
-
|
128 |
],
|
129 |
[
|
130 |
"""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.
|
@@ -136,7 +141,7 @@ Prithvi WxC (Prithvi, "ΰ€ͺΰ₯ΰ€₯ΰ₯ΰ€΅ΰ₯", is the Sanskrit name for Earth) - is
|
|
136 |
π‘ 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.
|
137 |
|
138 |
How many weather variables can Prithvi predict? Prithvi can""",
|
139 |
-
|
140 |
],
|
141 |
[
|
142 |
"""Transformers v4.45.0 released: includes a lightning-fast method to build tools! β‘οΈ
|
@@ -148,7 +153,7 @@ During user research with colleagues @MoritzLaurer and @Jofthomas , we discovere
|
|
148 |
β
VoilΓ , you're good to go!
|
149 |
|
150 |
How can you build tools simply in transformers? Just use the decorator""",
|
151 |
-
|
152 |
]
|
153 |
]
|
154 |
|
@@ -156,7 +161,7 @@ with gr.Blocks(css=css) as demo:
|
|
156 |
gr.Markdown("# Token Generation with Hover Notes")
|
157 |
|
158 |
input_text = gr.Textbox(label="Enter your prompt:", lines=10, value=examples[0][0])
|
159 |
-
num_tokens = gr.Slider(minimum=1, maximum=50, value=
|
160 |
generate_button = gr.Button("Generate")
|
161 |
|
162 |
output_html = gr.HTML(label="Generated Output")
|
|
|
5 |
import gradio as gr
|
6 |
import numpy as np
|
7 |
import spaces
|
8 |
+
from scipy.signal import convolve2d
|
9 |
|
10 |
+
model = LlamaForCausalLM.from_pretrained("TinyLlama/TinyLlama-1.1B-Chat-v1.0", torch_dtype=torch.bfloat16, device_map="cuda")
|
11 |
+
tokenizer = AutoTokenizer.from_pretrained("TinyLlama/TinyLlama-1.1B-Chat-v1.0")
|
12 |
+
attnlrp.register(model)
|
|
|
|
|
|
|
|
|
13 |
|
|
|
14 |
|
15 |
def really_clean_tokens(tokens):
|
16 |
tokens = clean_tokens(tokens)
|
|
|
25 |
generated_tokens_ids = []
|
26 |
all_relevances = []
|
27 |
|
|
|
|
|
28 |
for _ in range(num_tokens):
|
29 |
output_logits = model(inputs_embeds=input_embeds.requires_grad_(), use_cache=False).logits
|
30 |
max_logits, max_indices = torch.max(output_logits[0, -1, :], dim=-1)
|
|
|
47 |
|
48 |
def process_relevances(input_tokens, all_relevances, generated_tokens):
|
49 |
attention_matrix = np.array([el[:len(all_relevances[0])] for el in all_relevances])
|
50 |
+
|
51 |
+
### FIND ZONES OF INTEREST
|
52 |
+
threshold_per_token = 0.3
|
53 |
+
kernel_width = 5
|
54 |
+
kernel = np.ones((kernel_width, kernel_width))
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
55 |
|
56 |
+
# Compute the rolling sum using 2D convolution
|
57 |
+
rolled_sum = convolve2d(attention_matrix, kernel, mode='valid')
|
58 |
|
59 |
+
# Find where the rolled sum is greater than 1.0
|
60 |
+
significant_areas = rolled_sum > kernel_width**2 * threshold_per_token
|
61 |
+
print("SUMM:", significant_areas.sum())
|
62 |
+
|
63 |
+
def find_largest_contiguous_patch(array):
|
64 |
+
current_patch_end=None
|
65 |
+
best_width, best_patch_end = None, None
|
66 |
+
current_width=0
|
67 |
+
for i in range(len(array)):
|
68 |
+
if array[i]:
|
69 |
+
if current_patch_end is not None and current_patch_end == i-1:
|
70 |
+
current_width +=1
|
71 |
+
current_patch_end = i
|
72 |
+
else:
|
73 |
+
current_patch_end = i
|
74 |
+
current_width = 1
|
75 |
+
if current_patch_end and best_width is None or current_width > best_width:
|
76 |
+
best_end = current_patch_end
|
77 |
+
best_width = current_width
|
78 |
+
else:
|
79 |
+
current_width = 0
|
80 |
+
return best_width, best_patch_end
|
81 |
+
|
82 |
+
output_with_notes = [generated_tokens[:kernel_width]]
|
83 |
+
for row in range(significant_areas.shape[0]):
|
84 |
+
best_width, best_patch_end = find_largest_contiguous_patch(significant_areas[row])
|
85 |
+
if best_width is not None:
|
86 |
+
output_with_notes.append((generated_tokens[row + kernel_width], input_tokens[best_patch_end-best_width, best_width+kernel_width]))
|
87 |
+
print("OUTPUT WIHT NOTES/::")
|
88 |
+
print(output_with_notes)
|
89 |
return output_with_notes
|
90 |
|
91 |
def create_html_with_hover(output_with_notes):
|
|
|
129 |
"""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.
|
130 |
|
131 |
Question: How high did they climb in 1922? According to the text, the 1922 expedition reached 8,""",
|
132 |
+
40
|
133 |
],
|
134 |
[
|
135 |
"""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.
|
|
|
141 |
π‘ 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.
|
142 |
|
143 |
How many weather variables can Prithvi predict? Prithvi can""",
|
144 |
+
40
|
145 |
],
|
146 |
[
|
147 |
"""Transformers v4.45.0 released: includes a lightning-fast method to build tools! β‘οΈ
|
|
|
153 |
β
VoilΓ , you're good to go!
|
154 |
|
155 |
How can you build tools simply in transformers? Just use the decorator""",
|
156 |
+
40
|
157 |
]
|
158 |
]
|
159 |
|
|
|
161 |
gr.Markdown("# Token Generation with Hover Notes")
|
162 |
|
163 |
input_text = gr.Textbox(label="Enter your prompt:", lines=10, value=examples[0][0])
|
164 |
+
num_tokens = gr.Slider(minimum=1, maximum=50, value=30, step=1, label="Number of tokens to generate")
|
165 |
generate_button = gr.Button("Generate")
|
166 |
|
167 |
output_html = gr.HTML(label="Generated Output")
|
requirements.txt
CHANGED
@@ -1,2 +1,3 @@
|
|
1 |
lxt
|
2 |
-
numpy
|
|
|
|
1 |
lxt
|
2 |
+
numpy
|
3 |
+
scipy
|