tddandroid commited on
Commit
3a74c67
·
verified ·
1 Parent(s): ccf6cda

Upload 2 files

Browse files
Files changed (2) hide show
  1. app.py +16 -61
  2. requirements.txt +1 -0
app.py CHANGED
@@ -2,31 +2,28 @@
2
 
3
  HF's free tier no longer offers Docker Spaces, so the FastAPI backend runs
4
  inside a Gradio Space instead: a minimal Gradio app is mounted onto the
5
- FastAPI app (at /gradio). The real Studio UI is served by FastAPI at / as
6
- usual.
7
-
8
- On ZeroGPU Spaces the runtime serves the mounted app itself, so this
9
- module must not bind port 7860 right away: it first gives the runtime a
10
- grace period to serve the mounted app, and only starts its own uvicorn
11
- server if nothing is serving the API and the port is free.
 
 
12
  """
13
- import socket
14
- import time
15
- import urllib.request
16
 
17
  import gradio as gr
18
- import spaces
19
  import uvicorn
20
- from server import app as fastapi_app
21
 
 
22
 
23
- @spaces.GPU
24
- def _gpu_check(prompt: str) -> str:
25
- """ZeroGPU probe: registers a GPU function in the Gradio demo.
26
 
27
- The real generation path is the FastAPI endpoint in server.py.
28
- """
29
- return f"GPU ready: {prompt}"
 
30
 
31
 
32
  with gr.Blocks(title="AllOutMV") as demo:
@@ -35,50 +32,8 @@ with gr.Blocks(title="AllOutMV") as demo:
35
  "The full Studio runs at this Space's root URL. "
36
  "This page only exists so the Space registers as a Gradio app."
37
  )
38
- _prompt = gr.Textbox(label="GPU check prompt", value="ping")
39
- _result = gr.Textbox(label="Result")
40
- _check = gr.Button("Run GPU check")
41
- _check.click(_gpu_check, inputs=_prompt, outputs=_result)
42
 
43
  app = gr.mount_gradio_app(fastapi_app, demo, path="/gradio")
44
 
45
-
46
- def _api_up():
47
- try:
48
- with urllib.request.urlopen("http://127.0.0.1:7860/api/health", timeout=3) as resp:
49
- return resp.status == 200
50
- except Exception:
51
- return False
52
-
53
-
54
- def _port_free(port):
55
- with socket.socket(socket.AF_INET, socket.SOCK_STREAM) as sock:
56
- try:
57
- sock.bind(("0.0.0.0", port))
58
- return True
59
- except OSError:
60
- return False
61
-
62
-
63
  if __name__ == "__main__":
64
- # Give the ZeroGPU runtime up to 30s to serve the mounted app.
65
- deadline = time.monotonic() + 30
66
- runtime_serving = False
67
- while time.monotonic() < deadline:
68
- if _api_up():
69
- runtime_serving = True
70
- break
71
- time.sleep(2)
72
-
73
- if runtime_serving:
74
- print("[alloutmv] runtime is serving the API; staying alive", flush=True)
75
- while True:
76
- time.sleep(3600)
77
-
78
- if _port_free(7860):
79
- print("[alloutmv] starting uvicorn on 0.0.0.0:7860", flush=True)
80
- uvicorn.run(app, host="0.0.0.0", port=7860)
81
- else:
82
- print("[alloutmv] port 7860 is held by another server; waiting", flush=True)
83
- while True:
84
- time.sleep(3600)
 
2
 
3
  HF's free tier no longer offers Docker Spaces, so the FastAPI backend runs
4
  inside a Gradio Space instead: a minimal Gradio app is mounted onto the
5
+ FastAPI app (at /gradio) and uvicorn serves everything on port 7860.
6
+ The real Studio UI is served by FastAPI at / as usual.
7
+
8
+ ZeroGPU note: a ZeroGPU Space refuses to start unless at least one function
9
+ is decorated with @spaces.GPU ("No @spaces.GPU function detected during
10
+ startup"). AllOutMV itself runs on CPU (transcribe/render) and uses fal.ai
11
+ cloud GPUs for clips, so it needs no Space GPU — _zerogpu_probe below is
12
+ never called and exists only to satisfy that startup check. Per HF's own
13
+ ZeroGPU docs, @spaces.GPU is effect-free outside a ZeroGPU environment.
14
  """
15
+ import spaces
 
 
16
 
17
  import gradio as gr
 
18
  import uvicorn
 
19
 
20
+ from server import app as fastapi_app
21
 
 
 
 
22
 
23
+ @spaces.GPU(duration=120)
24
+ def _zerogpu_probe():
25
+ """Never called. Satisfies ZeroGPU's startup check for a @spaces.GPU function."""
26
+ return None
27
 
28
 
29
  with gr.Blocks(title="AllOutMV") as demo:
 
32
  "The full Studio runs at this Space's root URL. "
33
  "This page only exists so the Space registers as a Gradio app."
34
  )
 
 
 
 
35
 
36
  app = gr.mount_gradio_app(fastapi_app, demo, path="/gradio")
37
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
38
  if __name__ == "__main__":
39
+ uvicorn.run(app, host="0.0.0.0", port=7860)
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
requirements.txt CHANGED
@@ -6,3 +6,4 @@ av==14.2.0 # pinned: faster-whisper 1.2.x passes metadata_errors= to av.open, r
6
  # torch is installed with your Wan2.2 / lip-sync model — server works CPU-only without it
7
 
8
  gradio>=5.0
 
 
6
  # torch is installed with your Wan2.2 / lip-sync model — server works CPU-only without it
7
 
8
  gradio>=5.0
9
+ spaces # ZeroGPU runtime: provides @spaces.GPU; required at startup even though AllOutMV runs on CPU