├── requirements.txt ├── README.md ├── LICENSE └── app.py /requirements.txt: -------------------------------------------------------------------------------- 1 | git+https://github.com/huggingface/diffusers.git 2 | git+https://github.com/huggingface/transformers.git 3 | git+https://github.com/huggingface/accelerate.git 4 | git+https://github.com/huggingface/peft 5 | huggingface_hub 6 | sentencepiece 7 | torch 8 | pillow 9 | hf_xet 10 | gradio 11 | numpy 12 | torchvision 13 | protobuf 14 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | # **FLUX-REALISM [FLUX Super Realism Image Generator]** 2 | 3 | > FLUX.1-Krea-dev + FLUX.1-dev 4 | 5 | A Gradio-based web application for generating hyper-realistic images using FLUX.1-dev with Super Realism LoRA enhancement. This application provides an intuitive interface for creating high-quality, photorealistic images with customizable parameters and styles. 6 | 7 | ## Features 8 | 9 | - **Hyper-realistic image generation** using FLUX.1-dev with Super Realism LoRA 10 | - **Multiple quality presets** (8K, 4K, 2K, and Style Zero) 11 | - **Customizable parameters** including dimensions, guidance scale, and inference steps 12 | - **Batch generation** support for up to 5 images simultaneously 13 | - **ZIP download** option for generated image batches 14 | - **Seed control** with randomization option for reproducible results 15 | - **Negative prompting** support for better content control 16 | - **Real-time generation tracking** with timing information 17 | 18 | ## Requirements 19 | 20 | ### System Requirements 21 | - CUDA-compatible GPU (recommended: 8GB+ VRAM) 22 | - Python 3.8+ 23 | - CUDA toolkit installed 24 | 25 | ### Dependencies 26 | ``` 27 | torch 28 | gradio 29 | diffusers 30 | Pillow 31 | numpy 32 | spaces 33 | ``` 34 | 35 | ## Installation 36 | 37 | 1. Clone or download the application files 38 | 2. Install required dependencies: 39 | ```bash 40 | pip install torch gradio diffusers Pillow numpy spaces 41 | ``` 42 | 3. Ensure CUDA is properly configured for GPU acceleration 43 | 44 | ## Usage 45 | 46 | ### Running the Application 47 | 48 | Execute the main script to start the Gradio interface: 49 | ```bash 50 | python app.py 51 | ``` 52 | 53 | The application will launch a web interface accessible at `http://localhost:7860` 54 | 55 | ### Interface Components 56 | 57 | #### Main Controls 58 | - **Prompt Input**: Enter your image description 59 | - **Run Button**: Generate images based on current settings 60 | 61 | #### Quality Styles 62 | - **3840 x 2160**: 8K hyper-realistic output 63 | - **2560 x 1440**: 4K hyper-realistic output 64 | - **HD+**: 2K hyper-realistic output 65 | - **Style Zero**: Basic prompt without enhancement 66 | 67 | #### Advanced Options 68 | - **Negative Prompt**: Specify elements to exclude from generation 69 | - **Seed Control**: Set specific seed or use randomization 70 | - **Dimensions**: Adjust width and height (512-2048px) 71 | - **Guidance Scale**: Control adherence to prompt (0.1-20.0) 72 | - **Inference Steps**: Quality vs speed trade-off (1-40 steps) 73 | - **Batch Size**: Generate 1-5 images simultaneously 74 | - **ZIP Export**: Download all generated images as archive 75 | 76 | ### Example Prompts 77 | 78 | The application includes several example prompts demonstrating effective usage: 79 | 80 | 1. Professional portrait photography 81 | 2. Environmental character shots 82 | 3. Studio lighting setups 83 | 4. Artistic portrait compositions 84 | 85 | ## Model Information 86 | 87 | - **Base Model**: black-forest-labs/FLUX.1-dev 88 | - **LoRA Enhancement**: strangerzonehf/Flux-Super-Realism-LoRA 89 | - **Trigger Word**: "Super Realism" (automatically prepended) 90 | - **Precision**: bfloat16 for optimal performance 91 | 92 | ## Configuration 93 | 94 | ### Style Presets 95 | The application includes predefined style templates that automatically enhance prompts with quality descriptors: 96 | - Ultra-detailed rendering 97 | - Lifelike textures 98 | - High-resolution output 99 | - Sharp focus and vibrant colors 100 | - Photorealistic results 101 | 102 | ### Default Settings 103 | - Resolution: 1024x1024px 104 | - Guidance Scale: 3.0 105 | - Inference Steps: 30 106 | - Randomized seed enabled 107 | - Single image generation 108 | 109 | ## Performance Notes 110 | 111 | - GPU acceleration required for practical usage 112 | - Generation time varies based on resolution and step count 113 | - Higher inference steps improve quality but increase processing time 114 | - Batch generation processes images sequentially 115 | 116 | ## File Management 117 | 118 | Generated images are automatically saved with unique UUID filenames in PNG format. The ZIP export feature creates compressed archives containing all generated images with sequential naming. 119 | 120 | ## Troubleshooting 121 | 122 | ### Common Issues 123 | - **CUDA out of memory**: Reduce image dimensions or batch size 124 | - **Slow generation**: Decrease inference steps or resolution 125 | - **Model loading errors**: Ensure stable internet connection for initial model download 126 | 127 | ### Performance Optimization 128 | - Use lower guidance scales (2.0-4.0) for faster generation 129 | - Reduce inference steps for quicker results 130 | - Monitor VRAM usage with multiple image generation 131 | 132 | ## License 133 | 134 | This application uses models and components with their respective licenses: 135 | - FLUX.1-dev model licensing applies 136 | - LoRA weights subject to their repository terms 137 | - Application code available for modification and redistribution 138 | 139 | ## Support 140 | 141 | For technical issues or feature requests, refer to the respective model repositories: 142 | - FLUX.1-dev: black-forest-labs/FLUX.1-dev 143 | - Super Realism LoRA: strangerzonehf/Flux-Super-Realism-LoRA 144 | 145 | 146 | -------------------------------------------------------------------------------- /LICENSE: -------------------------------------------------------------------------------- 1 | Apache License 2 | Version 2.0, January 2004 3 | http://www.apache.org/licenses/ 4 | 5 | TERMS AND CONDITIONS FOR USE, REPRODUCTION, AND DISTRIBUTION 6 | 7 | 1. Definitions. 8 | 9 | "License" shall mean the terms and conditions for use, reproduction, 10 | and distribution as defined by Sections 1 through 9 of this document. 11 | 12 | "Licensor" shall mean the copyright owner or entity authorized by 13 | the copyright owner that is granting the License. 14 | 15 | "Legal Entity" shall mean the union of the acting entity and all 16 | other entities that control, are controlled by, or are under common 17 | control with that entity. For the purposes of this definition, 18 | "control" means (i) the power, direct or indirect, to cause the 19 | direction or management of such entity, whether by contract or 20 | otherwise, or (ii) ownership of fifty percent (50%) or more of the 21 | outstanding shares, or (iii) beneficial ownership of such entity. 22 | 23 | "You" (or "Your") shall mean an individual or Legal Entity 24 | exercising permissions granted by this License. 25 | 26 | "Source" form shall mean the preferred form for making modifications, 27 | including but not limited to software source code, documentation 28 | source, and configuration files. 29 | 30 | "Object" form shall mean any form resulting from mechanical 31 | transformation or translation of a Source form, including but 32 | not limited to compiled object code, generated documentation, 33 | and conversions to other media types. 34 | 35 | "Work" shall mean the work of authorship, whether in Source or 36 | Object form, made available under the License, as indicated by a 37 | copyright notice that is included in or attached to the work 38 | (an example is provided in the Appendix below). 39 | 40 | "Derivative Works" shall mean any work, whether in Source or Object 41 | form, that is based on (or derived from) the Work and for which the 42 | editorial revisions, annotations, elaborations, or other modifications 43 | represent, as a whole, an original work of authorship. For the purposes 44 | of this License, Derivative Works shall not include works that remain 45 | separable from, or merely link (or bind by name) to the interfaces of, 46 | the Work and Derivative Works thereof. 47 | 48 | "Contribution" shall mean any work of authorship, including 49 | the original version of the Work and any modifications or additions 50 | to that Work or Derivative Works thereof, that is intentionally 51 | submitted to Licensor for inclusion in the Work by the copyright owner 52 | or by an individual or Legal Entity authorized to submit on behalf of 53 | the copyright owner. For the purposes of this definition, "submitted" 54 | means any form of electronic, verbal, or written communication sent 55 | to the Licensor or its representatives, including but not limited to 56 | communication on electronic mailing lists, source code control systems, 57 | and issue tracking systems that are managed by, or on behalf of, the 58 | Licensor for the purpose of discussing and improving the Work, but 59 | excluding communication that is conspicuously marked or otherwise 60 | designated in writing by the copyright owner as "Not a Contribution." 61 | 62 | "Contributor" shall mean Licensor and any individual or Legal Entity 63 | on behalf of whom a Contribution has been received by Licensor and 64 | subsequently incorporated within the Work. 65 | 66 | 2. Grant of Copyright License. Subject to the terms and conditions of 67 | this License, each Contributor hereby grants to You a perpetual, 68 | worldwide, non-exclusive, no-charge, royalty-free, irrevocable 69 | copyright license to reproduce, prepare Derivative Works of, 70 | publicly display, publicly perform, sublicense, and distribute the 71 | Work and such Derivative Works in Source or Object form. 72 | 73 | 3. Grant of Patent License. Subject to the terms and conditions of 74 | this License, each Contributor hereby grants to You a perpetual, 75 | worldwide, non-exclusive, no-charge, royalty-free, irrevocable 76 | (except as stated in this section) patent license to make, have made, 77 | use, offer to sell, sell, import, and otherwise transfer the Work, 78 | where such license applies only to those patent claims licensable 79 | by such Contributor that are necessarily infringed by their 80 | Contribution(s) alone or by combination of their Contribution(s) 81 | with the Work to which such Contribution(s) was submitted. If You 82 | institute patent litigation against any entity (including a 83 | cross-claim or counterclaim in a lawsuit) alleging that the Work 84 | or a Contribution incorporated within the Work constitutes direct 85 | or contributory patent infringement, then any patent licenses 86 | granted to You under this License for that Work shall terminate 87 | as of the date such litigation is filed. 88 | 89 | 4. Redistribution. You may reproduce and distribute copies of the 90 | Work or Derivative Works thereof in any medium, with or without 91 | modifications, and in Source or Object form, provided that You 92 | meet the following conditions: 93 | 94 | (a) You must give any other recipients of the Work or 95 | Derivative Works a copy of this License; and 96 | 97 | (b) You must cause any modified files to carry prominent notices 98 | stating that You changed the files; and 99 | 100 | (c) You must retain, in the Source form of any Derivative Works 101 | that You distribute, all copyright, patent, trademark, and 102 | attribution notices from the Source form of the Work, 103 | excluding those notices that do not pertain to any part of 104 | the Derivative Works; and 105 | 106 | (d) If the Work includes a "NOTICE" text file as part of its 107 | distribution, then any Derivative Works that You distribute must 108 | include a readable copy of the attribution notices contained 109 | within such NOTICE file, excluding those notices that do not 110 | pertain to any part of the Derivative Works, in at least one 111 | of the following places: within a NOTICE text file distributed 112 | as part of the Derivative Works; within the Source form or 113 | documentation, if provided along with the Derivative Works; or, 114 | within a display generated by the Derivative Works, if and 115 | wherever such third-party notices normally appear. The contents 116 | of the NOTICE file are for informational purposes only and 117 | do not modify the License. You may add Your own attribution 118 | notices within Derivative Works that You distribute, alongside 119 | or as an addendum to the NOTICE text from the Work, provided 120 | that such additional attribution notices cannot be construed 121 | as modifying the License. 122 | 123 | You may add Your own copyright statement to Your modifications and 124 | may provide additional or different license terms and conditions 125 | for use, reproduction, or distribution of Your modifications, or 126 | for any such Derivative Works as a whole, provided Your use, 127 | reproduction, and distribution of the Work otherwise complies with 128 | the conditions stated in this License. 129 | 130 | 5. Submission of Contributions. Unless You explicitly state otherwise, 131 | any Contribution intentionally submitted for inclusion in the Work 132 | by You to the Licensor shall be under the terms and conditions of 133 | this License, without any additional terms or conditions. 134 | Notwithstanding the above, nothing herein shall supersede or modify 135 | the terms of any separate license agreement you may have executed 136 | with Licensor regarding such Contributions. 137 | 138 | 6. Trademarks. This License does not grant permission to use the trade 139 | names, trademarks, service marks, or product names of the Licensor, 140 | except as required for reasonable and customary use in describing the 141 | origin of the Work and reproducing the content of the NOTICE file. 142 | 143 | 7. Disclaimer of Warranty. Unless required by applicable law or 144 | agreed to in writing, Licensor provides the Work (and each 145 | Contributor provides its Contributions) on an "AS IS" BASIS, 146 | WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or 147 | implied, including, without limitation, any warranties or conditions 148 | of TITLE, NON-INFRINGEMENT, MERCHANTABILITY, or FITNESS FOR A 149 | PARTICULAR PURPOSE. You are solely responsible for determining the 150 | appropriateness of using or redistributing the Work and assume any 151 | risks associated with Your exercise of permissions under this License. 152 | 153 | 8. Limitation of Liability. In no event and under no legal theory, 154 | whether in tort (including negligence), contract, or otherwise, 155 | unless required by applicable law (such as deliberate and grossly 156 | negligent acts) or agreed to in writing, shall any Contributor be 157 | liable to You for damages, including any direct, indirect, special, 158 | incidental, or consequential damages of any character arising as a 159 | result of this License or out of the use or inability to use the 160 | Work (including but not limited to damages for loss of goodwill, 161 | work stoppage, computer failure or malfunction, or any and all 162 | other commercial damages or losses), even if such Contributor 163 | has been advised of the possibility of such damages. 164 | 165 | 9. Accepting Warranty or Additional Liability. While redistributing 166 | the Work or Derivative Works thereof, You may choose to offer, 167 | and charge a fee for, acceptance of support, warranty, indemnity, 168 | or other liability obligations and/or rights consistent with this 169 | License. However, in accepting such obligations, You may act only 170 | on Your own behalf and on Your sole responsibility, not on behalf 171 | of any other Contributor, and only if You agree to indemnify, 172 | defend, and hold each Contributor harmless for any liability 173 | incurred by, or claims asserted against, such Contributor by reason 174 | of your accepting any such warranty or additional liability. 175 | 176 | END OF TERMS AND CONDITIONS 177 | 178 | APPENDIX: How to apply the Apache License to your work. 179 | 180 | To apply the Apache License to your work, attach the following 181 | boilerplate notice, with the fields enclosed by brackets "[]" 182 | replaced with your own identifying information. (Don't include 183 | the brackets!) The text should be enclosed in the appropriate 184 | comment syntax for the file format. We also recommend that a 185 | file or class name and description of purpose be included on the 186 | same "printed page" as the copyright notice for easier 187 | identification within third-party archives. 188 | 189 | Copyright [yyyy] [name of copyright owner] 190 | 191 | Licensed under the Apache License, Version 2.0 (the "License"); 192 | you may not use this file except in compliance with the License. 193 | You may obtain a copy of the License at 194 | 195 | http://www.apache.org/licenses/LICENSE-2.0 196 | 197 | Unless required by applicable law or agreed to in writing, software 198 | distributed under the License is distributed on an "AS IS" BASIS, 199 | WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 200 | See the License for the specific language governing permissions and 201 | limitations under the License. 202 | -------------------------------------------------------------------------------- /app.py: -------------------------------------------------------------------------------- 1 | import spaces 2 | import os 3 | import gradio as gr 4 | import torch 5 | from PIL import Image 6 | from diffusers import DiffusionPipeline, AutoencoderTiny, AutoencoderKL 7 | import random 8 | import uuid 9 | from typing import Tuple, Union, List, Optional, Any, Dict 10 | import numpy as np 11 | import time 12 | import zipfile 13 | from transformers import CLIPTextModel, CLIPTokenizer, T5EncoderModel, T5TokenizerFast 14 | 15 | # ---- CUDA Check ---- 16 | print("CUDA_VISIBLE_DEVICES=", os.environ.get("CUDA_VISIBLE_DEVICES")) 17 | print("torch.__version__ =", torch.__version__) 18 | print("torch.version.cuda =", torch.version.cuda) 19 | print("cuda available:", torch.cuda.is_available()) 20 | print("cuda device count:", torch.cuda.device_count()) 21 | if torch.cuda.is_available(): 22 | print("current device:", torch.cuda.current_device()) 23 | print("device name:", torch.cuda.get_device_name(torch.cuda.current_device())) 24 | 25 | # Description for the app 26 | DESCRIPTION = """## flux realism hpc/.""" 27 | 28 | # Helper functions 29 | def save_image(img): 30 | unique_name = str(uuid.uuid4()) + ".png" 31 | img.save(unique_name) 32 | return unique_name 33 | 34 | def randomize_seed_fn(seed: int, randomize_seed: bool) -> int: 35 | if randomize_seed: 36 | seed = random.randint(0, MAX_SEED) 37 | return seed 38 | 39 | MAX_SEED = np.iinfo(np.int32).max 40 | MAX_IMAGE_SIZE = 2048 41 | 42 | # Load pipelines for both models 43 | # Flux.1-dev-realism 44 | base_model_dev = "black-forest-labs/FLUX.1-dev" 45 | pipe_dev = DiffusionPipeline.from_pretrained(base_model_dev, torch_dtype=torch.bfloat16) 46 | lora_repo = "strangerzonehf/Flux-Super-Realism-LoRA" 47 | trigger_word = "Super Realism" 48 | pipe_dev.load_lora_weights(lora_repo) 49 | pipe_dev.to("cuda") 50 | 51 | # Flux.1-krea 52 | dtype = torch.bfloat16 53 | device = torch.device("cuda" if torch.cuda.is_available() else "cpu") 54 | 55 | # --- Model Loading --- 56 | taef1 = AutoencoderTiny.from_pretrained("madebyollin/taef1", torch_dtype=dtype).to(device) 57 | good_vae = AutoencoderKL.from_pretrained("black-forest-labs/FLUX.1-Krea-dev", subfolder="vae", torch_dtype=dtype).to(device) 58 | pipe_krea = DiffusionPipeline.from_pretrained("black-forest-labs/FLUX.1-Krea-dev", torch_dtype=dtype, vae=taef1).to(device) 59 | 60 | # Define the flux_pipe_call_that_returns_an_iterable_of_images for flux.1-krea 61 | @torch.inference_mode() 62 | def flux_pipe_call_that_returns_an_iterable_of_images( 63 | self, 64 | prompt: Union[str, List[str]] = None, 65 | prompt_2: Optional[Union[str, List[str]]] = None, 66 | height: Optional[int] = None, 67 | width: Optional[int] = None, 68 | num_inference_steps: int = 28, 69 | timesteps: List[int] = None, 70 | guidance_scale: float = 3.5, 71 | num_images_per_prompt: Optional[int] = 1, 72 | generator: Optional[Union[torch.Generator, List[torch.Generator]]] = None, 73 | latents: Optional[torch.FloatTensor] = None, 74 | prompt_embeds: Optional[torch.FloatTensor] = None, 75 | pooled_prompt_embeds: Optional[torch.FloatTensor] = None, 76 | output_type: Optional[str] = "pil", 77 | return_dict: bool = True, 78 | joint_attention_kwargs: Optional[Dict[str, Any]] = None, 79 | max_sequence_length: int = 512, 80 | good_vae: Optional[Any] = None, 81 | ): 82 | height = height or self.default_sample_size * self.vae_scale_factor 83 | width = width or self.default_sample_size * self.vae_scale_factor 84 | 85 | self.check_inputs( 86 | prompt, 87 | prompt_2, 88 | height, 89 | width, 90 | prompt_embeds=prompt_embeds, 91 | pooled_prompt_embeds=pooled_prompt_embeds, 92 | max_sequence_length=max_sequence_length, 93 | ) 94 | 95 | self._guidance_scale = guidance_scale 96 | self._joint_attention_kwargs = joint_attention_kwargs 97 | self._interrupt = False 98 | 99 | batch_size = 1 if isinstance(prompt, str) else len(prompt) 100 | device = self._execution_device 101 | 102 | lora_scale = joint_attention_kwargs.get("scale", None) if joint_attention_kwargs is not None else None 103 | prompt_embeds, pooled_prompt_embeds, text_ids = self.encode_prompt( 104 | prompt=prompt, 105 | prompt_2=prompt_2, 106 | prompt_embeds=prompt_embeds, 107 | pooled_prompt_embeds=pooled_prompt_embeds, 108 | device=device, 109 | num_images_per_prompt=num_images_per_prompt, 110 | max_sequence_length=max_sequence_length, 111 | lora_scale=lora_scale, 112 | ) 113 | 114 | num_channels_latents = self.transformer.config.in_channels // 4 115 | latents, latent_image_ids = self.prepare_latents( 116 | batch_size * num_images_per_prompt, 117 | num_channels_latents, 118 | height, 119 | width, 120 | prompt_embeds.dtype, 121 | device, 122 | generator, 123 | latents, 124 | ) 125 | 126 | sigmas = np.linspace(1.0, 1 / num_inference_steps, num_inference_steps) 127 | image_seq_len = latents.shape[1] 128 | mu = calculate_shift( 129 | image_seq_len, 130 | self.scheduler.config.base_image_seq_len, 131 | self.scheduler.config.max_image_seq_len, 132 | self.scheduler.config.base_shift, 133 | self.scheduler.config.max_shift, 134 | ) 135 | timesteps, num_inference_steps = retrieve_timesteps( 136 | self.scheduler, 137 | num_inference_steps, 138 | device, 139 | timesteps, 140 | sigmas, 141 | mu=mu, 142 | ) 143 | self._num_timesteps = len(timesteps) 144 | 145 | guidance = torch.full([1], guidance_scale, device=device, dtype=torch.float32).expand(latents.shape[0]) if self.transformer.config.guidance_embeds else None 146 | 147 | for i, t in enumerate(timesteps): 148 | if self.interrupt: 149 | continue 150 | 151 | timestep = t.expand(latents.shape[0]).to(latents.dtype) 152 | 153 | noise_pred = self.transformer( 154 | hidden_states=latents, 155 | timestep=timestep / 1000, 156 | guidance=guidance, 157 | pooled_projections=pooled_prompt_embeds, 158 | encoder_hidden_states=prompt_embeds, 159 | txt_ids=text_ids, 160 | img_ids=latent_image_ids, 161 | joint_attention_kwargs=self.joint_attention_kwargs, 162 | return_dict=False, 163 | )[0] 164 | 165 | latents_for_image = self._unpack_latents(latents, height, width, self.vae_scale_factor) 166 | latents_for_image = (latents_for_image / self.vae.config.scaling_factor) + self.vae.config.shift_factor 167 | image = self.vae.decode(latents_for_image, return_dict=False)[0] 168 | yield self.image_processor.postprocess(image, output_type=output_type)[0] 169 | 170 | latents = self.scheduler.step(noise_pred, t, latents, return_dict=False)[0] 171 | torch.cuda.empty_cache() 172 | 173 | latents = self._unpack_latents(latents, height, width, self.vae_scale_factor) 174 | latents = (latents / good_vae.config.scaling_factor) + good_vae.config.shift_factor 175 | image = good_vae.decode(latents, return_dict=False)[0] 176 | self.maybe_free_model_hooks() 177 | torch.cuda.empty_cache() 178 | yield self.image_processor.postprocess(image, output_type=output_type)[0] 179 | 180 | pipe_krea.flux_pipe_call_that_returns_an_iterable_of_images = flux_pipe_call_that_returns_an_iterable_of_images.__get__(pipe_krea) 181 | 182 | # Helper functions for flux.1-krea 183 | def calculate_shift( 184 | image_seq_len, 185 | base_seq_len: int = 256, 186 | max_seq_len: int = 4096, 187 | base_shift: float = 0.5, 188 | max_shift: float = 1.16, 189 | ): 190 | m = (max_shift - base_shift) / (max_seq_len - base_seq_len) 191 | b = base_shift - m * base_seq_len 192 | mu = image_seq_len * m + b 193 | return mu 194 | 195 | def retrieve_timesteps( 196 | scheduler, 197 | num_inference_steps: Optional[int] = None, 198 | device: Optional[Union[str, torch.device]] = None, 199 | timesteps: Optional[List[int]] = None, 200 | sigmas: Optional[List[float]] = None, 201 | **kwargs, 202 | ): 203 | if timesteps is not None and sigmas is not None: 204 | raise ValueError("Only one of `timesteps` or `sigmas` can be passed.") 205 | if timesteps is not None: 206 | scheduler.set_timesteps(timesteps=timesteps, device=device, **kwargs) 207 | timesteps = scheduler.timesteps 208 | num_inference_steps = len(timesteps) 209 | elif sigmas is not None: 210 | scheduler.set_timesteps(sigmas=sigmas, device=device, **kwargs) 211 | timesteps = scheduler.timesteps 212 | num_inference_steps = len(timesteps) 213 | else: 214 | scheduler.set_timesteps(num_inference_steps, device=device, **kwargs) 215 | timesteps = scheduler.timesteps 216 | return timesteps, num_inference_steps 217 | 218 | # Styles for flux.1-dev-realism 219 | style_list = [ 220 | {"name": "3840 x 2160", "prompt": "hyper-realistic 8K image of {prompt}. ultra-detailed, lifelike, high-resolution, sharp, vibrant colors, photorealistic", "negative_prompt": ""}, 221 | {"name": "2560 x 1440", "prompt": "hyper-realistic 4K image of {prompt}. ultra-detailed, lifelike, high-resolution, sharp, vibrant colors, photorealistic", "negative_prompt": ""}, 222 | {"name": "HD+", "prompt": "hyper-realistic 2K image of {prompt}. ultra-detailed, lifelike, high-resolution, sharp, vibrant colors, photorealistic", "negative_prompt": ""}, 223 | {"name": "Style Zero", "prompt": "{prompt}", "negative_prompt": ""}, 224 | ] 225 | 226 | styles = {k["name"]: (k["prompt"], k["negative_prompt"]) for k in style_list} 227 | DEFAULT_STYLE_NAME = "3840 x 2160" 228 | STYLE_NAMES = list(styles.keys()) 229 | 230 | def apply_style(style_name: str, positive: str) -> Tuple[str, str]: 231 | p, n = styles.get(style_name, styles[DEFAULT_STYLE_NAME]) 232 | return p.replace("{prompt}", positive), n 233 | 234 | # Generation function for flux.1-dev-realism 235 | @spaces.GPU 236 | def generate_dev( 237 | prompt: str, 238 | negative_prompt: str = "", 239 | use_negative_prompt: bool = False, 240 | seed: int = 0, 241 | width: int = 1024, 242 | height: int = 1024, 243 | guidance_scale: float = 3, 244 | randomize_seed: bool = False, 245 | style_name: str = DEFAULT_STYLE_NAME, 246 | num_inference_steps: int = 30, 247 | num_images: int = 1, 248 | zip_images: bool = False, 249 | progress=gr.Progress(track_tqdm=True), 250 | ): 251 | positive_prompt, style_negative_prompt = apply_style(style_name, prompt) 252 | 253 | if use_negative_prompt: 254 | final_negative_prompt = style_negative_prompt + " " + negative_prompt 255 | else: 256 | final_negative_prompt = style_negative_prompt 257 | 258 | final_negative_prompt = final_negative_prompt.strip() 259 | 260 | if trigger_word: 261 | positive_prompt = f"{trigger_word} {positive_prompt}" 262 | 263 | seed = int(randomize_seed_fn(seed, randomize_seed)) 264 | generator = torch.Generator(device="cuda").manual_seed(seed) 265 | 266 | start_time = time.time() 267 | 268 | images = pipe_dev( 269 | prompt=positive_prompt, 270 | negative_prompt=final_negative_prompt if final_negative_prompt else None, 271 | width=width, 272 | height=height, 273 | guidance_scale=guidance_scale, 274 | num_inference_steps=num_inference_steps, 275 | num_images_per_prompt=num_images, 276 | generator=generator, 277 | output_type="pil", 278 | ).images 279 | 280 | end_time = time.time() 281 | duration = end_time - start_time 282 | 283 | image_paths = [save_image(img) for img in images] 284 | 285 | zip_path = None 286 | if zip_images: 287 | zip_name = str(uuid.uuid4()) + ".zip" 288 | with zipfile.ZipFile(zip_name, 'w') as zipf: 289 | for i, img_path in enumerate(image_paths): 290 | zipf.write(img_path, arcname=f"Img_{i}.png") 291 | zip_path = zip_name 292 | 293 | return image_paths, seed, f"{duration:.2f}", zip_path 294 | 295 | # Generation function for flux.1-krea 296 | @spaces.GPU 297 | def generate_krea( 298 | prompt: str, 299 | seed: int = 0, 300 | width: int = 1024, 301 | height: int = 1024, 302 | guidance_scale: float = 4.5, 303 | randomize_seed: bool = False, 304 | num_inference_steps: int = 28, 305 | num_images: int = 1, 306 | zip_images: bool = False, 307 | progress=gr.Progress(track_tqdm=True), 308 | ): 309 | if randomize_seed: 310 | seed = random.randint(0, MAX_SEED) 311 | generator = torch.Generator().manual_seed(seed) 312 | 313 | start_time = time.time() 314 | 315 | images = [] 316 | for _ in range(num_images): 317 | final_img = list(pipe_krea.flux_pipe_call_that_returns_an_iterable_of_images( 318 | prompt=prompt, 319 | guidance_scale=guidance_scale, 320 | num_inference_steps=num_inference_steps, 321 | width=width, 322 | height=height, 323 | generator=generator, 324 | output_type="pil", 325 | good_vae=good_vae, 326 | ))[-1] # Take the final image only 327 | images.append(final_img) 328 | 329 | end_time = time.time() 330 | duration = end_time - start_time 331 | 332 | image_paths = [save_image(img) for img in images] 333 | 334 | zip_path = None 335 | if zip_images: 336 | zip_name = str(uuid.uuid4()) + ".zip" 337 | with zipfile.ZipFile(zip_name, 'w') as zipf: 338 | for i, img_path in enumerate(image_paths): 339 | zipf.write(img_path, arcname=f"Img_{i}.png") 340 | zip_path = zip_name 341 | 342 | return image_paths, seed, f"{duration:.2f}", zip_path 343 | 344 | # Main generation function to handle model choice 345 | @spaces.GPU 346 | def generate( 347 | model_choice: str, 348 | prompt: str, 349 | negative_prompt: str = "", 350 | use_negative_prompt: bool = False, 351 | seed: int = 0, 352 | width: int = 1024, 353 | height: int = 1024, 354 | guidance_scale: float = 3, 355 | randomize_seed: bool = False, 356 | style_name: str = DEFAULT_STYLE_NAME, 357 | num_inference_steps: int = 30, 358 | num_images: int = 1, 359 | zip_images: bool = False, 360 | progress=gr.Progress(track_tqdm=True), 361 | ): 362 | if model_choice == "flux.1-dev-realism": 363 | return generate_dev( 364 | prompt=prompt, 365 | negative_prompt=negative_prompt, 366 | use_negative_prompt=use_negative_prompt, 367 | seed=seed, 368 | width=width, 369 | height=height, 370 | guidance_scale=guidance_scale, 371 | randomize_seed=randomize_seed, 372 | style_name=style_name, 373 | num_inference_steps=num_inference_steps, 374 | num_images=num_images, 375 | zip_images=zip_images, 376 | progress=progress, 377 | ) 378 | elif model_choice == "flux.1-krea-dev": 379 | return generate_krea( 380 | prompt=prompt, 381 | seed=seed, 382 | width=width, 383 | height=height, 384 | guidance_scale=guidance_scale, 385 | randomize_seed=randomize_seed, 386 | num_inference_steps=num_inference_steps, 387 | num_images=num_images, 388 | zip_images=zip_images, 389 | progress=progress, 390 | ) 391 | else: 392 | raise ValueError("Invalid model choice") 393 | 394 | # Examples (tailored for flux.1-dev-realism) 395 | examples = [ 396 | "An attractive young woman with blue eyes lying face down on the bed, in the style of animated gifs, light white and light amber, jagged edges, the snapshot aesthetic, timeless beauty, goosepunk, sunrays shine upon it --no freckles --chaos 65 --ar 1:2 --profile yruxpc2 --stylize 750 --v 6.1", 397 | "Headshot of handsome young man, wearing dark gray sweater with buttons and big shawl collar, brown hair and short beard, serious look on his face, black background, soft studio lighting, portrait photography --ar 85:128 --v 6.0 --style", 398 | "Purple Dreamy, a medium-angle shot of a young woman with long brown hair, wearing a pair of eye-level glasses, stands in front of a backdrop of purple and white lights.", 399 | "High-resolution photograph, woman, UHD, photorealistic, shot on a Sony A7III --chaos 20 --ar 1:2 --style raw --stylize 250" 400 | ] 401 | 402 | css = ''' 403 | .gradio-container { 404 | max-width: 590px !important; 405 | margin: 0 auto !important; 406 | } 407 | h1 { 408 | text-align: center; 409 | } 410 | footer { 411 | visibility: hidden; 412 | } 413 | ''' 414 | 415 | # Gradio interface 416 | with gr.Blocks() as demo: 417 | gr.Markdown(DESCRIPTION) 418 | with gr.Row(): 419 | prompt = gr.Text( 420 | label="Prompt", 421 | show_label=False, 422 | max_lines=1, 423 | placeholder="Enter your prompt", 424 | container=False, 425 | ) 426 | run_button = gr.Button("Run", scale=0, variant="primary") 427 | result = gr.Gallery(label="Result", columns=1, show_label=False, preview=True) 428 | 429 | with gr.Row(): 430 | # Model choice radio button above additional options 431 | model_choice = gr.Radio( 432 | choices=["flux.1-krea-dev", "flux.1-dev-realism"], 433 | label="Select Model", 434 | value="flux.1-krea-dev" 435 | ) 436 | 437 | with gr.Accordion("Additional Options", open=False): 438 | style_selection = gr.Dropdown( 439 | label="Quality Style (for flux.1-dev-realism only)", 440 | choices=STYLE_NAMES, 441 | value=DEFAULT_STYLE_NAME, 442 | interactive=True, 443 | ) 444 | use_negative_prompt = gr.Checkbox(label="Use negative prompt (for flux.1-dev-realism only)", value=False) 445 | negative_prompt = gr.Text( 446 | label="Negative prompt", 447 | max_lines=1, 448 | placeholder="Enter a negative prompt", 449 | visible=False, 450 | ) 451 | seed = gr.Slider( 452 | label="Seed", 453 | minimum=0, 454 | maximum=MAX_SEED, 455 | step=1, 456 | value=0, 457 | ) 458 | randomize_seed = gr.Checkbox(label="Randomize seed", value=True) 459 | with gr.Row(): 460 | width = gr.Slider( 461 | label="Width", 462 | minimum=512, 463 | maximum=2048, 464 | step=64, 465 | value=1024, 466 | ) 467 | height = gr.Slider( 468 | label="Height", 469 | minimum=512, 470 | maximum=2048, 471 | step=64, 472 | value=1024, 473 | ) 474 | guidance_scale = gr.Slider( 475 | label="Guidance Scale", 476 | minimum=0.1, 477 | maximum=20.0, 478 | step=0.1, 479 | value=4.5, 480 | ) 481 | num_inference_steps = gr.Slider( 482 | label="Number of inference steps", 483 | minimum=1, 484 | maximum=40, 485 | step=1, 486 | value=28, 487 | ) 488 | num_images = gr.Slider( 489 | label="Number of images", 490 | minimum=1, 491 | maximum=5, 492 | step=1, 493 | value=1, 494 | ) 495 | zip_images = gr.Checkbox(label="Zip generated images", value=False) 496 | 497 | gr.Markdown("### Output Information") 498 | seed_display = gr.Textbox(label="Seed used", interactive=False) 499 | generation_time = gr.Textbox(label="Generation time (seconds)", interactive=False) 500 | zip_file = gr.File(label="Download ZIP") 501 | 502 | gr.Examples( 503 | examples=examples, 504 | inputs=prompt, 505 | outputs=[result, seed_display, generation_time, zip_file], 506 | fn=generate, 507 | cache_examples=False, 508 | ) 509 | 510 | use_negative_prompt.change( 511 | fn=lambda x: gr.update(visible=x), 512 | inputs=use_negative_prompt, 513 | outputs=negative_prompt, 514 | api_name=False, 515 | ) 516 | 517 | gr.on( 518 | triggers=[ 519 | prompt.submit, 520 | run_button.click, 521 | ], 522 | fn=generate, 523 | inputs=[ 524 | model_choice, 525 | prompt, 526 | negative_prompt, 527 | use_negative_prompt, 528 | seed, 529 | width, 530 | height, 531 | guidance_scale, 532 | randomize_seed, 533 | style_selection, 534 | num_inference_steps, 535 | num_images, 536 | zip_images, 537 | ], 538 | outputs=[result, seed_display, generation_time, zip_file], 539 | api_name="run", 540 | ) 541 | 542 | if __name__ == "__main__": 543 | demo.queue(max_size=30).launch(css=css, theme="bethecloud/storj_theme", mcp_server=True, ssr_mode=False, show_error=True) 544 | --------------------------------------------------------------------------------