Skip to content

Easily use Flux with python on your Macbook Pro - Apple silicon chips

Notifications You must be signed in to change notification settings

einar-hansen/flux-python-package

Repository files navigation

Flux Image Generation

This project provides a flexible framework for generating images using the Flux model from Black Forest Labs. It supports both text-to-image and image-to-image generation using either the Schnell or Dev model variants.

Setup

  1. Clone this repository
    git clone https://github.com/einar-hansen/flux-python-package.git
  2. Create and activate a virtual environment:
    python3 -m venv flux_env
    source flux_env/bin/activate
  3. Install the required dependencies:
    pip install git+https://github.com/huggingface/diffusers.git
    pip install transformers torch pillow pyyaml term-image accelerate protobuf sentencepiece

Usage

Run the main script run_flux.py with the following syntax:

# source flux_env/bin/activate
python run_flux.py [options] <prompt>

Options

  • --mode {text2img,img2img,upscale}: Mode of operation (default: text2img)
  • --model {schnell,dev}: Model to use (default: schnell)
  • -n, --num_images: Number of images to generate (default in config.yaml)
  • -o, --output_dir: Output directory for generated images (default in config.yaml)
  • -b, --base_filename: Base filename for generated images (default: None, uses SHA256 hash)
  • -g, --guidance_scale: Guidance scale (default in config.yaml)
  • -H, --height: Height of the generated image (default in config.yaml)
  • -W, --width: Width of the generated image (default in config.yaml)
  • -s, --num_inference_steps: Number of inference steps (default in config.yaml)
  • -v, --view-image: View the image after generation (default: False)
  • -f, --force: Force mode: generate all images without prompting (default: False)
  • --lora_model: Path to the LoRA model (default: None)
  • --lora_scale: Scale for the LoRA model (default in config.yaml)
  • -i, --input_image: Path to the input image (required for img2img mode)
  • --strength: Strength for img2img generation (default in config.yaml)
  • -r, --randomness: Generate random prompt variants for each image
  • --output_format: The image format ['webp', 'png', 'jpg'] for the output image

Examples

1. Basic Text-to-Image Generation

Generate a single image from a text prompt:

# source flux_env/bin/activate
python run_flux.py --model schnell --mode text2img "A cyberpunk cityscape"

A cyberpunk cityscape

2. Multiple Images with Random Variants

Generate multiple images with random prompt variants:

# source flux_env/bin/activate
python run_flux.py --model schnell --mode text2img --num_images 3 --randomness "a developer that sits in the office working on a apple mac, very concentrated, can partially see the code on the screen, the office is professional and has a few green plants, scandinavian style."

This appends f.example ", during golden hour time of day", ", in the style of surrealism" or ", in a fantasy setting". You can control what kind of styles you randomly want to apply in the config.yaml file.

A developer at work A developer at work A developer at work

3. Image-to-Image Generation

Transform an existing image based on a prompt:

# source flux_env/bin/activate
python run_flux.py --model dev --mode img2img --strength 0.70 --num_inference_steps 50 --input_image images/cityscape.png "turn the landscape into a snowy, white winter wonderland"

A wonderland cyberpunk cityscape 1 A wonderland cyberpunk cityscape 2 A wonderland cyberpunk cityscape 3

Click on the links to learn more about how to use the strength and guidance_scale arguments.

4. Upscaling an Image

Generate a larger/upscaled image. This only works on the dev model.

# source flux_env/bin/activate
python run_flux.py --model dev --mode upscale --input_image images/portrait-rounded-xs.png "improve image the quality"

Upscaled image Original image

Configuration

Default settings can be adjusted in the config.yaml file. There are separate configurations for Schnell and Dev models, as well as options for prompt variants.

Logging

The script generates a log file named generation_log.csv in the same directory as the script. This CSV file uses semicolons (;) as delimiters and contains information about each generated image.

Performance Considerations

  • The initial model loading may take some time, but subsequent image generations will be faster.
  • Adjust the num_inference_steps parameter to balance between generation speed and image quality.
  • Using a GPU can significantly speed up the image generation process.

Contributing

Feel free to submit issues or pull requests if you have suggestions for improvements or encounter any problems.

License

This project is open-source and available under the MIT License.

About

Easily use Flux with python on your Macbook Pro - Apple silicon chips

Topics

Resources

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published

Languages