OjciecTadeusz commited on
Commit
b451886
·
verified ·
1 Parent(s): c491391

Create app.py

Browse files
Files changed (1) hide show
  1. app.py +179 -0
app.py ADDED
@@ -0,0 +1,179 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
+
10
+ from torch.cuda.amp import autocast
11
+
12
+ import logging
13
+ import sys
14
+ from datetime import datetime
15
+ import gc
16
+
17
+ # Configure logging
18
+ logging.basicConfig(
19
+ level=logging.INFO,
20
+ format='%(asctime)s - %(levelname)s - %(message)s',
21
+ handlers=[
22
+ logging.StreamHandler(sys.stdout),
23
+ logging.FileHandler('transcription.log')
24
+ ]
25
+ )
26
+ logger = logging.getLogger(__name__)
27
+
28
+ dtype = torch.bfloat16
29
+ device = "cuda" if torch.cuda.is_available() else "cpu"
30
+
31
+ taef1 = AutoencoderTiny.from_pretrained("madebyollin/taef1", torch_dtype=dtype).to(device)
32
+ good_vae = AutoencoderKL.from_pretrained("black-forest-labs/FLUX.1-dev", subfolder="vae", torch_dtype=dtype).to(device)
33
+ pipe = DiffusionPipeline.from_pretrained("black-forest-labs/FLUX.1-dev", torch_dtype=dtype, vae=taef1).to(device)
34
+ torch.cuda.empty_cache()
35
+
36
+ MAX_SEED = np.iinfo(np.int32).max
37
+ MAX_IMAGE_SIZE = 2048
38
+
39
+ pipe.flux_pipe_call_that_returns_an_iterable_of_images = flux_pipe_call_that_returns_an_iterable_of_images.__get__(pipe)
40
+
41
+ @spaces.GPU(duration=75)
42
+ def infer(prompt, seed=42, randomize_seed=False, width=1024, height=1024, guidance_scale=3.5, num_inference_steps=28, progress=gr.Progress(track_tqdm=True)):
43
+ if randomize_seed:
44
+ seed = random.randint(0, MAX_SEED)
45
+ generator = torch.Generator().manual_seed(seed)
46
+
47
+ with torch.no_grad(), torch.autocast(device_type='cuda', dtype=torch.bfloat16):
48
+ for img in pipe.flux_pipe_call_that_returns_an_iterable_of_images(
49
+ prompt=prompt,
50
+ guidance_scale=guidance_scale,
51
+ num_inference_steps=num_inference_steps,
52
+ width=width,
53
+ height=height,
54
+ generator=generator,
55
+ output_type="pil",
56
+ good_vae=good_vae,
57
+ ):
58
+ logger.info(f"PROMPT: {prompt}")
59
+
60
+ yield img, seed
61
+
62
+ # @spaces.GPU(duration=75)
63
+ # def infer(prompt, seed=42, randomize_seed=False, width=1024, height=1024, guidance_scale=3.5, num_inference_steps=28, progress=gr.Progress(track_tqdm=True)):
64
+ # if randomize_seed:
65
+ # seed = random.randint(0, MAX_SEED)
66
+ # generator = torch.Generator().manual_seed(seed)
67
+
68
+ # with torch.no_grad(), autocast(dtype=torch.bfloat16, device_type='cuda'):
69
+ # for img in pipe.flux_pipe_call_that_returns_an_iterable_of_images(
70
+ # prompt=prompt,
71
+ # guidance_scale=guidance_scale,
72
+ # num_inference_steps=num_inference_steps,
73
+ # width=width,
74
+ # height=height,
75
+ # generator=generator,
76
+ # output_type="pil",
77
+ # good_vae=good_vae,
78
+ # ):
79
+ # logger.info(f"PROMPT: {prompt}")
80
+
81
+ # yield img, seed
82
+
83
+ examples = [
84
+ "a tiny astronaut hatching from an egg on the moon",
85
+ "a cat holding a sign that says hello world",
86
+ "an anime illustration of a wiener schnitzel",
87
+ ]
88
+
89
+ css="""#col-container {
90
+ margin: 0 auto;
91
+ max-width: 520px;
92
+ }"""
93
+
94
+ with gr.Blocks(css=css) as demo:
95
+
96
+ with gr.Column(elem_id="col-container"):
97
+ gr.Markdown(f"""# FLUX.1 [dev]
98
+ 12B param rectified flow transformer guidance-distilled from [FLUX.1 [pro]](https://blackforestlabs.ai/)
99
+ [[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)]
100
+ """)
101
+
102
+ with gr.Row():
103
+
104
+ prompt = gr.Text(
105
+ label="Prompt",
106
+ show_label=False,
107
+ max_lines=1,
108
+ placeholder="Enter your prompt",
109
+ container=False,
110
+ )
111
+
112
+ run_button = gr.Button("Run", scale=0)
113
+
114
+ result = gr.Image(label="Result", show_label=False)
115
+
116
+ with gr.Accordion("Advanced Settings", open=False):
117
+
118
+ seed = gr.Slider(
119
+ label="Seed",
120
+ minimum=0,
121
+ maximum=MAX_SEED,
122
+ step=1,
123
+ value=0,
124
+ )
125
+
126
+ randomize_seed = gr.Checkbox(label="Randomize seed", value=True)
127
+
128
+ with gr.Row():
129
+
130
+ width = gr.Slider(
131
+ label="Width",
132
+ minimum=256,
133
+ maximum=MAX_IMAGE_SIZE,
134
+ step=32,
135
+ value=1024,
136
+ )
137
+
138
+ height = gr.Slider(
139
+ label="Height",
140
+ minimum=256,
141
+ maximum=MAX_IMAGE_SIZE,
142
+ step=32,
143
+ value=1024,
144
+ )
145
+
146
+ with gr.Row():
147
+
148
+ guidance_scale = gr.Slider(
149
+ label="Guidance Scale",
150
+ minimum=1,
151
+ maximum=15,
152
+ step=0.1,
153
+ value=3.5,
154
+ )
155
+
156
+ num_inference_steps = gr.Slider(
157
+ label="Number of inference steps",
158
+ minimum=1,
159
+ maximum=50,
160
+ step=1,
161
+ value=28,
162
+ )
163
+
164
+ gr.Examples(
165
+ examples = examples,
166
+ fn = infer,
167
+ inputs = [prompt],
168
+ outputs = [result, seed],
169
+ cache_examples="lazy"
170
+ )
171
+
172
+ gr.on(
173
+ triggers=[run_button.click, prompt.submit],
174
+ fn = infer,
175
+ inputs = [prompt, seed, randomize_seed, width, height, guidance_scale, num_inference_steps],
176
+ outputs = [result, seed]
177
+ )
178
+
179
+ demo.launch()