Spaces:
Running
on
Zero
Running
on
Zero
Update app.py
Browse files
app.py
CHANGED
|
@@ -1,30 +1,60 @@
|
|
|
|
|
| 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 |
-
#
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 9 |
@spaces.GPU(duration=10)
|
| 10 |
-
def gpu_probe(a: int = 1, b: int = 1):
|
|
|
|
| 11 |
|
| 12 |
@spaces.GPU(duration=10)
|
| 13 |
-
def gpu_echo(x: str = "ok"):
|
| 14 |
-
|
| 15 |
-
|
| 16 |
-
|
| 17 |
-
|
| 18 |
-
|
| 19 |
-
|
|
|
|
| 20 |
inp.submit(lambda s: f"echo: {s}", inp, out)
|
| 21 |
|
| 22 |
-
|
|
|
|
| 23 |
|
| 24 |
-
#
|
| 25 |
app = FastAPI()
|
| 26 |
|
| 27 |
@app.get("/health")
|
| 28 |
-
def _health():
|
|
|
|
| 29 |
|
|
|
|
| 30 |
app = gr.mount_gradio_app(app, demo, path="/")
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 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)
|