[{"data":1,"prerenderedAt":-1},["ShallowReactive",2],{"similar-damian0815--compel":3,"tool-damian0815--compel":61},[4,18,26,36,44,53],{"id":5,"name":6,"github_repo":7,"description_zh":8,"stars":9,"difficulty_score":10,"last_commit_at":11,"category_tags":12,"status":17},4358,"openclaw","openclaw\u002Fopenclaw","OpenClaw 是一款专为个人打造的本地化 AI 助手，旨在让你在自己的设备上拥有完全可控的智能伙伴。它打破了传统 AI 助手局限于特定网页或应用的束缚，能够直接接入你日常使用的各类通讯渠道，包括微信、WhatsApp、Telegram、Discord、iMessage 等数十种平台。无论你在哪个聊天软件中发送消息，OpenClaw 都能即时响应，甚至支持在 macOS、iOS 和 Android 设备上进行语音交互，并提供实时的画布渲染功能供你操控。\n\n这款工具主要解决了用户对数据隐私、响应速度以及“始终在线”体验的需求。通过将 AI 部署在本地，用户无需依赖云端服务即可享受快速、私密的智能辅助，真正实现了“你的数据，你做主”。其独特的技术亮点在于强大的网关架构，将控制平面与核心助手分离，确保跨平台通信的流畅性与扩展性。\n\nOpenClaw 非常适合希望构建个性化工作流的技术爱好者、开发者，以及注重隐私保护且不愿被单一生态绑定的普通用户。只要具备基础的终端操作能力（支持 macOS、Linux 及 Windows WSL2），即可通过简单的命令行引导完成部署。如果你渴望拥有一个懂你",349277,3,"2026-04-06T06:32:30",[13,14,15,16],"Agent","开发框架","图像","数据工具","ready",{"id":19,"name":20,"github_repo":21,"description_zh":22,"stars":23,"difficulty_score":10,"last_commit_at":24,"category_tags":25,"status":17},3808,"stable-diffusion-webui","AUTOMATIC1111\u002Fstable-diffusion-webui","stable-diffusion-webui 是一个基于 Gradio 构建的网页版操作界面，旨在让用户能够轻松地在本地运行和使用强大的 Stable Diffusion 图像生成模型。它解决了原始模型依赖命令行、操作门槛高且功能分散的痛点，将复杂的 AI 绘图流程整合进一个直观易用的图形化平台。\n\n无论是希望快速上手的普通创作者、需要精细控制画面细节的设计师，还是想要深入探索模型潜力的开发者与研究人员，都能从中获益。其核心亮点在于极高的功能丰富度：不仅支持文生图、图生图、局部重绘（Inpainting）和外绘（Outpainting）等基础模式，还独创了注意力机制调整、提示词矩阵、负向提示词以及“高清修复”等高级功能。此外，它内置了 GFPGAN 和 CodeFormer 等人脸修复工具，支持多种神经网络放大算法，并允许用户通过插件系统无限扩展能力。即使是显存有限的设备，stable-diffusion-webui 也提供了相应的优化选项，让高质量的 AI 艺术创作变得触手可及。",162132,"2026-04-05T11:01:52",[14,15,13],{"id":27,"name":28,"github_repo":29,"description_zh":30,"stars":31,"difficulty_score":32,"last_commit_at":33,"category_tags":34,"status":17},1381,"everything-claude-code","affaan-m\u002Feverything-claude-code","everything-claude-code 是一套专为 AI 编程助手（如 Claude Code、Codex、Cursor 等）打造的高性能优化系统。它不仅仅是一组配置文件，而是一个经过长期实战打磨的完整框架，旨在解决 AI 代理在实际开发中面临的效率低下、记忆丢失、安全隐患及缺乏持续学习能力等核心痛点。\n\n通过引入技能模块化、直觉增强、记忆持久化机制以及内置的安全扫描功能，everything-claude-code 能显著提升 AI 在复杂任务中的表现，帮助开发者构建更稳定、更智能的生产级 AI 代理。其独特的“研究优先”开发理念和针对 Token 消耗的优化策略，使得模型响应更快、成本更低，同时有效防御潜在的攻击向量。\n\n这套工具特别适合软件开发者、AI 研究人员以及希望深度定制 AI 工作流的技术团队使用。无论您是在构建大型代码库，还是需要 AI 协助进行安全审计与自动化测试，everything-claude-code 都能提供强大的底层支持。作为一个曾荣获 Anthropic 黑客大奖的开源项目，它融合了多语言支持与丰富的实战钩子（hooks），让 AI 真正成长为懂上",147882,2,"2026-04-09T11:32:47",[14,13,35],"语言模型",{"id":37,"name":38,"github_repo":39,"description_zh":40,"stars":41,"difficulty_score":32,"last_commit_at":42,"category_tags":43,"status":17},2271,"ComfyUI","Comfy-Org\u002FComfyUI","ComfyUI 是一款功能强大且高度模块化的视觉 AI 引擎，专为设计和执行复杂的 Stable Diffusion 图像生成流程而打造。它摒弃了传统的代码编写模式，采用直观的节点式流程图界面，让用户通过连接不同的功能模块即可构建个性化的生成管线。\n\n这一设计巧妙解决了高级 AI 绘图工作流配置复杂、灵活性不足的痛点。用户无需具备编程背景，也能自由组合模型、调整参数并实时预览效果，轻松实现从基础文生图到多步骤高清修复等各类复杂任务。ComfyUI 拥有极佳的兼容性，不仅支持 Windows、macOS 和 Linux 全平台，还广泛适配 NVIDIA、AMD、Intel 及苹果 Silicon 等多种硬件架构，并率先支持 SDXL、Flux、SD3 等前沿模型。\n\n无论是希望深入探索算法潜力的研究人员和开发者，还是追求极致创作自由度的设计师与资深 AI 绘画爱好者，ComfyUI 都能提供强大的支持。其独特的模块化架构允许社区不断扩展新功能，使其成为当前最灵活、生态最丰富的开源扩散模型工具之一，帮助用户将创意高效转化为现实。",108111,"2026-04-08T11:23:26",[14,15,13],{"id":45,"name":46,"github_repo":47,"description_zh":48,"stars":49,"difficulty_score":32,"last_commit_at":50,"category_tags":51,"status":17},4721,"markitdown","microsoft\u002Fmarkitdown","MarkItDown 是一款由微软 AutoGen 团队打造的轻量级 Python 工具，专为将各类文件高效转换为 Markdown 格式而设计。它支持 PDF、Word、Excel、PPT、图片（含 OCR）、音频（含语音转录）、HTML 乃至 YouTube 链接等多种格式的解析，能够精准提取文档中的标题、列表、表格和链接等关键结构信息。\n\n在人工智能应用日益普及的今天，大语言模型（LLM）虽擅长处理文本，却难以直接读取复杂的二进制办公文档。MarkItDown 恰好解决了这一痛点，它将非结构化或半结构化的文件转化为模型“原生理解”且 Token 效率极高的 Markdown 格式，成为连接本地文件与 AI 分析 pipeline 的理想桥梁。此外，它还提供了 MCP（模型上下文协议）服务器，可无缝集成到 Claude Desktop 等 LLM 应用中。\n\n这款工具特别适合开发者、数据科学家及 AI 研究人员使用，尤其是那些需要构建文档检索增强生成（RAG）系统、进行批量文本分析或希望让 AI 助手直接“阅读”本地文件的用户。虽然生成的内容也具备一定可读性，但其核心优势在于为机器",93400,"2026-04-06T19:52:38",[52,14],"插件",{"id":54,"name":55,"github_repo":56,"description_zh":57,"stars":58,"difficulty_score":10,"last_commit_at":59,"category_tags":60,"status":17},4487,"LLMs-from-scratch","rasbt\u002FLLMs-from-scratch","LLMs-from-scratch 是一个基于 PyTorch 的开源教育项目，旨在引导用户从零开始一步步构建一个类似 ChatGPT 的大型语言模型（LLM）。它不仅是同名技术著作的官方代码库，更提供了一套完整的实践方案，涵盖模型开发、预训练及微调的全过程。\n\n该项目主要解决了大模型领域“黑盒化”的学习痛点。许多开发者虽能调用现成模型，却难以深入理解其内部架构与训练机制。通过亲手编写每一行核心代码，用户能够透彻掌握 Transformer 架构、注意力机制等关键原理，从而真正理解大模型是如何“思考”的。此外，项目还包含了加载大型预训练权重进行微调的代码，帮助用户将理论知识延伸至实际应用。\n\nLLMs-from-scratch 特别适合希望深入底层原理的 AI 开发者、研究人员以及计算机专业的学生。对于不满足于仅使用 API，而是渴望探究模型构建细节的技术人员而言，这是极佳的学习资源。其独特的技术亮点在于“循序渐进”的教学设计：将复杂的系统工程拆解为清晰的步骤，配合详细的图表与示例，让构建一个虽小但功能完备的大模型变得触手可及。无论你是想夯实理论基础，还是为未来研发更大规模的模型做准备",90106,"2026-04-06T11:19:32",[35,15,13,14],{"id":62,"github_repo":63,"name":64,"description_en":65,"description_zh":66,"ai_summary_zh":66,"readme_en":67,"readme_zh":68,"quickstart_zh":69,"use_case_zh":70,"hero_image_url":71,"owner_login":72,"owner_name":73,"owner_avatar_url":74,"owner_bio":75,"owner_company":76,"owner_location":77,"owner_email":76,"owner_twitter":76,"owner_website":78,"owner_url":79,"languages":80,"stars":89,"forks":90,"last_commit_at":91,"license":92,"difficulty_score":32,"env_os":93,"env_gpu":94,"env_ram":95,"env_deps":96,"category_tags":103,"github_topics":76,"view_count":32,"oss_zip_url":76,"oss_zip_packed_at":76,"status":17,"created_at":104,"updated_at":105,"faqs":106,"releases":136},5875,"damian0815\u002Fcompel","compel","A prompting enhancement library for transformers-type text embedding systems","Compel 是一款专为基于 Transformer 架构的文本嵌入系统（如 Stable Diffusion）设计的提示词增强库。它主要解决了用户在生成式 AI 创作中难以精准控制画面细节的痛点：传统提示词往往对所有词汇一视同仁，而 Compel 允许用户通过简单直观的语法（例如在词语后添加\"+\"号），灵活调整提示句中特定部分的权重。这意味着你可以轻松强调\"ball++\"让球体更突出，或弱化背景干扰，从而更精确地引导图像生成结果。\n\n该工具特别适合开发者、AI 研究人员以及希望深入掌控生成效果的高级创作者使用。虽然普通用户也能受益，但其核心价值在于为需要编程集成或批量处理提示词的场景提供技术支持。Compel 的技术亮点在于其高度兼容性，不仅完美适配 Hugging Face Diffusers 库中的 Stable Diffusion 及 SDXL 模型，还支持正负提示词的加权处理与批量生成。它源自 InvokeAI 项目的核心代码，经过优化后提供了更灵活的 API，让用户无需修改底层扩散循环，即可通过简单的 Python 调用实现复杂的注意力控制，显著提升创作效率与成品质量。","# Compel\nA text prompt weighting and blending library for transformers-type text embedding systems, by [@damian0815](https:\u002F\u002Fgithub.com\u002Fdamian0815).\n\nWith a flexible and intuitive syntax, you can re-weight different parts of a prompt string and thus re-weight the different parts of the embedding tensor produced from the string.\n\nTested and developed against Hugging Face's `StableDiffusionPipeline` but it should work with any diffusers-based system that uses an `Tokenizer` and a `Text Encoder` of some kind.  \n\nAdapted from the [InvokeAI](https:\u002F\u002Fgithub.com\u002Finvoke-ai) prompting code (also by [@damian0815](https:\u002F\u002Fgithub.com\u002Fdamian0815)).\n\nNote that cross-attention control `.swap()` is currently ignored by Compel, but you can use it by calling `build_conditioning_tensor_for_prompt_object()` yourself, and implementing cross-attention control in your diffusion loop.\n\n## Installation\n\n`pip install compel`\n\n## Documentation\n\nDocumentation is [here](doc\u002F).\n\n## Demo\n\nSee [compel-demo.ipynb](compel-demo.ipynb)\n\n\u003Ca target=\"_blank\" href=\"https:\u002F\u002Fcolab.research.google.com\u002Fgithub\u002Fdamian0815\u002Fcompel\u002Fblob\u002Fmain\u002Fcompel-demo.ipynb\">\n  \u003Cimg src=\"https:\u002F\u002Fcolab.research.google.com\u002Fassets\u002Fcolab-badge.svg\" alt=\"Open In Colab\"\u002F>\n\u003C\u002Fa>\n\n## Quickstart\n\nWith Hugging Face diffusers >=0.12:\n\n```python\nfrom diffusers import StableDiffusionPipeline\nfrom compel import CompelForSD\n\npipeline = StableDiffusionPipeline.from_pretrained(\"runwayml\u002Fstable-diffusion-v1-5\")\ncompel = CompelForSD(pipeline)\n\n# upweight \"ball\"\nprompt = \"a cat playing with a ball++ in the forest\"\nconditioning = compel(prompt)\n\n# generate image\nimages = pipeline(prompt_embeds=conditioning.embeds, num_inference_steps=20).images\nimages[0].save(\"image.jpg\")\n```\n\n#### With negative prompt:\n\n```python\nfrom diffusers import StableDiffusionPipeline\nfrom compel import CompelForSD\n\npipeline = StableDiffusionPipeline.from_pretrained(\"runwayml\u002Fstable-diffusion-v1-5\")\ncompel = CompelForSD(pipeline)\n\n# upweight \"ball\"\nprompt = \"a cat playing with a ball++ in the forest\"\nnegative_prompt = \"blurry, low quality, deformed\"\nconditioning = compel(prompt, negative_prompt=negative_prompt)\n\n# generate image\nimages = pipeline(prompt_embeds=conditioning.embeds, negative_prompt_embed=conditioning.negative_embeds, num_inference_steps=20).images\nimages[0].save(\"image.jpg\")\n```\n\n#### Batched:\n\n```python\nfrom diffusers import StableDiffusionPipeline\nfrom compel import CompelForSD\n\npipeline = StableDiffusionPipeline.from_pretrained(\"runwayml\u002Fstable-diffusion-v1-5\")\ncompel = CompelForSD(pipeline)\n\nprompt = [\"a cat playing with a ball++ in the forest\", \"a dog wearing a hat++\"]\nnegative_prompt = [\"blurry, low quality, deformed\", \"painting\"] \n# or: negative_prompt = \"blurry, low quality, deformed\" # shared across all generated images\nconditioning = compel(prompt, negative_prompt=negative_prompt)\n\n# generate image\nimages = pipeline(prompt_embeds=conditioning.embeds, negative_prompt_embed=conditioning.negative_embeds, num_inference_steps=20).images\nimages[0].save(\"image.jpg\")\n```\n\n### SDXL:\n\n```python\nfrom diffusers import DiffusionPipeline\nfrom compel import CompelForSDXL\nimport torch\n\ndevice = 'cuda'\npipeline = DiffusionPipeline.from_pretrained(\"stabilityai\u002Fstable-diffusion-xl-base-1.0\", variant=\"fp16\", \n                                             use_safetensors=True, torch_dtype=torch.float16).to(device)\n\nprompt = \"a cat playing with a ball++ in the forest\"\nnegative_prompt = \"low quality, blurry\"\ncompel = CompelForSDXL(pipeline)\nconditioning = compel(prompt, negative_prompt=negative_prompt)\n\ngenerator = torch.Generator().manual_seed(42)\nimage = pipeline(prompt_embeds=conditioning.embeds, \n                 pooled_prompt_embeds=conditioning.pooled_embeds,\n                 negative_prompt_embeds=conditioning.negative_embeds,\n                 negative_pooled_prompt_embeds=conditioning.negative_pooled_embeds,\n                 num_inference_steps=25, width=1024, height=1024, generator=generator).images[0]\nimage.save('sdxl_image.jpg')\n```\n\n### Flux\n\n```python\nfrom diffusers import FluxPipeline\nfrom compel import CompelForFlux\nimport torch\n\ndevice = \"mps\"\npipe = FluxPipeline.from_pretrained(\"black-forest-labs\u002FFLUX.1-schnell\", torch_dtype=torch.float32).to(device) # bfloat16 causes NaN on MPS\ncompel = CompelForFlux(pipe)\n\nprompt = \"Astronaut---- in a jungle++++, cold color palette, muted colors, detailed, 8k\"\n\nconditioning = compel(prompt)\ngenerator = torch.Generator().manual_seed(42)\nimages = pipe(prompt_embeds=conditioning.embeds, pooled_prompt_embeds=conditioning.pooled_embeds,\n             num_inference_steps=4, width=512, height=512, generator=generator)\n```\n\n### Style prompt\n\nWorks with SDXL or Flux:\n\n```python\nfrom diffusers import DiffusionPipeline\nfrom compel import CompelForSDXL\nimport torch\n\ndevice = 'cuda'\npipeline = DiffusionPipeline.from_pretrained(\"stabilityai\u002Fstable-diffusion-xl-base-1.0\", variant=\"fp16\", \n                                             use_safetensors=True, torch_dtype=torch.float16).to(device)\n\nprompt = \"a cat playing with a ball++ in the forest\"\nstyle_prompt = \"painting by van gogh, impasto, thick brush strokes\"\nnegative_prompt = \"low quality, blurry\"\nnegative_style_prompt = \"photography\"\ncompel = CompelForSDXL(pipeline) # or CompelForFlux\nconditioning = compel(prompt=prompt, negative_prompt=negative_prompt, \n                      style_prompt=style_prompt, negative_style_prompt=negative_style_prompt)\n\ngenerator = torch.Generator().manual_seed(42)\nimage = pipeline(prompt_embeds=conditioning.embeds, \n                 pooled_prompt_embeds=conditioning.pooled_embeds,\n                 negative_prompt_embeds=conditioning.negative_embeds,\n                 negative_pooled_prompt_embeds=conditioning.negative_pooled_embeds,\n                 num_inference_steps=25, width=1024, height=1024, generator=generator).images[0]\nimage.save('sdxl_image.jpg')\n```\n\n## Textual Inversion support\n\nIf you want to have access to 🤗diffusers textual inversions, instantiate a `DiffusersTextualInversionManager` and pass it on Compel init:\n\n```python\nfrom diffusers import StableDiffusionPipeline\nfrom compel import CompelForSD, DiffusersTextualInversionManager\npipeline = StableDiffusionPipeline.from_pretrained(\"runwayml\u002Fstable-diffusion-v1-5\")\ntextual_inversion_manager = DiffusersTextualInversionManager(pipeline)\ncompel = CompelForSD(pipe=pipeline, textual_inversion_manager=textual_inversion_manager)\n```\n\n## Memory usage\u002FVRAM leaks\n\nIf you run into memory issues, please make sure you're running compel inside `with torch.no_grad():` blocks. \n\nIf this doesn't help, you could try this advice offered by @kshieh1: \n> After image generation, you should explictly de-reference the tensor object (i.e., prompt_embeds = None) and call gc.collect()\n\nSee https:\u002F\u002Fgithub.com\u002Fdamian0815\u002Fcompel\u002Fissues\u002F24 for more details. Thanks @kshieh1 !\n\n## Changelog\n\n#### 2.3.1 - Fix for 78 tokens \u002F 77 tokens issue with SDXL; add `device` arg to `CompelFor*` constructors (thanks @dx2-66)\n\n### 2.3.0 - Tokenization info, SplitLongTextMode CLS token handling, negative\u002Fstyle bugfixes\n\n* `CompelFor*` objects now return tokenization info via `conditioning.tokenization_info` dict, which contains keys for `main_positive` and (where appropriate) `main_negative`, `style_positive` and `style_negative`.\n* `SplitLongTextMode` enum has additional options for handling the CLS (EOS) token - either the CLS token can be copied from the first chunk to subsequent chunks, or all CLS tokens can be merged and the merge duplicated on all chunks. See `SplitLongTextMode` enum for details.\n* Misc edge case fixes for handling different combinations of style prompts and negative prompts with SDXL\u002FFlux\n\n#### 2.2.1 - fix for #116\n\n### 2.2.0 - Flux support, usability improvement\n\n* Flux is now supported\n* Added `CompelForSD`, `CompelForFlux` and `CompelForSDXL` to simplify and streamline embeds vs pooled_embeds, negative prompts, and style prompts handling. See compel-demo-*.py\u002F.ipynb for usage examples.\n* Performance improvements when working with non-weighted prompts. If a prompt is passed with no weighting, the weighting logic is completely bypassed. **This may slightly change the outputs when no weighting is applied** - revert to the old slower behaviour by calling `compel.disable_no_weights_bypass()`.\n* When working with `CompelForSD` and `CompelForSDXL` classes, non-truncation is now the default.\n\n#### 2.1.1 - expose `split_long_text_mode` to top-level Compel, default to `SENTENCES`, fix bug where splitting would fail sometimes.\n\n### 2.1.0 - add `split_long_text_mode` arg to control word splitting for long prompts when `truncate_long_prompts` is False. See `SplitLongTextMode`. Defaults to `WORDS`. \n\n#### 2.0.3 - include contributed fixes #64, #80 and fix license in pyproject.toml\u002Fpypi\n\n#### 2.0.2 - fix for `pipeline.enable_sequential_cpu_offloading()` with SDXL models (you need to pass `device='cuda'` on compel init)\n\n#### 2.0.1 - fix for [#45](https:\u002F\u002Fgithub.com\u002Fdamian0815\u002Fcompel\u002Fissues\u002F45) padding issue with SDXL non-truncated prompts and `.and()` \n\n### 2.0.0 - SDXL Support\n\nWith big thanks to Patrick von Platen from Hugging Face for [the pull request](https:\u002F\u002Fgithub.com\u002Fdamian0815\u002Fcompel\u002Fpull\u002F41), Compel now supports SDXL. Use it like this: \n\n```py\nfrom compel import Compel, ReturnedEmbeddingsType\nfrom diffusers import DiffusionPipeline\nimport torch\n\npipeline = DiffusionPipeline.from_pretrained(\"stabilityai\u002Fstable-diffusion-xl-base-1.0\", variant=\"fp16\", use_safetensors=True, torch_dtype=torch.float16).to(\"cuda\")\ncompel = Compel(tokenizer=[pipeline.tokenizer, pipeline.tokenizer_2] , text_encoder=[pipeline.text_encoder, pipeline.text_encoder_2], returned_embeddings_type=ReturnedEmbeddingsType.PENULTIMATE_HIDDEN_STATES_NON_NORMALIZED, requires_pooled=[False, True])\n# upweight \"ball\"\nprompt = \"a cat playing with a ball++ in the forest\"\nconditioning, pooled = compel(prompt)\n# generate image\nimage = pipeline(prompt_embeds=conditioning, pooled_prompt_embeds=pooled, num_inference_steps=30).images[0]\n```\n\nPlease note that this is a **breaking change** if you've been using clip skip: the old boolean arg `use_penultimate_clip_layer` has been replaced with an enum `ReturnedEmbeddingsType.PENULTIMATE_HIDDEN_STATES_NORMALIZED`.\n\n\n#### 1.2.1 - actually apply `.and()` weights\n\n### 1.2.0 - Concatenate embeddings using `.and()`\n\nFor Stable Diffusion 2.1 I've been experimenting with a new feature: concatenated embeddings. What I noticed, for example, is that for more complex prompts image generation quality becomes wildly better when the prompt is broken into multiple parts and fed to OpenCLIP separately.\n\nTL;DR: you can now experiment with breaking up your prompts into segments, which for SD2.1 appears to improve the generated image. The syntax is `(\"prompt part 1\", \"prompt part 2\").and()`. You can have more than one part, and you can also weight them, eg `(\"a man eating an apple\", \"sitting on the roof of a car\", \"high quality, trending on artstation, 8K UHD\").and(1, 0.5, 0.5)` which will assign weight `1` to `man eating an apple` and `0.5` to `sitting on the roof of a car` and `high quality, trending on artstation, 8K UHD`. \n\nHere's a nonsense example from the InvokeAI discord #garbage-bin channel, created by gogurt enjoyer's incredible [nightmare prompt generator](https:\u002F\u002Fhuggingface.co\u002Fcactusfriend\u002Fnightmare-invokeai-prompts):\n\n```\na moist sloppy pindlesackboy sloppy hamblin' bogomadong, Clem Fandango is pissed-off, Wario's Woods in background, making a noise like ga-woink-a\n```\n\nPlugging this straight into SD2.1 we get this, which is really not a good image:\n![](https:\u002F\u002Foss.gittoolsai.com\u002Fimages\u002Fdamian0815_compel_readme_dbcc03928ab3.png)\n\nHowever, if the prompt is broken up into chunks and fed into OpenCLIP separately as four separate prompts, and then concatenated:\n\n```\na moist sloppy pindlesackboy sloppy hamblin' bogomadong\n\nClem Fandango is pissed-off\n\nWario's Woods in background\n\nmaking a noise like ga-woink-a\n```\n\nthen output image with the same seed is *so much* better:\n![](https:\u002F\u002Foss.gittoolsai.com\u002Fimages\u002Fdamian0815_compel_readme_9649a348df81.png)\n\nIn the new `.and()` syntax you would prompt this as follows:\n```\n(\"a moist sloppy pindlesackboy sloppy hamblin' bogomadong\", \"Clem Fandango is pissed-off\", \"Wario's Woods in background\", \"making a noise like ga-woink-a\").and()\n```\n\nThe effect can be more or less subtle. Here for example is \n```\nA dream of a distant galaxy, by Caspar David Friedrich, matte painting, trending on artstation, HQ\n```\n![](https:\u002F\u002Foss.gittoolsai.com\u002Fimages\u002Fdamian0815_compel_readme_44ed29c62531.png)\n\nAnd the same split into two parts:\n```\nA dream of a distant galaxy, by Caspar David Friedrich, matte painting\n\ntrending on artstation, HQ\n```\n![](https:\u002F\u002Foss.gittoolsai.com\u002Fimages\u002Fdamian0815_compel_readme_61639196ef37.png)\n\nThe Compel prompt for this is: \n```\n(\"A dream of a distant galaxy, by Caspar David Friedrich, matte painting\", \"trending on artstation, HQ\").and()\n```\n\n\n\n\n#### 1.1.6 - misc small fixes\n- add `DiffusersTextualInversionManager` (thanks @pdoane)\n- fix batch embedding generation with truncated\u002Fnon-truncated prompt lengths (#18, thanks @abassino)\n- add note about memory leakage (ref #24, thanks @kshieh1) \n- fix incorrect parsing when commas are not followed by whitespace (#34, thanks @moono)\n\n#### 1.1.5 - fix for compel turning numbers into floats for text inside parentheses\n\n#### 1.1.4 - fixes for #23 (sequential offload) and InvokeAI issue #3442 (allow hyphens in LoRA names) \n\n#### 1.1.3 - enable fetching the penultimate CLIP hidden layer (aka \"clip skip\")\n\nTo use, pass `use_penultimate_clip_layer=True` when initializing your `Compel` instance. Note that there's no need to pass this flag for SD2.0\u002FSD2.1 because diffusers already throws away the last hidden layer when loading the SD2.0+ text encoder.\n\n#### 1.1.2 - fix for #21 (crash when parsing long prompts with truncation enabled if there is weighted fragments beyond the truncation boundary)\n\n#### 1.1.1 - fix for #22 (issues parsing `.` characters inside parentheses)\n\n#### 1.1.0 - support for parsing `withLora`\u002F`useLora` on `parse_prompt_string()`.\n\n* `Compel.parse_prompt_string()` now returns a `Conjunction`\n* any appearances of `withLora(name[, weight])` or `useLora(name[, weight])` anywhere in the prompt string will be parsed to `LoraWeight` instances, and returned on the outermost `Conjunction` returned by `parse_prompt_string()`.\n\n#### 1.0.5 - fix incorrect parsing when passing invalid (auto1111) syntax that has a float\n\nalso fix test case for default swap parameters\n\n#### 1.0.4 - fix embeddings for empty swap target (eg `cat.swap(\"\")`) when truncation is disabled \n\n#### 1.0.3 - better defaults for .swap (https:\u002F\u002Fgithub.com\u002Fdamian0815\u002Fcompel\u002Fissues\u002F8)\n\n#### 1.0.2 - fix padding for non-truncated batched embeddings (https:\u002F\u002Fgithub.com\u002Fdamian0815\u002Fcompel\u002Fissues\u002F9)\n\n#### 1.0.1 - fix for InvokeAI's `--free_gpu_mem` option\n\n### 1.0.0 - new downweighting algorithm \n\nDownweighting now works by applying an attention mask to remove the downweighted tokens, rather than literally removing them from the sequence. This behaviour is the default, but the old behaviour can be re-enabled by passing `downweight_mode=DownweightMode.REMOVE` on init of the `Compel` instance.\n\nFormerly, downweighting a token worked by both multiplying the weighting of the token's embedding, and doing an inverse-weighted blend with a copy of the token sequence that had the downweighted tokens removed. The intuition is that as weight approaches zero, the tokens being downweighted should be actually removed from the sequence. However, removing the tokens resulted in the positioning of all downstream tokens becoming messed up. The blend ended up blending a lot more than just the tokens in question. \n\nAs of v1.0.0, taking advice from @keturn and @bonlime (https:\u002F\u002Fgithub.com\u002Fdamian0815\u002Fcompel\u002Fissues\u002F7) the procedure is by default different. Downweighting still involves a blend but what is blended is a version of the token sequence with the downweighted tokens masked out, rather than removed. This correctly preserves positioning embeddings of the other tokens. \n\nAlso a bugfix: fix black images on weight 0 (https:\u002F\u002Fgithub.com\u002Finvoke-ai\u002FInvokeAI\u002Fissues\u002F2832)\n\n### 0.1.10 - add support for prompts longer than the model's max token length. \n\nTo enable, initialize `Compel` with `truncate_long_prompts=False` (default is True). Prompts that are longer than the model's `max_token_length` will be chunked and padded out to an integer multiple of `max_token_length`. \n\nNote that even if you don't use a negative prompt, you'll need to build a conditioning tensor for a negative prompt of at least `\"\"`, and use `compel.pad_conditioning_tensors_to_same_length()`, otherwise the you'll get an error about mismatched conditioning tensor lengths:\n\n```python\ncompel = Compel(..., truncate_long_prompts=False)\nprompt = \"a cat playing with a ball++ in the forest, amazing, exquisite, stunning, masterpiece, skilled, powerful, incredible, amazing, trending on gregstation, greg, greggy, greggs greggson, greggy mcgregface, ...\" # very long prompt\nconditioning = compel.build_conditioning_tensor(prompt)\nnegative_prompt = \"\" # it's necessary to create an empty prompt - it can also be very long, if you want\nnegative_conditioning = compel.build_conditioning_tensor(negative_prompt)\n[conditioning, negative_conditioning] = compel.pad_conditioning_tensors_to_same_length([conditioning, negative_conditioning])\n```\n\n#### 0.1.9 - broken\n\n#### 0.1.8 - downgrade Python min version to 3.7\n\n#### 0.1.7 - InvokeAI compatibility\n\n","# Compel\n由 [@damian0815](https:\u002F\u002Fgithub.com\u002Fdamian0815) 开发的、用于 Transformer 类文本嵌入系统的文本提示加权与混合库。\n\n借助灵活直观的语法，你可以对提示字符串的不同部分进行重新加权，从而调整由此字符串生成的嵌入张量中各部分的权重。\n\n该库已在 Hugging Face 的 `StableDiffusionPipeline` 上进行了测试和开发，但理论上也适用于任何基于 diffusers 并使用某种 `Tokenizer` 和 `Text Encoder` 的系统。\n\n本库改编自 [InvokeAI](https:\u002F\u002Fgithub.com\u002Finvoke-ai) 的提示编写代码（同样由 [@damian0815](https:\u002F\u002Fgithub.com\u002Fdamian0815) 编写）。\n\n需要注意的是，当前 Compel 会忽略交叉注意力控制 `.swap()`，不过你也可以通过自行调用 `build_conditioning_tensor_for_prompt_object()` 方法，并在扩散循环中实现交叉注意力控制来使用它。\n\n## 安装\n\n`pip install compel`\n\n## 文档\n\n文档请见 [这里](doc\u002F)。\n\n## 演示\n\n参见 [compel-demo.ipynb](compel-demo.ipynb)\n\n\u003Ca target=\"_blank\" href=\"https:\u002F\u002Fcolab.research.google.com\u002Fgithub\u002Fdamian0815\u002Fcompel\u002Fblob\u002Fmain\u002Fcompel-demo.ipynb\">\n  \u003Cimg src=\"https:\u002F\u002Fcolab.research.google.com\u002Fassets\u002Fcolab-badge.svg\" alt=\"在 Colab 中打开\"\u002F>\n\u003C\u002Fa>\n\n## 快速入门\n\n使用 Hugging Face diffusers >=0.12：\n\n```python\nfrom diffusers import StableDiffusionPipeline\nfrom compel import CompelForSD\n\npipeline = StableDiffusionPipeline.from_pretrained(\"runwayml\u002Fstable-diffusion-v1-5\")\ncompel = CompelForSD(pipeline)\n\n# 加强“ball”的权重\nprompt = \"a cat playing with a ball++ in the forest\"\nconditioning = compel(prompt)\n\n# 生成图像\nimages = pipeline(prompt_embeds=conditioning.embeds, num_inference_steps=20).images\nimages[0].save(\"image.jpg\")\n```\n\n#### 使用负面提示：\n\n```python\nfrom diffusers import StableDiffusionPipeline\nfrom compel import CompelForSD\n\npipeline = StableDiffusionPipeline.from_pretrained(\"runwayml\u002Fstable-diffusion-v1-5\")\ncompel = CompelForSD(pipeline)\n\n# 加强“ball”的权重\nprompt = \"a cat playing with a ball++ in the forest\"\nnegative_prompt = \"blurry, low quality, deformed\"\nconditioning = compel(prompt, negative_prompt=negative_prompt)\n\n# 生成图像\nimages = pipeline(prompt_embeds=conditioning.embeds, negative_prompt_embed=conditioning.negative_embeds, num_inference_steps=20).images\nimages[0].save(\"image.jpg\")\n```\n\n#### 批量处理：\n\n```python\nfrom diffusers import StableDiffusionPipeline\nfrom compel import CompelForSD\n\npipeline = StableDiffusionPipeline.from_pretrained(\"runwayml\u002Fstable-diffusion-v1-5\")\ncompel = CompelForSD(pipeline)\n\nprompt = [\"a cat playing with a ball++ in the forest\", \"a dog wearing a hat++\"]\nnegative_prompt = [\"blurry, low quality, deformed\", \"painting\"] \n# 或者：negative_prompt = \"blurry, low quality, deformed\" # 共享给所有生成的图像\nconditioning = compel(prompt, negative_prompt=negative_prompt)\n\n# 生成图像\nimages = pipeline(prompt_embeds=conditioning.embeds, negative_prompt_embed=conditioning.negative_embeds, num_inference_steps=20).images\nimages[0].save(\"image.jpg\")\n```\n\n### SDXL：\n\n```python\nfrom diffusers import DiffusionPipeline\nfrom compel import CompelForSDXL\nimport torch\n\ndevice = 'cuda'\npipeline = DiffusionPipeline.from_pretrained(\"stabilityai\u002Fstable-diffusion-xl-base-1.0\", variant=\"fp16\", \n                                             use_safetensors=True, torch_dtype=torch.float16).to(device)\n\nprompt = \"a cat playing with a ball++ in the forest\"\nnegative_prompt = \"low quality, blurry\"\ncompel = CompelForSDXL(pipeline)\nconditioning = compel(prompt, negative_prompt=negative_prompt)\n\ngenerator = torch.Generator().manual_seed(42)\nimage = pipeline(prompt_embeds=conditioning.embeds, \n                 pooled_prompt_embeds=conditioning.pooled_embeds,\n                 negative_prompt_embeds=conditioning.negative_embeds,\n                 negative_pooled_prompt_embeds=conditioning.negative_pooled_embeds,\n                 num_inference_steps=25, width=1024, height=1024, generator=generator).images[0]\nimage.save('sdxl_image.jpg')\n```\n\n### Flux\n\n```python\nfrom diffusers import FluxPipeline\nfrom compel import CompelForFlux\nimport torch\n\ndevice = \"mps\"\npipe = FluxPipeline.from_pretrained(\"black-forest-labs\u002FFLUX.1-schnell\", torch_dtype=torch.float32).to(device) # bfloat16 在 MPS 上会导致 NaN\ncompel = CompelForFlux(pipe)\n\nprompt = \"Astronaut---- in a jungle++++, cold color palette, muted colors, detailed, 8k\"\n\nconditioning = compel(prompt)\ngenerator = torch.Generator().manual_seed(42)\nimages = pipe(prompt_embeds=conditioning.embeds, pooled_prompt_embeds=conditioning.pooled_embeds,\n             num_inference_steps=4, width=512, height=512, generator=generator)\n```\n\n### 风格提示\n\n适用于 SDXL 或 Flux：\n\n```python\nfrom diffusers import DiffusionPipeline\nfrom compel import CompelForSDXL\nimport torch\n\ndevice = 'cuda'\npipeline = DiffusionPipeline.from_pretrained(\"stabilityai\u002Fstable-diffusion-xl-base-1.0\", variant=\"fp16\", \n                                             use_safetensors=True, torch_dtype=torch.float16).to(device)\n\nprompt = \"a cat playing with a ball++ in the forest\"\nstyle_prompt = \"painting by van gogh, impasto, thick brush strokes\"\nnegative_prompt = \"low quality, blurry\"\nnegative_style_prompt = \"photography\"\ncompel = CompelForSDXL(pipeline) # 或 CompelForFlux\nconditioning = compel(prompt=prompt, negative_prompt=negative_prompt, \n                      style_prompt=style_prompt, negative_style_prompt=negative_style_prompt)\n\ngenerator = torch.Generator().manual_seed(42)\nimage = pipeline(prompt_embeds=conditioning.embeds, \n                 pooled_prompt_embeds=conditioning.pooled_embeds,\n                 negative_prompt_embeds=conditioning.negative_embeds,\n                 negative_pooled_prompt_embeds=conditioning.negative_pooled_embeds,\n                 num_inference_steps=25, width=1024, height=1024, generator=generator).images[0]\nimage.save('sdxl_image.jpg')\n```\n\n## 文本反转支持\n\n如果你想使用 🤗diffusers 的文本反转功能，请实例化一个 `DiffusersTextualInversionManager`，并在初始化 Compel 时传入：\n\n```python\nfrom diffusers import StableDiffusionPipeline\nfrom compel import CompelForSD, DiffusersTextualInversionManager\npipeline = StableDiffusionPipeline.from_pretrained(\"runwayml\u002Fstable-diffusion-v1-5\")\ntextual_inversion_manager = DiffusersTextualInversionManager(pipeline)\ncompel = CompelForSD(pipe=pipeline, textual_inversion_manager=textual_inversion_manager)\n```\n\n## 内存使用\u002F显存泄漏\n\n如果遇到内存问题，请确保在 `with torch.no_grad():` 块中运行 Compel。\n\n如果这样仍无法解决问题，可以尝试 @kshieh1 提供的建议：\n> 图像生成后，应显式地解除对张量对象的引用（即 prompt_embeds = None），并调用 gc.collect()。\n\n更多详情请参阅 https:\u002F\u002Fgithub.com\u002Fdamian0815\u002Fcompel\u002Fissues\u002F24。感谢 @kshieh1！\n\n## 更改记录\n\n#### 2.3.1 - 修复 SDXL 中 78 个 token \u002F 77 个 token 的问题；为 `CompelFor*` 构造函数添加 `device` 参数（感谢 @dx2-66）\n\n### 2.3.0 - 分词信息、SplitLongTextMode 中 CLS 令牌处理、负向\u002F风格提示错误修复\n\n* `CompelFor*` 对象现在通过 `conditioning.tokenization_info` 字典返回分词信息，该字典包含 `main_positive` 和（在适用时）`main_negative`、`style_positive` 和 `style_negative` 键。\n* `SplitLongTextMode` 枚举新增了处理 CLS（EOS）令牌的选项——CLS 令牌可以从第一个分块复制到后续分块，或者将所有 CLS 令牌合并后在所有分块上重复。详情请参阅 `SplitLongTextMode` 枚举。\n* 针对 SDXL\u002FFlux 处理不同组合的风格提示和负向提示的一些边缘情况进行了修复。\n\n#### 2.2.1 - 修复 #116 问题\n\n### 2.2.0 - 支持 Flux，提升易用性\n\n* 现已支持 Flux。\n* 新增 `CompelForSD`、`CompelForFlux` 和 `CompelForSDXL`，以简化并优化嵌入与池化嵌入、负向提示以及风格提示的处理。使用示例请参见 `compel-demo-*.py\u002F.ipynb`。\n* 在处理非加权提示时性能有所提升。如果传递的提示未加权重，则会完全跳过加权逻辑。**这可能会略微改变未加权时的输出结果**——可通过调用 `compel.disable_no_weights_bypass()` 恢复旧版较慢的行为。\n* 使用 `CompelForSD` 和 `CompelForSDXL` 类时，非截断现为默认设置。\n\n#### 2.1.1 - 将 `split_long_text_mode` 暴露至顶层 Compel，默认值设为 `SENTENCES`，修复有时分割会失败的 bug。\n\n### 2.1.0 - 添加 `split_long_text_mode` 参数，用于在 `truncate_long_prompts` 为 False 时控制长提示的分词方式。详见 `SplitLongTextMode`。默认值为 `WORDS`。\n\n#### 2.0.3 - 包含贡献的修复 #64、#80，并修复 pyproject.toml\u002Fpypi 中的许可证问题。\n\n#### 2.0.2 - 修复 SDXL 模型配合 `pipeline.enable_sequential_cpu_offloading()` 使用时的问题（需在初始化 Compel 时传入 `device='cuda'`）。\n\n#### 2.0.1 - 修复 [#45](https:\u002F\u002Fgithub.com\u002Fdamian0815\u002Fcompel\u002Fissues\u002F45) 关于 SDXL 非截断提示及 `.and()` 方法的填充问题。\n\n### 2.0.0 - 支持 SDXL\n\n特别感谢 Hugging Face 的 Patrick von Platen 提供的 [拉取请求](https:\u002F\u002Fgithub.com\u002Fdamian0815\u002Fcompel\u002Fpull\u002F41)，Compel 现已支持 SDXL。使用方法如下：\n\n```py\nfrom compel import Compel, ReturnedEmbeddingsType\nfrom diffusers import DiffusionPipeline\nimport torch\n\npipeline = DiffusionPipeline.from_pretrained(\"stabilityai\u002Fstable-diffusion-xl-base-1.0\", variant=\"fp16\", use_safetensors=True, torch_dtype=torch.float16).to(\"cuda\")\ncompel = Compel(tokenizer=[pipeline.tokenizer, pipeline.tokenizer_2] , text_encoder=[pipeline.text_encoder, pipeline.text_encoder_2], returned_embeddings_type=ReturnedEmbeddingsType.PENULTIMATE_HIDDEN_STATES_NON_NORMALIZED, requires_pooled=[False, True])\n# 提升“ball”的权重\nprompt = \"a cat playing with a ball++ in the forest\"\nconditioning, pooled = compel(prompt)\n# 生成图像\nimage = pipeline(prompt_embeds=conditioning, pooled_prompt_embeds=pooled, num_inference_steps=30).images[0]\n```\n\n请注意，如果您一直使用 clip skip 功能，此次更新属于**破坏性变更**：旧的布尔型参数 `use_penultimate_clip_layer` 已被枚举类型 `ReturnedEmbeddingsType.PENULTIMATE_HIDDEN_STATES_NORMALIZED` 所取代。\n\n#### 1.2.1 - 正确应用 `.and()` 权重\n\n### 1.2.0 - 使用 `.and()` 连接嵌入\n\n对于 Stable Diffusion 2.1，我一直在尝试一项新功能：连接嵌入。我发现，例如，当提示词较为复杂时，如果将提示词拆分成多个部分并分别输入到 OpenCLIP 中，生成图像的质量会显著提升。\n\n简而言之：现在你可以尝试将提示词拆分成若干段，这对 SD2.1 来说似乎能改善生成的图像质量。语法是 `(\"prompt part 1\", \"prompt part 2\").and()`。你也可以有超过两个部分，并且可以为它们设置权重，比如 `(\"a man eating an apple\", \"sitting on the roof of a car\", \"high quality, trending on artstation, 8K UHD\").and(1, 0.5, 0.5)`，这会为 `man eating an apple` 分配权重 1，而为 `sitting on the roof of a car` 和 `high quality, trending on artstation, 8K UHD` 各分配权重 0.5。\n\n这里有一个来自 InvokeAI Discord #garbage-bin 频道的无厘头例子，由 gogurt enjoyer 的超赞 [噩梦提示词生成器](https:\u002F\u002Fhuggingface.co\u002Fcactusfriend\u002Fnightmare-invokeai-prompts) 创作：\n\n```\na moist sloppy pindlesackboy sloppy hamblin' bogomadong, Clem Fandango is pissed-off, Wario's Woods in background, making a noise like ga-woink-a\n```\n\n直接将这个提示词输入 SD2.1，得到的结果并不理想：\n![](https:\u002F\u002Foss.gittoolsai.com\u002Fimages\u002Fdamian0815_compel_readme_dbcc03928ab3.png)\n\n然而，如果将提示词拆分成几块，分别作为四个独立的提示词输入 OpenCLIP，然后再进行连接：\n\n```\na moist sloppy pindlesackboy sloppy hamblin' bogomadong\n\nClem Fandango is pissed-off\n\nWario's Woods in background\n\nmaking a noise like ga-woink-a\n```\n\n那么使用相同种子生成的图像就会好很多：\n![](https:\u002F\u002Foss.gittoolsai.com\u002Fimages\u002Fdamian0815_compel_readme_9649a348df81.png)\n\n在新的 `.and()` 语法中，你可以这样写提示词：\n```\n(\"a moist sloppy pindlesackboy sloppy hamblin' bogomadong\", \"Clem Fandango is pissed-off\", \"Wario's Woods in background\", \"making a noise like ga-woink-a\").and()\n```\n\n这种效果有时可能不太明显。例如，以下是一个例子：\n\n```\nA dream of a distant galaxy, by Caspar David Friedrich, matte painting, trending on artstation, HQ\n```\n![](https:\u002F\u002Foss.gittoolsai.com\u002Fimages\u002Fdamian0815_compel_readme_44ed29c62531.png)\n\n而将其拆分为两部分：\n```\nA dream of a distant galaxy, by Caspar David Friedrich, matte painting\n\ntrending on artstation, HQ\n```\n![](https:\u002F\u002Foss.gittoolsai.com\u002Fimages\u002Fdamian0815_compel_readme_61639196ef37.png)\n\n对应的 Compel 提示词是：\n```\n(\"A dream of a distant galaxy, by Caspar David Friedrich, matte painting\", \"trending on artstation, HQ\").and()\n```\n\n\n\n\n#### 1.1.6 - 其他小修复\n- 添加 `DiffusersTextualInversionManager`（感谢 @pdoane）\n- 修复批处理嵌入生成时提示词长度截断或未截断的问题（#18，感谢 @abassino）\n- 增加关于内存泄漏的说明（ref #24，感谢 @kshieh1）\n- 修复逗号后没有空格时解析错误的问题（#34，感谢 @moono）\n\n#### 1.1.5 - 修复 Compel 将括号内的数字转换为浮点数的问题\n\n#### 1.1.4 - 修复 #23（顺序卸载）和 InvokeAI 问题 #3442（允许 LoRA 名称中包含连字符）的相关问题\n\n#### 1.1.3 - 支持获取倒数第二层 CLIP 隐藏层（即“clip skip”）\n\n要使用，请在初始化 `Compel` 实例时传入 `use_penultimate_clip_layer=True`。请注意，对于 SD2.0\u002FSD2.1，无需传递此标志，因为 diffusers 在加载 SD2.0 及以上版本的文本编码器时已经会丢弃最后一层隐藏层。\n\n#### 1.1.2 - 修复 #21（启用截断时解析长提示词，若存在超出截断边界的加权片段，则会导致崩溃）的问题\n\n#### 1.1.1 - 修复 #22（解析括号内 `.` 字符时出现问题）的问题\n\n#### 1.1.0 - 支持在 `parse_prompt_string()` 中解析 `withLora`\u002F`useLora`\n\n* `Compel.parse_prompt_string()` 现在会返回一个 `Conjunction`\n* 提示词字符串中任何出现的 `withLora(name[, weight])` 或 `useLora(name[, weight])` 都会被解析为 `LoraWeight` 实例，并在 `parse_prompt_string()` 返回的最外层 `Conjunction` 中体现出来。\n\n#### 1.0.5 - 修复输入无效（auto1111）语法中的浮点数时的解析错误\n\n同时修复默认交换参数的测试用例\n\n#### 1.0.4 - 修复禁用截断时空交换目标（如 `cat.swap(\"\")`）的嵌入问题\n\n#### 1.0.3 - 改进 .swap 的默认行为（https:\u002F\u002Fgithub.com\u002Fdamian0815\u002Fcompel\u002Fissues\u002F8）\n\n#### 1.0.2 - 修复非截断批量嵌入的填充问题（https:\u002F\u002Fgithub.com\u002Fdamian0815\u002Fcompel\u002Fissues\u002F9）\n\n#### 1.0.1 - 修复 InvokeAI 的 `--free_gpu_mem` 选项问题\n\n### 1.0.0 - 新的降权算法\n\n现在的降权机制是通过应用注意力掩码来移除被降权的标记，而不是直接从序列中删除它们。这是默认行为，但可以通过在初始化 `Compel` 实例时传入 `downweight_mode=DownweightMode.REMOVE` 来恢复旧的行为。\n\n过去，降权一个标记的做法是同时乘以该标记嵌入的权重，并与一个移除了被降权标记的标记序列副本进行反向加权混合。其背后的直觉是：当权重趋近于零时，被降权的标记应该真正从序列中移除。然而，移除这些标记会导致所有后续标记的位置错乱，最终混合的效果远远超出了原本需要处理的标记范围。\n\n自 v1.0.0 起，根据 @keturn 和 @bonlime 的建议（https:\u002F\u002Fgithub.com\u002Fdamian0815\u002Fcompel\u002Fissues\u002F7），默认的处理方式发生了变化。降权仍然涉及混合操作，但现在混合的是一个将被降权标记屏蔽掉的标记序列版本，而不是直接移除它们。这样可以正确地保留其他标记的嵌入位置。\n\n此外还修复了一个 bug：解决了权重为 0 时生成黑色图像的问题（https:\u002F\u002Fgithub.com\u002Finvoke-ai\u002FInvokeAI\u002Fissues\u002F2832）\n\n### 0.1.10 - 添加对超过模型最大标记长度的提示的支持。\n\n要启用此功能，请使用 `truncate_long_prompts=False` 初始化 `Compel`（默认值为 True）。超过模型 `max_token_length` 的提示将被分块，并填充到 `max_token_length` 的整数倍。\n\n请注意，即使您不使用负面提示，也需要为至少包含空字符串 `\"\"` 的负面提示构建条件张量，并调用 `compel.pad_conditioning_tensors_to_same_length()`，否则会因条件张量长度不匹配而报错：\n\n```python\ncompel = Compel(..., truncate_long_prompts=False)\nprompt = \"一只猫在森林里玩球++，惊艳、精致、令人惊叹、杰作、技艺高超、强大、不可思议、精彩、在 gregstation 上热门，格雷格、格雷吉、格雷格森、麦克格雷夫的格雷吉……\" # 非常长的提示\nconditioning = compel.build_conditioning_tensor(prompt)\nnegative_prompt = \"\" # 必须创建一个空提示——如果您愿意，它也可以很长\nnegative_conditioning = compel.build_conditioning_tensor(negative_prompt)\n[conditioning, negative_conditioning] = compel.pad_conditioning_tensors_to_same_length([conditioning, negative_conditioning])\n```\n\n#### 0.1.9 - 已损坏\n\n#### 0.1.8 - 将 Python 最低版本降级至 3.7\n\n#### 0.1.7 - 兼容 InvokeAI","# Compel 快速上手指南\n\nCompel 是一个专为 Transformers 类文本嵌入系统（如 Stable Diffusion、SDXL、Flux）设计的提示词权重与混合库。它允许你使用直观灵活的语法对提示词的不同部分进行加权，从而精细控制生成图像的语义重点。\n\n## 环境准备\n\n*   **系统要求**：支持 Linux、macOS 和 Windows。\n*   **Python 版本**：建议 Python 3.8 及以上。\n*   **前置依赖**：\n    *   `diffusers` (版本 >= 0.12)\n    *   `torch` (PyTorch)\n    *   `transformers`\n    *   确保已安装对应的模型依赖（如 Stable Diffusion 或 Flux 相关包）。\n\n> **国内加速建议**：\n> 在安装依赖时，推荐使用清华或阿里镜像源以提升下载速度：\n> ```bash\n> pip install -i https:\u002F\u002Fpypi.tuna.tsinghua.edu.cn\u002Fsimple \u003Cpackage_name>\n> ```\n\n## 安装步骤\n\n使用 pip 直接安装最新版本的 Compel：\n\n```bash\npip install compel\n```\n\n若需使用国内镜像加速安装：\n\n```bash\npip install -i https:\u002F\u002Fpypi.tuna.tsinghua.edu.cn\u002Fsimple compel\n```\n\n## 基本使用\n\n以下示例展示如何结合 Hugging Face `diffusers` 库，使用 Compel 对提示词中的特定词汇进行加权（例如增强 \"ball\" 的权重）。\n\n### 1. 基础用法 (Stable Diffusion v1.5)\n\n```python\nfrom diffusers import StableDiffusionPipeline\nfrom compel import CompelForSD\n\n# 加载管道\npipeline = StableDiffusionPipeline.from_pretrained(\"runwayml\u002Fstable-diffusion-v1-5\")\ncompel = CompelForSD(pipeline)\n\n# 定义提示词：使用 ++ 增加 \"ball\" 的权重\nprompt = \"a cat playing with a ball++ in the forest\"\nconditioning = compel(prompt)\n\n# 生成图像\nimages = pipeline(prompt_embeds=conditioning.embeds, num_inference_steps=20).images\nimages[0].save(\"image.jpg\")\n```\n\n### 2. 包含负向提示词\n\n```python\nfrom diffusers import StableDiffusionPipeline\nfrom compel import CompelForSD\n\npipeline = StableDiffusionPipeline.from_pretrained(\"runwayml\u002Fstable-diffusion-v1-5\")\ncompel = CompelForSD(pipeline)\n\nprompt = \"a cat playing with a ball++ in the forest\"\nnegative_prompt = \"blurry, low quality, deformed\"\n\n# 同时处理正向和负向提示词\nconditioning = compel(prompt, negative_prompt=negative_prompt)\n\nimages = pipeline(\n    prompt_embeds=conditioning.embeds, \n    negative_prompt_embeds=conditioning.negative_embeds, \n    num_inference_steps=20\n).images\nimages[0].save(\"image.jpg\")\n```\n\n### 3. SDXL 模型支持\n\n针对 SDXL 模型，需使用 `CompelForSDXL` 并处理 pooled embeddings：\n\n```python\nfrom diffusers import DiffusionPipeline\nfrom compel import CompelForSDXL\nimport torch\n\ndevice = 'cuda'\npipeline = DiffusionPipeline.from_pretrained(\n    \"stabilityai\u002Fstable-diffusion-xl-base-1.0\", \n    variant=\"fp16\", \n    use_safetensors=True, \n    torch_dtype=torch.float16\n).to(device)\n\ncompel = CompelForSDXL(pipeline)\n\nprompt = \"a cat playing with a ball++ in the forest\"\nnegative_prompt = \"low quality, blurry\"\n\nconditioning = compel(prompt, negative_prompt=negative_prompt)\n\ngenerator = torch.Generator().manual_seed(42)\nimage = pipeline(\n    prompt_embeds=conditioning.embeds, \n    pooled_prompt_embeds=conditioning.pooled_embeds,\n    negative_prompt_embeds=conditioning.negative_embeds,\n    negative_pooled_prompt_embeds=conditioning.negative_pooled_embeds,\n    num_inference_steps=25, \n    width=1024, \n    height=1024, \n    generator=generator\n).images[0]\n\nimage.save('sdxl_image.jpg')\n```\n\n### 4. Flux 模型支持\n\n```python\nfrom diffusers import FluxPipeline\nfrom compel import CompelForFlux\nimport torch\n\n# 注意：MPS 设备建议使用 float32 以避免 NaN\ndevice = \"mps\" \npipe = FluxPipeline.from_pretrained(\"black-forest-labs\u002FFLUX.1-schnell\", torch_dtype=torch.float32).to(device)\n\ncompel = CompelForFlux(pipe)\n\n# 使用 ---- 降低权重，++++ 增加权重\nprompt = \"Astronaut---- in a jungle++++, cold color palette, muted colors, detailed, 8k\"\n\nconditioning = compel(prompt)\ngenerator = torch.Generator().manual_seed(42)\n\nimages = pipe(\n    prompt_embeds=conditioning.embeds, \n    pooled_prompt_embeds=conditioning.pooled_embeds,\n    num_inference_steps=4, \n    width=512, \n    height=512, \n    generator=generator\n)\n```\n\n### 语法说明\n*   `word++`：增加权重（相当于 `(word:1.1)`）。\n*   `word--`：降低权重。\n*   多个 `+` 或 `-` 可叠加效果（如 `++++` 表示大幅增强）。\n*   支持批量处理提示词列表。","一位数字艺术家正在使用 Stable Diffusion 创作一系列商业海报，需要精准控制画面中特定元素（如产品主体）的视觉权重，同时保持背景氛围的自然协调。\n\n### 没有 compel 时\n- 难以通过自然语言强调重点，反复修改提示词措辞（如重复“一个巨大的球”）往往导致语句不通顺或引发模型误解。\n- 调整元素权重依赖复杂的括号语法 `(word:1.5)`，不仅记忆成本高，且在长提示词中极易因括号嵌套错误导致生成失败。\n- 无法对同一批次中的不同图片进行差异化权重设置，批量生成时只能牺牲个性化需求以换取效率。\n- 在 SDXL 等双编码器架构下，手动处理正向与负向提示词的张量对齐极其繁琐，容易引发维度不匹配报错。\n\n### 使用 compel 后\n- 支持直观的 `++` 或 `--` 语法直接增减权重（如 `ball++`），让艺术家能像调节音量一样微调画面元素的显著性。\n- 自动解析并构建正确的嵌入张量，彻底消除手动计算权重系数和检查括号匹配的痛苦，大幅降低试错成本。\n- 原生支持列表化批量处理，可为一组提示词分别定义独立的权重策略，轻松实现“同背景、不同主体强调”的多样化输出。\n- 针对 SDXL 架构自动管理复杂的 `pooled_embeds` 与常规嵌入，开发者只需关注创意描述，无需深究底层张量结构。\n\ncompel 将晦涩的向量权重调控转化为简单的文本标记，让创作者从繁琐的代码调试中解放，真正专注于视觉创意的精细打磨。","https:\u002F\u002Foss.gittoolsai.com\u002Fimages\u002Fdamian0815_compel_e9953606.png","damian0815","Damian","https:\u002F\u002Foss.gittoolsai.com\u002Favatars\u002Fdamian0815_a60d4876.png","Gluten free damian solution. You can assured we proivde highes quality Damian & Solutions direct",null,"Vienna, Austria","http:\u002F\u002Fdamianstewart.com","https:\u002F\u002Fgithub.com\u002Fdamian0815",[81,85],{"name":82,"color":83,"percentage":84},"Jupyter Notebook","#DA5B0B",97.6,{"name":86,"color":87,"percentage":88},"Python","#3572A5",2.4,604,51,"2026-04-09T07:52:51","MIT","Linux, macOS, Windows","非必需（支持 CPU），但推荐 NVIDIA GPU 以加速生成；示例中使用了 CUDA ('cuda') 和 Apple MPS ('mps')；显存大小取决于所加载的扩散模型（如 SDXL 或 Flux），通常建议 8GB+；CUDA 版本未说明","未说明（取决于具体扩散模型的大小，运行 SDXL 或 Flux 时建议 16GB+）",{"notes":97,"python":98,"dependencies":99},"该工具是一个提示词加权库，本身不包含模型，需配合 Hugging Face Diffusers 管道（如 Stable Diffusion, SDXL, Flux）使用。若在 MPS (macOS) 上运行 Flux 模型，必须使用 float32 精度以避免 NaN 错误。如遇内存泄漏，建议在 torch.no_grad() 块中运行并手动垃圾回收。支持文本反转（Textual Inversion）。","未说明",[100,101,102],"diffusers>=0.12","torch","transformers",[15,14],"2026-03-27T02:49:30.150509","2026-04-09T21:34:17.864210",[107,112,117,122,127,132],{"id":108,"question_zh":109,"answer_zh":110,"source_url":111},26667,"如何在 Stable Diffusion XL (SDXL) 中正确使用 Compel 处理提示词以避免训练数据偏差？","在 SDXL 中，建议使用提示词分段（prompt segmentation）来克服训练数据不平衡导致的权重问题。不要使用长且连贯的提示词，而是使用短提示词并结合 `.and()` 语法。例如：`('the pope', 'dressed as ronald mcdonald').and(0.9, 0.95)`。这种写法有助于平衡主体权重，提高生成的灵活性。","https:\u002F\u002Fgithub.com\u002Fdamian0815\u002Fcompel\u002Fissues\u002F37",{"id":113,"question_zh":114,"answer_zh":115,"source_url":116},26668,"如何在 Diffusers 管道中结合使用 Compel 和 Textual Inversion（文本反转）？","在将提示词传递给 Compel 进行嵌入转换之前，必须先调用 Diffusers 管道的 `maybe_convert_prompt` 方法。这能确保文本反转令牌被正确解析。代码示例如下：\n1. 加载模型并应用文本反转：`pipeline.load_textual_inversion(EMBEDDING_PATH, token='charturnerv2')`\n2. 转换提示词：`prompts = pipeline.maybe_convert_prompt(prompts, pipeline.tokenizer)`\n3. 使用 Compel 生成嵌入：`prompt_embeds = compel(prompts)`","https:\u002F\u002Fgithub.com\u002Fdamian0815\u002Fcompel\u002Fissues\u002F28",{"id":118,"question_zh":119,"answer_zh":120,"source_url":121},26669,"Compel 如何解析和处理提示词中的 LoRA 权重？","你可以调用 `Compel.parse_prompt_string(prompt)` 来解析提示词字符串，它会返回一个包含 `lora_weights` 属性的对象。该属性是一个列表，包含 `LoraWeight` 对象，每个对象都有 `model` (LoRA 文件名) 和 `weight` (权重值)。\n注意：Compel 主要负责解析提示词中的 LoRA 语法（如 `\u003Clora:name:weight>`），但实际加载 LoRA 模型通常需要通过 Diffusers 的 `load_lora_weights` 方法单独完成，因为 LoRA 是直接修改模型权重而非像文本反转那样仅作为嵌入。","https:\u002F\u002Fgithub.com\u002Fdamian0815\u002Fcompel\u002Fissues\u002F30",{"id":123,"question_zh":124,"answer_zh":125,"source_url":126},26670,"当提示词超过 75 个 token 时，Compel 如何处理分割以避免生成质量下降？","Compel 默认会在 75 个 token 处硬分割提示词，这可能导致单词被切断从而影响自注意力机制。虽然官方建议 Compel 应回溯寻找标点符号（`.`、`;` 或 `,`）进行分割，但在某些版本中如果遇到 `Token indices sequence length is longer than...` 的错误，可能需要手动实现或使用外部脚本来优化分割逻辑。对于 SDXL，如果仍遇到此问题，请确保使用的是支持长提示词处理的最新版本（如 2.0.2+），或者参考社区提供的自定义解析脚本（如 `prompt_parser.py`）来处理分块。","https:\u002F\u002Fgithub.com\u002Fdamian0815\u002Fcompel\u002Fissues\u002F59",{"id":128,"question_zh":129,"answer_zh":130,"source_url":131},26671,"在使用 SDXL 和 Compel 时，如果遇到 `addition_embed_type` 相关的配置错误该怎么办？","该错误通常是因为未正确传递 SDXL 所需的额外嵌入参数。在使用 Compel 初始化时，必须明确指定 `returned_embeddings_type` 为 `ReturnedEmbeddingsType.PENULTIMATE_HIDDEN_STATES_NON_NORMALIZED`，并设置 `requires_pooled=[False, True]` 以匹配 SDXL 的双编码器架构。同时，调用管道生成图像时，需将 Compel 返回的 `pooled` 嵌入传递给 `pooled_prompt_embeds` 参数，将负向提示词的对应结果传递给 `negative_pooled_prompt_embeds`。","https:\u002F\u002Fgithub.com\u002Fdamian0815\u002Fcompel\u002Fissues\u002F45",{"id":133,"question_zh":134,"answer_zh":135,"source_url":126},26672,"为什么在使用长提示词时会收到关于序列长度超过最大限制的警告，需要担心吗？","如果你看到类似 `Running this sequence through the model will result in indexing errors` 的警告，但代码仍在运行，通常可以忽略它。Compel 的内部逻辑已经设计了绕过此限制的处理机制（通过分块处理）。只要最终生成的图像没有明显异常，该警告不影响实际使用。如果确实遇到索引错误，请检查是否禁用了 `truncate_long_prompts` 并确保你的 Compel 版本是最新的。",[137,142,147,152,157,162,167],{"id":138,"version":139,"summary_zh":140,"released_at":141},171868,"v2.3.1","## 变更内容\n* 改进了在 EOS 后额外插入填充令牌时去除 EOS\u002FBOS 的处理方式，由 @damian0815 在 https:\u002F\u002Fgithub.com\u002Fdamian0815\u002Fcompel\u002Fpull\u002F119 中完成。\n* 由 @damian0815 在 https:\u002F\u002Fgithub.com\u002Fdamian0815\u002Fcompel\u002Fpull\u002F120 中添加了分词信息、针对较长提示的 CLS 令牌处理选项，并修复了一些 bug。\n* 由 @damian0815 在 https:\u002F\u002Fgithub.com\u002Fdamian0815\u002Fcompel\u002Fpull\u002F126 中修复了 SDXL 中“序列长度为 78”的问题。\n* 由 @dx2-66 在 https:\u002F\u002Fgithub.com\u002Fdamian0815\u002Fcompel\u002Fpull\u002F123 中为便捷封装添加了设备选项。\n* v2.3.1 版本由 @damian0815 在 https:\u002F\u002Fgithub.com\u002Fdamian0815\u002Fcompel\u002Fpull\u002F127 中发布。\n\n## 新贡献者\n* @dx2-66 在 https:\u002F\u002Fgithub.com\u002Fdamian0815\u002Fcompel\u002Fpull\u002F123 中完成了首次贡献。\n\n**完整变更日志**: https:\u002F\u002Fgithub.com\u002Fdamian0815\u002Fcompel\u002Fcompare\u002Fv2.3.0...v2.3.1","2025-11-02T14:18:54",{"id":143,"version":144,"summary_zh":145,"released_at":146},171869,"v2.3.0","PR #120 分词信息、针对较长提示的 CLS 令牌处理选项、错误修复\n\n**完整变更日志**: https:\u002F\u002Fgithub.com\u002Fdamian0815\u002Fcompel\u002Fcompare\u002Fv2.2.1...v2.3.0","2025-10-05T18:25:30",{"id":148,"version":149,"summary_zh":150,"released_at":151},171870,"v2.2.1","## 变更内容\n修复 #116 ","2025-10-01T14:44:44",{"id":153,"version":154,"summary_zh":155,"released_at":156},171871,"2.2.0","## 变更内容\n* 通过添加对 `T5TokenizerFast` 和 `T5EncoderModel` 的内部支持，新增对 Flux 的支持。这很可能也支持其他基于 T5 的模型——目前我只测试过 Flux。\n* 新增 `CompelForSD`、`CompelForSDXL` 和 `CompelForFlux` 类，以帮助处理不同的嵌入和提示意图（输入：主提示、风格提示、负面主提示、负面风格提示；输出：嵌入、负面嵌入、池化嵌入、负面池化嵌入）。\n* 在不应用权重时，通过绕过 Compel 的权重计算逻辑来提升性能。使用 FP16 时会导致输出略有不同——可通过调用 `compel.disable_no_weights_bypass()` 来禁用此优化。","2025-10-01T07:58:16",{"id":158,"version":159,"summary_zh":160,"released_at":161},171872,"2.1.1","修复 `RuntimeError: 令牌序列不匹配` 错误，并将默认分词方式设置为按句子分割。\n\n**完整变更日志**：https:\u002F\u002Fgithub.com\u002Fdamian0815\u002Fcompel\u002Fcompare\u002F2.1.0...2.1.1","2025-05-17T21:48:58",{"id":163,"version":164,"summary_zh":165,"released_at":166},171873,"2.1.0","## 变更内容\n* @GerbertBless 在 https:\u002F\u002Fgithub.com\u002Fdamian0815\u002Fcompel\u002Fpull\u002F101 中更新了 README.md\n* @damian0815 在 https:\u002F\u002Fgithub.com\u002Fdamian0815\u002Fcompel\u002Fpull\u002F112 中添加了用于拆分长提示的更好选项\n\n## 新贡献者\n* @GerbertBless 在 https:\u002F\u002Fgithub.com\u002Fdamian0815\u002Fcompel\u002Fpull\u002F101 中完成了首次贡献\n\n**完整变更日志**: https:\u002F\u002Fgithub.com\u002Fdamian0815\u002Fcompel\u002Fcompare\u002Fv2.0.3...2.1.0","2025-05-11T15:14:58",{"id":168,"version":169,"summary_zh":170,"released_at":171},171874,"v2.0.2","我打算尝试使用 Git 进行版本发布。这是“第一个”版本。\n\nCompel 2.0.2 已在 PyPI 上线：\n\n`pip install compel==2.0.2`\n\n此版本修复了在 SDXL 中的一个问题：如果你已经在管道上调用了 `enable_sequential_cpu_offload()`，现在需要将 `device='cuda'` 传递给 Compel 的 `__init__` 方法。\n\n此外，我还清理并扩展了 SDXL 的示例笔记本，演示如何通过 `__call__` 接口同时使用正向和反向提示，以及支持长提示（即 `compel([positive_prompt, negative_prompt])`）。","2023-08-20T15:27:57"]