Spaces:
Sleeping
Sleeping
Isolate CPU numerical service from ZeroGPU CUDA emulation
Browse files
app.py
CHANGED
|
@@ -6,6 +6,9 @@ import os
|
|
| 6 |
import subprocess
|
| 7 |
import sys
|
| 8 |
import tarfile
|
|
|
|
|
|
|
|
|
|
| 9 |
|
| 10 |
os.environ["INTERVAL_PUBLIC_DEMO"] = "1"
|
| 11 |
os.environ["INTERVAL_RUNTIME"] = "/tmp/interval-runtime"
|
|
@@ -22,10 +25,36 @@ sys.path.insert(0, str(source))
|
|
| 22 |
if not (source / "models" / "base" / "config.json").exists():
|
| 23 |
subprocess.run([sys.executable, str(source / "scripts" / "download_model.py")], check=True)
|
| 24 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 25 |
import gradio as gr
|
| 26 |
import spaces
|
| 27 |
-
|
| 28 |
-
from
|
|
|
|
| 29 |
from interval.planner import load_model, score_candidates
|
| 30 |
|
| 31 |
# ZeroGPU intercepts this placement, then packs weights before serving requests.
|
|
@@ -58,7 +87,33 @@ with gr.Blocks() as diagnostics:
|
|
| 58 |
|
| 59 |
diagnostics.queue()
|
| 60 |
app = gr.mount_gradio_app(FastAPI(), diagnostics, path="/runtime-status")
|
| 61 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 62 |
|
| 63 |
if __name__ == "__main__":
|
| 64 |
import uvicorn
|
|
|
|
| 6 |
import subprocess
|
| 7 |
import sys
|
| 8 |
import tarfile
|
| 9 |
+
import atexit
|
| 10 |
+
import time
|
| 11 |
+
import urllib.request
|
| 12 |
|
| 13 |
os.environ["INTERVAL_PUBLIC_DEMO"] = "1"
|
| 14 |
os.environ["INTERVAL_RUNTIME"] = "/tmp/interval-runtime"
|
|
|
|
| 25 |
if not (source / "models" / "base" / "config.json").exists():
|
| 26 |
subprocess.run([sys.executable, str(source / "scripts" / "download_model.py")], check=True)
|
| 27 |
|
| 28 |
+
# Keep CPU optimization outside ZeroGPU's process-wide CUDA emulation.
|
| 29 |
+
# This is the unmodified local server, bound to a private loopback port.
|
| 30 |
+
child_env = dict(os.environ)
|
| 31 |
+
child_env["PYTHONPATH"] = str(source) + os.pathsep + child_env.get("PYTHONPATH", "")
|
| 32 |
+
cpu_backend = subprocess.Popen([sys.executable, "-m", "interval.cli", "serve", "--port", "8765"],
|
| 33 |
+
cwd=source, env=child_env)
|
| 34 |
+
atexit.register(cpu_backend.terminate)
|
| 35 |
+
|
| 36 |
+
|
| 37 |
+
def status():
|
| 38 |
+
with urllib.request.urlopen("http://127.0.0.1:8765/api/status", timeout=5) as response:
|
| 39 |
+
return json.load(response)
|
| 40 |
+
|
| 41 |
+
|
| 42 |
+
for attempt in range(120):
|
| 43 |
+
if cpu_backend.poll() is not None:
|
| 44 |
+
raise RuntimeError("The isolated CPU research backend exited during startup")
|
| 45 |
+
try:
|
| 46 |
+
status()
|
| 47 |
+
break
|
| 48 |
+
except (OSError, ValueError):
|
| 49 |
+
time.sleep(.25)
|
| 50 |
+
else:
|
| 51 |
+
raise RuntimeError("The isolated CPU research backend did not become ready")
|
| 52 |
+
|
| 53 |
import gradio as gr
|
| 54 |
import spaces
|
| 55 |
+
import httpx
|
| 56 |
+
from fastapi import FastAPI, Request
|
| 57 |
+
from starlette.responses import JSONResponse, Response
|
| 58 |
from interval.planner import load_model, score_candidates
|
| 59 |
|
| 60 |
# ZeroGPU intercepts this placement, then packs weights before serving requests.
|
|
|
|
| 87 |
|
| 88 |
diagnostics.queue()
|
| 89 |
app = gr.mount_gradio_app(FastAPI(), diagnostics, path="/runtime-status")
|
| 90 |
+
|
| 91 |
+
|
| 92 |
+
class ResearchProxy:
|
| 93 |
+
async def __call__(self, scope, receive, send):
|
| 94 |
+
if scope["type"] != "http":
|
| 95 |
+
return
|
| 96 |
+
request = Request(scope, receive)
|
| 97 |
+
body = bytearray()
|
| 98 |
+
async for chunk in request.stream():
|
| 99 |
+
body.extend(chunk)
|
| 100 |
+
if len(body) > 16384:
|
| 101 |
+
await JSONResponse({"detail": "Request body exceeds its budget"}, status_code=413)(scope, receive, send)
|
| 102 |
+
return
|
| 103 |
+
excluded = {"connection", "keep-alive", "transfer-encoding", "te", "trailer", "upgrade", "content-length", "content-encoding"}
|
| 104 |
+
headers = {k: v for k, v in request.headers.items() if k.lower() not in excluded}
|
| 105 |
+
try:
|
| 106 |
+
async with httpx.AsyncClient(timeout=120) as client:
|
| 107 |
+
result = await client.request(request.method, "http://127.0.0.1:8765" + request.url.path,
|
| 108 |
+
params=request.query_params, headers=headers, content=bytes(body))
|
| 109 |
+
response = Response(result.content, status_code=result.status_code,
|
| 110 |
+
headers={k: v for k, v in result.headers.items() if k.lower() not in excluded})
|
| 111 |
+
except httpx.RequestError:
|
| 112 |
+
response = JSONResponse({"detail": "The research backend is unavailable"}, status_code=503)
|
| 113 |
+
await response(scope, receive, send)
|
| 114 |
+
|
| 115 |
+
|
| 116 |
+
app.mount("/", ResearchProxy())
|
| 117 |
|
| 118 |
if __name__ == "__main__":
|
| 119 |
import uvicorn
|