ambujm22 commited on
Commit
dde1ff3
·
verified ·
1 Parent(s): c4fc17a

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +13 -46
app.py CHANGED
@@ -1,63 +1,30 @@
1
- # ---------- MUST BE FIRST ----------
2
  import os
3
  os.environ.setdefault("GRADIO_USE_CDN", "true")
4
 
5
  import spaces
6
  import gradio as gr
7
  from fastapi import FastAPI
8
- from starlette.responses import PlainTextResponse
9
- try:
10
- from starlette.exceptions import ClientDisconnect # Starlette ≥0.27
11
- except Exception:
12
- from starlette.requests import ClientDisconnect # fallback
13
-
14
- # Log versions to container logs for sanity
15
- try:
16
- print("== Sanity ==")
17
- print("spaces.__version__:", getattr(spaces, "__version__", "unknown"))
18
- import gradio
19
- print("gradio.__version__:", getattr(gradio, "__version__", "unknown"))
20
- import sys
21
- print("python:", sys.version)
22
- print("SPACE_RUNTIME:", os.getenv("SPACE_RUNTIME"))
23
- print("HF_SPACE_ENTRYPOINT default: app.py")
24
- except Exception as _e:
25
- print("version log error:", _e)
26
-
27
- # ---------- ZeroGPU probes (PUBLIC NAMES) ----------
28
  @spaces.GPU(duration=10)
29
- def gpu_probe(a: int = 1, b: int = 1) -> int:
30
- # Not called; existence is enough for ZeroGPU startup check
31
- return a + b
32
 
33
  @spaces.GPU(duration=10)
34
- def gpu_echo(x: str = "ok") -> str:
35
- return x
36
 
37
- # ---------- Tiny Gradio UI ----------
38
- with gr.Blocks(title="ZeroGPU Probe") as demo:
39
- gr.Markdown("### ✅ Minimal app is running.\n"
40
- "If you see this UI, import succeeded and GPU probes are registered.")
41
- t = gr.Textbox(label="Echo input", value="hello")
42
- o = gr.Textbox(label="Echo output")
43
- t.submit(lambda s: f"echo: {s}", t, o)
44
 
45
- demo = demo.queue(max_size=8)
46
 
47
- # ---------- ASGI app with fast health ----------
48
  app = FastAPI()
49
 
50
  @app.get("/health")
51
- def _health():
52
- return {"ok": True}
53
-
54
- @app.exception_handler(ClientDisconnect)
55
- async def client_disconnect_handler(request, exc):
56
- return PlainTextResponse("Client disconnected", status_code=499)
57
 
58
- # Mount at root (Spaces health-check expects this)
59
  app = gr.mount_gradio_app(app, demo, path="/")
60
-
61
- if __name__ == "__main__":
62
- import uvicorn
63
- uvicorn.run(app, host="0.0.0.0", port=7860)
 
 
1
  import os
2
  os.environ.setdefault("GRADIO_USE_CDN", "true")
3
 
4
  import spaces
5
  import gradio as gr
6
  from fastapi import FastAPI
7
+
8
+ # Public GPU probes
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
9
  @spaces.GPU(duration=10)
10
+ def gpu_probe(a: int = 1, b: int = 1): return a + b
 
 
11
 
12
  @spaces.GPU(duration=10)
13
+ def gpu_echo(x: str = "ok"): return x
 
14
 
15
+ # Simple UI
16
+ with gr.Blocks() as demo:
17
+ gr.Markdown("### ✅ Minimal Space is running")
18
+ inp = gr.Textbox(label="Say something")
19
+ out = gr.Textbox(label="Echo")
20
+ inp.submit(lambda s: f"echo: {s}", inp, out)
 
21
 
22
+ demo = demo.queue()
23
 
24
+ # FastAPI wrapper with health
25
  app = FastAPI()
26
 
27
  @app.get("/health")
28
+ def _health(): return {"ok": True}
 
 
 
 
 
29
 
 
30
  app = gr.mount_gradio_app(app, demo, path="/")