{
 "nbformat": 4,
 "nbformat_minor": 0,
 "metadata": {
  "colab": {
   "private_outputs": true,
   "provenance": [],
   "machine_shape": "hm",
   "name": "facechain-demo.ipynb"
  },
  "kernelspec": {
   "name": "python3",
   "display_name": "Python 3"
  }
 },
 "cells": [
  {
   "cell_type": "markdown",
   "source": [
    "Requirements:\n",
    "- GPU Mem Usage: 19G\n",
    "- Disk Usage: About 50G"
   ],
   "metadata": {
    "id": "AzOycrSUkK-0"
   }
  },
  {
   "cell_type": "markdown",
   "source": [
    "## Get facechain source code from GitHub"
   ],
   "metadata": {
    "id": "8yQEWTkekIx9"
   }
  },
  {
   "cell_type": "code",
   "source": [
    "!GIT_LFS_SKIP_SMUDGE=1 git clone https://github.com/modelscope/facechain.git --depth 1"
   ],
   "metadata": {
    "id": "WK_uDw0NkHTP"
   },
   "execution_count": null,
   "outputs": []
  },
  {
   "cell_type": "markdown",
   "source": [
    "## Installation requirements\n",
    "- Note that you can ignore warning on dependencies conflicts at the end\n",
    "- You may use conda virtual env to avoid warning info."
   ],
   "metadata": {
    "id": "jUWWSGDG1ZGK"
   }
  },
  {
   "cell_type": "code",
   "source": [
    "!pip3 install -r facechain/requirements.txt"
   ],
   "metadata": {
    "id": "QrwRDBipxKZw"
   },
   "execution_count": null,
   "outputs": []
  },
  {
   "cell_type": "markdown",
   "source": [
    "## Check environment informaiton"
   ],
   "metadata": {
    "id": "0miaB1nC2VrA"
   }
  },
  {
   "cell_type": "code",
   "source": [
    "!nvidia-smi\n",
    "!pip3 show torch\n",
    "\n",
    "# Note that this setup is verified on (cuda 12.0, torch2.0.1+cu118, Nvidia-A100 40G)"
   ],
   "metadata": {
    "id": "uvNE0kRxyI4q"
   },
   "execution_count": null,
   "outputs": []
  },
  {
   "cell_type": "markdown",
   "source": [],
   "metadata": {
    "id": "klba298h2bbu"
   }
  },
  {
   "cell_type": "markdown",
   "source": [
    "## Installing mmcv-full\n",
    "- Several of the underlying models depend on mmcv-full, which could be tricky to install since it is environment-dependent, you may refer to [mmcv's official documentation](https://mmcv.readthedocs.io/zh_CN/latest/get_started/installation.html) for more details..\n",
    "- A prebuilt package is provided here for convenience, and was verified on (cuda 12.0, torch2.0.1+cu118, Nvidia-A100 40G)\n",
    "- If the prebuilt package does not work on your env setup, please use the alternative installation as suggested by [mmcv's official documentation](https://mmcv.readthedocs.io/zh_CN/latest/get_started/)"
   ],
   "metadata": {
    "id": "ciSxxSzI4l_u"
   }
  },
  {
   "cell_type": "code",
   "source": [
    "# use prebuilt mmcv-full package provided by ModelScope (verified on cuda 12.0, torch2.0.1+cu118, Nvidia-A100 40G)\n",
    "!pip3 install https://modelscope.oss-cn-beijing.aliyuncs.com/packages/mmcv/mmcv_full-1.7.0-cp310-cp310-linux_x86_64.whl\n",
    "\n",
    "# alternative manual installtation. note that it may take 30+ mins for building dependencies.\n",
    "# !pip3 install -U openmim\n",
    "# !mim install mmcv-full==1.7.0\n"
   ],
   "metadata": {
    "id": "t2lciBXyyuzA"
   },
   "execution_count": null,
   "outputs": []
  },
  {
   "cell_type": "markdown",
   "source": [
    "## Pre-download models to mitigate connection timeout issue"
   ],
   "metadata": {
    "collapsed": false
   }
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "outputs": [],
   "source": [
    "import os\n",
    "os.chdir('/content/facechain') # Note: replace with your facechain root\n",
    "print(os.getcwd())\n",
    "\n",
    "# Not required\n",
    "from facechain.utils import pre_download_models\n",
    "pre_download_models()"
   ],
   "metadata": {
    "collapsed": false
   }
  },
  {
   "cell_type": "markdown",
   "source": [
    "## Start facechain WebUI service"
   ],
   "metadata": {
    "id": "OxDgRY8H4vta"
   }
  },
  {
   "cell_type": "code",
   "source": [
    "# Install service dependencies\n",
    "!pip3 install gradio==3.50.2\n",
    "!pip3 install controlnet_aux==0.0.6\n",
    "!pip3 install python-slugify\n",
    "!pip3 install onnxruntime==1.15.1\n",
    "!pip3 install edge-tts\n",
    "\n",
    "# Click on the gradio URL to start building your FaceChain digital-twin!\n",
    "!python3 app.py"
   ],
   "metadata": {
    "id": "NW5o0iiezTCg"
   },
   "execution_count": null,
   "outputs": []
  }
 ]
}
