ambujm22 commited on
Commit
ed317eb
·
verified ·
1 Parent(s): 3387a76

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +10 -47
app.py CHANGED
@@ -1,60 +1,23 @@
1
- # ---------- DO THIS FIRST ----------
2
  import os
3
  os.environ.setdefault("GRADIO_USE_CDN", "true")
4
 
5
- # Safe import of 'spaces' (works on CPU or ZeroGPU)
6
- try:
7
- import spaces # HF Spaces SDK
8
- except Exception:
9
- class _DummySpaces:
10
- def GPU(self, *_, **__):
11
- def _decorator(fn): return fn
12
- return _decorator
13
- spaces = _DummySpaces()
14
-
15
  import gradio as gr
16
- from fastapi import FastAPI
17
 
18
- # Log a few things so we can see them in the container logs
19
  try:
20
- import sys
21
- print("== Sanity ==")
22
- print("gradio.__version__:", getattr(gr, "__version__", "unknown"))
23
- print("SPACE_RUNTIME:", os.getenv("SPACE_RUNTIME"))
24
- print("PY:", sys.version)
25
- except Exception as e:
26
- print("sanity log error:", e)
27
-
28
- # ---------- PUBLIC GPU PROBES (harmless on CPU) ----------
29
- @spaces.GPU(duration=10)
30
- def gpu_probe(a: int = 1, b: int = 1):
31
- return a + b
32
-
33
- @spaces.GPU(duration=10)
34
- def gpu_echo(x: str = "ok"):
35
- return x
36
 
37
- # ---------- Tiny Gradio UI ----------
38
  with gr.Blocks(title="Hello Space") as demo:
39
  gr.Markdown("### ✅ App is alive\nType to echo.")
40
  inp = gr.Textbox(label="Input", value="hello")
41
  out = gr.Textbox(label="Output")
42
  inp.submit(lambda s: f"echo: {s}", inp, out)
43
 
44
- # Queue BEFORE mounting
45
- demo = demo.queue(max_size=8)
46
-
47
- # ---------- ASGI app and fast health ----------
48
- app = FastAPI()
49
-
50
- @app.get("/health")
51
- def _health():
52
- return {"ok": True}
53
-
54
- # Mount Gradio at "/" so the supervisor sees it
55
- app = gr.mount_gradio_app(app, demo, path="/")
56
-
57
- # (Do NOT call demo.launch() on Spaces)
58
- if __name__ == "__main__":
59
- import uvicorn
60
- uvicorn.run(app, host="0.0.0.0", port=7860)
 
1
+ # ---------- Gradio Space entrypoint (no FastAPI/Uvicorn) ----------
2
  import os
3
  os.environ.setdefault("GRADIO_USE_CDN", "true")
4
 
 
 
 
 
 
 
 
 
 
 
5
  import gradio as gr
 
6
 
7
+ # (Optional) ZeroGPU probe harmless on CPU. Will work once you switch HW to ZeroGPU.
8
  try:
9
+ import spaces
10
+ @spaces.GPU(duration=10)
11
+ def gpu_probe(a: int = 1, b: int = 1):
12
+ return a + b
13
+ except Exception:
14
+ pass
 
 
 
 
 
 
 
 
 
 
15
 
 
16
  with gr.Blocks(title="Hello Space") as demo:
17
  gr.Markdown("### ✅ App is alive\nType to echo.")
18
  inp = gr.Textbox(label="Input", value="hello")
19
  out = gr.Textbox(label="Output")
20
  inp.submit(lambda s: f"echo: {s}", inp, out)
21
 
22
+ # IMPORTANT: expose a top-level `demo`, and DO NOT call .launch()
23
+ demo = demo.queue(max_size=8)