realflux / app.py
jiuface's picture
Update app.py
9a7c2c6 verified
raw
history blame
5.36 kB
import spaces
import gradio as gr
import torch
from PIL import Image
from diffusers import DiffusionPipeline
import random
from huggingface_hub import login
import os
from diffusers import FluxTransformer2DModel, FluxPipeline
from transformers import T5EncoderModel, CLIPTextModel
from optimum.quanto import freeze, qfloat8, quantize
torch.backends.cudnn.deterministic = True
torch.backends.cudnn.benchmark = False
torch.backends.cuda.matmul.allow_tf32 = True
# Login Hugging Face Hub
HF_TOKEN = os.environ.get("HF_TOKEN")
login(token=HF_TOKEN)
bfl_repo = "black-forest-labs/FLUX.1-dev"
dtype = torch.bfloat16
transformer = FluxTransformer2DModel.from_single_file("https://huggingface.co./SG161222/RealFlux_1.0b_Dev/resolve/main/1%20-%20Compact%20Version/RealFlux_1.0b_Dev_Compact.safetensors", torch_dtype=dtype)
quantize(transformer, weights=qfloat8)
freeze(transformer)
text_encoder_2 = T5EncoderModel.from_pretrained(bfl_repo, subfolder="text_encoder_2", torch_dtype=dtype)
quantize(text_encoder_2, weights=qfloat8)
freeze(text_encoder_2)
pipe = FluxPipeline.from_pretrained(bfl_repo, transformer=None, text_encoder_2=None, torch_dtype=dtype)
pipe.transformer = transformer
pipe.text_encoder_2 = text_encoder_2
pipe.to("cuda")
MAX_SEED = 2**32-1
@spaces.GPU()
def run_lora(prompt, cfg_scale, steps, randomize_seed, seed, width, height, lora_scale, progress=gr.Progress(track_tqdm=True)):
# Set random seed for reproducibility
if randomize_seed:
seed = random.randint(0, MAX_SEED)
generator = torch.Generator(device="cuda").manual_seed(seed)
# Update progress bar (0% saat mulai)
progress(0, "Starting image generation...")
# Generate image with progress updates
for i in range(1, steps + 1):
# Simulate the processing step (in a real scenario, you would integrate this with your image generation process)
if i % (steps // 10) == 0: # Update every 10% of the steps
progress(i / steps * 100, f"Processing step {i} of {steps}...")
# Generate image using the pipeline
image = pipe(
prompt=f"{prompt} {trigger_word}",
num_inference_steps=steps,
guidance_scale=cfg_scale,
width=width,
height=height,
generator=generator,
joint_attention_kwargs={"scale": lora_scale},
).images[0]
# Final update (100%)
progress(100, "Completed!")
yield image, seed
# Example cached image and settings
example_image_path = "example0.webp" # Replace with the actual path to the example image
example_prompt = """A Jelita Sukawati speaker is captured mid-speech. She has long, dark brown hair that cascades over her shoulders, framing her radiant, smiling face. Her Latina features are highlighted by warm, sun-kissed skin and bright, expressive eyes. She gestures with her left hand, displaying a delicate ring on her pinky finger, as she speaks passionately.
The woman is wearing a colorful, patterned dress with a green lanyard featuring multiple badges and logos hanging around her neck. The lanyard prominently displays the "CagliostroLab" text.
Behind her, there is a blurred background with a white banner containing logos and text, indicating a professional or conference setting. The overall scene captures the energy and vibrancy of her presentation."""
example_cfg_scale = 3.2
example_steps = 32
example_width = 1152
example_height = 896
example_seed = 3981632454
example_lora_scale = 0.85
def load_example():
# Load example image from file
example_image = Image.open(example_image_path)
return example_prompt, example_cfg_scale, example_steps, True, example_seed, example_width, example_height, example_lora_scale, example_image
with gr.Blocks() as app:
gr.Markdown("# Flux RealismLora Image Generator")
with gr.Row():
with gr.Column(scale=3):
prompt = gr.TextArea(label="Prompt", placeholder="Type a prompt", lines=5)
generate_button = gr.Button("Generate")
cfg_scale = gr.Slider(label="CFG Scale", minimum=1, maximum=20, step=0.5, value=example_cfg_scale)
steps = gr.Slider(label="Steps", minimum=1, maximum=100, step=1, value=example_steps)
width = gr.Slider(label="Width", minimum=256, maximum=1536, step=64, value=example_width)
height = gr.Slider(label="Height", minimum=256, maximum=1536, step=64, value=example_height)
randomize_seed = gr.Checkbox(True, label="Randomize seed")
seed = gr.Slider(label="Seed", minimum=0, maximum=MAX_SEED, step=1, value=example_seed)
lora_scale = gr.Slider(label="LoRA Scale", minimum=0, maximum=1, step=0.01, value=example_lora_scale)
with gr.Column(scale=1):
result = gr.Image(label="Generated Image")
gr.Markdown("Generate images using RealismLora and a text prompt.\n[[non-commercial license, Flux.1 Dev](https://huggingface.co./black-forest-labs/FLUX.1-dev/blob/main/LICENSE.md)]")
# Automatically load example data and image when the interface is launched
app.load(load_example, inputs=[], outputs=[prompt, cfg_scale, steps, randomize_seed, seed, width, height, lora_scale, result])
generate_button.click(
run_lora,
inputs=[prompt, cfg_scale, steps, randomize_seed, seed, width, height, lora_scale],
outputs=[result, seed]
)
app.queue()
app.launch()