kadirnar commited on
Commit
e4014ea
·
verified ·
1 Parent(s): eea1716

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +226 -59
app.py CHANGED
@@ -1,73 +1,240 @@
 
 
 
1
  import spaces
2
  from diffusers import AuraFlowPipeline
3
  import torch
4
- import gradio as gr
5
 
6
- def initialize_auraflow_pipeline():
7
- """Initialize and return the AuraFlowPipeline."""
8
- pipeline = AuraFlowPipeline.from_pretrained(
9
- "fal/AuraFlow-v0.3",
10
- torch_dtype=torch.float16,
11
- variant="fp16",
12
- ).to("cuda")
13
- return pipeline
14
 
15
- @spaces.GPU(duration=95)
16
- def generate_image(pipeline, prompt, width, height, num_inference_steps, seed, guidance_scale):
17
- """Generate an image using the AuraFlowPipeline."""
18
- generator = torch.Generator().manual_seed(seed)
19
 
20
- image = pipeline(
21
- prompt=prompt,
22
- width=width,
23
- height=height,
24
- num_inference_steps=num_inference_steps,
25
- generator=generator,
26
- guidance_scale=guidance_scale,
27
- ).images[0]
28
 
29
- return image
 
 
 
30
 
31
- # Initialize the pipeline once
32
- auraflow_pipeline = initialize_auraflow_pipeline()
 
 
33
 
34
- # Gradio interface
35
- def gradio_generate_image(prompt, width, height, num_inference_steps, seed, guidance_scale):
36
- return generate_image(auraflow_pipeline, prompt, width, height, num_inference_steps, seed, guidance_scale)
 
 
 
 
 
37
 
38
- # Create Gradio Blocks
39
- with gr.Blocks(theme="bethecloud/storj_theme") as demo:
40
- gr.HTML(
41
- """
42
- <h1 style='text-align: center'>
43
- AuraFlow v0.3
44
- </h1>
45
- """)
46
- gr.HTML(
47
- """
48
- <h3 style='text-align: center'>
49
- Follow me for more!
50
- <a href='https://twitter.com/kadirnar_ai' target='_blank'>Twitter</a> | <a href='https://github.com/kadirnar' target='_blank'>Github</a> | <a href='https://www.linkedin.com/in/kadir-nar/' target='_blank'>Linkedin</a> | <a href='https://www.huggingface.co/kadirnar/' target='_blank'>HuggingFace</a>
51
- </h3>
52
- """)
53
- with gr.Row():
54
- with gr.Column():
55
- prompt_input = gr.Textbox(label="Prompt", placeholder="Enter your image prompt here...")
56
- width_input = gr.Slider(minimum=256, maximum=1536, step=64, value=1024, label="Width")
57
- height_input = gr.Slider(minimum=256, maximum=1536, step=64, value=1024, label="Height")
58
- steps_input = gr.Slider(minimum=10, maximum=100, step=1, value=20, label="Number of Inference Steps")
59
- seed_input = gr.Number(label="Seed", value=1)
60
- guidance_input = gr.Slider(minimum=1, maximum=10, step=0.1, value=3.5, label="Guidance Scale")
61
- generate_btn = gr.Button("Generate Image")
 
 
 
 
 
 
 
 
 
 
 
 
62
 
63
- with gr.Column():
64
- image_output = gr.Image(label="Generated Image")
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
65
 
66
- generate_btn.click(
67
- fn=gradio_generate_image,
68
- inputs=[prompt_input, width_input, height_input, steps_input, seed_input, guidance_input],
69
- outputs=image_output
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
70
  )
71
 
72
- # Launch the Gradio interface
73
- demo.launch()
 
1
+ import gradio as gr
2
+ import numpy as np
3
+ import random
4
  import spaces
5
  from diffusers import AuraFlowPipeline
6
  import torch
7
+ from gradio_imageslider import ImageSlider
8
 
9
+ device = "cuda" if torch.cuda.is_available() else "cpu"
 
 
 
 
 
 
 
10
 
11
+ #torch.set_float32_matmul_precision("high")
 
 
 
12
 
13
+ #torch._inductor.config.conv_1x1_as_mm = True
14
+ #torch._inductor.config.coordinate_descent_tuning = True
15
+ #torch._inductor.config.epilogue_fusion = False
16
+ #torch._inductor.config.coordinate_descent_check_all_directions = True
 
 
 
 
17
 
18
+ #pipe_v1 = AuraFlowPipeline.from_pretrained(
19
+ # "fal/AuraFlow",
20
+ # torch_dtype=torch.float16
21
+ #).to("cuda")
22
 
23
+ pipe_v2 = AuraFlowPipeline.from_pretrained(
24
+ "fal/AuraFlow-v0.2",
25
+ torch_dtype=torch.float16
26
+ ).to("cuda")
27
 
28
+ pipe = AuraFlowPipeline.from_pretrained(
29
+ "fal/AuraFlow-v0.3",
30
+ torch_dtype=torch.float16
31
+ ).to("cuda")
32
+ #pipe.transformer.to(memory_format=torch.channels_last)
33
+ #pipe.transformer = torch.compile(pipe.transformer, mode="reduce-overhead", fullgraph=True)
34
+ #pipe.transformer.to(memory_format=torch.channels_last)
35
+ #pipe.vae.to(memory_format=torch.channels_last)
36
 
37
+ #pipe.transformer = torch.compile(pipe.transformer, mode="max-autotune", fullgraph=True)
38
+ #pipe.vae.decode = torch.compile(pipe.vae.decode, mode="max-autotune", fullgraph=True)
39
+
40
+ MAX_SEED = np.iinfo(np.int32).max
41
+ MAX_IMAGE_SIZE = 1024
42
+
43
+ @spaces.GPU()
44
+ def infer_example(prompt, negative_prompt="", seed=42, randomize_seed=False, width=1024, height=1024, guidance_scale=5.0, num_inference_steps=28, model_version="0.2", comparison_mode=False, progress=gr.Progress(track_tqdm=True)):
45
+ generator = torch.Generator().manual_seed(seed)
46
+ image = pipe(
47
+ prompt = prompt,
48
+ negative_prompt = negative_prompt,
49
+ width = width,
50
+ height = height,
51
+ guidance_scale = guidance_scale,
52
+ num_inference_steps = num_inference_steps,
53
+ generator = generator
54
+ ).images[0]
55
+ return image, seed
56
+
57
+ @spaces.GPU(duration=95)
58
+ def infer(prompt,
59
+ negative_prompt="",
60
+ seed=42,
61
+ randomize_seed=False,
62
+ width=1024,
63
+ height=1024,
64
+ guidance_scale=5.0,
65
+ num_inference_steps=28,
66
+ model_version="0.3",
67
+ comparison_mode=False,
68
+ progress=gr.Progress(track_tqdm=True)
69
+ ):
70
+
71
+ if randomize_seed:
72
+ seed = random.randint(0, MAX_SEED)
73
 
74
+ generator = torch.Generator().manual_seed(seed)
75
+ if(comparison_mode):
76
+ image_1 = pipe_v2(
77
+ prompt = prompt,
78
+ negative_prompt = negative_prompt,
79
+ width=width,
80
+ height=height,
81
+ guidance_scale = guidance_scale,
82
+ num_inference_steps = num_inference_steps,
83
+ generator = generator
84
+ ).images[0]
85
+ generator = torch.Generator().manual_seed(seed)
86
+ image_2 = pipe(
87
+ prompt = prompt,
88
+ negative_prompt = negative_prompt,
89
+ width=width,
90
+ height=height,
91
+ guidance_scale = guidance_scale,
92
+ num_inference_steps = num_inference_steps,
93
+ generator = generator
94
+ ).images[0]
95
+ return gr.update(visible=False), gr.update(visible=True, value=(image_1, image_2)), seed
96
+ if(model_version == "0.1"):
97
+ image = pipe_v1(
98
+ prompt = prompt,
99
+ negative_prompt = negative_prompt,
100
+ width=width,
101
+ height=height,
102
+ guidance_scale = guidance_scale,
103
+ num_inference_steps = num_inference_steps,
104
+ generator = generator
105
+ ).images[0]
106
+ elif(model_version == "0.2"):
107
+ image = pipe_v2(
108
+ prompt = prompt,
109
+ negative_prompt = negative_prompt,
110
+ width=width,
111
+ height=height,
112
+ guidance_scale = guidance_scale,
113
+ num_inference_steps = num_inference_steps,
114
+ generator = generator
115
+ ).images[0]
116
+ else:
117
+ image = pipe(
118
+ prompt = prompt,
119
+ negative_prompt = negative_prompt,
120
+ width=width,
121
+ height=height,
122
+ guidance_scale = guidance_scale,
123
+ num_inference_steps = num_inference_steps,
124
+ generator = generator
125
+ ).images[0]
126
+
127
+ return gr.update(visible=True, value=image), gr.update(visible=False), seed
128
+
129
+ examples = [
130
+ "A photo of a lavender cat",
131
+ "Astronaut in a jungle, cold color palette, muted colors, detailed, 8k",
132
+ "An astronaut riding a green horse",
133
+ "A delicious ceviche cheesecake slice",
134
+ ]
135
+
136
+ css="""
137
+ #col-container {
138
+ margin: 0 auto;
139
+ max-width: 520px;
140
+ }
141
+ """
142
+
143
+ with gr.Blocks(css=css) as demo:
144
 
145
+ with gr.Column(elem_id="col-container"):
146
+ gr.Markdown(f"""
147
+ # AuraFlow 0.3
148
+ Demo of the [AuraFlow 0.3](https://huggingface.co/fal/AuraFlow-v0.3) 6.8B parameters open source diffusion transformer model
149
+ [[blog](https://blog.fal.ai/auraflow/)] [[model](https://huggingface.co/fal/AuraFlow)] [[fal](https://fal.ai/models/fal-ai/aura-flow)]
150
+ """)
151
+
152
+ with gr.Row():
153
+
154
+ prompt = gr.Text(
155
+ label="Prompt",
156
+ show_label=False,
157
+ max_lines=1,
158
+ placeholder="Enter your prompt",
159
+ container=False,
160
+ )
161
+
162
+ run_button = gr.Button("Run", scale=0)
163
+
164
+ result = gr.Image(label="Result", show_label=False)
165
+ result_compare = ImageSlider(visible=False, label="Left 0.2, Right 0.3")
166
+ comparison_mode = gr.Checkbox(label="Comparison mode", info="Compare v0.2 with v0.3", value=False)
167
+ with gr.Accordion("Advanced Settings", open=False):
168
+
169
+ model_version = gr.Dropdown(
170
+ ["0.2", "0.3"], label="Model version", value="0.3"
171
+ )
172
+
173
+ negative_prompt = gr.Text(
174
+ label="Negative prompt",
175
+ max_lines=1,
176
+ placeholder="Enter a negative prompt",
177
+ )
178
+
179
+ seed = gr.Slider(
180
+ label="Seed",
181
+ minimum=0,
182
+ maximum=MAX_SEED,
183
+ step=1,
184
+ value=0,
185
+ )
186
+
187
+ randomize_seed = gr.Checkbox(label="Randomize seed", value=True)
188
+
189
+ with gr.Row():
190
+
191
+ width = gr.Slider(
192
+ label="Width",
193
+ minimum=256,
194
+ maximum=MAX_IMAGE_SIZE,
195
+ step=32,
196
+ value=1024,
197
+ )
198
+
199
+ height = gr.Slider(
200
+ label="Height",
201
+ minimum=256,
202
+ maximum=MAX_IMAGE_SIZE,
203
+ step=32,
204
+ value=1024,
205
+ )
206
+
207
+ with gr.Row():
208
+
209
+ guidance_scale = gr.Slider(
210
+ label="Guidance scale",
211
+ minimum=0.0,
212
+ maximum=10.0,
213
+ step=0.1,
214
+ value=5.0,
215
+ )
216
+
217
+ num_inference_steps = gr.Slider(
218
+ label="Number of inference steps",
219
+ minimum=1,
220
+ maximum=50,
221
+ step=1,
222
+ value=28,
223
+ )
224
+
225
+ gr.Examples(
226
+ examples = examples,
227
+ fn = infer_example,
228
+ inputs = [prompt],
229
+ outputs = [result, seed],
230
+ cache_examples="lazy"
231
+ )
232
+
233
+ gr.on(
234
+ triggers=[run_button.click, prompt.submit, negative_prompt.submit],
235
+ fn = infer,
236
+ inputs = [prompt, negative_prompt, seed, randomize_seed, width, height, guidance_scale, num_inference_steps, model_version, comparison_mode],
237
+ outputs = [result, result_compare, seed]
238
  )
239
 
240
+ demo.queue().launch()