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|
+
{
|
| 2 |
+
"cells": [
|
| 3 |
+
{
|
| 4 |
+
"cell_type": "markdown",
|
| 5 |
+
"metadata": {
|
| 6 |
+
"id": "a58c955f"
|
| 7 |
+
},
|
| 8 |
+
"source": [
|
| 9 |
+
"# SDXL Text Embedding Generator\n",
|
| 10 |
+
"This notebook mounts Google Drive, loads CLIP-L and CLIP-G using your Hugging Face token, and generates dual text embeddings formatted for SDXL models saved to Drive as `.safetensors` files."
|
| 11 |
+
]
|
| 12 |
+
},
|
| 13 |
+
{
|
| 14 |
+
"cell_type": "code",
|
| 15 |
+
"source": [
|
| 16 |
+
"#@title SDXL Text Embedding Generator (TI format)\n",
|
| 17 |
+
"# Mount Google Drive & Install\n",
|
| 18 |
+
"from google.colab import drive\n",
|
| 19 |
+
"import os\n",
|
| 20 |
+
"drive.mount('/content/drive')\n",
|
| 21 |
+
"\n",
|
| 22 |
+
"!pip install -q transformers huggingface_hub safetensors torch\n",
|
| 23 |
+
"\n",
|
| 24 |
+
"#@title Load Models\n",
|
| 25 |
+
"import torch\n",
|
| 26 |
+
"from google.colab import userdata\n",
|
| 27 |
+
"from huggingface_hub import login\n",
|
| 28 |
+
"from transformers import CLIPTextModel, CLIPTokenizer, CLIPTextModelWithProjection\n",
|
| 29 |
+
"from safetensors.torch import save_file\n",
|
| 30 |
+
"\n",
|
| 31 |
+
"# Login (optional but recommended)\n",
|
| 32 |
+
"try:\n",
|
| 33 |
+
" hf_token = userdata.get('HF_TOKEN')\n",
|
| 34 |
+
" login(token=hf_token)\n",
|
| 35 |
+
"except Exception as e:\n",
|
| 36 |
+
" print(f\"HF token warning: {e}\")\n",
|
| 37 |
+
"\n",
|
| 38 |
+
"device = \"cuda\" if torch.cuda.is_available() else \"cpu\"\n",
|
| 39 |
+
"print(f\"Using device: {device}\")\n",
|
| 40 |
+
"\n",
|
| 41 |
+
"# Correct model IDs\n",
|
| 42 |
+
"clip_l_id = \"openai/clip-vit-large-patch14\"\n",
|
| 43 |
+
"clip_g_id = \"laion/CLIP-ViT-bigG-14-laion2B-39B-b160k\"\n",
|
| 44 |
+
"\n",
|
| 45 |
+
"print(\"Loading CLIP-L...\")\n",
|
| 46 |
+
"tokenizer_l = CLIPTokenizer.from_pretrained(clip_l_id)\n",
|
| 47 |
+
"text_encoder_l = CLIPTextModel.from_pretrained(clip_l_id).to(device)\n",
|
| 48 |
+
"text_encoder_l.eval()\n",
|
| 49 |
+
"\n",
|
| 50 |
+
"print(\"Loading CLIP-G...\")\n",
|
| 51 |
+
"tokenizer_g = CLIPTokenizer.from_pretrained(clip_g_id)\n",
|
| 52 |
+
"text_encoder_g = CLIPTextModelWithProjection.from_pretrained(clip_g_id).to(device)\n",
|
| 53 |
+
"text_encoder_g.eval()\n",
|
| 54 |
+
"\n",
|
| 55 |
+
"print(\"Models loaded.\")"
|
| 56 |
+
],
|
| 57 |
+
"metadata": {
|
| 58 |
+
"id": "3L8XEw6RoRvF"
|
| 59 |
+
},
|
| 60 |
+
"execution_count": null,
|
| 61 |
+
"outputs": []
|
| 62 |
+
},
|
| 63 |
+
{
|
| 64 |
+
"cell_type": "code",
|
| 65 |
+
"source": [
|
| 66 |
+
"#@title Generate Embedding\n",
|
| 67 |
+
"#@markdown ### Settings\n",
|
| 68 |
+
"text_to_encode = \"A cinematic shot of a majestic castle on a mountain\" #@param {type:\"string\"}\n",
|
| 69 |
+
"embedding_file_name = \"castle_embedding\" #@param {type:\"string\"}\n",
|
| 70 |
+
"keep_special_tokens = False # Keep false. This setting minimizes the embedding sized. If true , the embedding is 77 tokens long , leaving no room for the user to prompt anything with it inside a 77 token batch encoding. Keep in mind , a 77 token embedding will still sort of work , as the prompt will extend to 154 tokens and take the average of the two 77 token batch encodings\n",
|
| 71 |
+
"\n",
|
| 72 |
+
"# Tokenize\n",
|
| 73 |
+
"inputs_l = tokenizer_l(\n",
|
| 74 |
+
" [text_to_encode],\n",
|
| 75 |
+
" padding=\"max_length\",\n",
|
| 76 |
+
" max_length=77,\n",
|
| 77 |
+
" truncation=True,\n",
|
| 78 |
+
" return_tensors=\"pt\"\n",
|
| 79 |
+
").to(device)\n",
|
| 80 |
+
"\n",
|
| 81 |
+
"inputs_g = tokenizer_g(\n",
|
| 82 |
+
" [text_to_encode],\n",
|
| 83 |
+
" padding=\"max_length\",\n",
|
| 84 |
+
" max_length=77,\n",
|
| 85 |
+
" truncation=True,\n",
|
| 86 |
+
" return_tensors=\"pt\"\n",
|
| 87 |
+
").to(device)\n",
|
| 88 |
+
"\n",
|
| 89 |
+
"with torch.no_grad():\n",
|
| 90 |
+
" # Get INPUT embeddings (token embedding layer) – this is what TI files use\n",
|
| 91 |
+
" emb_l = text_encoder_l.get_input_embeddings()(inputs_l.input_ids) # [1, 77, 768]\n",
|
| 92 |
+
" emb_g = text_encoder_g.get_input_embeddings()(inputs_g.input_ids) # [1, 77, 1280]\n",
|
| 93 |
+
"\n",
|
| 94 |
+
"# Optional: strip special tokens (BOS, EOS, padding)\n",
|
| 95 |
+
"if not keep_special_tokens:\n",
|
| 96 |
+
" # Find actual content tokens (non-pad)\n",
|
| 97 |
+
" # CLIP pad token is usually 0\n",
|
| 98 |
+
" mask_l = inputs_l.input_ids[0] != tokenizer_l.pad_token_id\n",
|
| 99 |
+
" mask_g = inputs_g.input_ids[0] != tokenizer_g.pad_token_id\n",
|
| 100 |
+
"\n",
|
| 101 |
+
" # Also drop BOS (usually first token) and EOS if present\n",
|
| 102 |
+
" # Safer approach: keep everything that is not pad\n",
|
| 103 |
+
" emb_l = emb_l[0, mask_l] # [N, 768]\n",
|
| 104 |
+
" emb_g = emb_g[0, mask_g] # [N, 1280]\n",
|
| 105 |
+
"else:\n",
|
| 106 |
+
" emb_l = emb_l.squeeze(0) # [77, 768]\n",
|
| 107 |
+
" emb_g = emb_g.squeeze(0) # [77, 1280]\n",
|
| 108 |
+
"\n",
|
| 109 |
+
"print(f\"CLIP-L shape: {emb_l.shape}\")\n",
|
| 110 |
+
"print(f\"CLIP-G shape: {emb_g.shape}\")\n",
|
| 111 |
+
"\n",
|
| 112 |
+
"# Save\n",
|
| 113 |
+
"save_dir = \"/content/drive/MyDrive/SDXL_Embeddings\"\n",
|
| 114 |
+
"os.makedirs(save_dir, exist_ok=True)\n",
|
| 115 |
+
"save_path = os.path.join(save_dir, f\"{embedding_file_name}.safetensors\")\n",
|
| 116 |
+
"\n",
|
| 117 |
+
"tensors_to_save = {\n",
|
| 118 |
+
" \"clip_l\": emb_l.cpu().contiguous(),\n",
|
| 119 |
+
" \"clip_g\": emb_g.cpu().contiguous()\n",
|
| 120 |
+
"}\n",
|
| 121 |
+
"\n",
|
| 122 |
+
"save_file(tensors_to_save, save_path)\n",
|
| 123 |
+
"print(f\"\\nSuccessfully saved: {save_path}\")\n",
|
| 124 |
+
"print(\"You can now use it as: embedding:castle_embedding\")"
|
| 125 |
+
],
|
| 126 |
+
"metadata": {
|
| 127 |
+
"id": "XquwZtTIow0p"
|
| 128 |
+
},
|
| 129 |
+
"execution_count": null,
|
| 130 |
+
"outputs": []
|
| 131 |
+
}
|
| 132 |
+
],
|
| 133 |
+
"metadata": {
|
| 134 |
+
"colab": {
|
| 135 |
+
"provenance": [],
|
| 136 |
+
"gpuType": "T4"
|
| 137 |
+
},
|
| 138 |
+
"kernelspec": {
|
| 139 |
+
"display_name": "Python 3",
|
| 140 |
+
"name": "python3"
|
| 141 |
+
},
|
| 142 |
+
"language_info": {
|
| 143 |
+
"name": "python"
|
| 144 |
+
},
|
| 145 |
+
"accelerator": "GPU"
|
| 146 |
+
},
|
| 147 |
+
"nbformat": 4,
|
| 148 |
+
"nbformat_minor": 0
|
| 149 |
+
}
|