xu3kev commited on
Commit
b1e507c
·
verified ·
1 Parent(s): 373c064

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +65 -17
app.py CHANGED
@@ -6,13 +6,16 @@ import os
6
 
7
  import gradio as gr
8
  import requests
9
- from openai import OpenAI
10
  from func_timeout import FunctionTimedOut, func_timeout
11
  from tqdm import tqdm
12
 
13
  HUGGINGFACE=True
14
- MOCK = False
15
  TEST_FOLDER = "c4f5"
 
 
 
16
 
17
  if HUGGINGFACE:
18
  MODEL_NAME="xu3kev/deepseekcoder-7b-logo-pbe"
@@ -36,7 +39,7 @@ MOCK_RESPONSE = [
36
  forward(2*i)
37
  left(90.0)
38
  """
39
- ] * 16
40
 
41
  LOGO_HEADER = """from myturtle_cv import Turtle
42
  from myturtle import HALF_INF, INF, EPS_DIST, EPS_ANGLE
@@ -218,13 +221,14 @@ def generate_grid_images(gif_results):
218
  plt.close(fig)
219
  return image_array
220
 
 
221
  @spaces.GPU
222
  def llm_call(question_prompt, model_name,
223
  temperature=1, max_tokens=320,
224
  top_p=1, n_samples=64, stop=None):
225
  if HUGGINGFACE:
226
  model_inputs = hug_tokenizer([question_prompt], return_tensors="pt").to('cuda')
227
- generated_ids = hug_model.generate(**model_inputs, max_length=1400, temperature=1, num_return_sequences=10, do_sample=True)
228
  responses = hug_tokenizer.batch_decode(generated_ids, skip_special_tokens=True)
229
  codes = []
230
  for response in responses:
@@ -334,8 +338,9 @@ def run(img_str):
334
 
335
 
336
 
337
- for code in tqdm(codes):
338
- pass
 
339
 
340
  from concurrent.futures import ProcessPoolExecutor
341
  from concurrent.futures import as_completed
@@ -381,10 +386,26 @@ def create_tmp_folder():
381
  return folder_name
382
 
383
 
 
384
  def img_to_code_img(sketchpad_img):
385
- img = sketchpad_img['layers'][0]
 
 
 
 
 
 
 
 
 
 
 
 
 
 
386
  image_array = np.array(img)
387
- image_array = 255 - image_array[:,:,3]
 
388
 
389
  # height, width = image_array.shape
390
  # output_size = 512
@@ -417,13 +438,16 @@ def img_to_code_img(sketchpad_img):
417
  # return generated_grid_img[0]
418
 
419
  folder = create_tmp_folder()
420
- img_names = []
 
421
  for i in range(len(gif_results)):
422
  if gif_results[i]:
423
  with open(f"{folder}/img{i}.gif", "wb") as f:
424
  f.write(gif_results[i])
425
- img_names.append(f"img{i}.gif")
426
- return [f"{folder}/{name}" for name in img_names]
 
 
427
 
428
 
429
  def main():
@@ -434,22 +458,46 @@ def main():
434
  from gradio import Brush
435
  theme = gr.themes.Default().set(
436
  )
 
 
 
 
 
 
 
 
 
 
437
  with gr.Blocks(theme=theme) as demo:
438
  gr.Markdown('# Visual Program Synthesis with LLM')
439
  gr.Markdown("""LOGO/Turtle graphics Programming-by-Example problems aims to synthesize a program that generates the given target image, where the program uses drawing library similar to Python Turtle.""")
440
  gr.Markdown("""Here we can draw a target image using the sketchpad, and see what kinds of graphics program LLM generates. To allow the LLM to visually perceive the input image, we convert the image to ASCII strings.""")
441
  gr.Markdown("Please check out our [paper](https://arxiv.org/abs/2406.08316) for more details!")
442
- gr.Markdown("## Draw logo")
443
  with gr.Row():
444
  with gr.Column(scale=1):
445
- canvas = gr.Sketchpad(canvas_size=(512,512), brush=Brush(colors=["black"], default_size=2, color_mode='fixed'))
446
- submit_button = gr.Button("Submit")
 
447
  with gr.Column(scale=4):
448
  output_gallery = gr.Gallery(
449
- label="Generated images", show_label=False, elem_id="gallery"
450
- , columns=[5], rows=[2], object_fit="contain", height="auto")
451
- # output_image = gr.Image(label="output")
 
 
 
 
 
 
 
 
 
 
 
452
 
 
 
453
  submit_button.click(img_to_code_img, inputs=canvas, outputs=output_gallery)
454
  demo.load(
455
  None,
 
6
 
7
  import gradio as gr
8
  import requests
9
+ # from openai import OpenAI
10
  from func_timeout import FunctionTimedOut, func_timeout
11
  from tqdm import tqdm
12
 
13
  HUGGINGFACE=True
14
+ MOCK = not HUGGINGFACE
15
  TEST_FOLDER = "c4f5"
16
+ NUM_RETURN_SEQ = 10
17
+
18
+ DROPDOWN = None
19
 
20
  if HUGGINGFACE:
21
  MODEL_NAME="xu3kev/deepseekcoder-7b-logo-pbe"
 
39
  forward(2*i)
40
  left(90.0)
41
  """
42
+ ] * 10
43
 
44
  LOGO_HEADER = """from myturtle_cv import Turtle
45
  from myturtle import HALF_INF, INF, EPS_DIST, EPS_ANGLE
 
221
  plt.close(fig)
222
  return image_array
223
 
224
+
225
  @spaces.GPU
226
  def llm_call(question_prompt, model_name,
227
  temperature=1, max_tokens=320,
228
  top_p=1, n_samples=64, stop=None):
229
  if HUGGINGFACE:
230
  model_inputs = hug_tokenizer([question_prompt], return_tensors="pt").to('cuda')
231
+ generated_ids = hug_model.generate(**model_inputs, max_length=1400, temperature=1, num_return_sequences=NUM_RETURN_SEQ, do_sample=True)
232
  responses = hug_tokenizer.batch_decode(generated_ids, skip_special_tokens=True)
233
  codes = []
234
  for response in responses:
 
338
 
339
 
340
 
341
+ # for code in tqdm(codes):
342
+ # pass
343
+ print(f"Running {len(codes)} codes")
344
 
345
  from concurrent.futures import ProcessPoolExecutor
346
  from concurrent.futures import as_completed
 
386
  return folder_name
387
 
388
 
389
+ CODES = []
390
  def img_to_code_img(sketchpad_img):
391
+
392
+ from PIL import Image
393
+ # with open("debug_background.png", "wb") as f:
394
+ # # convert numpy to png
395
+ # numpy_array = sketchpad_img['background']
396
+ # img = Image.fromarray(numpy_array)
397
+ # img.save(f)
398
+ # with open("debug_composite.png", "wb") as f:
399
+ # # convert numpy to png
400
+ # numpy_array = sketchpad_img['composite']
401
+ # img = Image.fromarray(numpy_array)
402
+ # img.save(f)
403
+
404
+ # img = sketchpad_img['layers'][0]
405
+ img = sketchpad_img['composite']
406
  image_array = np.array(img)
407
+ image_array = image_array[:,:,0]
408
+ # image_array = 255 - image_array[:,:,3]
409
 
410
  # height, width = image_array.shape
411
  # output_size = 512
 
438
  # return generated_grid_img[0]
439
 
440
  folder = create_tmp_folder()
441
+ global CODES
442
+ CODES = []
443
  for i in range(len(gif_results)):
444
  if gif_results[i]:
445
  with open(f"{folder}/img{i}.gif", "wb") as f:
446
  f.write(gif_results[i])
447
+ CODES.append(f"```python\n{codes[i]}\n```")
448
+ else:
449
+ CODES.append("#### Execution Error/Timeout; Skip")
450
+ return [f"{folder}/img{i}.gif" for i in range(len(gif_results))]
451
 
452
 
453
  def main():
 
458
  from gradio import Brush
459
  theme = gr.themes.Default().set(
460
  )
461
+ import os
462
+ # get all png files under demo_example
463
+
464
+ example_input_images = []
465
+ for root, dirs, files in os.walk("demo_example"):
466
+ for file in files:
467
+ if file.endswith(".png"):
468
+ example_input_images.append(os.path.join(root, file))
469
+
470
+ canvas = gr.Sketchpad(canvas_size=(512,512), brush=Brush(colors=["black"], default_size=2, color_mode='fixed'))
471
  with gr.Blocks(theme=theme) as demo:
472
  gr.Markdown('# Visual Program Synthesis with LLM')
473
  gr.Markdown("""LOGO/Turtle graphics Programming-by-Example problems aims to synthesize a program that generates the given target image, where the program uses drawing library similar to Python Turtle.""")
474
  gr.Markdown("""Here we can draw a target image using the sketchpad, and see what kinds of graphics program LLM generates. To allow the LLM to visually perceive the input image, we convert the image to ASCII strings.""")
475
  gr.Markdown("Please check out our [paper](https://arxiv.org/abs/2406.08316) for more details!")
476
+ gr.Markdown("## Select an example logo input or draw your own logo!")
477
  with gr.Row():
478
  with gr.Column(scale=1):
479
+ gr.Examples(example_input_images, inputs=canvas)
480
+ canvas.render()
481
+ submit_button = gr.Button("Generate Programs")
482
  with gr.Column(scale=4):
483
  output_gallery = gr.Gallery(
484
+ label="Generated Images", show_label=True, elem_id="gallery"
485
+ , columns=[5], rows=[2], object_fit="contain", height="auto")
486
+ with gr.Group():
487
+ dropdown = gr.Dropdown([f"sample {i+1}" for i in range(NUM_RETURN_SEQ)], label='show generated program samples')
488
+ code_block = gr.Markdown('')
489
+ def update_code(sample_idx):
490
+ int_idx = int(sample_idx.split(" ")[1]) - 1
491
+ if int_idx < len(CODES):
492
+ return CODES[int_idx]
493
+ else:
494
+ return "### Please submit an image to generate programs."
495
+ #return gr.Markdown('333')
496
+ dropdown.input(update_code, dropdown, code_block)
497
+ # output_image = gr.Image(label="output")
498
 
499
+ global DROPDOWN
500
+ DROPDOWN = dropdown
501
  submit_button.click(img_to_code_img, inputs=canvas, outputs=output_gallery)
502
  demo.load(
503
  None,