multimodalart HF Staff commited on
Commit
9dd6110
·
verified ·
1 Parent(s): 3eefe14

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +15 -43
app.py CHANGED
@@ -28,58 +28,30 @@ def make_html_file(generated_level):
28
  '''.join(view_level(generated_level,mario_lm.tokenizer))}"""
29
  unique_id = uuid.uuid1()
30
  html_filename = f"demo-{unique_id}.html"
31
-
32
- # This is the final, simplified solution that respects all source code discoveries.
33
- # 1. The JAR is run from its standard "/app/" location.
34
- # 2. The level data is placed at the hardcoded "/str/mylevel.txt" path.
35
- html_content = f'''<!DOCTYPE html>
36
  <html lang="en">
 
37
  <head>
38
  <meta charset="utf-8">
39
  <title>Mario Game</title>
40
- <script src="https://cjrtnc.leaningtech.com/3.1/cj3loader.js"></script>
41
  </head>
 
42
  <body>
43
- <p id="loading-status">Loading game, please wait...</p>
44
  </body>
45
  <script>
46
- async function runGame() {{
47
- const statusElement = document.getElementById("loading-status");
48
- try {{
49
- // Step 1: Initialize the CheerpJ runtime.
50
- statusElement.textContent = "Initializing Java runtime...";
51
- await cheerpjInit();
52
-
53
- // Step 2: Add the level file to the virtual filesystem.
54
- // This is the hardcoded path from the PlayLevel.java source code.
55
- statusElement.textContent = "Loading level...";
56
- cheerpjAddStringFile("/str/mylevel.txt", `{level_text}`);
57
-
58
- console.log("Runtime ready and level loaded.");
59
-
60
- // Step 3: Run the game.
61
- statusElement.textContent = "Starting game...";
62
- document.body.innerHTML = '';
63
- cheerpjCreateDisplay(512, 500);
64
-
65
- // Let CheerpJ fetch the JAR from its standard "/app/" classpath.
66
- // The Java code will ignore arguments and load the level from /str/mylevel.txt on its own.
67
- cheerpjRunJar("/app/gradio_api/file=static/mario.jar");
68
-
69
- }} catch (error) {{
70
- console.error("Failed to load the Mario game:", error);
71
- statusElement.innerHTML = "<h1>Error</h1><p>Could not load the game. Please check the browser console for details.</p>";
72
- }}
73
- }}
74
-
75
- runGame();
76
  </script>
77
- </html>'''
78
-
79
- with open(Path("static") / html_filename, 'w', encoding='utf-8') as f:
80
- f.write(html_content)
81
-
82
- return html_filename
83
 
84
  @spaces.GPU
85
  def generate(pipes, enemies, blocks, elevation, temperature = 2.0, level_size = 1399, prompt = ""):
 
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
+
35
  <head>
36
  <meta charset="utf-8">
37
  <title>Mario Game</title>
38
+ <script src="https://cjrtnc.leaningtech.com/2.3/loader.js"></script>
39
  </head>
40
+
41
  <body>
 
42
  </body>
43
  <script>
44
+ const response = await fetch("/gradio_api/file=static/mario.jar");
45
+ const jarData = await response.arrayBuffer();
46
+ cheerpjInit().then(function () {{
47
+ cheerpjAddStringFile("/str/mylevel.txt", `{level_text}`);
48
+ await cheerpjAddStringFile("/str/mario.jar", new Uint8Array(jarData));
49
+ cheerpjCreateDisplay(512, 500);
50
+ cheerpjRunJar("/str/mario.jar");
51
+ }});
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
52
  </script>
53
+ </html>''')
54
+ return html_filename # Return just the filename
 
 
 
 
55
 
56
  @spaces.GPU
57
  def generate(pipes, enemies, blocks, elevation, temperature = 2.0, level_size = 1399, prompt = ""):