{
  "nbformat": 4,
  "nbformat_minor": 0,
  "metadata": {
    "accelerator": "GPU",
    "kernelspec": {
      "name": "python3",
      "display_name": "Python 3"
    }
  },
  "cells": [
    {
      "cell_type": "markdown",
      "metadata": {},
      "source": [
        "# Crucible Photo Performance\n",
        "Select a GPU runtime, then run the cells in order. Your files go to your own Colab session. Use a front-facing portrait and a recording with a clear lead voice. The finished MP4 includes the original audio."
      ]
    },
    {
      "cell_type": "code",
      "metadata": {},
      "execution_count": null,
      "outputs": [],
      "source": [
        "import os, subprocess, shutil, sys, urllib.request, zipfile\n",
        "from pathlib import Path\n",
        "\n",
        "root = Path(\"/content/SadTalker\")\n",
        "if not root.exists():\n",
        "    subprocess.run([\"git\", \"clone\", \"--depth\", \"1\", \"https://github.com/OpenTalker/SadTalker.git\", str(root)], check=True)\n",
        "os.chdir(root)\n",
        "# Isolate the model's legacy Python dependencies from the notebook kernel.\n",
        "subprocess.run([sys.executable, \"-m\", \"pip\", \"install\", \"-q\", \"uv\"], check=True)\n",
        "env = Path(\"/content/crucible-portrait-env\")\n",
        "subprocess.run([\"uv\", \"venv\", \"--python\", \"3.8\", \"--seed\", \"--allow-existing\", str(env)], check=True)\n",
        "python = str(env / \"bin/python\")\n",
        "subprocess.run([\"uv\", \"pip\", \"install\", \"--python\", python, \"torch==1.12.1+cu113\", \"torchvision==0.13.1+cu113\", \"torchaudio==0.12.1\", \"--extra-index-url\", \"https://download.pytorch.org/whl/cu113\", \"--index-strategy\", \"unsafe-best-match\"], check=True)\n",
        "subprocess.run([\"uv\", \"pip\", \"install\", \"--python\", python, \"-r\", \"requirements.txt\"], check=True)\n",
        "subprocess.run([python, \"-c\", \"import torch; assert torch.cuda.is_available(), 'Select a GPU runtime and reconnect'\"], check=True)\n",
        "weights = root / \"checkpoints\"\n",
        "weights.mkdir(exist_ok=True)\n",
        "for name in [\"SadTalker_V0.0.2_512.safetensors\", \"mapping_00229-model.pth.tar\", \"mapping_00109-model.pth.tar\"]:\n",
        "    target = weights / name\n",
        "    if not target.exists():\n",
        "        temporary = target.with_suffix(\".part\")\n",
        "        urllib.request.urlretrieve(\"https://github.com/OpenTalker/SadTalker/releases/download/v0.0.2-rc/\" + name, temporary)\n",
        "        temporary.replace(target)"
      ]
    },
    {
      "cell_type": "code",
      "metadata": {},
      "execution_count": null,
      "outputs": [],
      "source": [
        "from google.colab import files\n",
        "from tempfile import mkdtemp\n",
        "job = Path(mkdtemp(prefix=\"crucible-performance-\"))\n",
        "os.chdir(job)\n",
        "def upload_one(label, suffixes):\n",
        "    print(label)\n",
        "    uploaded = files.upload()\n",
        "    if len(uploaded) != 1:\n",
        "        raise ValueError(\"Select one file per prompt.\")\n",
        "    name, data = next(iter(uploaded.items()))\n",
        "    suffix = Path(name).suffix.lower()\n",
        "    if suffix not in suffixes:\n",
        "        raise ValueError(\"Supported extensions: \" + \", \".join(suffixes))\n",
        "    target = job / (label.split()[0] + suffix)\n",
        "    target.write_bytes(data)\n",
        "    return target\n",
        "\n",
        "photo = upload_one(\"Portrait photo\", [\".png\", \".jpg\", \".jpeg\"])\n",
        "audio = upload_one(\"Audio recording\", [\".mp3\", \".m4a\", \".wav\", \".flac\", \".aac\"])\n",
        "voice = job / \"driver.wav\"\n",
        "subprocess.run([\"ffmpeg\", \"-y\", \"-loglevel\", \"error\", \"-i\", str(audio), \"-vn\", \"-ac\", \"1\", \"-ar\", \"16000\", str(voice)], check=True)\n",
        "output = job / \"render\"\n",
        "subprocess.run([python, \"inference.py\", \"--source_image\", str(photo), \"--driven_audio\", str(voice), \"--preprocess\", \"full\", \"--size\", \"512\", \"--result_dir\", str(output)], cwd=root, check=True)\n",
        "videos = list(output.glob(\"*.mp4\"))\n",
        "if len(videos) != 1:\n",
        "    raise RuntimeError(\"Renderer did not return a finished video. Check the portrait for a clear, visible face.\")\n",
        "final = job / \"Crucible-Photo-Performance.mp4\"\n",
        "subprocess.run([\"ffmpeg\", \"-y\", \"-loglevel\", \"error\", \"-i\", str(videos[0]), \"-i\", str(audio), \"-map\", \"0:v:0\", \"-map\", \"1:a:0\", \"-c:v\", \"copy\", \"-c:a\", \"aac\", \"-b:a\", \"256k\", \"-movflags\", \"+faststart\", \"-shortest\", str(final)], check=True)\n",
        "files.download(str(final))"
      ]
    }
  ]
}