File size: 1,990 Bytes
02a313e
3c1f2a9
 
 
08f8861
02a313e
 
 
 
 
 
 
9c3c7f1
02a313e
 
 
 
 
 
 
 
 
 
 
 
 
 
edb1292
addbeeb
02a313e
e22a58e
edb1292
addbeeb
 
 
 
02a313e
 
 
 
 
 
 
addbeeb
02a313e
edb1292
02a313e
7108aaf
9c3c7f1
e22a58e
 
 
 
3c1f2a9
02a313e
3c1f2a9
 
02a313e
e22a58e
9c3c7f1
3c1f2a9
 
02a313e
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
# ---------- MUST BE FIRST ----------
import os
os.environ.setdefault("GRADIO_USE_CDN", "true")

import spaces
import gradio as gr
from fastapi import FastAPI
from starlette.responses import PlainTextResponse
try:
    from starlette.exceptions import ClientDisconnect  # Starlette ≥0.27
except Exception:
    from starlette.requests import ClientDisconnect  # fallback

# Log versions to container logs for sanity
try:
    print("== Sanity ==")
    print("spaces.__version__:", getattr(spaces, "__version__", "unknown"))
    import gradio
    print("gradio.__version__:", getattr(gradio, "__version__", "unknown"))
    import sys
    print("python:", sys.version)
    print("SPACE_RUNTIME:", os.getenv("SPACE_RUNTIME"))
    print("HF_SPACE_ENTRYPOINT default: app.py")
except Exception as _e:
    print("version log error:", _e)

# ---------- ZeroGPU probes (PUBLIC NAMES) ----------
@spaces.GPU(duration=10)
def gpu_probe(a: int = 1, b: int = 1) -> int:
    # Not called; existence is enough for ZeroGPU startup check
    return a + b

@spaces.GPU(duration=10)
def gpu_echo(x: str = "ok") -> str:
    return x

# ---------- Tiny Gradio UI ----------
with gr.Blocks(title="ZeroGPU Probe") as demo:
    gr.Markdown("### ✅ Minimal app is running.\n"
                "If you see this UI, import succeeded and GPU probes are registered.")
    t = gr.Textbox(label="Echo input", value="hello")
    o = gr.Textbox(label="Echo output")
    t.submit(lambda s: f"echo: {s}", t, o)

demo = demo.queue(max_size=8)

# ---------- ASGI app with fast health ----------
app = FastAPI()

@app.get("/health")
def _health():
    return {"ok": True}

@app.exception_handler(ClientDisconnect)
async def client_disconnect_handler(request, exc):
    return PlainTextResponse("Client disconnected", status_code=499)

# Mount at root (Spaces health-check expects this)
app = gr.mount_gradio_app(app, demo, path="/")

if __name__ == "__main__":
    import uvicorn
    uvicorn.run(app, host="0.0.0.0", port=7860)