broadfield-dev commited on
Commit
8f8ea72
·
verified ·
1 Parent(s): 4ef3631

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +45 -26
app.py CHANGED
@@ -1,19 +1,8 @@
1
- from fastapi import FastAPI
2
- from fastapi.middleware.cors import CORSMiddleware
3
  import numpy as np
4
  import random
5
  from typing import Dict, List
6
- import uvicorn
7
-
8
- app = FastAPI()
9
-
10
- # Enable CORS for browser access
11
- app.add_middleware(
12
- CORSMiddleware,
13
- allow_origins=["*"],
14
- allow_methods=["*"],
15
- allow_headers=["*"],
16
- )
17
 
18
  # Mock InstantMesh (replace with real model later)
19
  def generate_mesh(chunk_x: int, chunk_z: int) -> List[List[float]]:
@@ -33,7 +22,7 @@ def generate_mesh(chunk_x: int, chunk_z: int) -> List[List[float]]:
33
  # Game state
34
  class Game:
35
  def __init__(self):
36
- self.player_pos = [0, 0] # [x, z]
37
  self.world: Dict[tuple, List[List[float]]] = {}
38
  self.chunk_size = 5
39
 
@@ -47,19 +36,49 @@ class Game:
47
  self.world[key] = generate_mesh(chunk_x, chunk_z)
48
  return self.world[key]
49
 
 
 
 
 
 
 
 
 
 
 
 
50
  game = Game()
51
 
52
- @app.get("/move")
53
- async def move(dx: int = 0, dz: int = 0):
54
- game.player_pos[0] += dx
55
- game.player_pos[1] += dz
56
- chunk_x, chunk_z = game.get_chunk_coords(game.player_pos[0], game.player_pos[1])
57
- chunk = game.generate_chunk(chunk_x, chunk_z)
58
- return {
59
- "player_pos": game.player_pos,
60
- "chunk": chunk,
61
- "chunk_coords": [chunk_x, chunk_z]
62
- }
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
63
 
 
64
  if __name__ == "__main__":
65
- uvicorn.run(app, host="0.0.0.0", port=7860)
 
1
+ import gradio as gr
 
2
  import numpy as np
3
  import random
4
  from typing import Dict, List
5
+ import json
 
 
 
 
 
 
 
 
 
 
6
 
7
  # Mock InstantMesh (replace with real model later)
8
  def generate_mesh(chunk_x: int, chunk_z: int) -> List[List[float]]:
 
22
  # Game state
23
  class Game:
24
  def __init__(self):
25
+ self.player_pos = [0, 0]
26
  self.world: Dict[tuple, List[List[float]]] = {}
27
  self.chunk_size = 5
28
 
 
36
  self.world[key] = generate_mesh(chunk_x, chunk_z)
37
  return self.world[key]
38
 
39
+ def move(self, dx: int, dz: int):
40
+ self.player_pos[0] += dx
41
+ self.player_pos[1] += dz
42
+ chunk_x, chunk_z = self.get_chunk_coords(self.player_pos[0], self.player_pos[1])
43
+ chunk = self.generate_chunk(chunk_x, chunk_z)
44
+ return {
45
+ "player_pos": self.player_pos,
46
+ "chunk": chunk,
47
+ "chunk_coords": [chunk_x, chunk_z]
48
+ }
49
+
50
  game = Game()
51
 
52
+ # Gradio interface with custom endpoint simulation
53
+ with gr.Blocks() as app:
54
+ state = gr.State(value=game)
55
+
56
+ def move_player(dx, dz, game_state):
57
+ result = game_state.move(dx, dz)
58
+ return game_state, json.dumps(result)
59
+
60
+ # Hidden button to simulate API call
61
+ move_btn = gr.Button("Move", visible=False)
62
+ output = gr.JSON(label="Game State")
63
+
64
+ # Trigger move on input
65
+ dx_input = gr.Number(label="DX", value=0, visible=False)
66
+ dz_input = gr.Number(label="DZ", value=0, visible=False)
67
+ move_btn.click(
68
+ fn=move_player,
69
+ inputs=[dx_input, dz_input, state],
70
+ outputs=[state, output]
71
+ )
72
+
73
+ # Custom route for Three.js to fetch from
74
+ def get_game_state(dx: int = 0, dz: int = 0):
75
+ result = game.move(dx, dz)
76
+ return result
77
+
78
+ # Expose a pseudo-API endpoint (not standard Gradio, but works with Spaces)
79
+ app.queue()
80
+ app.launch(share=True) # Remove share=True when deploying to Spaces
81
 
82
+ # For Hugging Face Spaces, ensure this is the entry point
83
  if __name__ == "__main__":
84
+ app.launch(server_port=7860)