Vipitis commited on
Commit
6273bce
1 Parent(s): d4e43d6

improved error handling and default pipeline load

Browse files
Files changed (1) hide show
  1. app.py +13 -8
app.py CHANGED
@@ -405,7 +405,7 @@ def alter_body(old_code, func_id: str, funcs_list: list, pipeline=PIPE):
405
  print(f"using for generation: {func_node=}")
406
 
407
 
408
-
409
  if pipeline is None:
410
  print("no pipeline found, loading default one")
411
  pipeline = _make_pipeline("Vipitis/santacoder-finetuned-Shadertoys-fine")
@@ -417,15 +417,21 @@ def alter_body(old_code, func_id: str, funcs_list: list, pipeline=PIPE):
417
  body_end_idx = _line_chr2char(old_code, body_node.end_point[0], body_node.end_point[1])
418
  print(f"{old_code[body_start_idx:body_end_idx]=}")
419
  model_context = identifier_str # just this
420
- num_new_tokens = (body_end_idx - body_start_idx) + 10 #TODO: approximation, we do have early stopping? maybe also use a number instead?
421
 
422
  print(f"generating up to {num_new_tokens} after {model_context!r}")
423
  generation = pipeline(model_context, max_new_tokens=num_new_tokens, return_full_text=False)[0]["generated_text"]
424
  print(f"{generation=}")
425
  id_with_generation = identifier_str + generation
426
  print(f"{id_with_generation=}")
427
- first_gened_func = _parse_functions(id_with_generation)[0] # truncate generation to a single function?
428
- # strip just the body.
 
 
 
 
 
 
429
  print(f"{first_gened_func=}")
430
  generated_body = first_gened_func.child_by_field_name("body").text.decode()
431
  print(f"{generated_body=}")
@@ -461,14 +467,13 @@ with gr.Blocks() as site:
461
  # update_funcs_button = gr.Button("update functions", label="update functions")
462
  with gr.Row():
463
  with gr.Column():
464
- source_embed = gr.HTML('<iframe width="640" height="360" frameborder="0" src="https://www.shadertoy.com/embed/WsBcWV?gui=true&t=0&paused=true&muted=true" allowfullscreen></iframe>', label="How this shader originally renders")
465
  our_embed = gr.HTML(label="glsl render of the current code")
466
- sample_code = gr.Code(label="Current Code (will update changes you generate)", language=None)
467
  bot_md = gr.Markdown(outro_text)
468
-
469
-
470
  sample_pass = gr.State(value={})
471
  pipe = gr.State(value=PIPE)
 
472
  funcs = gr.State(value=[])
473
  # hist_state = gr.State(Value={})
474
  # history_table = gr.JSON()
 
405
  print(f"using for generation: {func_node=}")
406
 
407
 
408
+ print(f"{pipeline=}") # check if default even loaded
409
  if pipeline is None:
410
  print("no pipeline found, loading default one")
411
  pipeline = _make_pipeline("Vipitis/santacoder-finetuned-Shadertoys-fine")
 
417
  body_end_idx = _line_chr2char(old_code, body_node.end_point[0], body_node.end_point[1])
418
  print(f"{old_code[body_start_idx:body_end_idx]=}")
419
  model_context = identifier_str # just this
420
+ num_new_tokens = max(160,(body_end_idx - body_start_idx) + 10) #TODO: approximation, we do have early stopping? maybe also use a number instead? HARD MAX for performance limits.
421
 
422
  print(f"generating up to {num_new_tokens} after {model_context!r}")
423
  generation = pipeline(model_context, max_new_tokens=num_new_tokens, return_full_text=False)[0]["generated_text"]
424
  print(f"{generation=}")
425
  id_with_generation = identifier_str + generation
426
  print(f"{id_with_generation=}")
427
+ try:
428
+ #strip the body
429
+ first_gened_func = _parse_functions(id_with_generation)[0] # truncate generation to a single function?
430
+ except IndexError:
431
+ print("generation wasn't a full function.")
432
+ altered_code = old_code[:body_start_idx] + generation + "//the generation didn't complete the function!\n" + old_code[body_end_idx:] #needs a newline to break out of the comment.
433
+ return altered_code, pipeline
434
+ # raise gr.Error(f"didn't generate a full function: {generation!r}]")
435
  print(f"{first_gened_func=}")
436
  generated_body = first_gened_func.child_by_field_name("body").text.decode()
437
  print(f"{generated_body=}")
 
467
  # update_funcs_button = gr.Button("update functions", label="update functions")
468
  with gr.Row():
469
  with gr.Column():
470
+ source_embed = gr.HTML('<iframe width="640" height="360" frameborder="0" src="" allowfullscreen></iframe>', label="How this shader originally renders")
471
  our_embed = gr.HTML(label="glsl render of the current code")
472
+ sample_code = gr.Code("// touch the slider to select a shader", label="Current Code (will update changes you generate)", language=None)
473
  bot_md = gr.Markdown(outro_text)
 
 
474
  sample_pass = gr.State(value={})
475
  pipe = gr.State(value=PIPE)
476
+ pipe.value=_make_pipeline("Vipitis/santacoder-finetuned-Shadertoys-fine") # set a default like this?
477
  funcs = gr.State(value=[])
478
  # hist_state = gr.State(Value={})
479
  # history_table = gr.JSON()