File size: 4,610 Bytes
aeec00e
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
{
  "cells": [
    {
      "cell_type": "markdown",
      "metadata": {},
      "source": [
        "# DiffuseCraft: Text-to-Image Generation on T4 Colab\n",
        "\n",
        "This script uses a custom Stable Diffusion model from Hugging Face for text-to-image generation, optimized for T4 GPU with low RAM usage.\n",
        "\n",
        "**Requirements**:\n",
        "- T4 GPU runtime in Colab\n",
        "- Hugging Face account and token (for gated models)\n",
        "\n",
        "**Features**:\n",
        "- Uses `diffusers` library with FP16 precision\n",
        "- Enables model CPU offloading for low RAM\n",
        "- Supports custom prompts and negative prompts\n"
      ]
    },
    {
      "cell_type": "code",
      "execution_count": null,
      "metadata": {},
      "outputs": [],
      "source": [
        "# Install required libraries\n",
        "!pip install -q diffusers==0.21.4 transformers==4.33.0 accelerate==0.22.0\n",
        "!pip install -q torch==2.0.1 torchvision==0.15.2 --index-url https://download.pytorch.org/whl/cu118\n",
        "!pip install -q xformers==0.0.22\n"
      ]
    },
    {
      "cell_type": "code",
      "execution_count": null,
      "metadata": {},
      "outputs": [],
      "source": [
        "# Import libraries\n",
        "import torch\n",
        "from diffusers import StableDiffusionPipeline\n",
        "from huggingface_hub import login\n",
        "import os\n",
        "\n",
        "# Set Hugging Face token (replace with your token)\n",
        "os.environ['HUGGINGFACE_TOKEN'] = 'your_hf_token_here'\n",
        "login(os.environ['HUGGINGFACE_TOKEN'])\n"
      ]
    },
    {
      "cell_type": "code",
      "execution_count": null,
      "metadata": {},
      "outputs": [],
      "source": [
        "# Initialize the pipeline with optimizations\n",
        "model_id = 'runwayml/stable-diffusion-v1-5'  # Replace with your custom HF model ID\n",
        "\n",
        "pipe = StableDiffusionPipeline.from_pretrained(\n",
        "    model_id,\n",
        "    torch_dtype=torch.float16,\n",
        "    use_auth_token=True\n",
        ")\n",
        "\n",
        "# Enable optimizations for T4\n",
        "pipe = pipe.to('cuda')\n",
        "pipe.enable_attention_slicing()  # Reduces memory usage\n",
        "pipe.enable_model_cpu_offload()  # Offloads model to CPU when not in use\n",
        "\n",
        "# Optional: Enable xformers for faster inference\n",
        "try:\n",
        "    pipe.enable_xformers_memory_efficient_attention()\n",
        "except:\n",
        "    print('xformers not supported, proceeding without it.')\n"
      ]
    },
    {
      "cell_type": "code",
      "execution_count": null,
      "metadata": {},
      "outputs": [],
      "source": [
        "# Define generation parameters\n",
        "prompt = 'A serene mountain landscape at sunset, vibrant colors, highly detailed'\n",
        "negative_prompt = 'blurry, low quality, artifacts, text, watermark'\n",
        "num_inference_steps = 30  # Lower steps for faster generation\n",
        "guidance_scale = 7.5\n",
        "\n",
        "# Generate image\n",
        "image = pipe(\n",
        "    prompt,\n",
        "    negative_prompt=negative_prompt,\n",
        "    num_inference_steps=num_inference_steps,\n",
        "    guidance_scale=guidance_scale,\n",
        "    height=512,\n",
        "    width=512\n",
        ").images[0]\n",
        "\n",
        "# Save and display image\n",
        "image.save('generated_image.png')\n",
        "image\n"
      ]
    },
    {
      "cell_type": "markdown",
      "metadata": {},
      "source": [
        "## Notes\n",
        "- Replace `'your_hf_token_here'` with your Hugging Face token.\n",
        "- Replace `'runwayml/stable-diffusion-v1-5'` with your custom model ID from Hugging Face.\n",
        "- Adjust `prompt`, `negative_prompt`, `num_inference_steps`, and `guidance_scale` as needed.\n",
        "- The script uses FP16 and attention slicing to minimize RAM usage.\n",
        "- Model CPU offloading reduces VRAM requirements, ideal for T4 GPUs.\n"
      ]
    }
  ],
  "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.8.10"
    }
  },
  "nbformat": 4,
  "nbformat_minor": 4
}