Spaces:
Running
Running
| import gradio as gr | |
| import subprocess, os, uuid | |
| BASE = "/app" | |
| WAV2LIP_DIR = f"{BASE}/Wav2Lip" | |
| MODEL_PATH = f"{WAV2LIP_DIR}/checkpoints/wav2lip_gan.pth" | |
| def setup(): | |
| if not os.path.exists(f"{WAV2LIP_DIR}/inference.py"): | |
| print("Cloning Wav2Lip...", flush=True) | |
| subprocess.run([ | |
| "git", "clone", | |
| "https://github.com/Rudrabha/Wav2Lip.git", | |
| WAV2LIP_DIR | |
| ], check=True, cwd=BASE) | |
| print("Clone done!", flush=True) | |
| os.makedirs(f"{WAV2LIP_DIR}/checkpoints", exist_ok=True) | |
| os.makedirs(f"{WAV2LIP_DIR}/temp", exist_ok=True) | |
| os.makedirs(f"{BASE}/temp", exist_ok=True) | |
| if not os.path.exists(MODEL_PATH): | |
| print("Downloading model...", flush=True) | |
| subprocess.run([ | |
| "wget", "-q", | |
| "https://huggingface.co/numz/wav2lip_studio/resolve/main/Wav2Lip/checkpoints/wav2lip_gan.pth", | |
| "-O", MODEL_PATH | |
| ], check=True) | |
| print("Model ready!", flush=True) | |
| print(f"inference.py exists: {os.path.exists(f'{WAV2LIP_DIR}/inference.py')}", flush=True) | |
| try: | |
| setup() | |
| print("Setup complete!", flush=True) | |
| except Exception as e: | |
| print(f"Setup error: {e}", flush=True) | |
| def lipsync(face, audio): | |
| os.makedirs(f"{BASE}/temp", exist_ok=True) | |
| os.makedirs(f"{WAV2LIP_DIR}/temp", exist_ok=True) | |
| out_path = f"/tmp/{str(uuid.uuid4())[:8]}.mp4" | |
| cmd = [ | |
| "python", f"{WAV2LIP_DIR}/inference.py", | |
| "--checkpoint_path", MODEL_PATH, | |
| "--face", face, | |
| "--audio", audio, | |
| "--outfile", out_path, | |
| "--nosmooth" | |
| ] | |
| result = subprocess.run(cmd, capture_output=True, text=True, cwd=WAV2LIP_DIR) | |
| print(result.stdout[-500:], flush=True) | |
| print(result.stderr[-500:], flush=True) | |
| if result.returncode != 0: | |
| raise gr.Error(result.stderr[-400:]) | |
| return out_path | |
| demo = gr.Interface( | |
| fn=lipsync, | |
| inputs=[ | |
| gr.Image(type="filepath", label="📸 Photo du visage"), | |
| gr.Audio(type="filepath", label="🎤 Audio"), | |
| ], | |
| outputs=gr.Video(label="🎬 Résultat"), | |
| title="LipSync AI 👄", | |
| api_name="lipsync" | |
| ) | |
| demo.launch() |