Update README.md
Browse files5 unmodified lines6789109101113141213141516171619201718192223242526272820212223242526273031282930313334323334353637363839404138424344414243444546475 unmodified lines
```pythonimport torchfrom diffusers.modular_pipelines import ModularPipelineBlocks, SequentialPipelineBlocksfrom diffusers.modular_pipelines.stable_diffusion_xl import TEXT2IMAGE_BLOCKS, CONTROLNET_BLOCKSfrom diffusers import ModularPipeline, ComponentsManager, ModularPipelineBlocksfrom diffusers.utils import load_image
# fetch the depth processor block that will create our depth mapdepth_processor_block = ModularPipelineBlocks.from_pretrained("diffusers/depth-processor-custom-block", trust_remote_code=true)# Use ComponentsManager to enable auto CPU offloading for memory efficiencymanager = ComponentsManager()manager.enable_auto_cpu_offload(device="cuda:0")
my_blocks = TEXT2IMAGE_BLOCKS.copy()my_blocks.insert("depth_processor", depth_processor_block, 1)
# replace text to image denoise block with controlnet denoise blockmy_blocks.sub_blocks["denoise"] = CONTROLNET_BLOCKS["denoise"]# Initialize pipelinepipe = ModularPipeline.from_pretrained("Qwen/Qwen-Image", components_manager=manager)
# create our initial set of controlnet blocksblocks = SequentialPipelineBlocks.from_blocks_dict(my_blocks)
repo_id = "diffusers/modular-stable-diffusion-xl-base-1.0"
# Initialize the pipeline object we can use to run our blockspipe = blocks.init_pipeline(repo_id)# Insert a depth processing blockblocks = pipe.blocks.get_workflow("controlnet_text2image")depth_block = ModularPipelineBlocks.from_pretrained( "diffusers/depth-processor-custom-block", trust_remote_code=True,)blocks.sub_blocks.insert("depth", depth_block, 0)
# Load model component weightspipe.load_components(torch_dtype=torch.float16, device_map="cuda")# Reinitialize the pipeline for ControlNetpipe = blocks.init_pipeline("Qwen/Qwen-Image", components_manager=manager)pipe.load_components(torch_dtype=torch.bfloat16)
image = load_image("https://huggingface.co/datasets/huggingface/documentation-images/resolve/main/transformers/tasks/car.jpg?download=true")image = image.resize((1024, 1024))# Load the ControlNet modelcontrolnet_spec = pipeline.get_component_spec("controlnet")controlnet_spec.pretrained_model_name_or_path = "InstantX/Qwen-Image-ControlNet-Union"controlnet = controlnet_spec.load(torch_dtype=torch.bfloat16)pipeline.update_components(controlnet=controlnet)
prompt = ["A red car"]# Inferprompt = "cat wizard with red hat, gandalf, lord of the rings, detailed, fantasy, cute, adorable, Pixar, Disney"image = load_image("https://github.com/Trgtuan10/Image_storage/blob/main/cute_cat.png?raw=true")
output = pipe(output = pipeline( prompt=prompt, image=image, num_inference_steps=35, guidance_scale=7.5, output_type="pil",)).images[0]output```README.md+27-25