multimodalart HF Staff commited on
Commit
db4b7db
·
verified ·
1 Parent(s): c801583

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +14 -10
app.py CHANGED
@@ -28,6 +28,10 @@ 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
  html_content = f'''<!DOCTYPE html>
32
  <html lang="en">
33
  <head>
@@ -42,25 +46,25 @@ def make_html_file(generated_level):
42
  async function runGame() {{
43
  const statusElement = document.getElementById("loading-status");
44
  try {{
45
- statusElement.textContent = "Downloading game assets...";
46
- const response = await fetch("/gradio_api/file=static/mario.jar");
47
- if (!response.ok) throw new Error(`HTTP error loading JAR: ${{response.status}}`);
48
- const jarData = await response.arrayBuffer();
49
-
50
  statusElement.textContent = "Initializing Java runtime...";
51
  await cheerpjInit();
52
 
53
- statusElement.textContent = "Setting up virtual filesystem...";
 
 
54
  cheerpjAddStringFile("/str/mylevel.txt", `{level_text}`);
55
-
56
- cheerpjAddStringFile("/str/mario.jar", new Uint8Array(jarData));
57
 
58
- console.log("Virtual filesystem is ready.");
59
 
 
60
  statusElement.textContent = "Starting game...";
61
  document.body.innerHTML = '';
62
  cheerpjCreateDisplay(512, 500);
63
- cheerpjRunJar("/str/mario.jar");
 
 
 
64
 
65
  }} catch (error) {{
66
  console.error("Failed to load the Mario game:", error);
 
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>
 
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);