Vipitis commited on
Commit
280eea5
1 Parent(s): 491ed03

fix byte string, (dropdown is broken)

Browse files
Files changed (4) hide show
  1. README.md +1 -1
  2. app.py +10 -10
  3. utils/generation.py +1 -1
  4. utils/tree_utils.py +1 -1
README.md CHANGED
@@ -4,7 +4,7 @@ emoji: 💻
4
  colorFrom: green
5
  colorTo: red
6
  sdk: gradio
7
- sdk_version: 3.47.1
8
  app_file: app.py
9
  pinned: true
10
  license: mit
 
4
  colorFrom: green
5
  colorTo: red
6
  sdk: gradio
7
+ sdk_version: 4.1.1
8
  app_file: app.py
9
  pinned: true
10
  license: mit
app.py CHANGED
@@ -2,7 +2,7 @@ import gradio as gr
2
  from transformers import pipeline, AutoModelForCausalLM, AutoTokenizer
3
  import datasets
4
  import numpy as np
5
- import torch
6
 
7
  from utils.tree_utils import parse_functions, get_docstrings, grab_before_comments, line_chr2char, node_str_idx, replace_function
8
  from utils.html_utils import make_iframe, construct_embed
@@ -211,7 +211,7 @@ def alter_body(old_code, func_id, funcs_list: list, prompt="", temperature=0.2,
211
  yield altered_code #yield once so it updates? -> works... gg but doesn't seem to do it for the dropdown
212
  return altered_code #never gets used by the code block? maybe I need to yield it first? but works in the ov_notebook
213
 
214
- def list_dropdown(in_code): #only used for auto update, not on sample pick?
215
  funcs = parse_functions(in_code)
216
  func_identifiers = [f"{idx:2d}: {n.child_by_field_name('declarator').text.decode()}" for idx, n in enumerate(funcs)]
217
  # funcs = [n for n in funcs] #wrapped as set to avoid json issues?
@@ -225,11 +225,11 @@ if __name__ == "__main__": #works on huggingface?
225
  all_single_passes = datasets.concatenate_datasets([single_passes["train"], single_passes["test"]])
226
  num_samples = len(all_single_passes)
227
 
228
- with gr.Blocks() as site:
229
  top_md = gr.Markdown(intro_text)
230
  model_cp = gr.Textbox(value="Vipitis/santacoder-finetuned-Shadertoys-fine", label="Model Checkpoint (Enter to load!)", interactive=True)
231
  sample_idx = gr.Slider(minimum=0, maximum=10513, value=3211, label="pick sample from dataset", step=1.0)
232
- func_dropdown = gr.Dropdown(value=["0: edit the Code (or load a shader) to update this dropdown"], label="chose a function to modify") #breaks if I add a string in before that? #TODO: use type="index" to get int - always gives None?
233
  prompt_text = gr.Textbox(value="the title used by the model has generation hint", label="prompt text", info="leave blank to skip", interactive=True)
234
  with gr.Accordion("Advanced settings", open=False): # from: https://huggingface.co/spaces/bigcode/bigcode-playground/blob/main/app.py
235
  with gr.Row():
@@ -273,8 +273,8 @@ if __name__ == "__main__": #works on huggingface?
273
  info="Penalize repeated tokens",
274
  )
275
  with gr.Row():
276
- gen_return_button = gr.Button("generate a alternate return statement", label="generate return", scale=0)
277
- gen_func_button = gr.Button("generate an alternate function body", label="generate function", scale=1)
278
  with gr.Row():
279
  with gr.Column():
280
  source_embed = gr.HTML('<iframe width="640" height="360" frameborder="0" src="" allowfullscreen></iframe>', label="How this shader originally renders")
@@ -290,10 +290,10 @@ if __name__ == "__main__": #works on huggingface?
290
  sample_idx.release(fn=grab_sample, inputs=[sample_idx], outputs=[sample_pass, sample_code, prompt_text, source_embed]) #funcs here?
291
  gen_return_button.click(fn=alter_return, inputs=[sample_code, func_dropdown, temperature, max_new_tokens, top_p, repetition_penalty, pipe], outputs=[sample_code])
292
  gen_func_button.click(fn=alter_body, inputs=[sample_code, func_dropdown, funcs, prompt_text, temperature, max_new_tokens, top_p, repetition_penalty, pipe], outputs=[sample_code]).then(
293
- fn=list_dropdown, inputs=[sample_code], outputs=[funcs, func_dropdown]
294
  )
295
- sample_code.change(fn=list_dropdown, inputs=[sample_code], outputs=[funcs, func_dropdown]).then(
296
  fn=make_iframe, inputs=[sample_code], outputs=[our_embed])
297
 
298
- site.queue()
299
- site.launch()
 
2
  from transformers import pipeline, AutoModelForCausalLM, AutoTokenizer
3
  import datasets
4
  import numpy as np
5
+ # import torch
6
 
7
  from utils.tree_utils import parse_functions, get_docstrings, grab_before_comments, line_chr2char, node_str_idx, replace_function
8
  from utils.html_utils import make_iframe, construct_embed
 
211
  yield altered_code #yield once so it updates? -> works... gg but doesn't seem to do it for the dropdown
212
  return altered_code #never gets used by the code block? maybe I need to yield it first? but works in the ov_notebook
213
 
214
+ def list_dropdown_options(in_code): #only used for auto update, not on sample pick?
215
  funcs = parse_functions(in_code)
216
  func_identifiers = [f"{idx:2d}: {n.child_by_field_name('declarator').text.decode()}" for idx, n in enumerate(funcs)]
217
  # funcs = [n for n in funcs] #wrapped as set to avoid json issues?
 
225
  all_single_passes = datasets.concatenate_datasets([single_passes["train"], single_passes["test"]])
226
  num_samples = len(all_single_passes)
227
 
228
+ with gr.Blocks() as demo:
229
  top_md = gr.Markdown(intro_text)
230
  model_cp = gr.Textbox(value="Vipitis/santacoder-finetuned-Shadertoys-fine", label="Model Checkpoint (Enter to load!)", interactive=True)
231
  sample_idx = gr.Slider(minimum=0, maximum=10513, value=3211, label="pick sample from dataset", step=1.0)
232
+ func_dropdown = gr.Dropdown(choices=["0: edit the Code (or load a shader) to update this dropdown"], label="chose a function to modify") #breaks if I add a string in before that? #TODO: use type="index" to get int - always gives None?
233
  prompt_text = gr.Textbox(value="the title used by the model has generation hint", label="prompt text", info="leave blank to skip", interactive=True)
234
  with gr.Accordion("Advanced settings", open=False): # from: https://huggingface.co/spaces/bigcode/bigcode-playground/blob/main/app.py
235
  with gr.Row():
 
273
  info="Penalize repeated tokens",
274
  )
275
  with gr.Row():
276
+ gen_return_button = gr.Button("generate a alternate return statement", scale=0)
277
+ gen_func_button = gr.Button("generate an alternate function body", scale=1)
278
  with gr.Row():
279
  with gr.Column():
280
  source_embed = gr.HTML('<iframe width="640" height="360" frameborder="0" src="" allowfullscreen></iframe>', label="How this shader originally renders")
 
290
  sample_idx.release(fn=grab_sample, inputs=[sample_idx], outputs=[sample_pass, sample_code, prompt_text, source_embed]) #funcs here?
291
  gen_return_button.click(fn=alter_return, inputs=[sample_code, func_dropdown, temperature, max_new_tokens, top_p, repetition_penalty, pipe], outputs=[sample_code])
292
  gen_func_button.click(fn=alter_body, inputs=[sample_code, func_dropdown, funcs, prompt_text, temperature, max_new_tokens, top_p, repetition_penalty, pipe], outputs=[sample_code]).then(
293
+ fn=list_dropdown_options, inputs=[sample_code], outputs=[funcs, func_dropdown]
294
  )
295
+ sample_code.change(fn=list_dropdown_options, inputs=[sample_code], outputs=[funcs, func_dropdown]).then(
296
  fn=make_iframe, inputs=[sample_code], outputs=[our_embed])
297
 
298
+ demo.queue()
299
+ demo.launch()
utils/generation.py CHANGED
@@ -35,7 +35,7 @@ def stream_generation(prompt:str, pipe, gen_kwargs:dict):
35
 
36
  # Start generation on a separate thread, so that we don't block the UI. The text is pulled from the streamer
37
  # in the main thread. Adds timeout to the streamer to handle exceptions in the generation thread.
38
- streamer = TextIteratorStreamer(pipe.tokenizer, skip_prompt=True, skip_special_tokens=True, timeout=15.0)
39
  generate_kwargs = dict(model_inputs, streamer=streamer, **gen_kwargs)
40
  t = Thread(target=pipe.model.generate, kwargs=generate_kwargs)
41
  t.start()
 
35
 
36
  # Start generation on a separate thread, so that we don't block the UI. The text is pulled from the streamer
37
  # in the main thread. Adds timeout to the streamer to handle exceptions in the generation thread.
38
+ streamer = TextIteratorStreamer(pipe.tokenizer, skip_prompt=True, skip_special_tokens=True, timeout=45.0) #IPEX takes a bit on first inference, to avoid an error with the empty queue timeout on the first time, we just wait longer.
39
  generate_kwargs = dict(model_inputs, streamer=streamer, **gen_kwargs)
40
  t = Thread(target=pipe.model.generate, kwargs=generate_kwargs)
41
  t.start()
utils/tree_utils.py CHANGED
@@ -13,7 +13,7 @@ def replace_function(old_func_node, new_func_node):
13
  tree = give_tree(old_func_node)
14
  old_func_start, old_func_end = node_str_idx(old_func_node)
15
  # new_func_start, new_func_end = node_str_idx(new_func_node)
16
- new_code = tree.text[:old_func_start] + new_func_node.text + tree.text[old_func_end:]
17
  return new_code
18
 
19
  def get_root(node):
 
13
  tree = give_tree(old_func_node)
14
  old_func_start, old_func_end = node_str_idx(old_func_node)
15
  # new_func_start, new_func_end = node_str_idx(new_func_node)
16
+ new_code = tree.text.decode()[:old_func_start] + new_func_node.text.decode() + tree.text.decode()[old_func_end:]
17
  return new_code
18
 
19
  def get_root(node):