How to Download the FLUX Model from Hugging Face
Hugging Face is a popular platform for machine learning models, datasets, and tools. This step-by-step tutorial will show you how to download “Flux” from Hugging Face.
Step 1: Create and Set Up a Hugging Face Account
Before you can access and download the FLUX.1-dev model, you need to have a Hugging Face account and be granted access to the model.
Go to Hugging Face.
Create an account or log in if you already have one.
Visit the FLUX.1-dev page.
Request access to the model by agreeing to the terms and conditions
Step 2: Install Required Libraries
To interact with Hugging Face models, you need to install the required Python libraries. Use the following commands to install them:
pip install diffusers transformers torch
pip install huggingface_hub
Step 3: Authenticate with Hugging Face
In order to download the model, you’ll need to authenticate your Hugging Face account using a token.
Go to your Hugging Face account settings.
Under the “Access Tokens” section, create a new token.
Copy the token
from huggingface_hub import login
# Replace with your actual Hugging Face token
login(token="your_huggingface_token")
Step 4: Initialize the Diffusion Pipeline
Once authenticated, you can load the FLUX.1-dev model using the DiffusionPipeline class. This pipeline will allow you to generate images using the model.
from diffusers import DiffusionPipeline
import torch
# Define the model name
model_name = "black-forest-labs/FLUX.1-dev"
# Initialize the pipeline
pipe = DiffusionPipeline.from_pretrained(model_name, torch_dtype=torch.bfloat16)
pipe = pipe.to("cuda") # Move to GPU if available
Step 5: Generate Images
Now that the pipeline is set up, you can generate images using a text prompt. For example:
# Example prompt
prompt = "A dog holding a sign that says 'Hello World'"
# Generate image
image = pipe(
prompt,
height=1024,
width=1024,
guidance_scale=3.5,
num_inference_steps=50,
generator=torch.manual_seed(0)
).images[0]
# Save the generated image
image.save("generated_image.png") # Save image with the name 'generated_image.png'
Step 6: Save the Model Locally
If you’d like to save the FLUX.1-dev model locally so that you don’t need to download it again in the future, you can use the save_pretrained method.
# Use your directory
save_directory = "./my_saved_flux_model"
pipe.save_pretrained(save_directory)
print(f"Model saved to {save_directory}")
Step 7: Load the Saved Model
If you want to use the saved model later, you can load it directly from your local directory:
pipe = DiffusionPipeline.from_pretrained("./my_saved_flux_model")
Conclusion
Now you know how to:
Authenticate and download the FLUX.1-dev model from Hugging Face.
Generate images using the model with a custom text prompt.
Save and load the model locally to avoid downloading it again in the future.