multimodalart HF Staff commited on
Commit
151dc74
·
verified ·
1 Parent(s): 7cc62e7

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +8 -4
app.py CHANGED
@@ -12,19 +12,23 @@ from fastapi.staticfiles import StaticFiles
12
 
13
  import os
14
  import uvicorn
 
15
 
16
  mario_lm = MarioLM()
17
  device = torch.device('cuda')
18
  mario_lm = mario_lm.to(device)
19
  TILE_DIR = "data/tiles"
20
 
 
 
21
  app = FastAPI()
22
 
23
  def make_html_file(generated_level):
24
  level_text = f"""{'''
25
  '''.join(view_level(generated_level,mario_lm.tokenizer))}"""
26
  unique_id = uuid.uuid1()
27
- with open(f"static/demo-{unique_id}.html", 'w', encoding='utf-8') as f:
 
28
  f.write(f'''<!DOCTYPE html>
29
  <html lang="en">
30
 
@@ -41,10 +45,10 @@ def make_html_file(generated_level):
41
  cheerpjAddStringFile("/str/mylevel.txt", `{level_text}`);
42
  }});
43
  cheerpjCreateDisplay(512, 500);
44
- cheerpjRunJar("/app/static/mario.jar");
45
  </script>
46
  </html>''')
47
- return f"demo-{unique_id}.html"
48
 
49
  @spaces.GPU
50
  def generate(pipes, enemies, blocks, elevation, temperature = 2.0, level_size = 1399, prompt = ""):
@@ -63,7 +67,7 @@ def generate(pipes, enemies, blocks, elevation, temperature = 2.0, level_size =
63
  img = convert_level_to_png(generated_level.squeeze(), TILE_DIR, mario_lm.tokenizer)[0]
64
 
65
  gradio_html = f'''<div>
66
- <iframe width=512 height=512 style="margin: 0 auto" src="static/{filename}"></iframe>
67
  <p style="text-align:center">Press the arrow keys to move. Press <code>a</code> to run, <code>s</code> to jump and <code>d</code> to shoot fireflowers</p>
68
  </div>'''
69
  return [img, gradio_html]
 
12
 
13
  import os
14
  import uvicorn
15
+ from pathlib import Path
16
 
17
  mario_lm = MarioLM()
18
  device = torch.device('cuda')
19
  mario_lm = mario_lm.to(device)
20
  TILE_DIR = "data/tiles"
21
 
22
+ gr.set_static_paths(paths=[Path("static").absolute()])
23
+
24
  app = FastAPI()
25
 
26
  def make_html_file(generated_level):
27
  level_text = f"""{'''
28
  '''.join(view_level(generated_level,mario_lm.tokenizer))}"""
29
  unique_id = uuid.uuid1()
30
+ html_filename = f"demo-{unique_id}.html"
31
+ with open(Path("static") / html_filename, 'w', encoding='utf-8') as f:
32
  f.write(f'''<!DOCTYPE html>
33
  <html lang="en">
34
 
 
45
  cheerpjAddStringFile("/str/mylevel.txt", `{level_text}`);
46
  }});
47
  cheerpjCreateDisplay(512, 500);
48
+ cheerpjRunJar("/gradio_api/file=static/mario.jar");
49
  </script>
50
  </html>''')
51
+ return html_filename # Return just the filename
52
 
53
  @spaces.GPU
54
  def generate(pipes, enemies, blocks, elevation, temperature = 2.0, level_size = 1399, prompt = ""):
 
67
  img = convert_level_to_png(generated_level.squeeze(), TILE_DIR, mario_lm.tokenizer)[0]
68
 
69
  gradio_html = f'''<div>
70
+ <iframe width=512 height=512 style="margin: 0 auto" src="/gradio_api/file=static/{filename}"></iframe>
71
  <p style="text-align:center">Press the arrow keys to move. Press <code>a</code> to run, <code>s</code> to jump and <code>d</code> to shoot fireflowers</p>
72
  </div>'''
73
  return [img, gradio_html]