xu3kev commited on
Commit
ae2a978
1 Parent(s): bcbc8c1

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +69 -42
app.py CHANGED
@@ -1,7 +1,7 @@
1
- import spaces
2
  import argparse
3
  import json
4
  import numpy as np
 
5
 
6
  import gradio as gr
7
  import requests
@@ -13,12 +13,12 @@ HUGGINGFACE=True
13
  MOCK = False
14
  TEST_FOLDER = "c4f5"
15
 
16
- MODEL_NAME="xu3kev/deepseekcoder-7b-logo-pbe"
17
- # MODEL_NAME="openlm-research/open_llama_3b"
18
- import torch
19
- from transformers import AutoModelForCausalLM, AutoTokenizer
20
- hug_model = AutoModelForCausalLM.from_pretrained(MODEL_NAME, torch_dtype=torch.float16, device_map='auto', load_in_8bit=True)
21
- hug_tokenizer = AutoTokenizer.from_pretrained(MODEL_NAME)
22
 
23
  INPUT_STRUCTION_TEMPLATE = """Here is a gray scale images representing with integer values 0-9.
24
  {image_str}
@@ -37,7 +37,7 @@ MOCK_RESPONSE = [
37
  """
38
  ] * 16
39
 
40
- LOGO_HEADER = """from myturtle import Turtle
41
  from myturtle import HALF_INF, INF, EPS_DIST, EPS_ANGLE
42
 
43
  turtle = Turtle()
@@ -169,7 +169,8 @@ PORT = 8008
169
  MODEL_NAME="./axolotl/lora-logo_fix_full_deepseek33b_ds33i_epoch3_lr_0.0002_alpha_512_r_512_merged"
170
  MODEL_NAME="./axolotl/lora-logo_fix_full_deepseek7b_ds33i_lr_0.0002_alpha_512_r_512_merged"
171
 
172
- def generate_grid_images(folder):
 
173
  import matplotlib.patches as patches
174
  import matplotlib.pyplot as plt
175
  num_rows, num_cols = 8,8
@@ -217,13 +218,12 @@ def generate_grid_images(folder):
217
  return image_array
218
 
219
 
220
- @spaces.GPU
221
  def llm_call(question_prompt, model_name,
222
  temperature=1, max_tokens=320,
223
  top_p=1, n_samples=64, stop=None):
224
  if HUGGINGFACE:
225
  model_inputs = hug_tokenizer([question_prompt], return_tensors="pt").to('cuda')
226
- generated_ids = hug_model.generate(**model_inputs, max_length=1400, temperature=1, num_return_sequences=32, do_sample=True)
227
  responses = hug_tokenizer.batch_decode(generated_ids, skip_special_tokens=True)
228
  codes = []
229
  for response in responses:
@@ -296,12 +296,15 @@ def run_code(new_folder, counter, code):
296
  code_with_header_and_save= f"""
297
  {LOGO_HEADER}
298
  {code}
299
- turtle.save('{fname}')
 
300
  """
301
  try:
302
- func_timeout(3, exec, args=(code_with_header_and_save, {}))
303
- matplotlib.pyplot.close()
304
  # exec(code_with_header_and_save, globals())
 
 
305
  except FunctionTimedOut:
306
  print("Timeout")
307
  except Exception as e:
@@ -317,8 +320,8 @@ def run(img_str):
317
  codes = MOCK_RESPONSE
318
 
319
  gradio_test_images_folder = "gradio_test_images"
320
- import os
321
- os.makedirs(gradio_test_images_folder, exist_ok=True)
322
 
323
  counter = 0
324
  # generate a random hash id
@@ -326,16 +329,21 @@ def run(img_str):
326
  import random
327
  random_id = hashlib.md5(str(random.random()).encode()).hexdigest()[0:4]
328
  new_folder = os.path.join(gradio_test_images_folder, random_id)
329
- os.makedirs(new_folder, exist_ok=True)
 
 
 
 
 
330
 
331
- print('about to execute')
332
  from concurrent.futures import ProcessPoolExecutor
333
  from concurrent.futures import as_completed
 
334
  with ProcessPoolExecutor() as executor:
335
  futures = [executor.submit(run_code, new_folder, i, code) for i, code in enumerate(codes)]
336
  for future in as_completed(futures):
337
  try:
338
- future.result()
339
  except Exception as exc:
340
  print(f'Generated an exception: {exc}')
341
 
@@ -346,10 +354,9 @@ def run(img_str):
346
  # out, errs = p.communicate()
347
  # out, errs, = out.decode(), errs.decode()
348
  # render
349
- print('finish execute')
350
  print(random_id)
351
  folder_path = f"gradio_test_images/{random_id}"
352
- return folder_path, codes
353
 
354
 
355
  def test_gen_img_wrapper(_):
@@ -363,6 +370,16 @@ def int_img_to_str(integer_img):
363
  image_str = "\n".join(lines)
364
  return image_str
365
 
 
 
 
 
 
 
 
 
 
 
366
  def img_to_code_img(sketchpad_img):
367
  img = sketchpad_img['layers'][0]
368
  image_array = np.array(img)
@@ -392,11 +409,18 @@ def img_to_code_img(sketchpad_img):
392
  img_str = int_img_to_str(int_img)
393
  print(img_str)
394
 
395
- folder_path, codes = run(img_str)
396
 
397
- generated_grid_img = generate_grid_images(folder_path)
398
 
399
- return generated_grid_img
 
 
 
 
 
 
 
400
 
401
 
402
  def main():
@@ -411,26 +435,29 @@ def main():
411
  gr.Markdown('# Visual Program Synthesis with LLM')
412
  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.""")
413
  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.""")
414
- gr.Markdown("Please checkout our [paper](https://arxiv.org/abs/2406.08316) for more details!")
415
  gr.Markdown("## Draw logo")
416
- with gr.Column():
417
- canvas = gr.Sketchpad(canvas_size=(512,512), brush=Brush(colors=["black"], default_size=2, color_mode='fixed'))
418
- submit_button = gr.Button("Submit")
419
- output_image = gr.Image(label="output")
 
 
 
 
420
 
421
- submit_button.click(img_to_code_img, inputs=canvas, outputs=output_image)
422
- # demo.load(
423
- # None,
424
- # None,
425
- # js="""
426
- # () => {
427
- # const params = new URLSearchParams(window.location.search);
428
- # if (!params.has('__theme')) {
429
- # params.set('__theme', 'light');
430
- # window.location.search = params.toString();
431
- # }
432
- # }""",
433
- # )
434
 
435
  demo.launch(share=True)
436
 
 
 
1
  import argparse
2
  import json
3
  import numpy as np
4
+ import os
5
 
6
  import gradio as gr
7
  import requests
 
13
  MOCK = False
14
  TEST_FOLDER = "c4f5"
15
 
16
+ if HUGGINGFACE:
17
+ MODEL_NAME="xu3kev/deepseekcoder-7b-logo-pbe"
18
+ import torch
19
+ from transformers import AutoModelForCausalLM, AutoTokenizer
20
+ hug_model = AutoModelForCausalLM.from_pretrained(MODEL_NAME, torch_dtype=torch.float16, attn_implementation="flash_attention_2",).to('cuda')
21
+ hug_tokenizer = AutoTokenizer.from_pretrained(MODEL_NAME)
22
 
23
  INPUT_STRUCTION_TEMPLATE = """Here is a gray scale images representing with integer values 0-9.
24
  {image_str}
 
37
  """
38
  ] * 16
39
 
40
+ LOGO_HEADER = """from myturtle_cv import Turtle
41
  from myturtle import HALF_INF, INF, EPS_DIST, EPS_ANGLE
42
 
43
  turtle = Turtle()
 
169
  MODEL_NAME="./axolotl/lora-logo_fix_full_deepseek33b_ds33i_epoch3_lr_0.0002_alpha_512_r_512_merged"
170
  MODEL_NAME="./axolotl/lora-logo_fix_full_deepseek7b_ds33i_lr_0.0002_alpha_512_r_512_merged"
171
 
172
+
173
+ def generate_grid_images(gif_results):
174
  import matplotlib.patches as patches
175
  import matplotlib.pyplot as plt
176
  num_rows, num_cols = 8,8
 
218
  return image_array
219
 
220
 
 
221
  def llm_call(question_prompt, model_name,
222
  temperature=1, max_tokens=320,
223
  top_p=1, n_samples=64, stop=None):
224
  if HUGGINGFACE:
225
  model_inputs = hug_tokenizer([question_prompt], return_tensors="pt").to('cuda')
226
+ generated_ids = hug_model.generate(**model_inputs, max_length=1400, temperature=1, num_return_sequences=8, do_sample=True)
227
  responses = hug_tokenizer.batch_decode(generated_ids, skip_special_tokens=True)
228
  codes = []
229
  for response in responses:
 
296
  code_with_header_and_save= f"""
297
  {LOGO_HEADER}
298
  {code}
299
+ # turtle.save('{fname}')
300
+ gif = turtle.save_gif('')
301
  """
302
  try:
303
+ results = {}
304
+ func_timeout(3, exec, args=(code_with_header_and_save, results))
305
  # exec(code_with_header_and_save, globals())
306
+ if 'gif' in results:
307
+ return results['gif']
308
  except FunctionTimedOut:
309
  print("Timeout")
310
  except Exception as e:
 
320
  codes = MOCK_RESPONSE
321
 
322
  gradio_test_images_folder = "gradio_test_images"
323
+ # import os
324
+ # os.makedirs(gradio_test_images_folder, exist_ok=True)
325
 
326
  counter = 0
327
  # generate a random hash id
 
329
  import random
330
  random_id = hashlib.md5(str(random.random()).encode()).hexdigest()[0:4]
331
  new_folder = os.path.join(gradio_test_images_folder, random_id)
332
+ # os.makedirs(new_folder, exist_ok=True)
333
+
334
+
335
+
336
+ for code in tqdm(codes):
337
+ pass
338
 
 
339
  from concurrent.futures import ProcessPoolExecutor
340
  from concurrent.futures import as_completed
341
+ gif_results = []
342
  with ProcessPoolExecutor() as executor:
343
  futures = [executor.submit(run_code, new_folder, i, code) for i, code in enumerate(codes)]
344
  for future in as_completed(futures):
345
  try:
346
+ gif_results.append(future.result())
347
  except Exception as exc:
348
  print(f'Generated an exception: {exc}')
349
 
 
354
  # out, errs = p.communicate()
355
  # out, errs, = out.decode(), errs.decode()
356
  # render
 
357
  print(random_id)
358
  folder_path = f"gradio_test_images/{random_id}"
359
+ return gif_results, codes
360
 
361
 
362
  def test_gen_img_wrapper(_):
 
370
  image_str = "\n".join(lines)
371
  return image_str
372
 
373
+
374
+ def create_tmp_folder():
375
+ import hashlib
376
+ import random
377
+ random_id = hashlib.md5(str(random.random()).encode()).hexdigest()[0:4]
378
+ folder_name = f"generated_images_{random_id}"
379
+ os.makedirs(folder_name, exist_ok=True)
380
+ return folder_name
381
+
382
+
383
  def img_to_code_img(sketchpad_img):
384
  img = sketchpad_img['layers'][0]
385
  image_array = np.array(img)
 
409
  img_str = int_img_to_str(int_img)
410
  print(img_str)
411
 
412
+ gif_results, codes = run(img_str)
413
 
414
+ # generated_grid_img = generate_grid_images()
415
 
416
+ # return generated_grid_img[0]
417
+
418
+ folder = create_tmp_folder()
419
+ for i in range(len(gif_results)):
420
+ if gif_results[i]:
421
+ with open(f"{folder}/img{i}.gif", "wb") as f:
422
+ f.write(gif_results[i])
423
+ return [f"{folder}/img{i}.gif" for i in range(len(gif_results))]
424
 
425
 
426
  def main():
 
435
  gr.Markdown('# Visual Program Synthesis with LLM')
436
  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.""")
437
  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.""")
 
438
  gr.Markdown("## Draw logo")
439
+ with gr.Row():
440
+ with gr.Column():
441
+ canvas = gr.Sketchpad(canvas_size=(512,512), brush=Brush(colors=["black"], default_size=2, color_mode='fixed'))
442
+ submit_button = gr.Button("Submit")
443
+ output_gallery = gr.Gallery(
444
+ label="Generated images", show_label=False, elem_id="gallery"
445
+ , columns=[3], rows=[3], object_fit="contain", height="auto")
446
+ # output_image = gr.Image(label="output")
447
 
448
+ submit_button.click(img_to_code_img, inputs=canvas, outputs=output_gallery)
449
+ demo.load(
450
+ None,
451
+ None,
452
+ js="""
453
+ () => {
454
+ const params = new URLSearchParams(window.location.search);
455
+ if (!params.has('__theme')) {
456
+ params.set('__theme', 'light');
457
+ window.location.search = params.toString();
458
+ }
459
+ }""",
460
+ )
461
 
462
  demo.launch(share=True)
463