Spaces:
Sleeping

LazyHuman10 commited on
Commit
0328993
·
1 Parent(s): e191606

Launch as plain Gradio app for ZeroGPU

Browse files
Files changed (1) hide show
  1. app.py +17 -32
app.py CHANGED
@@ -1,4 +1,4 @@
1
- """FastAPI + Gradio API backend for the NPCverse hackathon app."""
2
 
3
  from __future__ import annotations
4
 
@@ -10,10 +10,7 @@ from typing import Any
10
 
11
  import gradio as gr
12
  import spaces
13
- import uvicorn
14
  from dotenv import load_dotenv
15
- from fastapi import FastAPI
16
- from fastapi.responses import HTMLResponse, JSONResponse
17
 
18
  load_dotenv()
19
 
@@ -27,28 +24,6 @@ from model_engine import ( # noqa: E402
27
  )
28
  from share_card import generate_share_card # noqa: E402
29
 
30
- api = FastAPI(title="NPCverse")
31
-
32
-
33
- @api.get("/", response_class=HTMLResponse)
34
- async def homepage() -> HTMLResponse:
35
- """Serve the NPCverse frontend HTML shell."""
36
- index_path = os.path.join(os.path.dirname(__file__), "index.html")
37
-
38
- try:
39
- with open(index_path, "r", encoding="utf-8") as index_file:
40
- return HTMLResponse(index_file.read())
41
- except FileNotFoundError:
42
- return HTMLResponse(
43
- "<!doctype html><html><body><h1>UI loading...</h1></body></html>"
44
- )
45
-
46
-
47
- @api.get("/health")
48
- async def health() -> JSONResponse:
49
- """Return a lightweight health check for deployment probes."""
50
- return JSONResponse({"status": "ok", "model": "MiniCPM-V-4.6"})
51
-
52
 
53
  @spaces.GPU
54
  def summon_npc(image_path: Any) -> dict:
@@ -118,10 +93,10 @@ def get_share_card(npc_json: str) -> dict:
118
  return {"image_b64": f"data:image/png;base64,{image_b64}"}
119
 
120
 
121
- def build_gradio_api() -> gr.Blocks:
122
- """Create hidden Gradio endpoints consumed by @gradio/client."""
123
- with gr.Blocks(title="NPCverse API") as demo:
124
- gr.Markdown("NPCverse API is mounted for the custom frontend.")
125
 
126
  with gr.Group(visible=False):
127
  image_path = gr.File(label="image_path", type="filepath")
@@ -168,6 +143,16 @@ def build_gradio_api() -> gr.Blocks:
168
  return demo
169
 
170
 
 
 
 
 
 
 
 
 
 
 
171
  def _extract_file_path(file_value: Any) -> str:
172
  """Extract an uploaded file path from Gradio's possible file payloads."""
173
  if isinstance(file_value, str):
@@ -214,8 +199,8 @@ def _json_safe_dict(payload: dict) -> dict:
214
  return json.loads(json.dumps(payload, ensure_ascii=False))
215
 
216
 
217
- app = gr.mount_gradio_app(api, build_gradio_api(), path="/gradio")
218
 
219
 
220
  if __name__ == "__main__":
221
- uvicorn.run(app, host="0.0.0.0", port=7860)
 
1
+ """Gradio backend for the NPCverse hackathon app."""
2
 
3
  from __future__ import annotations
4
 
 
10
 
11
  import gradio as gr
12
  import spaces
 
13
  from dotenv import load_dotenv
 
 
14
 
15
  load_dotenv()
16
 
 
24
  )
25
  from share_card import generate_share_card # noqa: E402
26
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
27
 
28
  @spaces.GPU
29
  def summon_npc(image_path: Any) -> dict:
 
93
  return {"image_b64": f"data:image/png;base64,{image_b64}"}
94
 
95
 
96
+ def build_app() -> gr.Blocks:
97
+ """Create the Gradio app with custom HTML and hidden API endpoints."""
98
+ with gr.Blocks(title="NPCverse") as demo:
99
+ gr.HTML(_load_index_html())
100
 
101
  with gr.Group(visible=False):
102
  image_path = gr.File(label="image_path", type="filepath")
 
143
  return demo
144
 
145
 
146
+ def _load_index_html() -> str:
147
+ """Load the custom frontend HTML if available."""
148
+ index_path = os.path.join(os.path.dirname(__file__), "index.html")
149
+ try:
150
+ with open(index_path, "r", encoding="utf-8") as index_file:
151
+ return index_file.read()
152
+ except FileNotFoundError:
153
+ return "<main><h1>NPCverse</h1><p>UI loading...</p></main>"
154
+
155
+
156
  def _extract_file_path(file_value: Any) -> str:
157
  """Extract an uploaded file path from Gradio's possible file payloads."""
158
  if isinstance(file_value, str):
 
199
  return json.loads(json.dumps(payload, ensure_ascii=False))
200
 
201
 
202
+ demo = build_app()
203
 
204
 
205
  if __name__ == "__main__":
206
+ demo.launch(server_name="0.0.0.0", server_port=7860, show_error=True)