Upload app.py
Browse files270 unmodified lines27127227327427527627727827928028128228328428528628727427527627727827928028128228328429228528629529628728828930030129029129229329429530829629729829930030130230330430530630730830931031131231331431531631731831932020 unmodified lines34134234334434534634734854 unmodified lines40340440539740640740840969 unmodified lines47948048148248348448548648748848949049149249349449549649719 unmodified lines517518519498520521522523270 unmodified lines encode_video(frames, fps=FPS, output_path=output)
def _lipsync(video_path: str, audio_path: str) -> str: from gradio_client import Client, handle_file
space_id = os.environ.get("LIPSYNC_SPACE", "scratchyourbrain123/MuseTalk") client = Client(space_id)
# scratchyourbrain123/MuseTalk exposes: # inference(video_file, audio_file, bbox_shift=0) result = client.predict( handle_file(video_path), handle_file(audio_path), 0, api_name="/inference", )LIPSYNC_BACKENDS = { "scratchyourbrain123/MuseTalk · simple": "scratchyourbrain123/MuseTalk", "TMElyralab/MuseTalk · original": "TMElyralab/MuseTalk", "segestic/MusetalkLive · v1.5 controls": "segestic/MusetalkLive",}DEFAULT_LIPSYNC_BACKEND = "scratchyourbrain123/MuseTalk · simple"
def _extract_result(result, client): candidate = result[0] if isinstance(result, (list, tuple)) else result if isinstance(candidate, dict): candidate = candidate.get("path") or candidate.get("url")
if not candidate: raise RuntimeError("MuseTalk returned no output video.")
# gradio_client normally downloads FileData results to a local path. candidate = str(candidate) if os.path.exists(candidate): return candidate
# Some Gradio versions return a URL/path object. Try download_file if available. try: downloaded = client.download_file(candidate) if downloaded and os.path.exists(downloaded): return downloaded except Exception: pass
raise RuntimeError(f"MuseTalk returned an inaccessible output: {candidate}")
def _lipsync(video_path: str, audio_path: str, backend: str, bbox_shift: int) -> str: from gradio_client import Client, handle_file space_id = LIPSYNC_BACKENDS.get(backend, backend) client = Client(space_id) video_file = handle_file(video_path) audio_file = handle_file(audio_path) shift = int(max(-10, min(10, bbox_shift))) try: if space_id == "scratchyourbrain123/MuseTalk": result = client.predict(video_file, audio_file, shift, api_name="/inference") elif space_id == "TMElyralab/MuseTalk": result = client.predict(audio_file, video_file, shift, api_name="/inference") elif space_id == "segestic/MusetalkLive": result = client.predict(audio_file, video_file, shift, 10, "jaw", 90, 90, api_name="/inference") else: raise RuntimeError(f"Unsupported MuseTalk backend: {space_id}") return _extract_result(result, client) except Exception as exc: raise RuntimeError(f"MuseTalk backend '{space_id}' failed with bbox_shift={shift}: {exc}") from exc
def _mux_audio(video_path: str, audio_path: str) -> str: output = os.path.join(OUTPUT_DIR, f"music-video-{time.time_ns()}.mp4")20 unmodified lines duration: float, seed: int, enhance_prompt: bool = False, lipsync_backend: str = DEFAULT_LIPSYNC_BACKEND, bbox_shift: int = 0, progress=gr.Progress(track_tqdm=True),): if not image_path:54 unmodified lines
progress(0.70, desc="Synchronizing the singing / rap to the track...")
lipsynced_video = _lipsync(h3_video, audio_for_clip) lipsynced_video = _lipsync(h3_video, audio_for_clip, lipsync_backend, int(bbox_shift))
progress(0.92, desc="Muxing the original song into the final MP4...")
69 unmodified lines label="Seed", )
lipsync_backend = gr.Dropdown( choices=list(LIPSYNC_BACKENDS), value=DEFAULT_LIPSYNC_BACKEND, label="MuseTalk backend", info="Choose which Hugging Face MuseTalk Space performs the lip-sync.", )
bbox_shift = gr.Slider( minimum=-10, maximum=10, value=0, step=1, label="MuseTalk BBox Shift", info="Adjust the detected mouth/face crop.", )
generate_button = gr.Button( "Generate music video", variant="primary",19 unmodified lines
generate_button.click( fn=generate, inputs=[prompt, image, audio, canvas, duration, seed, enhance], inputs=[prompt, image, audio, canvas, duration, seed, enhance, lipsync_backend, bbox_shift], outputs=[video, status, enhanced_box, enhanced_panel], api_name="generate", )app.py+44-22