Spaces:
Sleeping
Sleeping
Fix web playback (faststart) and add result download
Browse files- SPACE_DEPLOY.md +6 -4
- __pycache__/app.cpython-311.pyc +0 -0
- __pycache__/pipeline.cpython-311.pyc +0 -0
- app.py +43 -17
- pipeline.py +41 -6
SPACE_DEPLOY.md
CHANGED
|
@@ -77,14 +77,16 @@ git subtree push --prefix=hf_space https://huggingface.co/spaces/<user>/Soundsta
|
|
| 77 |
|
| 78 |
(Requires HF git credentials / token with write access to the Space.)
|
| 79 |
|
| 80 |
-
### Option C —
|
|
|
|
|
|
|
| 81 |
|
| 82 |
```powershell
|
| 83 |
-
#
|
| 84 |
-
|
| 85 |
```
|
| 86 |
|
| 87 |
-
|
| 88 |
|
| 89 |
## 5. First build
|
| 90 |
|
|
|
|
| 77 |
|
| 78 |
(Requires HF git credentials / token with write access to the Space.)
|
| 79 |
|
| 80 |
+
### Option C — Python upload (recommended on free accounts)
|
| 81 |
+
|
| 82 |
+
`huggingface-cli upload` always calls `create_repo` first. On free accounts that API returns **402** even when the Space **already exists**. Bypass it:
|
| 83 |
|
| 84 |
```powershell
|
| 85 |
+
# From Foley-Omni-API root — Space must already exist (web UI + ZeroGPU)
|
| 86 |
+
py -3.11 -c "from pathlib import Path; from huggingface_hub import HfApi; print(HfApi().upload_folder(folder_path=str(Path('hf_space').resolve()), repo_id='mamathew/Soundstage', repo_type='space', commit_message='Update Soundstage Space'))"
|
| 87 |
```
|
| 88 |
|
| 89 |
+
Replace `mamathew/Soundstage` with your Space id.
|
| 90 |
|
| 91 |
## 5. First build
|
| 92 |
|
__pycache__/app.cpython-311.pyc
ADDED
|
Binary file (12.6 kB). View file
|
|
|
__pycache__/pipeline.cpython-311.pyc
ADDED
|
Binary file (22 kB). View file
|
|
|
app.py
CHANGED
|
@@ -16,7 +16,7 @@ try:
|
|
| 16 |
except ImportError:
|
| 17 |
spaces = None # type: ignore
|
| 18 |
|
| 19 |
-
from pipeline import SAMPLE_STEPS_DEFAULT, generate, warmup
|
| 20 |
|
| 21 |
HERE = Path(__file__).resolve().parent
|
| 22 |
THEME_CSS = (HERE / "theme.css").read_text(encoding="utf-8")
|
|
@@ -26,6 +26,14 @@ ADMIN_TOKEN = (os.getenv("ADMIN_TOKEN") or "").strip()
|
|
| 26 |
MAX_VIDEO_SECONDS = float(os.getenv("MAX_VIDEO_SECONDS", "10"))
|
| 27 |
|
| 28 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 29 |
def _check_token(token: str) -> tuple[bool, bool]:
|
| 30 |
"""Return (ok, is_admin)."""
|
| 31 |
t = (token or "").strip()
|
|
@@ -35,7 +43,6 @@ def _check_token(token: str) -> tuple[bool, bool]:
|
|
| 35 |
return True, False
|
| 36 |
if ADMIN_TOKEN and secrets.compare_digest(t, ADMIN_TOKEN):
|
| 37 |
return True, True
|
| 38 |
-
# Either secret missing: refuse rather than open the Space
|
| 39 |
if not ACCESS_TOKEN and not ADMIN_TOKEN:
|
| 40 |
return False, False
|
| 41 |
return False, False
|
|
@@ -49,12 +56,12 @@ def enter_studio(token: str):
|
|
| 49 |
else:
|
| 50 |
msg = "Invalid access token."
|
| 51 |
return (
|
| 52 |
-
gr.update(visible=True),
|
| 53 |
-
gr.update(visible=False),
|
| 54 |
msg,
|
| 55 |
-
False,
|
| 56 |
-
False,
|
| 57 |
-
gr.update(visible=False),
|
| 58 |
"Private preview",
|
| 59 |
)
|
| 60 |
role = "Admin" if is_admin else "Guest"
|
|
@@ -78,11 +85,23 @@ def sign_out():
|
|
| 78 |
False,
|
| 79 |
gr.update(visible=False),
|
| 80 |
"Private preview",
|
| 81 |
-
None, #
|
| 82 |
-
|
|
|
|
| 83 |
)
|
| 84 |
|
| 85 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 86 |
def run_generate(
|
| 87 |
authed: bool,
|
| 88 |
video,
|
|
@@ -98,7 +117,7 @@ def run_generate(
|
|
| 98 |
if video is None:
|
| 99 |
raise gr.Error("Please upload a video first.")
|
| 100 |
|
| 101 |
-
path =
|
| 102 |
try:
|
| 103 |
out = generate(
|
| 104 |
video_path=path,
|
|
@@ -109,7 +128,9 @@ def run_generate(
|
|
| 109 |
sample_steps=int(steps),
|
| 110 |
progress=progress,
|
| 111 |
)
|
| 112 |
-
|
|
|
|
|
|
|
| 113 |
except ValueError as e:
|
| 114 |
raise gr.Error(str(e)) from e
|
| 115 |
except Exception as e:
|
|
@@ -119,11 +140,9 @@ def run_generate(
|
|
| 119 |
def clear_queue_admin(authed: bool, is_admin: bool):
|
| 120 |
if not authed or not is_admin:
|
| 121 |
raise gr.Error("Admin token required.")
|
| 122 |
-
# Gradio's built-in queue cannot cancel others' GPU work; this is a soft ack.
|
| 123 |
return "Queue note: new jobs wait behind the current GPU job. Running work cannot be aborted on ZeroGPU."
|
| 124 |
|
| 125 |
|
| 126 |
-
# Warm model on non-ZeroGPU / after fork-safe path; on ZeroGPU load is deferred to first generate.
|
| 127 |
if not os.environ.get("SPACES_ZERO_GPU"):
|
| 128 |
try:
|
| 129 |
warmup()
|
|
@@ -154,7 +173,6 @@ with gr.Blocks(
|
|
| 154 |
is_authed = gr.State(False)
|
| 155 |
is_admin_state = gr.State(False)
|
| 156 |
|
| 157 |
-
# ----- Gate -----
|
| 158 |
with gr.Column(visible=True, elem_classes=["ss-panel"]) as gate_col:
|
| 159 |
gr.HTML(
|
| 160 |
"""
|
|
@@ -178,7 +196,6 @@ with gr.Blocks(
|
|
| 178 |
f"Clips ≤ {MAX_VIDEO_SECONDS:.0f} seconds.</p>"
|
| 179 |
)
|
| 180 |
|
| 181 |
-
# ----- Studio -----
|
| 182 |
with gr.Column(visible=False) as studio_col:
|
| 183 |
with gr.Row(elem_classes=["ss-topbar"]):
|
| 184 |
gr.HTML('<span class="ss-brand">Soundstage</span>')
|
|
@@ -192,10 +209,10 @@ with gr.Blocks(
|
|
| 192 |
<p class="ss-lede">Describe what should be heard. At least one field below is enough.</p>
|
| 193 |
"""
|
| 194 |
)
|
|
|
|
| 195 |
video_in = gr.Video(
|
| 196 |
label="Video",
|
| 197 |
sources=["upload"],
|
| 198 |
-
format="mp4",
|
| 199 |
height=280,
|
| 200 |
)
|
| 201 |
words = gr.Textbox(
|
|
@@ -229,6 +246,11 @@ with gr.Blocks(
|
|
| 229 |
)
|
| 230 |
status = gr.Markdown("")
|
| 231 |
result = gr.Video(label="Result", interactive=False)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 232 |
|
| 233 |
with gr.Column(visible=False) as admin_col:
|
| 234 |
clear_btn = gr.Button("Clear queue note", variant="secondary")
|
|
@@ -247,6 +269,9 @@ with gr.Blocks(
|
|
| 247 |
enter_btn.click(enter_studio, inputs=[token_in], outputs=gate_outputs)
|
| 248 |
token_in.submit(enter_studio, inputs=[token_in], outputs=gate_outputs)
|
| 249 |
|
|
|
|
|
|
|
|
|
|
| 250 |
signout_btn.click(
|
| 251 |
sign_out,
|
| 252 |
outputs=[
|
|
@@ -258,6 +283,7 @@ with gr.Blocks(
|
|
| 258 |
admin_col,
|
| 259 |
caption,
|
| 260 |
result,
|
|
|
|
| 261 |
status,
|
| 262 |
],
|
| 263 |
)
|
|
@@ -265,7 +291,7 @@ with gr.Blocks(
|
|
| 265 |
gen_btn.click(
|
| 266 |
run_generate,
|
| 267 |
inputs=[is_authed, video_in, words, sfx, music, seed, steps],
|
| 268 |
-
outputs=[result, status],
|
| 269 |
)
|
| 270 |
|
| 271 |
clear_btn.click(
|
|
|
|
| 16 |
except ImportError:
|
| 17 |
spaces = None # type: ignore
|
| 18 |
|
| 19 |
+
from pipeline import SAMPLE_STEPS_DEFAULT, ensure_faststart, generate, warmup
|
| 20 |
|
| 21 |
HERE = Path(__file__).resolve().parent
|
| 22 |
THEME_CSS = (HERE / "theme.css").read_text(encoding="utf-8")
|
|
|
|
| 26 |
MAX_VIDEO_SECONDS = float(os.getenv("MAX_VIDEO_SECONDS", "10"))
|
| 27 |
|
| 28 |
|
| 29 |
+
def _video_path(video) -> str | None:
|
| 30 |
+
if video is None:
|
| 31 |
+
return None
|
| 32 |
+
if isinstance(video, str):
|
| 33 |
+
return video
|
| 34 |
+
return getattr(video, "name", None) or str(video)
|
| 35 |
+
|
| 36 |
+
|
| 37 |
def _check_token(token: str) -> tuple[bool, bool]:
|
| 38 |
"""Return (ok, is_admin)."""
|
| 39 |
t = (token or "").strip()
|
|
|
|
| 43 |
return True, False
|
| 44 |
if ADMIN_TOKEN and secrets.compare_digest(t, ADMIN_TOKEN):
|
| 45 |
return True, True
|
|
|
|
| 46 |
if not ACCESS_TOKEN and not ADMIN_TOKEN:
|
| 47 |
return False, False
|
| 48 |
return False, False
|
|
|
|
| 56 |
else:
|
| 57 |
msg = "Invalid access token."
|
| 58 |
return (
|
| 59 |
+
gr.update(visible=True),
|
| 60 |
+
gr.update(visible=False),
|
| 61 |
msg,
|
| 62 |
+
False,
|
| 63 |
+
False,
|
| 64 |
+
gr.update(visible=False),
|
| 65 |
"Private preview",
|
| 66 |
)
|
| 67 |
role = "Admin" if is_admin else "Guest"
|
|
|
|
| 85 |
False,
|
| 86 |
gr.update(visible=False),
|
| 87 |
"Private preview",
|
| 88 |
+
None, # result video
|
| 89 |
+
None, # download
|
| 90 |
+
"", # status
|
| 91 |
)
|
| 92 |
|
| 93 |
|
| 94 |
+
def on_upload(video):
|
| 95 |
+
"""Remux with faststart so first Gradio play starts immediately."""
|
| 96 |
+
path = _video_path(video)
|
| 97 |
+
if not path:
|
| 98 |
+
return None
|
| 99 |
+
try:
|
| 100 |
+
return ensure_faststart(path)
|
| 101 |
+
except Exception:
|
| 102 |
+
return path
|
| 103 |
+
|
| 104 |
+
|
| 105 |
def run_generate(
|
| 106 |
authed: bool,
|
| 107 |
video,
|
|
|
|
| 117 |
if video is None:
|
| 118 |
raise gr.Error("Please upload a video first.")
|
| 119 |
|
| 120 |
+
path = _video_path(video)
|
| 121 |
try:
|
| 122 |
out = generate(
|
| 123 |
video_path=path,
|
|
|
|
| 128 |
sample_steps=int(steps),
|
| 129 |
progress=progress,
|
| 130 |
)
|
| 131 |
+
# out already has faststart from mux; ensure again if needed
|
| 132 |
+
web = ensure_faststart(out) if out else out
|
| 133 |
+
return web, web, "Done — soundtrack ready. Play below or download the MP4."
|
| 134 |
except ValueError as e:
|
| 135 |
raise gr.Error(str(e)) from e
|
| 136 |
except Exception as e:
|
|
|
|
| 140 |
def clear_queue_admin(authed: bool, is_admin: bool):
|
| 141 |
if not authed or not is_admin:
|
| 142 |
raise gr.Error("Admin token required.")
|
|
|
|
| 143 |
return "Queue note: new jobs wait behind the current GPU job. Running work cannot be aborted on ZeroGPU."
|
| 144 |
|
| 145 |
|
|
|
|
| 146 |
if not os.environ.get("SPACES_ZERO_GPU"):
|
| 147 |
try:
|
| 148 |
warmup()
|
|
|
|
| 173 |
is_authed = gr.State(False)
|
| 174 |
is_admin_state = gr.State(False)
|
| 175 |
|
|
|
|
| 176 |
with gr.Column(visible=True, elem_classes=["ss-panel"]) as gate_col:
|
| 177 |
gr.HTML(
|
| 178 |
"""
|
|
|
|
| 196 |
f"Clips ≤ {MAX_VIDEO_SECONDS:.0f} seconds.</p>"
|
| 197 |
)
|
| 198 |
|
|
|
|
| 199 |
with gr.Column(visible=False) as studio_col:
|
| 200 |
with gr.Row(elem_classes=["ss-topbar"]):
|
| 201 |
gr.HTML('<span class="ss-brand">Soundstage</span>')
|
|
|
|
| 209 |
<p class="ss-lede">Describe what should be heard. At least one field below is enough.</p>
|
| 210 |
"""
|
| 211 |
)
|
| 212 |
+
# No format= remux — we apply faststart ourselves (avoids Gradio re-encode lag)
|
| 213 |
video_in = gr.Video(
|
| 214 |
label="Video",
|
| 215 |
sources=["upload"],
|
|
|
|
| 216 |
height=280,
|
| 217 |
)
|
| 218 |
words = gr.Textbox(
|
|
|
|
| 246 |
)
|
| 247 |
status = gr.Markdown("")
|
| 248 |
result = gr.Video(label="Result", interactive=False)
|
| 249 |
+
download = gr.File(
|
| 250 |
+
label="Download result MP4",
|
| 251 |
+
interactive=False,
|
| 252 |
+
visible=True,
|
| 253 |
+
)
|
| 254 |
|
| 255 |
with gr.Column(visible=False) as admin_col:
|
| 256 |
clear_btn = gr.Button("Clear queue note", variant="secondary")
|
|
|
|
| 269 |
enter_btn.click(enter_studio, inputs=[token_in], outputs=gate_outputs)
|
| 270 |
token_in.submit(enter_studio, inputs=[token_in], outputs=gate_outputs)
|
| 271 |
|
| 272 |
+
# Only on upload — not .change (would re-trigger after we write the remuxed path)
|
| 273 |
+
video_in.upload(on_upload, inputs=[video_in], outputs=[video_in])
|
| 274 |
+
|
| 275 |
signout_btn.click(
|
| 276 |
sign_out,
|
| 277 |
outputs=[
|
|
|
|
| 283 |
admin_col,
|
| 284 |
caption,
|
| 285 |
result,
|
| 286 |
+
download,
|
| 287 |
status,
|
| 288 |
],
|
| 289 |
)
|
|
|
|
| 291 |
gen_btn.click(
|
| 292 |
run_generate,
|
| 293 |
inputs=[is_authed, video_in, words, sfx, music, seed, steps],
|
| 294 |
+
outputs=[result, download, status],
|
| 295 |
)
|
| 296 |
|
| 297 |
clear_btn.click(
|
pipeline.py
CHANGED
|
@@ -244,6 +244,37 @@ def _write_wav(path: str, audio, sample_rate: int) -> None:
|
|
| 244 |
w.writeframes(pcm.tobytes())
|
| 245 |
|
| 246 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 247 |
def _mux(video_path: str, wav_path: str, out_mp4: str) -> None:
|
| 248 |
r = subprocess.run(
|
| 249 |
[
|
|
@@ -262,6 +293,8 @@ def _mux(video_path: str, wav_path: str, out_mp4: str) -> None:
|
|
| 262 |
"-c:a",
|
| 263 |
"aac",
|
| 264 |
"-shortest",
|
|
|
|
|
|
|
| 265 |
out_mp4,
|
| 266 |
],
|
| 267 |
capture_output=True,
|
|
@@ -274,12 +307,14 @@ def _mux(video_path: str, wav_path: str, out_mp4: str) -> None:
|
|
| 274 |
def _cleanup_old_results(max_age_s: int = 3600) -> None:
|
| 275 |
import time
|
| 276 |
|
| 277 |
-
|
| 278 |
-
|
| 279 |
-
|
| 280 |
-
|
| 281 |
-
|
| 282 |
-
|
|
|
|
|
|
|
| 283 |
|
| 284 |
|
| 285 |
def generate(
|
|
|
|
| 244 |
w.writeframes(pcm.tobytes())
|
| 245 |
|
| 246 |
|
| 247 |
+
def ensure_faststart(video_path: str) -> str:
|
| 248 |
+
"""Remux MP4 with moov at the front so Gradio/browser can start playback immediately."""
|
| 249 |
+
if not video_path:
|
| 250 |
+
return video_path
|
| 251 |
+
fd, out = tempfile.mkstemp(suffix=".mp4", prefix="soundstage_web_")
|
| 252 |
+
os.close(fd)
|
| 253 |
+
r = subprocess.run(
|
| 254 |
+
[
|
| 255 |
+
"ffmpeg",
|
| 256 |
+
"-y",
|
| 257 |
+
"-i",
|
| 258 |
+
video_path,
|
| 259 |
+
"-c",
|
| 260 |
+
"copy",
|
| 261 |
+
"-movflags",
|
| 262 |
+
"+faststart",
|
| 263 |
+
out,
|
| 264 |
+
],
|
| 265 |
+
capture_output=True,
|
| 266 |
+
text=True,
|
| 267 |
+
)
|
| 268 |
+
if r.returncode != 0:
|
| 269 |
+
# Fall back to original if remux fails (codec/container oddities)
|
| 270 |
+
try:
|
| 271 |
+
os.remove(out)
|
| 272 |
+
except OSError:
|
| 273 |
+
pass
|
| 274 |
+
return video_path
|
| 275 |
+
return out
|
| 276 |
+
|
| 277 |
+
|
| 278 |
def _mux(video_path: str, wav_path: str, out_mp4: str) -> None:
|
| 279 |
r = subprocess.run(
|
| 280 |
[
|
|
|
|
| 293 |
"-c:a",
|
| 294 |
"aac",
|
| 295 |
"-shortest",
|
| 296 |
+
"-movflags",
|
| 297 |
+
"+faststart",
|
| 298 |
out_mp4,
|
| 299 |
],
|
| 300 |
capture_output=True,
|
|
|
|
| 307 |
def _cleanup_old_results(max_age_s: int = 3600) -> None:
|
| 308 |
import time
|
| 309 |
|
| 310 |
+
patterns = ("soundstage_result_*.mp4", "soundstage_web_*.mp4")
|
| 311 |
+
for pat in patterns:
|
| 312 |
+
for f in glob.glob(os.path.join(tempfile.gettempdir(), pat)):
|
| 313 |
+
try:
|
| 314 |
+
if time.time() - os.path.getmtime(f) > max_age_s:
|
| 315 |
+
os.remove(f)
|
| 316 |
+
except OSError:
|
| 317 |
+
pass
|
| 318 |
|
| 319 |
|
| 320 |
def generate(
|