Sasha Rush commited on
Commit
ed381da
1 Parent(s): 6c33a5f
Files changed (2) hide show
  1. app.py +22 -10
  2. requirements.txt +1 -0
app.py CHANGED
@@ -238,6 +238,7 @@ def draw_board(grid, num=0):
238
  canvas = canvas.center_xy().frame(0.5)
239
  canvas = rectangle(canvas.get_envelope().width, canvas.get_envelope().height).line_width(0.5).fill_color(Color("orange")) + canvas
240
  canvas.render_svg(f"pic{num}.svg", 256)
 
241
  return canvas
242
 
243
 
@@ -354,12 +355,20 @@ evil_game = Game(boundary=(8, 15), key=(5, 1), flag=(7, 13), init=(0, 0), walls=
354
 
355
  games = {"Easy": easy_game, "Medium": medium_game, "Hard": hard_game, "Evil": evil_game}
356
 
357
- # Animate the outputs as a GIF
358
- def animate(l):
 
 
359
  images = []
360
- for i in range(l):
 
 
 
 
 
361
  images.append(imageio.v2.imread(f"pic{i}.png"))
362
- return imageio.v2.mimsave('movie.gif', images, **{ 'duration': 0.5 })
 
363
 
364
 
365
  def load(inp):
@@ -435,7 +444,7 @@ def move(board, action, old_pos):
435
  with gr.Column():
436
  im = gr.Gallery(label="Gallery of the Game")
437
  im.style(preview=True, object_fit="scale-down", columns=1, container=True)
438
- msg_box = gr.Text(label="", show_label=False)
439
 
440
  output = gr.Code(label="Generating Game Code (You can also edit and rerun)", language="python", value="""def my_example():
441
  b = Game(init=(0, 0), flag=(2, 2), walls= [], boundary= (3, 3), key= (1, 1))
@@ -490,7 +499,9 @@ def move(board, action, old_pos):
490
  state_val = (data[prompt], prefix, count, data[examples])
491
  else:
492
  final_msg = "Didn't make it"
493
- yield {im: [f"pic{j}.svg" for j in range(i-1, i)], counter: count, output: prefix,
 
 
494
  msg_box: final_msg, state: state_val}
495
 
496
 
@@ -502,8 +513,9 @@ def move(board, action, old_pos):
502
  c = data[output]
503
  print(c)
504
  i = 0
 
505
  for j in range(len(c)):
506
- q = {}
507
  prefix = c[:j]
508
  ps = prefix.split("\n")
509
  if len(ps) > 3 and not ps[-2].strip().startswith("#") and prefix.endswith("\n"):
@@ -511,11 +523,11 @@ def move(board, action, old_pos):
511
  exec(prefix + "\n return b\nq['board'] = my_example()")
512
  draw_board(q["board"].board.grid, i)
513
  i += 1
514
- animate(i)
515
- out = {im: [f"pic{j}.svg" for j in range(i)]}
516
  print(out)
517
  return out
518
- run_btn.click(run2, inputs={output}, outputs={im})
519
 
520
  gr.HTML("""<center><h2>Leaderboard</h2></center>
521
 
 
238
  canvas = canvas.center_xy().frame(0.5)
239
  canvas = rectangle(canvas.get_envelope().width, canvas.get_envelope().height).line_width(0.5).fill_color(Color("orange")) + canvas
240
  canvas.render_svg(f"pic{num}.svg", 256)
241
+ canvas.render(f"pic{num}.png", 256)
242
  return canvas
243
 
244
 
 
355
 
356
  games = {"Easy": easy_game, "Medium": medium_game, "Hard": hard_game, "Evil": evil_game}
357
 
358
+ # Anima
359
+ def animate(game):
360
+ cur = game.original
361
+ i = 0
362
  images = []
363
+ draw_board(cur.grid, i)
364
+ images.append(imageio.v2.imread(f"pic{i}.png"))
365
+ for act in game.actions:
366
+ cur = cur.move(act)
367
+ i += 1
368
+ draw_board(cur.grid, i)
369
  images.append(imageio.v2.imread(f"pic{i}.png"))
370
+
371
+ return imageio.v2.mimsave('movie.gif', images, **{ 'duration': 1000, 'loop': 100})
372
 
373
 
374
  def load(inp):
 
444
  with gr.Column():
445
  im = gr.Gallery(label="Gallery of the Game")
446
  im.style(preview=True, object_fit="scale-down", columns=1, container=True)
447
+ msg_box = gr.HTML(label="", show_label=False)
448
 
449
  output = gr.Code(label="Generating Game Code (You can also edit and rerun)", language="python", value="""def my_example():
450
  b = Game(init=(0, 0), flag=(2, 2), walls= [], boundary= (3, 3), key= (1, 1))
 
499
  state_val = (data[prompt], prefix, count, data[examples])
500
  else:
501
  final_msg = "Didn't make it"
502
+ animate(q["board"])
503
+
504
+ yield {im: ["movie.gif"], counter: count, output: prefix,
505
  msg_box: final_msg, state: state_val}
506
 
507
 
 
513
  c = data[output]
514
  print(c)
515
  i = 0
516
+ q = {}
517
  for j in range(len(c)):
518
+
519
  prefix = c[:j]
520
  ps = prefix.split("\n")
521
  if len(ps) > 3 and not ps[-2].strip().startswith("#") and prefix.endswith("\n"):
 
523
  exec(prefix + "\n return b\nq['board'] = my_example()")
524
  draw_board(q["board"].board.grid, i)
525
  i += 1
526
+ animate(q["board"])
527
+ out = {im: [f"movie.gif"], msg_box: ""}
528
  print(out)
529
  return out
530
+ run_btn.click(run2, inputs={output}, outputs={im, msg_box})
531
 
532
  gr.HTML("""<center><h2>Leaderboard</h2></center>
533
 
requirements.txt CHANGED
@@ -5,3 +5,4 @@ pycairo
5
  tiktoken
6
  imageio
7
  pandas
 
 
5
  tiktoken
6
  imageio
7
  pandas
8
+