WACATW commited on
Commit
f7f1bef
·
verified ·
1 Parent(s): f246822

Upload app.py

Browse files
Files changed (1) hide show
  1. app.py +61 -0
app.py ADDED
@@ -0,0 +1,61 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ """HF Space 入口(ZeroGPU 诊断版 C2):launch 后 mount 业务并容器内自测"""
2
+ import threading
3
+ from pathlib import Path
4
+
5
+ import gradio as gr
6
+ import spaces # noqa: F401
7
+ from fastapi.responses import FileResponse
8
+ from fastapi.routing import Route
9
+
10
+ from main import app as cc_app
11
+
12
+
13
+ @spaces.GPU
14
+ def _gpu_probe():
15
+ return "ok"
16
+
17
+
18
+ with gr.Blocks() as demo:
19
+ gr.Markdown("云端相机")
20
+ _b = gr.Button("自检", visible=False)
21
+ _b.click(lambda: _gpu_probe(), outputs=None)
22
+
23
+ demo.queue()
24
+
25
+ if __name__ == "__main__":
26
+ demo.launch(server_name="0.0.0.0", quiet=True)
27
+ fa = demo.app
28
+ fa.mount("/", cc_app)
29
+
30
+ async def _index(request):
31
+ return FileResponse(Path(__file__).parent / "index.html")
32
+
33
+ fa.router.routes.insert(0, Route("/", _index, methods=["GET"]))
34
+ fa.router.routes = cc_app.router.routes + fa.router.routes
35
+ print("[CC] setup complete, port:", demo.server_port, flush=True)
36
+
37
+ def self_test():
38
+ try:
39
+ import json
40
+ import urllib.request
41
+ port = demo.server_port
42
+ req = urllib.request.Request(
43
+ "http://127.0.0.1:%d/api/login" % port,
44
+ data=json.dumps({"username": "diag1", "pin": "1234"}).encode(),
45
+ headers={"Content-Type": "application/json"}, method="POST")
46
+ r = urllib.request.urlopen(req, timeout=10)
47
+ print("[CC] internal /api/login:", r.status, r.read()[:60], flush=True)
48
+ except Exception as e:
49
+ print("[CC] internal /api/login FAILED:", repr(e)[:130], flush=True)
50
+ try:
51
+ port = demo.server_port
52
+ r2 = urllib.request.urlopen("http://127.0.0.1:%d/" % port, timeout=10)
53
+ print("[CC] internal /:", r2.status, r2.read()[:40], flush=True)
54
+ except Exception as e:
55
+ print("[CC] internal / FAILED:", repr(e)[:130], flush=True)
56
+ try:
57
+ print("[CC] internal app id:", id(demo.app), "routes:", len(demo.app.router.routes), flush=True)
58
+ except Exception as e:
59
+ print("[CC] id check FAILED:", repr(e)[:130], flush=True)
60
+
61
+ threading.Timer(10.0, self_test).start()