stable diffusion automatic111 webui sagemaker studio lab aws update july 2023
ubuntu2204
Kernel: Python 3 (system-wide)
ติดตั้ง stable diffusion webui ใน sagemaker โดย จิ๋วหลิว
แนะนำ หรือแจ้งปัญหา https://www.facebook.com/inwit.me/
ขั้นตอนการติดตั้ง ให้ใช้ CPU เมื่อติดตั้งจนรัน webui ได้แล้ว ให้ปิด และ กลับไปรันด้วย GPU เพราะ CPU ช้ามากๆ ช้าสุดๆ
กดรันทีละขั้นตอน ห้ามรันทั้งหมด เพราะพื้นที่จะไม่พอ
ขั้นตอนติดตั้ง ทำครั้งเดียว
ติดตั้ง Stable Diffusion WebUI AUTOMATIC1111 จาก GitHub
In [0]:
%cd /home/studio-lab-user/ !git clone https://github.com/AUTOMATIC1111/stable-diffusion-webui.git %cd stable-diffusion-webui !pip install -r requirements.txt %cd /home/studio-lab-user/stable-diffusion-webui/models !mkdir Lora print("เสร็จสมบูรณ์ ไปขั้นตอนต่อไปได้ You're done, you can start the next step.")
ติดตั้ง ส่วนเสริม
In [0]:
%cd /home/studio-lab-user/stable-diffusion-webui/extensions !git clone --depth 1 https://github.com/yfszzx/stable-diffusion-webui-images-browser !git clone --depth 1 https://github.com/Mikubill/sd-webui-controlnet !git clone --depth 1 https://github.com/antfu/sd-webui-qrcode-toolkit #!git clone --depth 1 https://github.com/s0md3v/sd-webui-roop ลงละเหมือนจะมีปัญหา เลยตัดออกก่อน ใครใช้ได้สอนผมด้วยนะครับ !git clone --depth 1 https://github.com/kohya-ss/sd-webui-additional-networks print("เสร็จสมบูรณ์ ไปขั้นตอนต่อไปได้ You're done, you can start the next step.")
ติดตั้ง controlnet แก้ไขหรือข้ามได้หากไม่ใช้ อันนี้ไว้ทำ QR-code โดยเฉพาะ
In [0]:
import requests import os url_list = [ "https://huggingface.co/lllyasviel/ControlNet-v1-1/resolve/main/control_v11f1e_sd15_tile.pth", "https://huggingface.co/ioclab/ioc-controlnet/resolve/main/models/control_v1p_sd15_brightness.safetensors", "https://huggingface.co/lllyasviel/ControlNet-v1-1/resolve/main/control_v11p_sd15_lineart.pth", "https://huggingface.co/DionTimmer/controlnet_qrcode/resolve/main/control_v1p_sd15_qrcode.safetensors", "https://huggingface.co/DionTimmer/controlnet_qrcode/resolve/main/control_v1p_sd15_qrcode.yaml", "https://huggingface.co/monster-labs/control_v1p_sd15_qrcode_monster/resolve/main/control_v1p_sd15_qrcode_monster.safetensors", "https://huggingface.co/monster-labs/control_v1p_sd15_qrcode_monster/resolve/main/control_v1p_sd15_qrcode_monster.yaml", ] folder_path = "/home/studio-lab-user/stable-diffusion-webui/extensions/sd-webui-controlnet/models" os.makedirs(folder_path, exist_ok=True) for url in url_list: response = requests.get(url) filename = os.path.join(folder_path, os.path.basename(url)) with open(filename, 'wb') as file: file.write(response.content) #ถ้าใช้ control_v1p_sd15_brightness ตัวนี้ต้องใช้ ถ้าไม่ใช้ ก้ปล่อยผ่านไป ไฟล์ 1kb เอง %cd /home/studio-lab-user/stable-diffusion-webui/extensions/sd-webui-controlnet/models import urllib.request import os url = "https://huggingface.co/webui/ControlNet-modules-safetensors/resolve/main/cldm_v15.yaml" new_filename = "control_v1p_sd15_brightness.yaml" urllib.request.urlretrieve(url, new_filename) print("เสร็จสมบูรณ์ ไปขั้นตอนต่อไปได้ You're done, you can start the next step.")
โค้ดคำนวนพื้นที่ เพื่อบริหารโมเดล แนะนำว่าลงแค่ check point เดียว เพราะพื้นที่มีน้อย
In [0]:
#Check free disk space !df -h | grep -E 'Avail|home'
โหลด check point,lora เลือกรันได้ เอาแค่ที่ต้องการ แก้ไขหรือข้ามได้
โมดูลโหลดไฟล์ เพื่อใช้โหลดจาก civitai
In [0]:
!pip install --user aria2p print("เสร็จสมบูรณ์ ไปขั้นตอนต่อไปได้ You're done, you can start the next step.")
check point
In [0]:
#ReV Animated import subprocess from tqdm import tqdm download_url = 'https://civitai.com/api/download/models/46846' destination_dir = '/home/studio-lab-user/stable-diffusion-webui/models/Stable-diffusion/' filename = 'revAnimated_v122.safetensors' # สร้างคำสั่ง wget ในรูปแบบของรายการอาร์กิวเมนต์ command = f'wget -c {download_url} -P {destination_dir} -O {destination_dir}/{filename}' # ดำเนินการดาวน์โหลดไฟล์โดยใช้ subprocess และแสดงแถบความคืบหน้าในบรรทัดเดียว subprocess.run(command, shell=True, stdout=subprocess.DEVNULL, stderr=subprocess.DEVNULL) with tqdm(total=100) as pbar: pbar.update(100) print("เสร็จสมบูรณ์ ไปขั้นตอนต่อไปได้ You're done, you can start the next step.")
In [0]:
#DreamShaper import subprocess from tqdm import tqdm download_url = 'https://civitai.com/api/download/models/109123' destination_dir = '/home/studio-lab-user/stable-diffusion-webui/models/Stable-diffusion/' filename = 'dreamshaper_7.safetensors' # สร้างคำสั่ง wget ในรูปแบบของรายการอาร์กิวเมนต์ command = f'wget -c {download_url} -P {destination_dir} -O {destination_dir}/{filename}' # ดำเนินการดาวน์โหลดไฟล์โดยใช้ subprocess และแสดงแถบความคืบหน้าในบรรทัดเดียว subprocess.run(command, shell=True, stdout=subprocess.DEVNULL, stderr=subprocess.DEVNULL) with tqdm(total=100) as pbar: pbar.update(100) print("เสร็จสมบูรณ์ ไปขั้นตอนต่อไปได้ You're done, you can start the next step.")
In [0]:
#Beautiful Realistic Asians import subprocess from tqdm import tqdm download_url = 'https://civitai.com/api/download/models/63786' destination_dir = '/home/studio-lab-user/stable-diffusion-webui/models/Stable-diffusion/' filename = 'beautifulRealistic_brav5.safetensors' # สร้างคำสั่ง wget ในรูปแบบของรายการอาร์กิวเมนต์ command = f'wget -c {download_url} -P {destination_dir} -O {destination_dir}/{filename}' # ดำเนินการดาวน์โหลดไฟล์โดยใช้ subprocess และแสดงแถบความคืบหน้าในบรรทัดเดียว subprocess.run(command, shell=True, stdout=subprocess.DEVNULL, stderr=subprocess.DEVNULL) with tqdm(total=100) as pbar: pbar.update(100) print("เสร็จสมบูรณ์ ไปขั้นตอนต่อไปได้ You're done, you can start the next step.")
ตัวอย่างการโหลด lora
In [0]:
#Thai university uniform import subprocess from tqdm import tqdm download_url = 'https://civitai.com/api/download/models/58690' destination_dir = '/home/studio-lab-user/stable-diffusion-webui/models/lora/' filename = 'mahalaiuniform-000001.safetensors' # สร้างคำสั่ง wget ในรูปแบบของรายการอาร์กิวเมนต์ command = f'wget -c {download_url} -P {destination_dir} -O {destination_dir}/{filename}' # ดำเนินการดาวน์โหลดไฟล์โดยใช้ subprocess และแสดงแถบความคืบหน้าในบรรทัดเดียว subprocess.run(command, shell=True, stdout=subprocess.DEVNULL, stderr=subprocess.DEVNULL) with tqdm(total=100) as pbar: pbar.update(100) print("เสร็จสมบูรณ์ ไปขั้นตอนต่อไปได้ You're done, you can start the next step.")
In [0]:
#Thai High school uniform import subprocess from tqdm import tqdm download_url = 'https://civitai.com/api/download/models/26935' destination_dir = '/home/studio-lab-user/stable-diffusion-webui/models/lora/' filename = 'mathayom_uniform16.safetensors' # สร้างคำสั่ง wget ในรูปแบบของรายการอาร์กิวเมนต์ command = f'wget -c {download_url} -P {destination_dir} -O {destination_dir}/{filename}' # ดำเนินการดาวน์โหลดไฟล์โดยใช้ subprocess และแสดงแถบความคืบหน้าในบรรทัดเดียว subprocess.run(command, shell=True, stdout=subprocess.DEVNULL, stderr=subprocess.DEVNULL) with tqdm(total=100) as pbar: pbar.update(100) print("เสร็จสมบูรณ์ ไปขั้นตอนต่อไปได้ You're done, you can start the next step.")
In [0]:
#Thailand Tradition Dress import subprocess from tqdm import tqdm download_url = 'https://civitai.com/api/download/models/17804' destination_dir = '/home/studio-lab-user/stable-diffusion-webui/models/lora/' filename = 'thai_tradition-7.safetensors' # สร้างคำสั่ง wget ในรูปแบบของรายการอาร์กิวเมนต์ command = f'wget -c {download_url} -P {destination_dir} -O {destination_dir}/{filename}' # ดำเนินการดาวน์โหลดไฟล์โดยใช้ subprocess และแสดงแถบความคืบหน้าในบรรทัดเดียว subprocess.run(command, shell=True, stdout=subprocess.DEVNULL, stderr=subprocess.DEVNULL) with tqdm(total=100) as pbar: pbar.update(100) print("เสร็จสมบูรณ์ ไปขั้นตอนต่อไปได้ You're done, you can start the next step.")
In [0]:
#Thai Massage Dress import subprocess from tqdm import tqdm download_url = 'https://civitai.com/api/download/models/20470' destination_dir = '/home/studio-lab-user/stable-diffusion-webui/models/lora/' filename = 'ThaiMassageDressV2.safetensors' # สร้างคำสั่ง wget ในรูปแบบของรายการอาร์กิวเมนต์ command = f'wget -c {download_url} -P {destination_dir} -O {destination_dir}/{filename}' # ดำเนินการดาวน์โหลดไฟล์โดยใช้ subprocess และแสดงแถบความคืบหน้าในบรรทัดเดียว subprocess.run(command, shell=True, stdout=subprocess.DEVNULL, stderr=subprocess.DEVNULL) with tqdm(total=100) as pbar: pbar.update(100) print("เสร็จสมบูรณ์ ไปขั้นตอนต่อไปได้ You're done, you can start the next step.")
In [0]:
#Thai Khon Hanuman import subprocess from tqdm import tqdm download_url = 'https://civitai.com/api/download/models/49247' destination_dir = '/home/studio-lab-user/stable-diffusion-webui/models/lora/' filename = 'khonHNM2.safetensors' # สร้างคำสั่ง wget ในรูปแบบของรายการอาร์กิวเมนต์ command = f'wget -c {download_url} -P {destination_dir} -O {destination_dir}/{filename}' # ดำเนินการดาวน์โหลดไฟล์โดยใช้ subprocess และแสดงแถบความคืบหน้าในบรรทัดเดียว subprocess.run(command, shell=True, stdout=subprocess.DEVNULL, stderr=subprocess.DEVNULL) with tqdm(total=100) as pbar: pbar.update(100) print("เสร็จสมบูรณ์ ไปขั้นตอนต่อไปได้ You're done, you can start the next step.")
In [0]:
#Tossakan : Thai Tossakan Titan import subprocess from tqdm import tqdm download_url = 'https://civitai.com/api/download/models/47827' destination_dir = '/home/studio-lab-user/stable-diffusion-webui/models/lora/' filename = 'tossakan.safetensors' # สร้างคำสั่ง wget ในรูปแบบของรายการอาร์กิวเมนต์ command = f'wget -c {download_url} -P {destination_dir} -O {destination_dir}/{filename}' # ดำเนินการดาวน์โหลดไฟล์โดยใช้ subprocess และแสดงแถบความคืบหน้าในบรรทัดเดียว subprocess.run(command, shell=True, stdout=subprocess.DEVNULL, stderr=subprocess.DEVNULL) with tqdm(total=100) as pbar: pbar.update(100) print("เสร็จสมบูรณ์ ไปขั้นตอนต่อไปได้ You're done, you can start the next step.")
และรวมembeddings (ไฟล์มัดรวมจาก gg drive)
In [0]:
%pip install gdown print("เสร็จสมบูรณ์ ไปขั้นตอนต่อไปได้ You're done, you can start the next step.")
In [0]:
# all-negative embeddings ########################################################### GOOGLE_FILE_ID = "13Io8QPFxXfHgEOdoQEo0-gF8t9p_o_Go" ########################################################### import gdown %cd /home/studio-lab-user/stable-diffusion-webui/embeddings/ url = "https://drive.google.com/u/0/uc?id=" + GOOGLE_FILE_ID + "&export=download&confirm=t" print("Downloading " + url) gdown.download(url) !unzip -o embeddings.zip print("เสร็จสมบูรณ์ ไปขั้นตอนต่อไปได้ You're done, you can start the next step.")
โมดูลที่จำเป็น
In [0]:
%cd /home/studio-lab-user/stable-diffusion-webui/ ! conda install glib=2.51.0 -y !pip install pytorch-lightning print("เสร็จสมบูรณ์ ไปขั้นตอนต่อไปได้ You're done, you can start the next step.")
In [0]:
%cd /home/studio-lab-user/ !pip install jinja2 markupsafe !pip install pyngrok !pip install gitpython %cd /home/studio-lab-user/stable-diffusion-webui/ !wget https://bin.equinox.io/c/4VmDzA7iaHb/ngrok-stable-linux-amd64.zip !unzip -o ngrok-stable-linux-amd64.zip print("เสร็จสมบูรณ์ ไปขั้นตอนต่อไปได้ You're done, you can start the next step.")
ต่อไปนี้ทำทุกครั้งเมื่อเปิด โปรเจคมาใหม่ เพื่อเริ่มทำงาน webui
In [0]:
# update หากต้องการ %cd /home/studio-lab-user/stable-diffusion-webui/ !git pull %pip install -r requirements_versions.txt print("เสร็จสมบูรณ์ ไปขั้นตอนต่อไปได้ You're done, you can start the next step.")
run webui AUTOMATIC1111
แก้ไข YOUR_NGROK_TOKEN เป็น Your Authtoken ของคุณ สมัครได้ที่ ngrok.com แค่สมัครแล้วเอา Authtoken มาใส่ ไม่ต้องโหลดอะไร
ถ้าใช้ CPU ในการรัน ให้ลบ --xformers ออก ก่อนรัน ไม่งั้นรันไม่ได้ ถ้าเป็น GPU จำเป็นต้องใส่
In [0]:
%cd /home/studio-lab-user/stable-diffusion-webui/ import os import subprocess import shlex import time ngrok_token = 'YOUR_NGROK_TOKEN' ngrok_cmd = './ngrok authtoken {}'.format(ngrok_token) ngrok_proc = subprocess.Popen(shlex.split(ngrok_cmd), stdout=subprocess.PIPE) WEBUI_USERNAME="admin" WEBUI_PASSWORD="jiwliw" import subprocess import os import sys !mkdir -p /tmp/outputs !mkdir -p /tmp/models ARGS = "\"--skip-torch-cuda-test --no-half --disable-console-progressbars --gradio-debug --gradio-auth " + WEBUI_USERNAME + ":" + WEBUI_PASSWORD + " --ngrok " + ngrok_token + "\"" !COMMANDLINE_ARGS=$ARGS REQS_FILE="requirements.txt" python launch.py --xformers --skip-torch-cuda-test
เมื่อรันไปแล้วจะ error ไม่ต้องตกใจ ให้รันโค้ดด้านล่าง แล้วกดรัน webui ใหม่ (ทำแค่ครั้งแรก)
ในการเจนรูป กดแล้วจะไม่มีอะไรเกิดขึ้นที่ webui แต่โปรแกรมได้ทำงานอยู่ มาดูที่คอนโซลตรงนี้ได้ เจนเสร็จ รูปถึงจะโชว์
ยังแก้ไม่ได้เหนื่อยแล้ว ใครแก้ได้ให้ใช้งานได้สมบูรณ์ แบ่งปันกันด้วยนะครับ
In [0]:
!sed -i 's/from pytorch_lightning.utilities.distributed import rank_zero_only/from pytorch_lightning.utilities.rank_zero import rank_zero_only/' /home/studio-lab-user/stable-diffusion-webui/repositories/stable-diffusion-stability-ai/ldm/models/diffusion/ddpm.py print("เสร็จสมบูรณ์ ไปขั้นตอนต่อไปได้ You're done, you can start the next step.")