nightfury commited on
Commit
e9af91e
·
verified ·
1 Parent(s): fceab02

Create app.py

Browse files
Files changed (1) hide show
  1. app.py +270 -0
app.py ADDED
@@ -0,0 +1,270 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import spaces
2
+ import torch
3
+ import random
4
+ import numpy as np
5
+ from inspect import signature
6
+ from diffusers import (
7
+ FluxPipeline,
8
+ StableDiffusion3Pipeline,
9
+ PixArtSigmaPipeline,
10
+ SanaPipeline,
11
+ AuraFlowPipeline,
12
+ Kandinsky3Pipeline,
13
+ HunyuanDiTPipeline,
14
+ LuminaText2ImgPipeline,AutoPipelineForText2Image
15
+ )
16
+ import gradio as gr
17
+ from diffusers.pipelines.pipeline_utils import DiffusionPipeline
18
+
19
+ MAX_SEED = np.iinfo(np.int32).max
20
+ MAX_IMAGE_SIZE = 1024
21
+
22
+ class ProgressPipeline(DiffusionPipeline):
23
+ def __init__(self, original_pipeline):
24
+ super().__init__()
25
+ self.original_pipeline = original_pipeline
26
+ # Register all components from the original pipeline
27
+ for attr_name, attr_value in vars(original_pipeline).items():
28
+ setattr(self, attr_name, attr_value)
29
+
30
+ @torch.no_grad()
31
+ def __call__(
32
+ self,
33
+ prompt,
34
+ num_inference_steps=30,
35
+ generator=None,
36
+ guidance_scale=7.5,
37
+ callback=None,
38
+ callback_steps=1,
39
+ **kwargs
40
+ ):
41
+ # Initialize the progress tracking
42
+ self._num_inference_steps = num_inference_steps
43
+ self._step = 0
44
+
45
+ def progress_callback(step_index, timestep, callback_kwargs):
46
+ if callback and step_index % callback_steps == 0:
47
+ # Pass self (the pipeline) to the callback
48
+ callback(self, step_index, timestep, callback_kwargs)
49
+ return callback_kwargs
50
+
51
+ # Monkey patch the original pipeline's progress tracking
52
+ original_step = self.original_pipeline.scheduler.step
53
+ def wrapped_step(*args, **kwargs):
54
+ self._step += 1
55
+ progress_callback(self._step, None, {})
56
+ return original_step(*args, **kwargs)
57
+
58
+ self.original_pipeline.scheduler.step = wrapped_step
59
+
60
+ try:
61
+ # Call the original pipeline
62
+ result = self.original_pipeline(
63
+ prompt=prompt,
64
+ num_inference_steps=num_inference_steps,
65
+ generator=generator,
66
+ guidance_scale=guidance_scale,
67
+ **kwargs
68
+ )
69
+
70
+ return result
71
+ finally:
72
+ # Restore the original step function
73
+ self.original_pipeline.scheduler.step = original_step
74
+
75
+ cache_dir = '/workspace/hf_cache'
76
+
77
+ MODEL_CONFIGS = {
78
+ "FLUX": {
79
+ "repo_id": "black-forest-labs/FLUX.1-dev",
80
+ "pipeline_class": FluxPipeline,
81
+ },
82
+ "Stable Diffusion 3.5": {
83
+ "repo_id": "stabilityai/stable-diffusion-3.5-large",
84
+ "pipeline_class": StableDiffusion3Pipeline,
85
+
86
+ },
87
+ "PixArt": {
88
+ "repo_id": "PixArt-alpha/PixArt-Sigma-XL-2-1024-MS",
89
+ "pipeline_class": PixArtSigmaPipeline,
90
+
91
+ },
92
+ "SANA": {
93
+ "repo_id": "Efficient-Large-Model/Sana_1600M_1024px_BF16_diffusers",
94
+ "pipeline_class": SanaPipeline,
95
+
96
+ },
97
+ "AuraFlow": {
98
+ "repo_id": "fal/AuraFlow",
99
+ "pipeline_class": AuraFlowPipeline,
100
+
101
+ },
102
+ "Kandinsky": {
103
+ "repo_id": "kandinsky-community/kandinsky-3",
104
+ "pipeline_class": Kandinsky3Pipeline,
105
+
106
+ },
107
+ "Hunyuan": {
108
+ "repo_id": "Tencent-Hunyuan/HunyuanDiT-Diffusers",
109
+ "pipeline_class": HunyuanDiTPipeline,
110
+
111
+ },
112
+ "Lumina": {
113
+ "repo_id": "Alpha-VLLM/Lumina-Next-SFT-diffusers",
114
+ "pipeline_class": LuminaText2ImgPipeline,
115
+
116
+ }
117
+ }
118
+
119
+ def generate_image_with_progress(model_name,pipe, prompt, num_steps, guidance_scale=3.5, seed=None,negative_prompt=None, randomize_seed=None, width=1024, height=1024, num_inference_steps=40, progress=gr.Progress(track_tqdm=True)):
120
+ generator = None
121
+ if randomize_seed:
122
+ seed = random.randint(0, MAX_SEED)
123
+ if seed is not None:
124
+ generator = torch.Generator("cuda").manual_seed(seed)
125
+ else:
126
+ generator = torch.Generator("cuda")
127
+
128
+ def callback(pipe, step_index, timestep, callback_kwargs):
129
+ print(f" callback => {step_index}, {timestep}")
130
+ if step_index is None:
131
+ step_index = 0
132
+ cur_prg = step_index / num_steps
133
+ progress(cur_prg, desc=f"Step {step_index}/{num_steps}")
134
+ return callback_kwargs
135
+ print(f"START GENR ")
136
+ # Get the signature of the pipe
137
+ pipe_signature = signature(pipe)
138
+
139
+ # Check for the presence of "guidance_scale" and "callback_on_step_end" in the signature
140
+ has_guidance_scale = "guidance_scale" in pipe_signature.parameters
141
+ has_callback_on_step_end = "callback_on_step_end" in pipe_signature.parameters
142
+
143
+ # Define common arguments
144
+ common_args = {
145
+ "prompt": prompt,
146
+ "num_inference_steps": num_steps,
147
+ "negative_prompt": negative_prompt,
148
+ "width": width,
149
+ "height": height,
150
+ "generator": generator,
151
+ }
152
+
153
+ if has_guidance_scale:
154
+ common_args["guidance_scale"] = guidance_scale
155
+
156
+ if has_callback_on_step_end:
157
+ print("has callback_on_step_end and", "has guidance_scale" if has_guidance_scale else "NO guidance_scale")
158
+ common_args["callback_on_step_end"] = callback
159
+ else:
160
+ print("NO callback_on_step_end and", "has guidance_scale" if has_guidance_scale else "NO guidance_scale")
161
+ common_args["callback"] = callback
162
+ common_args["callback_steps"] = 1
163
+
164
+ # Generate image
165
+ image = pipe(**common_args).images[0]
166
+
167
+ return seed, image
168
+
169
+ @spaces.GPU(duration=170)
170
+ def create_pipeline_logic(prompt_text, model_name, negative_prompt="", seed=42, randomize_seed=False, width=1024, height=1024, guidance_scale=4.5, num_inference_steps=40,):
171
+ print(f"starting {model_name}")
172
+ progress = gr.Progress(track_tqdm=True)
173
+ config = MODEL_CONFIGS[model_name]
174
+ pipe_class = config["pipeline_class"]
175
+ pipe = None
176
+ b_pipe = AutoPipelineForText2Image.from_pretrained(
177
+ config["repo_id"],
178
+ #variant="fp16",
179
+ #cache_dir=config["cache_dir"],
180
+ torch_dtype=torch.bfloat16
181
+ ).to("cuda")
182
+ pipe_signature = signature(b_pipe)
183
+ # Check for the presence of "callback_on_step_end" in the signature
184
+ has_callback_on_step_end = "callback_on_step_end" in pipe_signature.parameters
185
+ if not has_callback_on_step_end:
186
+ pipe = ProgressPipeline(b_pipe)
187
+ print("ProgressPipeline specal")
188
+ else:
189
+ pipe = b_pipe
190
+
191
+ gen_seed,image = generate_image_with_progress(
192
+ model_name,pipe, prompt_text, num_steps=num_inference_steps, guidance_scale=guidance_scale, seed=seed,negative_prompt = negative_prompt, randomize_seed = randomize_seed, width = width, height = height, progress=progress
193
+ )
194
+ return f"Seed: {gen_seed}", image
195
+
196
+ def main():
197
+ with gr.Blocks() as app:
198
+ gr.Markdown("# Dynamic Multiple Model Image Generation")
199
+
200
+ prompt_text = gr.Textbox(label="Enter prompt")
201
+
202
+ with gr.Accordion("Advanced Settings", open=False):
203
+ negative_prompt = gr.Text(
204
+ label="Negative prompt",
205
+ max_lines=1,
206
+ placeholder="Enter a negative prompt",
207
+ )
208
+
209
+ seed = gr.Slider(
210
+ label="Seed",
211
+ minimum=0,
212
+ maximum=MAX_SEED,
213
+ step=100,
214
+ value=0,
215
+ )
216
+
217
+ randomize_seed = gr.Checkbox(label="Randomize seed", value=True)
218
+
219
+ with gr.Row():
220
+ width = gr.Slider(
221
+ label="Width",
222
+ minimum=512,
223
+ maximum=MAX_IMAGE_SIZE,
224
+ step=32,
225
+ value=1024,
226
+ )
227
+ height = gr.Slider(
228
+ label="Height",
229
+ minimum=512,
230
+ maximum=MAX_IMAGE_SIZE,
231
+ step=32,
232
+ value=1024,
233
+ )
234
+
235
+ with gr.Row():
236
+ guidance_scale = gr.Slider(
237
+ label="Guidance scale",
238
+ minimum=0.0,
239
+ maximum=7.5,
240
+ step=0.1,
241
+ value=4.5,
242
+ )
243
+ num_inference_steps = gr.Slider(
244
+ label="Number of inference steps",
245
+ minimum=1,
246
+ maximum=50,
247
+ step=1,
248
+ value=40,
249
+ )
250
+
251
+ for model_name, config in MODEL_CONFIGS.items():
252
+ with gr.Tab(model_name):
253
+ button = gr.Button(f"Run {model_name}")
254
+ output = gr.Textbox(label="Status")
255
+ img = gr.Image(label=model_name, height=300)
256
+
257
+ button.click(fn=create_pipeline_logic, inputs=[prompt_text, gr.Text(value= model_name,visible=False), negative_prompt,
258
+ seed,
259
+ randomize_seed,
260
+ width,
261
+ height,
262
+ guidance_scale,
263
+ num_inference_steps], outputs=[output, img])
264
+
265
+ app.launch()
266
+
267
+
268
+ if __name__ == "__main__":
269
+ main()
270
+