mgbam commited on
Commit
2414743
·
1 Parent(s): ec04b70

Add application file

Browse files
Files changed (1) hide show
  1. app.py +148 -0
app.py ADDED
@@ -0,0 +1,148 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import random
2
+ import torch
3
+ from PIL import Image
4
+ import gradio as gr
5
+ from diffusers import DiffusionPipeline
6
+
7
+ # Configure deterministic behavior for reproducibility
8
+ torch.backends.cudnn.deterministic = True
9
+ torch.backends.cudnn.benchmark = False
10
+ torch.backends.cuda.matmul.allow_tf32 = True
11
+
12
+ MAX_SEED = 2**32 - 1
13
+
14
+ class ModelManager:
15
+ """
16
+ Handles model initialization, LoRA weight loading, and image generation.
17
+ """
18
+ def __init__(self, base_model: str, lora_repo: str, trigger_word: str = ""):
19
+ self.trigger_word = trigger_word
20
+ self.pipe = DiffusionPipeline.from_pretrained(base_model, torch_dtype=torch.bfloat16)
21
+ self.pipe.load_lora_weights(lora_repo)
22
+ self.pipe.to("cuda")
23
+
24
+ def generate_image(self, prompt: str, cfg_scale: float, steps: int, seed: int,
25
+ width: int, height: int, lora_scale: float, progress_callback) -> Image.Image:
26
+ """
27
+ Generates an image based on the given prompt and parameters using a callback for progress updates.
28
+ """
29
+ # Establish reproducible generator
30
+ generator = torch.Generator(device="cuda").manual_seed(seed)
31
+ full_prompt = f"{prompt} {self.trigger_word}"
32
+
33
+ def callback_fn(step: int, timestep: int, latents):
34
+ percentage = int((step / steps) * 100)
35
+ message = f"Processing step {step} of {steps}..."
36
+ progress_callback(percentage, message)
37
+
38
+ # Generate image with integrated progress reporting
39
+ image = self.pipe(
40
+ prompt=full_prompt,
41
+ num_inference_steps=steps,
42
+ guidance_scale=cfg_scale,
43
+ width=width,
44
+ height=height,
45
+ generator=generator,
46
+ joint_attention_kwargs={"scale": lora_scale},
47
+ callback=callback_fn,
48
+ callback_steps=1,
49
+ ).images[0]
50
+
51
+ return image
52
+
53
+ # Initialize the model manager with specified models and LoRA weights
54
+ model_manager = ModelManager(
55
+ base_model="black-forest-labs/FLUX.1-dev",
56
+ lora_repo="XLabs-AI/flux-RealismLora",
57
+ trigger_word=""
58
+ )
59
+
60
+ def run_generation(prompt, cfg_scale, steps, randomize_seed, seed, width, height, lora_scale, progress=gr.Progress(track_tqdm=True)):
61
+ """
62
+ Gradio interface callback to manage seed randomization, progress updates,
63
+ and image generation using the ModelManager.
64
+ """
65
+ if randomize_seed:
66
+ seed = random.randint(0, MAX_SEED)
67
+
68
+ # Start the progress
69
+ progress(0, "Starting image generation...")
70
+
71
+ # Generate the image using the model manager with progress callback integration
72
+ image = model_manager.generate_image(
73
+ prompt, cfg_scale, steps, seed, width, height, lora_scale, progress
74
+ )
75
+
76
+ # Mark completion
77
+ progress(100, "Completed!")
78
+ return image, seed
79
+
80
+ # Example parameters and image path for initializing the interface with defaults
81
+ example_image_path = "example0.webp"
82
+ example_prompt = (
83
+ "A Jelita Sukawati speaker is captured mid-speech. She has long, dark brown hair that cascades over her shoulders, "
84
+ "framing her radiant, smiling face. Her Latina features are highlighted by warm, sun-kissed skin and bright, "
85
+ "expressive eyes. She gestures with her left hand, displaying a delicate ring on her pinky finger, as she speaks passionately. "
86
+ "The woman is wearing a colorful, patterned dress with a green lanyard featuring multiple badges and logos hanging around her neck. "
87
+ "The lanyard prominently displays the 'CagliostroLab' text. Behind her, there is a blurred background with a white banner "
88
+ "containing logos and text, indicating a professional or conference setting. The overall scene captures the energy and vibrancy "
89
+ "of her presentation."
90
+ )
91
+ example_cfg_scale = 3.2
92
+ example_steps = 32
93
+ example_width = 1152
94
+ example_height = 896
95
+ example_seed = 3981632454
96
+ example_lora_scale = 0.85
97
+
98
+ def load_example():
99
+ # Load example image for initial display
100
+ example_image = Image.open(example_image_path)
101
+ return (
102
+ example_prompt,
103
+ example_cfg_scale,
104
+ example_steps,
105
+ True,
106
+ example_seed,
107
+ example_width,
108
+ example_height,
109
+ example_lora_scale,
110
+ example_image
111
+ )
112
+
113
+ with gr.Blocks() as app:
114
+ gr.Markdown("# Flux RealismLora Image Generator")
115
+ with gr.Row():
116
+ with gr.Column(scale=3):
117
+ prompt = gr.TextArea(label="Prompt", placeholder="Type a prompt", lines=5)
118
+ generate_button = gr.Button("Generate")
119
+ cfg_scale = gr.Slider(label="CFG Scale", minimum=1, maximum=20, step=0.5, value=example_cfg_scale)
120
+ steps = gr.Slider(label="Steps", minimum=1, maximum=100, step=1, value=example_steps)
121
+ width = gr.Slider(label="Width", minimum=256, maximum=1536, step=64, value=example_width)
122
+ height = gr.Slider(label="Height", minimum=256, maximum=1536, step=64, value=example_height)
123
+ randomize_seed = gr.Checkbox(True, label="Randomize seed")
124
+ seed = gr.Slider(label="Seed", minimum=0, maximum=MAX_SEED, step=1, value=example_seed)
125
+ lora_scale = gr.Slider(label="LoRA Scale", minimum=0, maximum=1, step=0.01, value=example_lora_scale)
126
+ with gr.Column(scale=1):
127
+ result = gr.Image(label="Generated Image")
128
+ gr.Markdown(
129
+ "Generate images using RealismLora and a text prompt.\n"
130
+ "[[non-commercial license, Flux.1 Dev](https://huggingface.co/black-forest-labs/FLUX.1-dev/blob/main/LICENSE.md)]"
131
+ )
132
+
133
+ # Load example data on launch
134
+ app.load(
135
+ load_example,
136
+ inputs=[],
137
+ outputs=[prompt, cfg_scale, steps, randomize_seed, seed, width, height, lora_scale, result]
138
+ )
139
+
140
+ # Set up button interaction
141
+ generate_button.click(
142
+ run_generation,
143
+ inputs=[prompt, cfg_scale, steps, randomize_seed, seed, width, height, lora_scale],
144
+ outputs=[result, seed]
145
+ )
146
+
147
+ app.queue()
148
+ app.launch()