Nymbo commited on
Commit
09fc7e9
·
verified ·
1 Parent(s): c68697f

archiving, but i do hope we get somewhere with this!

Browse files
Files changed (1) hide show
  1. app.py +179 -179
app.py CHANGED
@@ -1,180 +1,180 @@
1
- import gradio as gr
2
- import numpy as np
3
- import random
4
- import spaces
5
- import torch
6
- from diffusers import DiffusionPipeline, FlowMatchEulerDiscreteScheduler, AutoencoderTiny, AutoencoderKL
7
- from transformers import CLIPTextModel, CLIPTokenizer,T5EncoderModel, T5TokenizerFast
8
- from live_preview_helpers import calculate_shift, retrieve_timesteps, flux_pipe_call_that_returns_an_iterable_of_images
9
- from gradio_imageslider import ImageSlider
10
- from PIL import Image, ImageDraw, ImageFont
11
-
12
- dtype = torch.bfloat16
13
- #model_id = "black-forest-labs/FLUX.1-dev"
14
- model_id = "camenduru/FLUX.1-dev-diffusers"
15
- device = "cuda" if torch.cuda.is_available() else "cpu"
16
-
17
- #taef1 = AutoencoderTiny.from_pretrained("madebyollin/taef1", torch_dtype=dtype).to(device)
18
- good_vae = AutoencoderKL.from_pretrained(model_id, subfolder="vae", torch_dtype=dtype).to(device)
19
- #pipe = DiffusionPipeline.from_pretrained(model_id, torch_dtype=dtype, vae=taef1).to(device)
20
- pipe = DiffusionPipeline.from_pretrained(model_id, torch_dtype=dtype, vae=good_vae).to(device)
21
- torch.cuda.empty_cache()
22
-
23
- MAX_SEED = np.iinfo(np.int32).max
24
- MAX_IMAGE_SIZE = 2048
25
-
26
- pipe.flux_pipe_call_that_returns_an_iterable_of_images = flux_pipe_call_that_returns_an_iterable_of_images.__get__(pipe)
27
-
28
- def get_cmp_image(im1: Image.Image, im2: Image.Image, sigmas: float):
29
- dst = Image.new('RGB', (im1.width + im2.width, im1.height))
30
- dst.paste(im1.convert('RGB'), (0, 0))
31
- dst.paste(im2.convert('RGB'), (im1.width, 0))
32
- font = ImageFont.truetype('Roboto-Regular.ttf', 72, encoding='unic')
33
- draw = ImageDraw.Draw(dst)
34
- draw.text((64, im1.height - 128), 'Default Flux', 'red', font=font)
35
- draw.text((im1.width + 64, im1.height - 128), f'Sigmas * factor {sigmas}', 'red', font=font)
36
- return dst
37
-
38
- @spaces.GPU(duration=90)
39
- def infer(prompt, seed=42, randomize_seed=False, width=1024, height=1024, guidance_scale=3.5, num_inference_steps=28, mul_sigmas=0.95, is_cmp=True, progress=gr.Progress(track_tqdm=True)):
40
- if randomize_seed:
41
- seed = random.randint(0, MAX_SEED)
42
- generator = torch.Generator().manual_seed(seed)
43
-
44
- sigmas = np.linspace(1.0, 1 / num_inference_steps, num_inference_steps)
45
- sigmas = sigmas * mul_sigmas
46
-
47
- image_sigmas = pipe(
48
- prompt=prompt,
49
- guidance_scale=guidance_scale,
50
- num_inference_steps=num_inference_steps,
51
- width=width,
52
- height=height,
53
- generator=generator,
54
- output_type="pil",
55
- sigmas=sigmas
56
- ).images[0]
57
-
58
- if is_cmp:
59
- image_def = pipe(
60
- prompt=prompt,
61
- guidance_scale=guidance_scale,
62
- num_inference_steps=num_inference_steps,
63
- width=width,
64
- height=height,
65
- generator=generator,
66
- output_type="pil",
67
- ).images[0]
68
- return [image_def, image_sigmas], get_cmp_image(image_def, image_sigmas, mul_sigmas), seed
69
- else: return [image_sigmas, image_sigmas], None, seed
70
-
71
- examples = [
72
- "a tiny astronaut hatching from an egg on the moon",
73
- "a cat holding a sign that says hello world",
74
- "an anime illustration of a wiener schnitzel",
75
- ]
76
-
77
- css="""
78
- #col-container {
79
- margin: 0 auto;
80
- max-width: 520px;
81
- }
82
- """
83
-
84
- with gr.Blocks(css=css) as demo:
85
-
86
- with gr.Column(elem_id="col-container"):
87
- gr.Markdown(f"""# FLUX.1 [dev] sigmas test
88
- 12B param rectified flow transformer guidance-distilled from [FLUX.1 [pro]](https://blackforestlabs.ai/)
89
- [[non-commercial license](https://huggingface.co/black-forest-labs/FLUX.1-dev/blob/main/LICENSE.md)] [[blog](https://blackforestlabs.ai/announcing-black-forest-labs/)] [[model](https://huggingface.co/black-forest-labs/FLUX.1-dev)]
90
- """)
91
-
92
- with gr.Row():
93
-
94
- prompt = gr.Text(
95
- label="Prompt",
96
- show_label=False,
97
- max_lines=1,
98
- placeholder="Enter your prompt",
99
- container=False,
100
- )
101
-
102
- run_button = gr.Button("Run", scale=0)
103
-
104
- #result = gr.Image(label="Result", show_label=False)
105
- result = ImageSlider(label="Result", show_label=False, type="pil", slider_color="pink")
106
- result_cmp = gr.Image(label="Result (comparing)", show_label=False, type="pil", format="png", height=256, show_download_button=True, show_share_button=False)
107
-
108
- with gr.Accordion("Advanced Settings", open=True):
109
- with gr.Row():
110
- sigmas = gr.Slider(
111
- label="Sigmas",
112
- minimum=0,
113
- maximum=1.0,
114
- step=0.01,
115
- value=0.95,
116
- )
117
- is_cmp = gr.Checkbox(label="Compare images with/without sigmas", value=True)
118
-
119
- seed = gr.Slider(
120
- label="Seed",
121
- minimum=0,
122
- maximum=MAX_SEED,
123
- step=1,
124
- value=9119,
125
- )
126
-
127
- randomize_seed = gr.Checkbox(label="Randomize seed", value=False)
128
-
129
- with gr.Row():
130
-
131
- width = gr.Slider(
132
- label="Width",
133
- minimum=256,
134
- maximum=MAX_IMAGE_SIZE,
135
- step=32,
136
- value=1024,
137
- )
138
-
139
- height = gr.Slider(
140
- label="Height",
141
- minimum=256,
142
- maximum=MAX_IMAGE_SIZE,
143
- step=32,
144
- value=1024,
145
- )
146
-
147
- with gr.Row():
148
-
149
- guidance_scale = gr.Slider(
150
- label="Guidance Scale",
151
- minimum=1,
152
- maximum=15,
153
- step=0.1,
154
- value=3.5,
155
- )
156
-
157
- num_inference_steps = gr.Slider(
158
- label="Number of inference steps",
159
- minimum=1,
160
- maximum=50,
161
- step=1,
162
- value=28,
163
- )
164
-
165
- gr.Examples(
166
- examples = examples,
167
- fn = infer,
168
- inputs = [prompt],
169
- outputs = [result, result_cmp, seed],
170
- cache_examples="lazy"
171
- )
172
-
173
- gr.on(
174
- triggers=[run_button.click, prompt.submit],
175
- fn = infer,
176
- inputs = [prompt, seed, randomize_seed, width, height, guidance_scale, num_inference_steps, sigmas, is_cmp],
177
- outputs = [result, result_cmp, seed]
178
- )
179
-
180
  demo.launch()
 
1
+ import gradio as gr
2
+ import numpy as np
3
+ import random
4
+ # import spaces
5
+ import torch
6
+ from diffusers import DiffusionPipeline, FlowMatchEulerDiscreteScheduler, AutoencoderTiny, AutoencoderKL
7
+ from transformers import CLIPTextModel, CLIPTokenizer,T5EncoderModel, T5TokenizerFast
8
+ from live_preview_helpers import calculate_shift, retrieve_timesteps, flux_pipe_call_that_returns_an_iterable_of_images
9
+ from gradio_imageslider import ImageSlider
10
+ from PIL import Image, ImageDraw, ImageFont
11
+
12
+ dtype = torch.bfloat16
13
+ #model_id = "black-forest-labs/FLUX.1-dev"
14
+ model_id = "camenduru/FLUX.1-dev-diffusers"
15
+ device = "cuda" if torch.cuda.is_available() else "cpu"
16
+
17
+ #taef1 = AutoencoderTiny.from_pretrained("madebyollin/taef1", torch_dtype=dtype).to(device)
18
+ good_vae = AutoencoderKL.from_pretrained(model_id, subfolder="vae", torch_dtype=dtype).to(device)
19
+ #pipe = DiffusionPipeline.from_pretrained(model_id, torch_dtype=dtype, vae=taef1).to(device)
20
+ pipe = DiffusionPipeline.from_pretrained(model_id, torch_dtype=dtype, vae=good_vae).to(device)
21
+ torch.cuda.empty_cache()
22
+
23
+ MAX_SEED = np.iinfo(np.int32).max
24
+ MAX_IMAGE_SIZE = 2048
25
+
26
+ pipe.flux_pipe_call_that_returns_an_iterable_of_images = flux_pipe_call_that_returns_an_iterable_of_images.__get__(pipe)
27
+
28
+ def get_cmp_image(im1: Image.Image, im2: Image.Image, sigmas: float):
29
+ dst = Image.new('RGB', (im1.width + im2.width, im1.height))
30
+ dst.paste(im1.convert('RGB'), (0, 0))
31
+ dst.paste(im2.convert('RGB'), (im1.width, 0))
32
+ font = ImageFont.truetype('Roboto-Regular.ttf', 72, encoding='unic')
33
+ draw = ImageDraw.Draw(dst)
34
+ draw.text((64, im1.height - 128), 'Default Flux', 'red', font=font)
35
+ draw.text((im1.width + 64, im1.height - 128), f'Sigmas * factor {sigmas}', 'red', font=font)
36
+ return dst
37
+
38
+ # @spaces.GPU(duration=90)
39
+ def infer(prompt, seed=42, randomize_seed=False, width=1024, height=1024, guidance_scale=3.5, num_inference_steps=28, mul_sigmas=0.95, is_cmp=True, progress=gr.Progress(track_tqdm=True)):
40
+ if randomize_seed:
41
+ seed = random.randint(0, MAX_SEED)
42
+ generator = torch.Generator().manual_seed(seed)
43
+
44
+ sigmas = np.linspace(1.0, 1 / num_inference_steps, num_inference_steps)
45
+ sigmas = sigmas * mul_sigmas
46
+
47
+ image_sigmas = pipe(
48
+ prompt=prompt,
49
+ guidance_scale=guidance_scale,
50
+ num_inference_steps=num_inference_steps,
51
+ width=width,
52
+ height=height,
53
+ generator=generator,
54
+ output_type="pil",
55
+ sigmas=sigmas
56
+ ).images[0]
57
+
58
+ if is_cmp:
59
+ image_def = pipe(
60
+ prompt=prompt,
61
+ guidance_scale=guidance_scale,
62
+ num_inference_steps=num_inference_steps,
63
+ width=width,
64
+ height=height,
65
+ generator=generator,
66
+ output_type="pil",
67
+ ).images[0]
68
+ return [image_def, image_sigmas], get_cmp_image(image_def, image_sigmas, mul_sigmas), seed
69
+ else: return [image_sigmas, image_sigmas], None, seed
70
+
71
+ examples = [
72
+ "a tiny astronaut hatching from an egg on the moon",
73
+ "a cat holding a sign that says hello world",
74
+ "an anime illustration of a wiener schnitzel",
75
+ ]
76
+
77
+ css="""
78
+ #col-container {
79
+ margin: 0 auto;
80
+ max-width: 520px;
81
+ }
82
+ """
83
+
84
+ with gr.Blocks(css=css) as demo:
85
+
86
+ with gr.Column(elem_id="col-container"):
87
+ gr.Markdown(f"""# FLUX.1 [dev] sigmas test
88
+ 12B param rectified flow transformer guidance-distilled from [FLUX.1 [pro]](https://blackforestlabs.ai/)
89
+ [[non-commercial license](https://huggingface.co/black-forest-labs/FLUX.1-dev/blob/main/LICENSE.md)] [[blog](https://blackforestlabs.ai/announcing-black-forest-labs/)] [[model](https://huggingface.co/black-forest-labs/FLUX.1-dev)]
90
+ """)
91
+
92
+ with gr.Row():
93
+
94
+ prompt = gr.Text(
95
+ label="Prompt",
96
+ show_label=False,
97
+ max_lines=1,
98
+ placeholder="Enter your prompt",
99
+ container=False,
100
+ )
101
+
102
+ run_button = gr.Button("Run", scale=0)
103
+
104
+ #result = gr.Image(label="Result", show_label=False)
105
+ result = ImageSlider(label="Result", show_label=False, type="pil", slider_color="pink")
106
+ result_cmp = gr.Image(label="Result (comparing)", show_label=False, type="pil", format="png", height=256, show_download_button=True, show_share_button=False)
107
+
108
+ with gr.Accordion("Advanced Settings", open=True):
109
+ with gr.Row():
110
+ sigmas = gr.Slider(
111
+ label="Sigmas",
112
+ minimum=0,
113
+ maximum=1.0,
114
+ step=0.01,
115
+ value=0.95,
116
+ )
117
+ is_cmp = gr.Checkbox(label="Compare images with/without sigmas", value=True)
118
+
119
+ seed = gr.Slider(
120
+ label="Seed",
121
+ minimum=0,
122
+ maximum=MAX_SEED,
123
+ step=1,
124
+ value=9119,
125
+ )
126
+
127
+ randomize_seed = gr.Checkbox(label="Randomize seed", value=False)
128
+
129
+ with gr.Row():
130
+
131
+ width = gr.Slider(
132
+ label="Width",
133
+ minimum=256,
134
+ maximum=MAX_IMAGE_SIZE,
135
+ step=32,
136
+ value=1024,
137
+ )
138
+
139
+ height = gr.Slider(
140
+ label="Height",
141
+ minimum=256,
142
+ maximum=MAX_IMAGE_SIZE,
143
+ step=32,
144
+ value=1024,
145
+ )
146
+
147
+ with gr.Row():
148
+
149
+ guidance_scale = gr.Slider(
150
+ label="Guidance Scale",
151
+ minimum=1,
152
+ maximum=15,
153
+ step=0.1,
154
+ value=3.5,
155
+ )
156
+
157
+ num_inference_steps = gr.Slider(
158
+ label="Number of inference steps",
159
+ minimum=1,
160
+ maximum=50,
161
+ step=1,
162
+ value=28,
163
+ )
164
+
165
+ gr.Examples(
166
+ examples = examples,
167
+ fn = infer,
168
+ inputs = [prompt],
169
+ outputs = [result, result_cmp, seed],
170
+ cache_examples="lazy"
171
+ )
172
+
173
+ gr.on(
174
+ triggers=[run_button.click, prompt.submit],
175
+ fn = infer,
176
+ inputs = [prompt, seed, randomize_seed, width, height, guidance_scale, num_inference_steps, sigmas, is_cmp],
177
+ outputs = [result, result_cmp, seed]
178
+ )
179
+
180
  demo.launch()