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
|
6 |
+
"source": [
|
7 |
+
"# SDXL with Custom LoRA on T4 GPU\n",
|
8 |
+
"\n",
|
9 |
+
"This notebook sets up Stable Diffusion XL (SDXL) on a T4 GPU in Google Colab with Python 3.11, downloads the base model from Hugging Face, and applies a custom LoRA model from Hugging Face or Civitai. It generates images using the configured pipeline.\n",
|
10 |
+
"\n",
|
11 |
+
"**Prerequisites:**\n",
|
12 |
+
"- Hugging Face account and token for gated model access (e.g., `stabilityai/stable-diffusion-xl-base-1.0`).\n",
|
13 |
+
"- Civitai API key if downloading LoRA from Civitai.\n",
|
14 |
+
"- Ensure Colab is set to T4 GPU (Runtime > Change runtime type > T4 GPU).\n",
|
15 |
+
"\n",
|
16 |
+
"**Note:** Replace placeholders (e.g., `YOUR_HF_TOKEN`, `YOUR_CIVITAI_API_KEY`) with your actual credentials."
|
17 |
+
]
|
18 |
+
},
|
19 |
+
{
|
20 |
+
"cell_type": "code",
|
21 |
+
"metadata": {},
|
22 |
+
"source": [
|
23 |
+
"# Install dependencies for Python 3.11\n",
|
24 |
+
"!pip install torch==2.2.0 torchvision==0.17.0 --index-url https://download.pytorch.org/whl/cu118\n",
|
25 |
+
"!pip install diffusers==0.29.2 transformers==4.44.2 accelerate==0.33.0 safetensors==0.4.5\n",
|
26 |
+
"!pip install requests\n",
|
27 |
+
"\n",
|
28 |
+
"# Verify Python version\n",
|
29 |
+
"import sys\n",
|
30 |
+
"print(sys.version)\n",
|
31 |
+
"\n",
|
32 |
+
"# Check GPU availability\n",
|
33 |
+
"import torch\n",
|
34 |
+
"print(f\"CUDA Available: {torch.cuda.is_available()}\")\n",
|
35 |
+
"print(f\"GPU: {torch.cuda.get_device_name(0) if torch.cuda.is_available() else 'No GPU'}\")"
|
36 |
+
],
|
37 |
+
"execution_count": null,
|
38 |
+
"outputs": []
|
39 |
+
},
|
40 |
+
{
|
41 |
+
"cell_type": "markdown",
|
42 |
+
"metadata": {},
|
43 |
+
"source": [
|
44 |
+
"## Step 1: Authenticate and Download Models\n",
|
45 |
+
"Authenticate with Hugging Face to download the SDXL base model. Optionally, provide a Civitai API key for LoRA download."
|
46 |
+
]
|
47 |
+
},
|
48 |
+
{
|
49 |
+
"cell_type": "code",
|
50 |
+
"metadata": {},
|
51 |
+
"source": [
|
52 |
+
"from huggingface_hub import login\n",
|
53 |
+
"import os\n",
|
54 |
+
"\n",
|
55 |
+
"# Log in to Hugging Face\n",
|
56 |
+
"HF_TOKEN = \"YOUR_HF_TOKEN\" # Replace with your Hugging Face token\n",
|
57 |
+
"login(HF_TOKEN)\n",
|
58 |
+
"\n",
|
59 |
+
"# Set Civitai API key (if downloading from Civitai)\n",
|
60 |
+
"CIVITAI_API_KEY = \"YOUR_CIVITAI_API_KEY\" # Replace with your Civitai API key or set to None\n",
|
61 |
+
"os.environ[\"CIVITAI_API_KEY\"] = CIVITAI_API_KEY if CIVITAI_API_KEY else \"\"\n",
|
62 |
+
"\n",
|
63 |
+
"# Download SDXL base model\n",
|
64 |
+
"from diffusers import StableDiffusionXLPipeline\n",
|
65 |
+
"base_model = \"stabilityai/stable-diffusion-xl-base-1.0\"\n",
|
66 |
+
"pipeline = StableDiffusionXLPipeline.from_pretrained(\n",
|
67 |
+
" base_model,\n",
|
68 |
+
" torch_dtype=torch.float16,\n",
|
69 |
+
" variant=\"fp16\",\n",
|
70 |
+
" use_safetensors=True\n",
|
71 |
+
").to(\"cuda\")"
|
72 |
+
],
|
73 |
+
"execution_count": null,
|
74 |
+
"outputs": []
|
75 |
+
},
|
76 |
+
{
|
77 |
+
"cell_type": "markdown",
|
78 |
+
"metadata": {},
|
79 |
+
"source": [
|
80 |
+
"## Step 2: Download Custom LoRA\n",
|
81 |
+
"Choose to download a LoRA model from Hugging Face or Civitai. Replace the URLs/IDs with your desired LoRA model."
|
82 |
+
]
|
83 |
+
},
|
84 |
+
{
|
85 |
+
"cell_type": "code",
|
86 |
+
"metadata": {},
|
87 |
+
"source": [
|
88 |
+
"import requests\n",
|
89 |
+
"import os\n",
|
90 |
+
"\n",
|
91 |
+
"def download_hf_lora(repo_id, filename, local_dir=\"./lora\"):\n",
|
92 |
+
" os.makedirs(local_dir, exist_ok=True)\n",
|
93 |
+
" local_path = os.path.join(local_dir, filename)\n",
|
94 |
+
" url = f\"https://huggingface.co/{repo_id}/resolve/main/{filename}\"\n",
|
95 |
+
" headers = {\"Authorization\": f\"Bearer {HF_TOKEN}\"}\n",
|
96 |
+
" response = requests.get(url, headers=headers)\n",
|
97 |
+
" response.raise_for_status()\n",
|
98 |
+
" with open(local_path, \"wb\") as f:\n",
|
99 |
+
" f.write(response.content)\n",
|
100 |
+
" return local_path\n",
|
101 |
+
"\n",
|
102 |
+
"def download_civitai_lora(model_id, filename, local_dir=\"./lora\"):\n",
|
103 |
+
" os.makedirs(local_dir, exist_ok=True)\n",
|
104 |
+
" local_path = os.path.join(local_dir, filename)\n",
|
105 |
+
" url = f\"https://civitai.com/api/download/models/{model_id}\"\n",
|
106 |
+
" headers = {\"Authorization\": f\"Bearer {os.environ['CIVITAI_API_KEY']}\"}\n",
|
107 |
+
" response = requests.get(url, headers=headers)\n",
|
108 |
+
" response.raise_for_status()\n",
|
109 |
+
" with open(local_path, \"wb\") as f:\n",
|
110 |
+
" f.write(response.content)\n",
|
111 |
+
" return local_path\n",
|
112 |
+
"\n",
|
113 |
+
"# Example: Download LoRA (choose one method)\n",
|
114 |
+
"# Hugging Face LoRA (e.g., a hypothetical LoRA model)\n",
|
115 |
+
"lora_path = download_hf_lora(\n",
|
116 |
+
" repo_id=\"username/sdxl-lora-model\", # Replace with actual Hugging Face repo ID\n",
|
117 |
+
" filename=\"model.safetensors\" # Replace with actual filename\n",
|
118 |
+
")\n",
|
119 |
+
"\n",
|
120 |
+
"# Civitai LoRA (uncomment to use)\n",
|
121 |
+
"# lora_path = download_civitai_lora(\n",
|
122 |
+
"# model_id=\"123456\", # Replace with Civitai model ID\n",
|
123 |
+
"# filename=\"lora_model.safetensors\"\n",
|
124 |
+
"# )"
|
125 |
+
],
|
126 |
+
"execution_count": null,
|
127 |
+
"outputs": []
|
128 |
+
},
|
129 |
+
{
|
130 |
+
"cell_type": "markdown",
|
131 |
+
"metadata": {},
|
132 |
+
"source": [
|
133 |
+
"## Step 3: Load LoRA into Pipeline\n",
|
134 |
+
"Load the custom LoRA weights into the SDXL pipeline."
|
135 |
+
]
|
136 |
+
},
|
137 |
+
{
|
138 |
+
"cell_type": "code",
|
139 |
+
"metadata": {},
|
140 |
+
"source": [
|
141 |
+
"# Load LoRA weights\n",
|
142 |
+
"pipeline.load_lora_weights(\n",
|
143 |
+
" lora_path,\n",
|
144 |
+
" adapter_name=\"custom_lora\"\n",
|
145 |
+
")\n",
|
146 |
+
"\n",
|
147 |
+
"# Enable LoRA\n",
|
148 |
+
"pipeline.set_adapters([\"custom_lora\"], adapter_weights=[1.0])\n",
|
149 |
+
"\n",
|
150 |
+
"# Optimize for T4 GPU\n",
|
151 |
+
"pipeline.enable_model_cpu_offload()\n",
|
152 |
+
"pipeline.enable_vae_slicing()"
|
153 |
+
],
|
154 |
+
"execution_count": null,
|
155 |
+
"outputs": []
|
156 |
+
},
|
157 |
+
{
|
158 |
+
"cell_type": "markdown",
|
159 |
+
"metadata": {},
|
160 |
+
"source": [
|
161 |
+
"## Step 4: Generate Images\n",
|
162 |
+
"Configure the prompt and generate images using the SDXL pipeline with LoRA."
|
163 |
+
]
|
164 |
+
},
|
165 |
+
{
|
166 |
+
"cell_type": "code",
|
167 |
+
"metadata": {},
|
168 |
+
"source": [
|
169 |
+
"prompt = \"A futuristic cityscape at sunset, cyberpunk style, highly detailed, vibrant colors\"\n",
|
170 |
+
"negative_prompt = \"blurry, low quality, artifacts\"\n",
|
171 |
+
"\n",
|
172 |
+
"images = pipeline(\n",
|
173 |
+
" prompt=prompt,\n",
|
174 |
+
" negative_prompt=negative_prompt,\n",
|
175 |
+
" num_inference_steps=30,\n",
|
176 |
+
" guidance_scale=7.5,\n",
|
177 |
+
" height=1024,\n",
|
178 |
+
" width=1024,\n",
|
179 |
+
" num_images_per_prompt=1\n",
|
180 |
+
").images\n",
|
181 |
+
"\n",
|
182 |
+
"# Save and display the image\n",
|
183 |
+
"images[0].save(\"output.png\")\n",
|
184 |
+
"images[0]"
|
185 |
+
],
|
186 |
+
"execution_count": null,
|
187 |
+
"outputs": []
|
188 |
+
},
|
189 |
+
{
|
190 |
+
"cell_type": "markdown",
|
191 |
+
"metadata": {},
|
192 |
+
"source": [
|
193 |
+
"## Notes\n",
|
194 |
+
"- **T4 GPU Optimization**: The notebook uses `float16` precision, VAE slicing, and model CPU offloading to fit within T4 GPU memory constraints (16GB VRAM).\n",
|
195 |
+
"- **LoRA Model**: Ensure the LoRA model is compatible with SDXL. Replace placeholder repo IDs or model IDs with actual values from Hugging Face or Civitai.\n",
|
196 |
+
"- **Performance**: Adjust `num_inference_steps` and `guidance_scale` for quality vs. speed trade-offs.\n",
|
197 |
+
"- **Storage**: Generated images are saved as `output.png` in the Colab environment. Download them manually if needed."
|
198 |
+
]
|
199 |
+
}
|
200 |
+
],
|
201 |
+
"metadata": {
|
202 |
+
"kernelspec": {
|
203 |
+
"display_name": "Python 3",
|
204 |
+
"language": "python",
|
205 |
+
"name": "python3"
|
206 |
+
},
|
207 |
+
"language_info": {
|
208 |
+
"codemirror_mode": {
|
209 |
+
"name": "ipython",
|
210 |
+
"version": 3
|
211 |
+
},
|
212 |
+
"file_extension": ".py",
|
213 |
+
"mimetype": "text/x-python",
|
214 |
+
"name": "python",
|
215 |
+
"nbconvert_exporter": "python",
|
216 |
+
"pygments_lexer": "ipython3",
|
217 |
+
"version": "3.11.0"
|
218 |
+
}
|
219 |
+
},
|
220 |
+
"nbformat": 4,
|
221 |
+
"nbformat_minor": 4
|
222 |
+
}
|