baby20cen commited on
Commit
d8f4b67
·
verified ·
1 Parent(s): f5514b0

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +308 -1
app.py CHANGED
@@ -7,4 +7,311 @@ with gr.Blocks(fill_height=True) as demo:
7
  button = gr.LoginButton("Sign in")
8
  gr.load("models/black-forest-labs/FLUX.1-dev", accept_token=button, provider="hf-inference")
9
 
10
- demo.launch()
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
7
  button = gr.LoginButton("Sign in")
8
  gr.load("models/black-forest-labs/FLUX.1-dev", accept_token=button, provider="hf-inference")
9
 
10
+ demo.launch()
11
+
12
+
13
+ import logging
14
+ import random
15
+ import warnings
16
+ import os
17
+ import gradio as gr
18
+ import numpy as np
19
+ import spaces
20
+ import torch
21
+ from diffusers import FluxControlNetModel
22
+ from diffusers.pipelines import FluxControlNetPipeline
23
+ from gradio_imageslider import ImageSlider
24
+ from PIL import Image
25
+ from huggingface_hub import snapshot_download
26
+ from huggingface_hub import login
27
+ import subprocess
28
+
29
+ css = """
30
+ #col-container {
31
+ margin: 0 auto;
32
+ max-width: 512px;
33
+ }
34
+ """
35
+
36
+ if torch.cuda.is_available():
37
+ power_device = "GPU"
38
+ device = "cuda"
39
+ else:
40
+ power_device = "CPU"
41
+ device = "cpu"
42
+
43
+ huggingface_token = os.getenv("token1704")
44
+
45
+
46
+ def login_to_huggingface(token):
47
+ command = f"huggingface-cli login --token {token} --add-to-git-credential"
48
+ process = subprocess.Popen(command, shell=True, stdout=subprocess.PIPE, stderr=subprocess.PIPE)
49
+ stdout, stderr = process.communicate()
50
+ if process.returncode == 0:
51
+ print("Login successful")
52
+ else:
53
+ print(f"Error: {stderr.decode()}")
54
+
55
+ login_to_huggingface(huggingface_token)
56
+
57
+
58
+ if huggingface_token:
59
+ login(token=huggingface_token)
60
+
61
+ model_path = snapshot_download(
62
+ repo_id="black-forest-labs/FLUX.1-dev",
63
+ repo_type="model",
64
+ ignore_patterns=["*.md", "*..gitattributes"],
65
+ local_dir="FLUX.1-dev",
66
+ token=huggingface_token, # type a new token-id.
67
+ )
68
+
69
+ # Load pipeline
70
+ controlnet = FluxControlNetModel.from_pretrained(
71
+ "jasperai/Flux.1-dev-Controlnet-Upscaler", torch_dtype=torch.bfloat16
72
+ ).to(device)
73
+ pipe = FluxControlNetPipeline.from_pretrained(
74
+ model_path, controlnet=controlnet, torch_dtype=torch.bfloat16
75
+ )
76
+ pipe.to(device)
77
+
78
+ MAX_SEED = 1000000
79
+ MAX_PIXEL_BUDGET = 1024 * 1024
80
+
81
+
82
+
83
+ def process_input(input_image, upscale_factor, **kwargs):
84
+ w, h = input_image.size
85
+ w_original, h_original = w, h
86
+ aspect_ratio = w / h
87
+
88
+ was_resized = False
89
+
90
+ if w * h * upscale_factor**2 > MAX_PIXEL_BUDGET:
91
+ warnings.warn(
92
+ f"Requested output image is too large ({w * upscale_factor}x{h * upscale_factor}). Resizing to ({int(aspect_ratio * MAX_PIXEL_BUDGET ** 0.5 // upscale_factor), int(MAX_PIXEL_BUDGET ** 0.5 // aspect_ratio // upscale_factor)}) pixels."
93
+ )
94
+ gr.Info(
95
+ f"Requested output image is too large ({w * upscale_factor}x{h * upscale_factor}). Resizing input to ({int(aspect_ratio * MAX_PIXEL_BUDGET ** 0.5 // upscale_factor), int(MAX_PIXEL_BUDGET ** 0.5 // aspect_ratio // upscale_factor)}) pixels budget."
96
+ )
97
+ input_image = input_image.resize(
98
+ (
99
+ int(aspect_ratio * MAX_PIXEL_BUDGET**0.5 // upscale_factor),
100
+ int(MAX_PIXEL_BUDGET**0.5 // aspect_ratio // upscale_factor),
101
+ )
102
+ )
103
+ was_resized = True
104
+
105
+ # resize to multiple of 8
106
+ w, h = input_image.size
107
+ w = w - w % 8
108
+ h = h - h % 8
109
+
110
+ return input_image.resize((w, h)), w_original, h_original, was_resized
111
+
112
+
113
+ @spaces.GPU#(duration=42)
114
+ def infer(
115
+ seed,
116
+ randomize_seed,
117
+ input_image,
118
+ num_inference_steps,
119
+ upscale_factor,
120
+ controlnet_conditioning_scale,
121
+ progress=gr.Progress(track_tqdm=True),
122
+ ):
123
+ if randomize_seed:
124
+ seed = random.randint(0, MAX_SEED)
125
+ true_input_image = input_image
126
+ input_image, w_original, h_original, was_resized = process_input(
127
+ input_image, upscale_factor
128
+ )
129
+
130
+ # rescale with upscale factor
131
+ w, h = input_image.size
132
+ control_image = input_image.resize((w * upscale_factor, h * upscale_factor))
133
+
134
+ generator = torch.Generator().manual_seed(seed)
135
+
136
+ gr.Info("Upscaling image...")
137
+ image = pipe(
138
+ prompt="",
139
+ control_image=control_image,
140
+ controlnet_conditioning_scale=controlnet_conditioning_scale,
141
+ num_inference_steps=num_inference_steps,
142
+ guidance_scale=3.5,
143
+ height=control_image.size[1],
144
+ width=control_image.size[0],
145
+ generator=generator,
146
+ ).images[0]
147
+
148
+ if was_resized:
149
+ gr.Info(
150
+ f"Resizing output image to targeted {w_original * upscale_factor}x{h_original * upscale_factor} size."
151
+ )
152
+
153
+ # resize to target desired size
154
+ image = image.resize((w_original * upscale_factor, h_original * upscale_factor))
155
+ image.save("output.jpg")
156
+
157
+ # convert to numpy
158
+ return [true_input_image, image, seed]
159
+
160
+
161
+
162
+ def create_snow_effect():
163
+ # CSS 스타일 정의
164
+ snow_css = """
165
+ @keyframes snowfall {
166
+ 0% {
167
+ transform: translateY(-10vh) translateX(0);
168
+ opacity: 1;
169
+ }
170
+ 100% {
171
+ transform: translateY(100vh) translateX(100px);
172
+ opacity: 0.3;
173
+ }
174
+ }
175
+ .snowflake {
176
+ position: fixed;
177
+ color: white;
178
+ font-size: 1.5em;
179
+ user-select: none;
180
+ z-index: 1000;
181
+ pointer-events: none;
182
+ animation: snowfall linear infinite;
183
+ }
184
+ """
185
+
186
+ # JavaScript 코드 정의
187
+ snow_js = """
188
+ function createSnowflake() {
189
+ const snowflake = document.createElement('div');
190
+ snowflake.innerHTML = '❄';
191
+ snowflake.className = 'snowflake';
192
+ snowflake.style.left = Math.random() * 100 + 'vw';
193
+ snowflake.style.animationDuration = Math.random() * 3 + 2 + 's';
194
+ snowflake.style.opacity = Math.random();
195
+ document.body.appendChild(snowflake);
196
+
197
+ setTimeout(() => {
198
+ snowflake.remove();
199
+ }, 5000);
200
+ }
201
+ setInterval(createSnowflake, 200);
202
+ """
203
+
204
+ # CSS와 JavaScript를 결합한 HTML
205
+ snow_html = f"""
206
+ <style>
207
+ {snow_css}
208
+ </style>
209
+ <script>
210
+ {snow_js}
211
+ </script>
212
+ """
213
+
214
+ return gr.HTML(snow_html)
215
+
216
+
217
+ with gr.Blocks(theme="Yntec/HaleyCH_Theme_Orange", css=css) as demo:
218
+
219
+ create_snow_effect()
220
+ with gr.Row():
221
+ run_button = gr.Button(value="Run")
222
+
223
+ with gr.Row():
224
+ with gr.Column(scale=4):
225
+ input_im = gr.Image(label="Input Image", type="pil")
226
+ with gr.Column(scale=1):
227
+ num_inference_steps = gr.Slider(
228
+ label="Number of Inference Steps",
229
+ minimum=8,
230
+ maximum=50,
231
+ step=1,
232
+ value=28,
233
+ )
234
+ upscale_factor = gr.Slider(
235
+ label="Upscale Factor",
236
+ minimum=1,
237
+ maximum=4,
238
+ step=1,
239
+ value=4,
240
+ )
241
+ controlnet_conditioning_scale = gr.Slider(
242
+ label="Controlnet Conditioning Scale",
243
+ minimum=0.1,
244
+ maximum=1.5,
245
+ step=0.1,
246
+ value=0.6,
247
+ )
248
+ seed = gr.Slider(
249
+ label="Seed",
250
+ minimum=0,
251
+ maximum=MAX_SEED,
252
+ step=1,
253
+ value=42,
254
+ )
255
+
256
+ randomize_seed = gr.Checkbox(label="Randomize seed", value=True)
257
+
258
+ with gr.Row():
259
+ result = ImageSlider(label="Input / Output", type="pil", interactive=True)
260
+
261
+ examples = gr.Examples(
262
+ examples=[
263
+ [42, False, "z1.webp", 28, 4, 0.6],
264
+ [42, False, "z2.webp", 28, 4, 0.6],
265
+
266
+ ],
267
+ inputs=[
268
+ seed,
269
+ randomize_seed,
270
+ input_im,
271
+ num_inference_steps,
272
+ upscale_factor,
273
+ controlnet_conditioning_scale,
274
+ ],
275
+ fn=infer,
276
+ outputs=result,
277
+ cache_examples="lazy",
278
+ )
279
+
280
+ # examples = gr.Examples(
281
+ # examples=[
282
+ # #[42, False, "examples/image_1.jpg", 28, 4, 0.6],
283
+ # [42, False, "examples/image_2.jpg", 28, 4, 0.6],
284
+ # #[42, False, "examples/image_3.jpg", 28, 4, 0.6],
285
+ # #[42, False, "examples/image_4.jpg", 28, 4, 0.6],
286
+ # [42, False, "examples/image_5.jpg", 28, 4, 0.6],
287
+ # [42, False, "examples/image_6.jpg", 28, 4, 0.6],
288
+ # [42, False, "examples/image_7.jpg", 28, 4, 0.6],
289
+ # ],
290
+ # inputs=[
291
+ # seed,
292
+ # randomize_seed,
293
+ # input_im,
294
+ # num_inference_steps,
295
+ # upscale_factor,
296
+ # controlnet_conditioning_scale,
297
+ # ],
298
+ # )
299
+
300
+
301
+ gr.on(
302
+ [run_button.click],
303
+ fn=infer,
304
+ inputs=[
305
+ seed,
306
+ randomize_seed,
307
+ input_im,
308
+ num_inference_steps,
309
+ upscale_factor,
310
+ controlnet_conditioning_scale,
311
+ ],
312
+ outputs=result,
313
+ show_api=False,
314
+ # show_progress="minimal",
315
+ )
316
+
317
+ demo.queue().launch(share=False, show_api=False)