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