Spaces:
Running
on
Zero
Running
on
Zero
Update app.py
Browse files
app.py
CHANGED
|
@@ -28,27 +28,67 @@ 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 |
-
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
|
| 39 |
</head>
|
| 40 |
|
| 41 |
<body>
|
|
|
|
| 42 |
</body>
|
| 43 |
<script>
|
| 44 |
-
|
| 45 |
-
|
| 46 |
-
|
| 47 |
-
|
| 48 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 49 |
</script>
|
| 50 |
-
</html>'''
|
| 51 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
| 52 |
|
| 53 |
@spaces.GPU
|
| 54 |
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 |
+
html_content = f'''<!DOCTYPE html>
|
|
|
|
| 32 |
<html lang="en">
|
| 33 |
|
| 34 |
<head>
|
| 35 |
<meta charset="utf-8">
|
| 36 |
<title>Mario Game</title>
|
| 37 |
+
<script src="https://cjrtnc.leaningtech.com/4.2/loader.js"></script>
|
| 38 |
</head>
|
| 39 |
|
| 40 |
<body>
|
| 41 |
+
<p>Loading game, please wait...</p>
|
| 42 |
</body>
|
| 43 |
<script>
|
| 44 |
+
async function runGame() {{
|
| 45 |
+
try {{
|
| 46 |
+
// Step 1: Pre-load the JAR to avoid server range-request issues.
|
| 47 |
+
const response = await fetch("/gradio_api/file=static/mario.jar");
|
| 48 |
+
if (!response.ok) {{
|
| 49 |
+
throw new Error(`HTTP error loading JAR: ${{response.status}}`);
|
| 50 |
+
}}
|
| 51 |
+
const jarData = await response.arrayBuffer();
|
| 52 |
+
|
| 53 |
+
// Step 2: Initialize CheerpJ.
|
| 54 |
+
await cheerpjInit();
|
| 55 |
+
|
| 56 |
+
// Step 3: Add files to the temporary /str/ mount point.
|
| 57 |
+
cheerpjAddStringFile("/str/mylevel.txt", `{level_text}`);
|
| 58 |
+
cheerpjAddStringFile("/str/mario.jar", new Uint8Array(jarData));
|
| 59 |
+
|
| 60 |
+
// Step 4: Copy the level file from /str/ to the /files/ working directory.
|
| 61 |
+
// This is the critical step to solve the "infinite load".
|
| 62 |
+
const lib = await cheerpjRunLibrary('/str/mario.jar');
|
| 63 |
+
const Files = await lib.java.nio.file.Files;
|
| 64 |
+
const Paths = await lib.java.nio.file.Paths;
|
| 65 |
+
const StandardCopyOption = await lib.java.nio.file.StandardCopyOption;
|
| 66 |
+
|
| 67 |
+
const sourcePath = await Paths.get("/str/mylevel.txt", []);
|
| 68 |
+
const destPath = await Paths.get("/files/mylevel.txt", []);
|
| 69 |
+
|
| 70 |
+
await Files.copy(sourcePath, destPath, [StandardCopyOption.REPLACE_EXISTING]);
|
| 71 |
+
console.log("Successfully copied mylevel.txt to /files/ working directory.");
|
| 72 |
+
|
| 73 |
+
// Step 5: Run the game now that its files are in place.
|
| 74 |
+
document.body.innerHTML = ''; // Clear the "Loading..." message
|
| 75 |
+
cheerpjCreateDisplay(512, 500);
|
| 76 |
+
cheerpjRunJar("/str/mario.jar");
|
| 77 |
+
|
| 78 |
+
}} catch (error) {{
|
| 79 |
+
console.error("Failed to load the Mario game:", error);
|
| 80 |
+
document.body.innerHTML = "<h1>Error</h1><p>Could not load the game. Please check the browser console for details.</p>";
|
| 81 |
+
}}
|
| 82 |
+
}}
|
| 83 |
+
|
| 84 |
+
runGame();
|
| 85 |
</script>
|
| 86 |
+
</html>'''
|
| 87 |
+
|
| 88 |
+
with open(Path("static") / html_filename, 'w', encoding='utf-8') as f:
|
| 89 |
+
f.write(html_content)
|
| 90 |
+
|
| 91 |
+
return html_filename
|
| 92 |
|
| 93 |
@spaces.GPU
|
| 94 |
def generate(pipes, enemies, blocks, elevation, temperature = 2.0, level_size = 1399, prompt = ""):
|