m-ric HF Staff commited on
Commit
7e6471e
Β·
1 Parent(s): 50b0870

Working nice version

Browse files
Files changed (1) hide show
  1. app.py +7 -6
app.py CHANGED
@@ -7,15 +7,15 @@ 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
  def really_clean_tokens(tokens):
15
  tokens = clean_tokens(tokens)
16
  cleaned_tokens = []
17
  for token in tokens:
18
- token = token.replace("_", " ").replace("▁", " ").replace("<s>", " ")
19
  if token.startswith("<0x") and token.endswith(">"):
20
  # Convert hex to character
21
  char_code = int(token[3:-1], 16)
@@ -53,7 +53,7 @@ def process_relevances(input_tokens, all_relevances, generated_tokens):
53
  attention_matrix = np.array([el[:len(all_relevances[0])] for el in all_relevances])
54
 
55
  ### FIND ZONES OF INTEREST
56
- threshold_per_token = 0.22
57
  kernel_width = 6
58
  context_width = 20 # Number of tokens to include as context on each side
59
  kernel = np.ones((kernel_width, kernel_width))
@@ -136,7 +136,8 @@ def create_html_with_hover(output_with_notes):
136
  html += f'{token}'
137
  i += 1
138
  else:
139
- text = "".join([element[0] for element in output_with_notes[i:i+width]])
 
140
  first_part, significant_part, final_part = notes
141
  formatted_note = f'{first_part}<strong>{significant_part}</strong>{final_part}'
142
  html += f'<span class="hoverable" data-note-id="note-{note_number}">{text}<sup>[{note_number+1}]</sup>'
@@ -200,7 +201,7 @@ with gr.Blocks(css=css) as demo:
200
  gr.Markdown("# Token Generation with Hover Notes")
201
 
202
  input_text = gr.Textbox(label="Enter your prompt:", lines=10, value=examples[0])
203
- num_tokens = gr.Slider(minimum=1, maximum=50, value=20, step=1, label="Number of tokens to generate")
204
  generate_button = gr.Button("Generate")
205
 
206
  output_html = gr.HTML(label="Generated Output")
 
7
  import spaces
8
  from scipy.signal import convolve2d
9
 
10
+ model = LlamaForCausalLM.from_pretrained("HuggingFaceTB/SmolLM-1.7B-Instruct", torch_dtype=torch.bfloat16, device_map="cuda")
11
+ tokenizer = AutoTokenizer.from_pretrained("HuggingFaceTB/SmolLM-1.7B-Instruct")
12
  attnlrp.register(model)
13
 
14
  def really_clean_tokens(tokens):
15
  tokens = clean_tokens(tokens)
16
  cleaned_tokens = []
17
  for token in tokens:
18
+ token = token.replace("_", " ").replace("▁", " ").replace("<s>", " ").replace("Ċ", " ").replace("Δ ", " ")
19
  if token.startswith("<0x") and token.endswith(">"):
20
  # Convert hex to character
21
  char_code = int(token[3:-1], 16)
 
53
  attention_matrix = np.array([el[:len(all_relevances[0])] for el in all_relevances])
54
 
55
  ### FIND ZONES OF INTEREST
56
+ threshold_per_token = 0.2
57
  kernel_width = 6
58
  context_width = 20 # Number of tokens to include as context on each side
59
  kernel = np.ones((kernel_width, kernel_width))
 
136
  html += f'{token}'
137
  i += 1
138
  else:
139
+ text = "".join(really_clean_tokens([element[0] for element in output_with_notes[i:i+width]]))
140
+ print(text)
141
  first_part, significant_part, final_part = notes
142
  formatted_note = f'{first_part}<strong>{significant_part}</strong>{final_part}'
143
  html += f'<span class="hoverable" data-note-id="note-{note_number}">{text}<sup>[{note_number+1}]</sup>'
 
201
  gr.Markdown("# Token Generation with Hover Notes")
202
 
203
  input_text = gr.Textbox(label="Enter your prompt:", lines=10, value=examples[0])
204
+ num_tokens = gr.Slider(minimum=1, maximum=100, value=20, step=1, label="Number of tokens to generate")
205
  generate_button = gr.Button("Generate")
206
 
207
  output_html = gr.HTML(label="Generated Output")