### Installing HiDream-E1 Dependencies (Shell) Source: https://github.com/hidream-ai/hidream-e1/blob/main/README.md Installs required Python packages including Flash Attention and the latest Diffusers library using pip. Prerequisites include CUDA 12.4 for manual Flash Attention installation. ```sh pip install -r requirements.txt pip install -U flash-attn --no-build-isolation pip install -U git+https://github.com/huggingface/diffusers.git ``` -------------------------------- ### Using HiDream-E1 Pipeline Programmatically (Python) Source: https://github.com/hidream-ai/hidream-e1/blob/main/README.md Demonstrates how to load and configure the HiDream-E1 pipeline in Python code. It includes loading necessary models (tokenizer, text encoder, transformer), optionally setting up LoRA adapters for refinement, initializing the pipeline, loading an image, defining an instruction, and moving the pipeline to a CUDA device. ```python import torch from transformers import PreTrainedTokenizerFast, LlamaForCausalLM from pipeline_hidream_image_editing import HiDreamImageEditingPipeline from PIL import Image from peft import LoraConfig from huggingface_hub import hf_hub_download from diffusers import HiDreamImageTransformer2DModel from instruction_refinement import refine_instruction from safetensors.torch import load_file # Set to True to enable instruction refinement and transformer model ENABLE_REFINE = True # Load models tokenizer_4 = PreTrainedTokenizerFast.from_pretrained("meta-llama/Llama-3.1-8B-Instruct") text_encoder_4 = LlamaForCausalLM.from_pretrained( "meta-llama/Llama-3.1-8B-Instruct", output_hidden_states=True, output_attentions=True, torch_dtype=torch.bfloat16, ) # Configure transformer model if refinement is enabled transformer = None reload_keys = None if ENABLE_REFINE: transformer = HiDreamImageTransformer2DModel.from_pretrained("HiDream-ai/HiDream-I1-Full", subfolder="transformer") lora_config = LoraConfig( r=16, lora_alpha=16, lora_dropout=0.0, target_modules=["to_k", "to_q", "to_v", "to_out", "to_k_t", "to_q_t", "to_v_t", "to_out_t", "w1", "w2", "w3", "final_layer.linear"], init_lora_weights="gaussian", ) transformer.add_adapter(lora_config) transformer.max_seq = 4608 lora_ckpt_path = hf_hub_download(repo_id="HiDream-ai/HiDream-E1-Full", filename="HiDream-E1-Full.safetensors") lora_ckpt = load_file(lora_ckpt_path, device="cuda") src_state_dict = transformer.state_dict() reload_keys = [k for k in lora_ckpt if "lora" not in k] reload_keys = { "editing": {k: v for k, v in lora_ckpt.items() if k in reload_keys}, "refine": {k: v for k, v in src_state_dict.items() if k in reload_keys}, } info = transformer.load_state_dict(lora_ckpt, strict=False) assert len(info.unexpected_keys) == 0 # Initialize pipeline if ENABLE_REFINE: pipe = HiDreamImageEditingPipeline.from_pretrained( "HiDream-ai/HiDream-I1-Full", tokenizer_4=tokenizer_4, text_encoder_4=text_encoder_4, torch_dtype=torch.bfloat16, transformer=transformer, ) else: pipe = HiDreamImageEditingPipeline.from_pretrained( "HiDream-ai/HiDream-E1-Full", tokenizer_4=tokenizer_4, text_encoder_4=text_encoder_4, torch_dtype=torch.bfloat16, ) # Load and preprocess test image test_image = Image.open("assets/test_1.png") original_width, original_height = test_image.size test_image = test_image.resize((768, 768)) # Define instruction instruction = 'Convert the image into a Ghibli style.' # Refine instruction if enabled refined_instruction = refine_instruction(src_image=test_image, src_instruction=instruction) print(f"Original instruction: {instruction}") print(f"Refined instruction: {refined_instruction}") # Move pipeline to GPU pipe = pipe.to("cuda", torch.bfloat16) ``` -------------------------------- ### Run Instruction Refinement Script (Bash) Source: https://github.com/hidream-ai/hidream-e1/blob/main/README.md This bash command executes the instruction refinement script provided with the project. It takes the source image and the initial instruction as arguments to refine the text instruction for better results. This script requires a VLM API key (either local vllm or OpenAI). ```bash python ./instruction_refinement.py --src_image ./test.jpeg --src_instruction "convert the image into a Ghibli style" ``` -------------------------------- ### Run Gradio Demo (Python) Source: https://github.com/hidream-ai/hidream-e1/blob/main/README.md This Python command launches a Gradio web interface for interactive image editing using the HiDream-E1 model. Running this script provides a user-friendly way to experiment with different instructions and images. ```python python gradio_demo.py ``` -------------------------------- ### Generate and Save Image using HiDream-E1 Pipeline (Python) Source: https://github.com/hidream-ai/hidream-e1/blob/main/README.md This Python snippet demonstrates how to use the HiDream-E1 pipeline to generate an image based on a prompt and input image, applying various parameters like guidance scales, inference steps, and refinement strength. It then resizes the output image back to original dimensions and saves it to a file. ```python image = pipe( prompt=refined_instruction, negative_prompt="low resolution, blur", image=test_image, guidance_scale=5.0, image_guidance_scale=4.0, num_inference_steps=28, generator=torch.Generator("cuda").manual_seed(3), refine_strength=0.3 if ENABLE_REFINE else 0.0, reload_keys=reload_keys, ).images[0] # Resize back to original dimensions and save image = image.resize((original_width, original_height)) image.save("output.jpg") ``` -------------------------------- ### Python Project Dependencies Source: https://github.com/hidream-ai/hidream-e1/blob/main/requirements.txt Specifies the necessary Python libraries and their minimum required versions or source locations for the project. ```Python torch>=2.5.1 torchvision>=0.20.1 git+https://github.com/huggingface/diffusers.git transformers>=4.47.1 einops>=0.7.0 accelerate>=1.2.1 safetensors peft ``` -------------------------------- ### Running HiDream-E1 Inference Script (Shell) Source: https://github.com/hidream-ai/hidream-e1/blob/main/README.md Executes the main inference script for HiDream-E1, which likely performs image editing using the pre-trained model. ```sh python ./inference.py ``` === COMPLETE CONTENT === This response contains all available snippets from this library. No additional content exists. Do not make further requests.