Upload app.py
Browse files270 unmodified lines27127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231320 unmodified lines33433533633733833954 unmodified lines39439539639739839940069 unmodified lines47047147247347447519 unmodified lines495496497498499500501270 unmodified linesencode_video(frames, fps=FPS, output_path=output)def _lipsync(video_path: str, audio_path: str) -> str:from gradio_client import Client, handle_filespace_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",)candidate = result[0] if isinstance(result, (list, tuple)) else resultif 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 downloadedexcept Exception:passraise RuntimeError(f"MuseTalk returned an inaccessible output: {candidate}")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 linesduration: float,seed: int,enhance_prompt: bool = False,progress=gr.Progress(track_tqdm=True),):if not image_path:54 unmodified linesprogress(0.70, desc="Synchronizing the singing / rap to the track...")lipsynced_video = _lipsync(h3_video, audio_for_clip)progress(0.92, desc="Muxing the original song into the final MP4...")69 unmodified lineslabel="Seed",)generate_button = gr.Button("Generate music video",variant="primary",19 unmodified linesgenerate_button.click(fn=generate,inputs=[prompt, image, audio, canvas, duration, seed, enhance],outputs=[video, status, enhanced_box, enhanced_panel],api_name="generate",)270 unmodified lines27127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932020 unmodified lines34134234334434534634734854 unmodified lines40340440540640740840969 unmodified lines47948048148248348448548648748848949049149249349449549649719 unmodified lines517518519520521522523270 unmodified linesencode_video(frames, fps=FPS, output_path=output)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 resultif isinstance(candidate, dict):candidate = candidate.get("path") or candidate.get("url")if not candidate:raise RuntimeError("MuseTalk returned no output video.")candidate = str(candidate)if os.path.exists(candidate):return candidatetry:downloaded = client.download_file(candidate)if downloaded and os.path.exists(downloaded):return downloadedexcept Exception:passraise 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_filespace_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 excdef _mux_audio(video_path: str, audio_path: str) -> str:output = os.path.join(OUTPUT_DIR, f"music-video-{time.time_ns()}.mp4")20 unmodified linesduration: 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 linesprogress(0.70, desc="Synchronizing the singing / rap to the track...")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 lineslabel="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 linesgenerate_button.click(fn=generate,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