|
| 1 | +#!/usr/bin/env python3 |
| 2 | +# SPDX-FileCopyrightText: Copyright (c) 2026 NVIDIA CORPORATION & AFFILIATES. All rights reserved. |
| 3 | +# SPDX-License-Identifier: Apache-2.0 |
| 4 | +# |
| 5 | +# Licensed under the Apache License, Version 2.0 (the "License"); |
| 6 | +# you may not use this file except in compliance with the License. |
| 7 | +# You may obtain a copy of the License at |
| 8 | +# |
| 9 | +# http://www.apache.org/licenses/LICENSE-2.0 |
| 10 | +# |
| 11 | +# Unless required by applicable law or agreed to in writing, software |
| 12 | +# distributed under the License is distributed on an "AS IS" BASIS, |
| 13 | +# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 14 | +# See the License for the specific language governing permissions and |
| 15 | +# limitations under the License. |
| 16 | +"""FLUX.1 text-to-image generation. |
| 17 | +
|
| 18 | +Usage: |
| 19 | + python flux1.py |
| 20 | + python flux1.py --visual_gen_args ../configs/flux1-dev-fp4-1gpu.yaml |
| 21 | +""" |
| 22 | + |
| 23 | +import argparse |
| 24 | +from pathlib import Path |
| 25 | + |
| 26 | +from tensorrt_llm import VisualGen, VisualGenArgs |
| 27 | + |
| 28 | + |
| 29 | +def _output_paths(output_path: str, num_images: int) -> str | list[str]: |
| 30 | + if num_images == 1: |
| 31 | + return output_path |
| 32 | + |
| 33 | + path = Path(output_path) |
| 34 | + return [str(path.with_name(f"{path.stem}_{idx + 1}{path.suffix}")) for idx in range(num_images)] |
| 35 | + |
| 36 | + |
| 37 | +def main(): |
| 38 | + parser = argparse.ArgumentParser(description="FLUX.1 Text-to-Image example") |
| 39 | + parser.add_argument( |
| 40 | + "--model", |
| 41 | + type=str, |
| 42 | + default="black-forest-labs/FLUX.1-dev", |
| 43 | + help="Model path or HuggingFace Hub ID", |
| 44 | + ) |
| 45 | + parser.add_argument( |
| 46 | + "--visual_gen_args", |
| 47 | + "--extra_visual_gen_options", |
| 48 | + dest="visual_gen_args", |
| 49 | + type=str, |
| 50 | + default=None, |
| 51 | + help="Path to YAML config (same as trtllm-serve --visual_gen_args)", |
| 52 | + ) |
| 53 | + parser.add_argument( |
| 54 | + "--prompt", |
| 55 | + type=str, |
| 56 | + default="A cat sitting on a windowsill, cinematic lighting, highly detailed", |
| 57 | + help="Text prompt for image generation", |
| 58 | + ) |
| 59 | + parser.add_argument( |
| 60 | + "--num_images_per_prompt", |
| 61 | + type=int, |
| 62 | + default=1, |
| 63 | + help="Number of images to generate for the prompt", |
| 64 | + ) |
| 65 | + parser.add_argument( |
| 66 | + "--output_path", |
| 67 | + type=str, |
| 68 | + default="flux1_output.png", |
| 69 | + help="Path to save the output image. For multiple images, an index is appended.", |
| 70 | + ) |
| 71 | + args = parser.parse_args() |
| 72 | + if args.num_images_per_prompt < 1: |
| 73 | + raise ValueError("--num_images_per_prompt must be >= 1") |
| 74 | + |
| 75 | + # Engine config from shared YAML (optional); model-specific defaults apply otherwise. |
| 76 | + extra_args = VisualGenArgs.from_yaml(args.visual_gen_args) if args.visual_gen_args else None |
| 77 | + visual_gen = VisualGen(model=args.model, args=extra_args) |
| 78 | + |
| 79 | + # --- Model-specific: T2I request construction --- |
| 80 | + # Start from per-model defaults (resolution, steps, guidance, seed, etc.) and set image count. |
| 81 | + params = visual_gen.default_params |
| 82 | + params.num_images_per_prompt = args.num_images_per_prompt |
| 83 | + |
| 84 | + output = visual_gen.generate(inputs=args.prompt, params=params) |
| 85 | + |
| 86 | + saved = output.save(_output_paths(args.output_path, args.num_images_per_prompt)) |
| 87 | + print(f"Saved: {saved}") |
| 88 | + |
| 89 | + |
| 90 | +if __name__ == "__main__": |
| 91 | + main() |
0 commit comments