Vipitis commited on
Commit
ed9c55f
1 Parent(s): dae26d8

docstrings in prompts!

Browse files
Files changed (1) hide show
  1. app.py +16 -8
app.py CHANGED
@@ -49,7 +49,7 @@ canvas {
49
  <canvas id="canvas"></canvas>
50
  <div class="playpause">▶</div>
51
  </div>
52
- blank canvas here indicates that some of the shadertoy specific functions are not yet supported with this implementation (like #define I believe). you can always copy and paste the code into a shadertoy.com window to try.
53
  </body>
54
  <!--
55
  for most samples webgl-utils only provides shader compiling/linking and
@@ -227,7 +227,7 @@ main();
227
 
228
  def make_iframe(shader_code): #keep a single function?
229
  script = make_script(shader_code)
230
- return f"""<iframe width="640" height="512" srcdoc=\'{script}\' allowfullscreen></iframe>"""
231
 
232
 
233
  intro_text = """
@@ -266,10 +266,11 @@ outro_text ="""
266
  - [x] generate whole functions (seems to work quite well)
267
  - [] dropdown for model selection (from curated list or all supported models?)
268
  - [] generation history stating which function and orig/generated returns. (use State ??). do it as comments in the code?
269
- - [] display errros/issues to the user (raise gr.Error could be one idea, but highlighting in the code would be awesome)
270
  - [] generate whole shaders (via prompts guidance, recursive from errors)
271
  - [] accordion with generation parameters (as pipeline_kwargs?) look up starcoder playround and take "inspiration" from there
272
  - [] support FIM task for better model context
 
273
  - [] gradio examples
274
 
275
  ### Notes:
@@ -310,10 +311,11 @@ def grab_sample(sample_idx):
310
  def _parse_functions(in_code):
311
  """
312
  returns all functions in the code as their actual nodes.
313
-
314
  """
315
  tree = parser.parse(bytes(in_code, "utf8"))
316
  funcs = [n for n in tree.root_node.children if n.type == "function_definition"]
 
317
  return funcs
318
 
319
  PIPE = None
@@ -411,12 +413,18 @@ def alter_body(old_code, func_id: str, funcs_list: list, pipeline=PIPE):
411
  pipeline = _make_pipeline("Vipitis/santacoder-finetuned-Shadertoys-fine")
412
 
413
  func_start_idx = _line_chr2char(old_code, func_node.start_point[0], func_node.start_point[1])
414
- identifier_str = func_node.child_by_field_name("type").text.decode() + " " + func_node.child_by_field_name("declarator").text.decode()
415
  body_node = func_node.child_by_field_name("body")
416
  body_start_idx = _line_chr2char(old_code, body_node.start_point[0], body_node.start_point[1])
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}")
@@ -429,13 +437,13 @@ def alter_body(old_code, func_id: str, funcs_list: list, pipeline=PIPE):
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=}")
438
- altered_code = old_code[:body_start_idx] + generated_body + old_code[body_end_idx:]
439
  return altered_code, pipeline
440
 
441
  def add_history(func_id, orig_rtn, gened_rtn, history):
 
49
  <canvas id="canvas"></canvas>
50
  <div class="playpause">▶</div>
51
  </div>
52
+ \nblank canvas here indicates that some of the shadertoy specific functions are not yet supported with this implementation (like #define I believe). you can always copy and paste the code into a shadertoy.com window to try.
53
  </body>
54
  <!--
55
  for most samples webgl-utils only provides shader compiling/linking and
 
227
 
228
  def make_iframe(shader_code): #keep a single function?
229
  script = make_script(shader_code)
230
+ return f"""<iframe width="640" height="420" srcdoc=\'{script}\' allowfullscreen></iframe>"""
231
 
232
 
233
  intro_text = """
 
266
  - [x] generate whole functions (seems to work quite well)
267
  - [] dropdown for model selection (from curated list or all supported models?)
268
  - [] generation history stating which function and orig/generated returns. (use State ??). do it as comments in the code?
269
+ - [~] display errros/issues to the user (raise gr.Error could be one idea, but highlighting in the code would be awesome) currently adds a comment to the code.
270
  - [] generate whole shaders (via prompts guidance, recursive from errors)
271
  - [] accordion with generation parameters (as pipeline_kwargs?) look up starcoder playround and take "inspiration" from there
272
  - [] support FIM task for better model context
273
+ - [~] include some context for prompt (title, comments before a functions) - now works with the first comment inside a function body (has to be first)
274
  - [] gradio examples
275
 
276
  ### Notes:
 
311
  def _parse_functions(in_code):
312
  """
313
  returns all functions in the code as their actual nodes.
314
+ includes any comment made directly after the function definition or diretly after #copilot trigger
315
  """
316
  tree = parser.parse(bytes(in_code, "utf8"))
317
  funcs = [n for n in tree.root_node.children if n.type == "function_definition"]
318
+
319
  return funcs
320
 
321
  PIPE = None
 
413
  pipeline = _make_pipeline("Vipitis/santacoder-finetuned-Shadertoys-fine")
414
 
415
  func_start_idx = _line_chr2char(old_code, func_node.start_point[0], func_node.start_point[1])
416
+ identifier_str = func_node.child_by_field_name("type").text.decode() + " " + func_node.child_by_field_name("declarator").text.decode() #func_start_idx:body_start_idx?
417
  body_node = func_node.child_by_field_name("body")
418
  body_start_idx = _line_chr2char(old_code, body_node.start_point[0], body_node.start_point[1])
419
  body_end_idx = _line_chr2char(old_code, body_node.end_point[0], body_node.end_point[1])
420
  print(f"{old_code[body_start_idx:body_end_idx]=}")
421
+ model_context = identifier_str # base case
422
+ # add any comments at the beginning of the function to the model_context
423
+ second_child = func_node.child_by_field_name("body").children[1] #might error out?
424
+ if second_child.type == "comment":
425
+ # print(second_child.text.decode())
426
+ model_context += second_child.text.decode()
427
+ print(f"{model_context=}")
428
  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.
429
 
430
  print(f"generating up to {num_new_tokens} after {model_context!r}")
 
437
  first_gened_func = _parse_functions(id_with_generation)[0] # truncate generation to a single function?
438
  except IndexError:
439
  print("generation wasn't a full function.")
440
+ altered_code = old_code[:func_start_idx] + model_context + generation + "//the generation didn't complete the function!\n" + old_code[body_end_idx:] #needs a newline to break out of the comment.
441
  return altered_code, pipeline
442
  # raise gr.Error(f"didn't generate a full function: {generation!r}]")
443
  print(f"{first_gened_func=}")
444
  generated_body = first_gened_func.child_by_field_name("body").text.decode()
445
  print(f"{generated_body=}")
446
+ altered_code = old_code[:func_start_idx] + model_context + generated_body + old_code[body_end_idx:]
447
  return altered_code, pipeline
448
 
449
  def add_history(func_id, orig_rtn, gened_rtn, history):