m-ric HF Staff commited on
Commit
2343db7
Β·
1 Parent(s): 9570f3d

Edit notes

Browse files
Files changed (1) hide show
  1. app.py +26 -27
app.py CHANGED
@@ -94,8 +94,11 @@ def process_relevances(input_tokens, all_relevances, generated_tokens):
94
  token, coords = output_with_notes[i]
95
  if coords is not None:
96
  prev_width, prev_patch_end = coords
97
- if prev_patch_end > best_patch_end - best_width: # then notes are overlapping, thus we delete the first one.
 
98
  output_with_notes[i] = (token, None)
 
 
99
  output_with_notes.append((generated_tokens[row], (best_width, best_patch_end)))
100
  else:
101
  output_with_notes.append((generated_tokens[row], None))
@@ -107,25 +110,25 @@ def process_relevances(input_tokens, all_relevances, generated_tokens):
107
  significant_end = best_patch_end + kernel_width
108
  context_start = max(0, significant_start - context_width)
109
  context_end = min(len(input_tokens), significant_end + context_width)
110
- context = input_tokens[context_start:context_end]
111
- output_with_notes[i] = (token, (context, significant_start, significant_end))
 
 
 
 
112
 
113
  return output_with_notes
114
 
115
  def create_html_with_hover(output_with_notes):
116
  html = "<div id='output-container'>"
117
- for i, (text, notes) in enumerate(output_with_notes):
 
118
  if notes:
119
- context, start, end = notes
120
- formatted_context = []
121
- for j, token in enumerate(context):
122
- if start <= j < end:
123
- formatted_context.append(f'<strong>{token}</strong>')
124
- else:
125
- formatted_context.append(token)
126
- formatted_note = "".join(formatted_context)
127
- html += f'<span class="hoverable" data-note-id="note-{i}">{text}<sup>[{i+1}]</sup>'
128
  html += f'<span class="hover-note">{formatted_note}</span></span>'
 
129
  else:
130
  html += f'{text}'
131
  html += "</div>"
@@ -148,7 +151,6 @@ css = """
148
  border-radius: 5px;
149
  bottom: 100%;
150
  left: 50%;
151
- transform: translateX(-50%);
152
  white-space: normal;
153
  background-color: rgba(240, 240, 240, 1);
154
  max-width: 600px;
@@ -159,14 +161,10 @@ css = """
159
  .hoverable:hover .hover-note { display: block; }
160
  """
161
  examples = [
162
- [
163
- """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.
164
 
165
  Question: How high did they climb in 1922? According to the text, the 1922 expedition reached 8,""",
166
- 40
167
- ],
168
- [
169
- """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.
170
 
171
  πŸ”₯ 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!
172
 
@@ -175,10 +173,7 @@ Prithvi WxC (Prithvi, "ΰ€ͺΰ₯ƒΰ€₯ΰ₯ΰ€΅ΰ₯€", is the Sanskrit name for Earth) - is
175
  πŸ’‘ 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.
176
 
177
  How many weather variables can Prithvi predict? Prithvi can""",
178
- 40
179
- ],
180
- [
181
- """Transformers v4.45.0 released: includes a lightning-fast method to build tools! ⚑️
182
 
183
  During user research with colleagues @MoritzLaurer and @Jofthomas , we discovered that the class definition currently in used to define a Tool in transformers.agents is a bit tedious to use, because it goes in great detail.
184
 
@@ -187,14 +182,12 @@ During user research with colleagues @MoritzLaurer and @Jofthomas , we discovere
187
  βœ… VoilΓ , you're good to go!
188
 
189
  How can you build tools simply in transformers? Just use the decorator""",
190
- 40
191
- ]
192
  ]
193
 
194
  with gr.Blocks(css=css) as demo:
195
  gr.Markdown("# Token Generation with Hover Notes")
196
 
197
- input_text = gr.Textbox(label="Enter your prompt:", lines=10, value=examples[0][0])
198
  num_tokens = gr.Slider(minimum=1, maximum=50, value=20, step=1, label="Number of tokens to generate")
199
  generate_button = gr.Button("Generate")
200
 
@@ -208,5 +201,11 @@ with gr.Blocks(css=css) as demo:
208
 
209
  gr.Markdown("Hover over the blue text with superscript numbers to see the important input tokens for that group.")
210
 
 
 
 
 
 
 
211
  if __name__ == "__main__":
212
  demo.launch()
 
94
  token, coords = output_with_notes[i]
95
  if coords is not None:
96
  prev_width, prev_patch_end = coords
97
+ if prev_patch_end > best_patch_end - best_width:
98
+ # then notes are overlapping: thus we delete the first one and make the last wider if needed
99
  output_with_notes[i] = (token, None)
100
+ if prev_patch_end - prev_width < best_patch_end - best_width:
101
+ best_width = best_patch_end - prev_patch_end - prev_width
102
  output_with_notes.append((generated_tokens[row], (best_width, best_patch_end)))
103
  else:
104
  output_with_notes.append((generated_tokens[row], None))
 
110
  significant_end = best_patch_end + kernel_width
111
  context_start = max(0, significant_start - context_width)
112
  context_end = min(len(input_tokens), significant_end + context_width)
113
+ first_part = "".join(input_tokens[context_start:significant_start])
114
+ significant_part = "".join(input_tokens[significant_start:significant_end])
115
+ final_part = "".join(input_tokens[significant_end:context_end])
116
+ print("KK", first_part, significant_part, final_part)
117
+
118
+ output_with_notes[i] = (token, (first_part, significant_part, final_part))
119
 
120
  return output_with_notes
121
 
122
  def create_html_with_hover(output_with_notes):
123
  html = "<div id='output-container'>"
124
+ note_number = 0
125
+ for (text, notes) in output_with_notes:
126
  if notes:
127
+ first_part, significant_part, final_part = notes
128
+ formatted_note = f'{first_part}<strong>{significant_part}</strong>{final_part}'
129
+ html += f'<span class="hoverable" data-note-id="note-{note_number}">{text}<sup>[{note_number+1}]</sup>'
 
 
 
 
 
 
130
  html += f'<span class="hover-note">{formatted_note}</span></span>'
131
+ note_number += 1
132
  else:
133
  html += f'{text}'
134
  html += "</div>"
 
151
  border-radius: 5px;
152
  bottom: 100%;
153
  left: 50%;
 
154
  white-space: normal;
155
  background-color: rgba(240, 240, 240, 1);
156
  max-width: 600px;
 
161
  .hoverable:hover .hover-note { display: block; }
162
  """
163
  examples = [
164
+ """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.
 
165
 
166
  Question: How high did they climb in 1922? According to the text, the 1922 expedition reached 8,""",
167
+ """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.
 
 
 
168
 
169
  πŸ”₯ 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!
170
 
 
173
  πŸ’‘ 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.
174
 
175
  How many weather variables can Prithvi predict? Prithvi can""",
176
+ """Transformers v4.45.0 released: includes a lightning-fast method to build tools! ⚑️
 
 
 
177
 
178
  During user research with colleagues @MoritzLaurer and @Jofthomas , we discovered that the class definition currently in used to define a Tool in transformers.agents is a bit tedious to use, because it goes in great detail.
179
 
 
182
  βœ… VoilΓ , you're good to go!
183
 
184
  How can you build tools simply in transformers? Just use the decorator""",
 
 
185
  ]
186
 
187
  with gr.Blocks(css=css) as demo:
188
  gr.Markdown("# Token Generation with Hover Notes")
189
 
190
+ input_text = gr.Textbox(label="Enter your prompt:", lines=10, value=examples[0])
191
  num_tokens = gr.Slider(minimum=1, maximum=50, value=20, step=1, label="Number of tokens to generate")
192
  generate_button = gr.Button("Generate")
193
 
 
201
 
202
  gr.Markdown("Hover over the blue text with superscript numbers to see the important input tokens for that group.")
203
 
204
+ # Add clickable examples
205
+ gr.Examples(
206
+ examples=examples,
207
+ inputs=[input_text],
208
+ )
209
+
210
  if __name__ == "__main__":
211
  demo.launch()