Build Your Own Face Swap Application Using Google Colab
Source: Dev.to
Goal: Build a complete face‑swap application that runs entirely in a Google Colab notebook (CPU only) and expose the UI publicly.
This guide assumes you will work in a single notebook named FaceSwap.ipynb.
Why CPU‑Only in Google Colab?
- No need to enable GPU/TPU – the InsightFace model is lightweight enough for CPU inference.
- Simpler setup: no CUDA drivers, no extra configuration.
- ONNX Runtime automatically falls back to CPU execution.
Tip: Keep the runtime type set to CPU (Runtime → Change runtime type → Hardware accelerator → None).
Prerequisites
- Google account with access to Google Colab
- Basic familiarity with shell commands in a notebook cell
- A few sample images for testing the face swap
Step 1 – Create a New Colab Notebook
- Open Google Colab.
- Create a new notebook and rename it
FaceSwap.ipynb. - Verify the runtime type is CPU (see tip above).
Step 2 – Clone the Face‑Swap UI Repository
%cd /content
!rm -rf face-swap-ui
!git clone https://github.com/TheMasterFX/face-swap-ui.git
What this does
- Downloads the web UI, backend logic, and face‑processing scripts.
- All files will be placed under
/content/face-swap-ui.
Step 3 – Download the Face‑Swap Model Manually
!mkdir -p /root/.insightface/models
!wget -O /root/.insightface/models/inswapper_128.onnx \
https://huggingface.co/ezioruan/inswapper_128.onnx/resolve/main/inswapper_128.onnx
- Creates the directory expected by InsightFace.
- Stores
inswapper_128.onnxlocally to avoid repeated online downloads.
Step 4 – Install Required Dependencies
%cd /content/face-swap-ui
!pip install --upgrade pip
!pip install -r requirements.txt
Typical packages installed:
insightfaceonnxruntime(CPU version)- UI‑related libraries (e.g.,
gradio,opencv-python) - Miscellaneous image‑processing tools
Step 5 – Apply Required Patch Fixes
Fix 1 – Force Local Model Loading
!sed -i "s|get_model('inswapper_128.onnx'.*|get_model('/root/.insightface/models/inswapper_128.onnx', download=False)|" face_swap_ui.py
Ensures the UI loads the model from the local path instead of trying to download it.
Fix 2 – Resolve Case‑Sensitivity Issues
!sed -i "s/Label=/label=/g" face_swap_ui.py
Linux file‑system is case‑sensitive; this aligns the label name.
Fix 3 – Fix UI Layout Issues
!sed -i "s/\.style(height=400)//g" face_swap_ui.py
Removes a hard‑coded height that can break rendering in a public browser session.
Step 6 – Install & Configure Pinggy Tunnel
Pinggy creates a public URL that forwards traffic to the notebook’s local server.
!pip install pinggy
import pinggy
tunnel = pinggy.start_tunnel(forwardto="localhost:7860")
print("Public URLs:", tunnel.urls)
- Copy the generated public URL; you’ll use it to access the UI from any browser.
Step 7 – Start the Face‑Swap Application
!python face_swap_ui.py
- The server will initialize (a few seconds on CPU).
- Keep this cell running; the UI will be served at the Pinggy URL printed earlier.
Step 8 – Test the Application in a Browser
- Open an incognito window (or a different device).
- Paste the Pinggy public URL and press Enter Site.
Using the UI
- Drop image here – upload the target image (the background).
- Upload the source image containing the face you want to swap in.
- Click Analyse – the system detects faces and shows the count.
- Click Swap – the face‑swap is performed.
- The swapped result appears directly in the UI.
🎉 You now have a fully functional, CPU‑only face‑swap demo running in Google Colab, accessible from anywhere via a public URL. Happy swapping!
UI
Performance Notes
Since this setup runs on CPU only:
- Image‑based face swaps work smoothly.
- Processing may take a few seconds per swap.
- Video face swapping is slower and not recommended for long videos.
Conclusion
Building a Face Swap application on Google Colab using CPU is a practical and accessible way to experiment with AI‑powered image processing. You avoid local setup issues while still getting a fully functional web UI.
With a public tunnel, this setup becomes ideal for:
- Demos
- Proof of concepts
- Learning computer‑vision workflows
- Sharing experiments with others
You can extend this further by adding video support, batch processing, or custom UI features.