Skip to content

Instantly share code, notes, and snippets.

@safa-dayo
Created June 28, 2025 12:26
Show Gist options
  • Save safa-dayo/b1203a93ddf5c2ef61ede225f4acf76f to your computer and use it in GitHub Desktop.
Save safa-dayo/b1203a93ddf5c2ef61ede225f4acf76f to your computer and use it in GitHub Desktop.
Flux 1 Kontext [dev] をColabノートブックで動かすためのスクリプト
# #@title Environment Setup (Flux 1 Kontext [dev])
### setup ComfyUI ###
from pathlib import Path
OPTIONS = {}
USE_GOOGLE_DRIVE = False #@param {type:"boolean"}
UPDATE_COMFY_UI = True #@param {type:"boolean"}
USE_COMFYUI_MANAGER = True #@param {type:"boolean"}
INSTALL_CUSTOM_NODES_DEPENDENCIES = True #@param {type:"boolean"}
OPTIONS.update(
USE_GOOGLE_DRIVE=USE_GOOGLE_DRIVE,
UPDATE_COMFY_UI=UPDATE_COMFY_UI,
USE_COMFYUI_MANAGER=USE_COMFYUI_MANAGER,
INSTALL_CUSTOM_NODES_DEPENDENCIES=INSTALL_CUSTOM_NODES_DEPENDENCIES,
)
current_dir = !pwd
WORKSPACE = f"{current_dir[0]}/ComfyUI"
if OPTIONS['USE_GOOGLE_DRIVE']:
!echo "Mounting Google Drive..."
%cd /
from google.colab import drive
drive.mount('/content/drive')
WORKSPACE = "/content/drive/MyDrive/ComfyUI"
%cd /content/drive/MyDrive
![ ! -d $WORKSPACE ] && echo -= Initial setup ComfyUI =- && git clone https://github.com/comfyanonymous/ComfyUI
%cd $WORKSPACE
if OPTIONS['UPDATE_COMFY_UI']:
!echo -= Updating ComfyUI =-
!find . -name "*.bat" -o -name "*.py" | xargs -I{} chmod 755 {}
!git pull
!echo -= Install dependencies =-
!pip3 install accelerate
!pip3 install "einops" "transformers>=4.28.1" "safetensors>=0.4.2" aiohttp pyyaml Pillow scipy tqdm psutil "tokenizers>=0.13.3"
!pip3 install torch torchvision torchaudio --index-url https://download.pytorch.org/whl/cu121
!pip3 install torchsde
!pip3 install "kornia>=0.7.1" spandrel soundfile sentencepiece
if OPTIONS['USE_COMFYUI_MANAGER']:
%cd custom_nodes
!find ComfyUI-Manager -type f -perm -000 | xargs -I{} chmod 755 {}
![ ! -d ComfyUI-Manager ] && echo -= Initial setup ComfyUI-Manager =- && git clone https://github.com/ltdrdata/ComfyUI-Manager
%cd ComfyUI-Manager
!git pull
%cd $WORKSPACE
if OPTIONS['INSTALL_CUSTOM_NODES_DEPENDENCIES']:
!echo -= Install custom nodes dependencies =-
!pip install GitPython
!python custom_nodes/ComfyUI-Manager/cm-cli.py restore-dependencies
### setup Flux 1 Kontext (fp8 weights) ###
# Text encoders & VAE
!wget -q https://huggingface.co/comfyanonymous/flux_text_encoders/resolve/main/clip_l.safetensors \
-O $WORKSPACE/models/clip/clip_l.safetensors
!wget -q https://huggingface.co/comfyanonymous/flux_text_encoders/resolve/main/t5xxl_fp16.safetensors \
-O $WORKSPACE/models/clip/t5xxl_fp16.safetensors
!wget -q https://huggingface.co/Comfy-Org/Lumina_Image_2.0_Repackaged/resolve/main/split_files/vae/ae.safetensors \
-O $WORKSPACE/models/vae/ae.safetensors
# Diffusion model fp8
!wget -q https://huggingface.co/Comfy-Org/flux1-kontext-dev_ComfyUI/resolve/main/split_files/diffusion_models/flux1-dev-kontext_fp8_scaled.safetensors \
-O $WORKSPACE/models/diffusion_models/flux1-dev-kontext_fp8_scaled.safetensors
### start ComfyUI ###
!wget -q -P ~ https://github.com/cloudflare/cloudflared/releases/latest/download/cloudflared-linux-amd64.deb
!dpkg -i ~/cloudflared-linux-amd64.deb
import subprocess, threading, time, socket
def iframe_thread(port=8188):
while True:
time.sleep(0.5)
with socket.socket() as sock:
if sock.connect_ex(("127.0.0.1", port)) == 0:
break
print("\nComfyUI finished loading, launching cloudflared...\n")
p = subprocess.Popen(
["cloudflared", "tunnel", "--url", f"http://127.0.0.1:{port}"],
stdout=subprocess.PIPE, stderr=subprocess.PIPE
)
for line in p.stderr:
l = line.decode()
if "trycloudflare.com " in l:
print("Access ComfyUI at:", l[l.find("http"):], end='')
threading.Thread(target=iframe_thread, daemon=True).start()
!python main.py --dont-print-server
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment