Skip to content

Instantly share code, notes, and snippets.

@chilang
Created December 5, 2024 09:13
Show Gist options
  • Save chilang/2b41e47b8876e754f3b3d21f887cc353 to your computer and use it in GitHub Desktop.
Save chilang/2b41e47b8876e754f3b3d21f887cc353 to your computer and use it in GitHub Desktop.
MLX Flux Image Generator - Mac Installation Script (Final)

MLX Flux Image Generator Installation

A simple installer for the MLX Flux Image Generator, optimized for Apple Silicon Macs.

One-Line Installation

curl -fsSL https://gist.githubusercontent.com/chilang/02482073c949f4f8e3125364a1c93073/raw/install.sh | bash

Requirements

  • macOS with Apple Silicon
  • Python 3.11 or later
  • Internet connection (for downloading models ~20GB)

Manual Installation

If you prefer to inspect the installation script first:

  1. Download the install script:
curl -O https://gist.githubusercontent.com/chilang/c9f6b7db9596282d5149a5611279da2f/raw/install.sh
  1. Review the script contents:
cat install.sh
  1. Run the installation:
bash install.sh

Usage

After installation:

cd ~/mlx_flux_generator
source .venv/bin/activate
./generate.py "your prompt here" --steps 2

Optional arguments:

  • --steps: Number of inference steps (default: 2)
  • --height: Image height in pixels (default: 1024)
  • --width: Image width in pixels (default: 1024)
  • --seed: Random seed for reproducibility (default: 42)
  • --output: Output directory (default: "outputs")

Note

The first run will download the FLUX model files (approximately 20GB).

#!/usr/bin/env python3
import argparse
import os
from pathlib import Path
import subprocess
def generate_image(prompt: str, output_dir: str = "outputs", num_steps: int = 2,
height: int = 1024, width: int = 1024, seed: int = 42) -> str:
"""Generate an image from a text prompt using MFLUX"""
# Ensure output directory exists
Path(output_dir).mkdir(parents=True, exist_ok=True)
# Generate a filename from the prompt
safe_prompt = "".join(x for x in prompt[:30] if x.isalnum() or x in (' ', '-', '_')).strip()
output_path = os.path.join(output_dir, f"{safe_prompt}.png")
print(f"Generating image for prompt: {prompt}")
print(f"Using {num_steps} inference steps")
print(f"Output will be saved to: {output_path}")
try:
# Use mflux-generate command
cmd = [
"mflux-generate",
"--model", "schnell", # Using the schnell model as recommended
"--steps", str(num_steps),
"--height", str(height),
"--width", str(width),
"--seed", str(seed),
"--output", output_path,
"--prompt", prompt
]
# Run the command
subprocess.run(cmd, check=True)
if os.path.exists(output_path):
print(f"Image generated successfully!")
return output_path
else:
raise Exception("Output image not found")
except Exception as e:
print(f"Error generating image: {str(e)}")
return None
def main():
parser = argparse.ArgumentParser(description="Generate images using MFLUX")
parser.add_argument("prompt", type=str, help="Text prompt for image generation")
parser.add_argument("--steps", type=int, default=2, help="Number of inference steps")
parser.add_argument("--output", type=str, default="outputs", help="Output directory")
parser.add_argument("--height", type=int, default=1024, help="Image height")
parser.add_argument("--width", type=int, default=1024, help="Image width")
parser.add_argument("--seed", type=int, default=42, help="Random seed")
args = parser.parse_args()
output_path = generate_image(
args.prompt,
args.output,
args.steps,
args.height,
args.width,
args.seed
)
if output_path:
print(f"\nImage saved to: {output_path}")
else:
print("\nFailed to generate image")
if __name__ == "__main__":
main()
#!/bin/bash
# Exit on error
set -e
# Check if Python 3.11+ is installed
if ! command -v python3 &> /dev/null; then
echo "Python 3 is required but not installed. Please install Python 3.11 or later."
exit 1
fi
# Find Python 3.11 or later
PYTHON_PATH=""
for py_path in "/opt/homebrew/bin/python3.11" "/usr/local/bin/python3.11" "/opt/homebrew/opt/[email protected]/bin/python3.11"; do
if [ -x "$py_path" ]; then
PYTHON_PATH="$py_path"
break
fi
done
if [ -z "$PYTHON_PATH" ]; then
echo "Python 3.11 or later is required but not found. Please install Python 3.11:"
echo "brew install [email protected]"
exit 1
fi
echo "Using Python at: $PYTHON_PATH"
# Install uv if not present
if ! command -v uv &> /dev/null; then
echo "Installing uv package manager..."
if command -v brew &> /dev/null; then
brew install uv
else
curl -LsSf https://astral.sh/uv/install.sh | sh
fi
fi
# Create project directory
PROJECT_DIR="${PWD}"
# Download project files
echo "Downloading project files..."
curl -fsSL https://gist.githubusercontent.com/chilang/02482073c949f4f8e3125364a1c93073/raw/generate.py > generate.py
curl -fsSL https://gist.githubusercontent.com/chilang/02482073c949f4f8e3125364a1c93073/raw/requirements.txt > requirements.txt
# Make generate.py executable
chmod +x generate.py
# Create virtual environment and install dependencies
echo "Setting up Python environment..."
uv venv --python "$PYTHON_PATH"
source .venv/bin/activate
uv pip install -r requirements.txt
# Create outputs directory
mkdir -p outputs
echo "Installation complete!"
echo "To generate images, run:"
echo "cd $PROJECT_DIR"
echo "source .venv/bin/activate"
echo "./generate.py \"your prompt here\" --steps 2"
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment