Spaces:
Sleeping
Sleeping
Upload app.py
Browse files
app.py
ADDED
|
@@ -0,0 +1,53 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
"""Hugging Face Space 入口(SDK: Gradio + ZeroGPU,业务 FastAPI 挂载于 Gradio 的 ASGI 应用)
|
| 2 |
+
|
| 3 |
+
ZeroGPU 平台强制要求:Space 启动时检测到 @spaces.GPU 装饰函数且调用
|
| 4 |
+
demo.launch()。本文件按此要求提供占位 GPU 函数与 Gradio demo;真正
|
| 5 |
+
的业务(前端页面 + /api/*)由 FastAPI 应用 main.app 提供,挂载在
|
| 6 |
+
Gradio 的底层 ASGI 应用(demo.app)上,并将根路径 / 指回云相机页面。
|
| 7 |
+
|
| 8 |
+
_gpu_probe 仅为满足平台检测,业务从不调用,不消耗 GPU 免费配额。
|
| 9 |
+
"""
|
| 10 |
+
import os
|
| 11 |
+
from pathlib import Path
|
| 12 |
+
|
| 13 |
+
import gradio as gr
|
| 14 |
+
import spaces # noqa: F401 (ZeroGPU 镜像自带)
|
| 15 |
+
from starlette.responses import FileResponse
|
| 16 |
+
from starlette.routing import Route
|
| 17 |
+
|
| 18 |
+
from main import app as cc_app
|
| 19 |
+
|
| 20 |
+
INDEX = Path(__file__).parent / "index.html"
|
| 21 |
+
|
| 22 |
+
|
| 23 |
+
print("[CC] module loaded", flush=True)
|
| 24 |
+
|
| 25 |
+
|
| 26 |
+
@spaces.GPU
|
| 27 |
+
def _gpu_probe(): # ZeroGPU 平台检测占位函数(业务不调用)
|
| 28 |
+
return "ok"
|
| 29 |
+
|
| 30 |
+
|
| 31 |
+
with gr.Blocks() as demo:
|
| 32 |
+
gr.Markdown("云端相机")
|
| 33 |
+
|
| 34 |
+
demo.queue()
|
| 35 |
+
|
| 36 |
+
|
| 37 |
+
if __name__ == "__main__":
|
| 38 |
+
print("[CC] main block executing", flush=True)
|
| 39 |
+
demo.launch(prevent_thread_lock=True, quiet=True)
|
| 40 |
+
fa = demo.app
|
| 41 |
+
print("[CC] demo.app type:", type(fa).__name__, flush=True)
|
| 42 |
+
fa.mount("/", cc_app)
|
| 43 |
+
print("[CC] mounted cc_app", flush=True)
|
| 44 |
+
|
| 45 |
+
async def _index(request):
|
| 46 |
+
return FileResponse(INDEX)
|
| 47 |
+
|
| 48 |
+
fa.router.routes.insert(0, Route("/", _index, methods=["GET"]))
|
| 49 |
+
print("[CC] index route inserted", flush=True)
|
| 50 |
+
|
| 51 |
+
# 保持主线程存活,防止进程退出导致服务关闭
|
| 52 |
+
import threading
|
| 53 |
+
threading.Event().wait()
|