$ cat node-template.py

Video Lipsync

// Synchronizes lip movements in a video to match a provided audio track using LatentSync 1.6 via a standalone native GPU service. Accepts a source video and an audio file, returns an MP4 with lip-synced output.

Process
Video
template.py
1import os2import sys3import json4import subprocess5import time6import traceback78try:9    import requests10except ImportError:11    subprocess.check_call([sys.executable, "-m", "pip", "install", "requests"])12    import requests1314NATIVE_VIDEO_LIPSYNC_SERVICE_URL = os.getenv(15    "NATIVE_VIDEO_LIPSYNC_SERVICE_URL", "http://native-video-lipsync-service:8107"16)17_EMBLEMA_VERSION = os.getenv("EMBLEMA_VERSION", "dev")18NATIVE_VIDEO_LIPSYNC_SERVICE_IMAGE = os.getenv(19    "NATIVE_VIDEO_LIPSYNC_SERVICE_IMAGE",20    f"emblema/native-video-lipsync-service:{_EMBLEMA_VERSION}",21)22HF_CACHE_HOST_PATH = os.getenv("HF_CACHE_HOST_PATH", "/root/.cache/huggingface")23CONTAINER_NAME = "native-video-lipsync-service"24INPUT_DIR = "/data/input"25OUTPUT_DIR = "/data/output"262728def start_container():29    """Create and start native-video-lipsync-service, removing any stale container first."""30    subprocess.run(31        ["docker", "rm", "-f", CONTAINER_NAME],32        capture_output=True, text=True33    )3435    hf_token = os.getenv("HUGGINGFACE_TOKEN", "")36    print(f"Creating container {CONTAINER_NAME}...", file=sys.stderr)37    run_cmd = [38        "docker", "run", "-d",39        "--name", CONTAINER_NAME,40        "--network", "emblema",41        "--gpus", "all",42        "-e", "PORT=8107",43        "-e", "DEVICE=cuda",44        "-e", f"HF_TOKEN={hf_token}",45        "-v", f"{HF_CACHE_HOST_PATH}:/root/.cache/huggingface",46        NATIVE_VIDEO_LIPSYNC_SERVICE_IMAGE,47    ]48    result = subprocess.run(run_cmd, capture_output=True, text=True)49    if result.returncode != 0:50        print(f"docker run failed (exit {result.returncode}): {result.stderr}", file=sys.stderr)51        raise RuntimeError(f"Failed to start container: {result.stderr}")5253    # Poll health endpoint54    timeout = 24055    interval = 356    elapsed = 057    health_url = f"{NATIVE_VIDEO_LIPSYNC_SERVICE_URL}/health"58    while elapsed < timeout:59        try:60            r = requests.get(health_url, timeout=5)61            if r.status_code == 200:62                print(f"Container healthy (waited {elapsed}s).", file=sys.stderr)63                return64        except requests.ConnectionError:65            pass66        time.sleep(interval)67        elapsed += interval6869    raise RuntimeError(f"Container did not become healthy within {timeout}s")707172def stop_container():73    """Remove the container."""74    try:75        subprocess.run(76            ["docker", "rm", "-f", CONTAINER_NAME],77            capture_output=True, text=True, timeout=3078        )79        print(f"Container {CONTAINER_NAME} removed.", file=sys.stderr)80    except Exception as e:81        print(f"Warning: failed to remove container: {e}", file=sys.stderr)828384def main():85    try:86        input_json = sys.stdin.read()87        execution_input = json.loads(input_json)88        inputs = execution_input.get("inputs", {})8990        video = inputs.get("video", "")91        audio = inputs.get("audio", "")9293        if not video:94            raise ValueError("Video input is required")95        if not audio:96            raise ValueError("Audio input is required")9798        video_path = os.path.join(INPUT_DIR, video)99        if not os.path.exists(video_path):100            raise FileNotFoundError(f"Input video not found: {video_path}")101102        audio_path = os.path.join(INPUT_DIR, audio)103        if not os.path.exists(audio_path):104            raise FileNotFoundError(f"Input audio not found: {audio_path}")105106        os.makedirs(OUTPUT_DIR, exist_ok=True)107108        # Start the container109        start_container()110111        try:112            # Send video and audio to service113            with open(video_path, "rb") as vf, open(audio_path, "rb") as af:114                resp = requests.post(115                    f"{NATIVE_VIDEO_LIPSYNC_SERVICE_URL}/lipsync",116                    files={117                        "video": (os.path.basename(video_path), vf, "video/mp4"),118                        "audio": (os.path.basename(audio_path), af, "audio/wav"),119                    },120                    timeout=900,121                )122123            if resp.status_code != 200:124                try:125                    error_detail = resp.json()126                except Exception:127                    error_detail = resp.text128                raise RuntimeError(129                    f"Lipsync service returned {resp.status_code}: {error_detail}"130                )131132            # Save result133            out_filename = "lipsync_result.mp4"134            out_path = os.path.join(OUTPUT_DIR, out_filename)135            with open(out_path, "wb") as f:136                f.write(resp.content)137138            inference_time = resp.headers.get("X-Inference-Time-Ms", "unknown")139            print(f"Lipsync complete: time={inference_time}ms", file=sys.stderr)140141            output = {142                "video": out_filename,143            }144            print(json.dumps(output, indent=2))145146        finally:147            stop_container()148149    except Exception as e:150        error_output = {151            "error": str(e),152            "errorType": type(e).__name__,153            "traceback": traceback.format_exc(),154        }155        print(json.dumps(error_output), file=sys.stderr)156        sys.exit(1)157158159if __name__ == "__main__":160    main()