Spaces:
Running
on
Zero
Running
on
Zero
z 3 files
Browse files- README.md +14 -17
- app.py +182 -880
- requirements.txt +15 -41
README.md
CHANGED
@@ -1,21 +1,18 @@
|
|
1 |
---
|
2 |
-
title:
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
3 |
sdk: gradio
|
4 |
-
|
5 |
-
|
|
|
6 |
app_file: app.py
|
7 |
-
|
8 |
-
|
9 |
-
colorTo: pink
|
10 |
-
tags:
|
11 |
-
- Upscaling
|
12 |
-
- Restoring
|
13 |
-
- Image-to-Image
|
14 |
-
- Image-2-Image
|
15 |
-
- Img-to-Img
|
16 |
-
- Img-2-Img
|
17 |
-
- language models
|
18 |
-
- LLMs
|
19 |
-
short_description: Restore blurred or small images with prompt
|
20 |
-
suggested_hardware: zero-a10g
|
21 |
---
|
|
|
1 |
---
|
2 |
+
title: Hunyuan Video
|
3 |
+
emoji: 🎥
|
4 |
+
colorFrom: red
|
5 |
+
colorTo: blue
|
6 |
+
tags:
|
7 |
+
- text-to-video
|
8 |
+
- video-generation
|
9 |
+
- LLM
|
10 |
+
short_description: Text-to-Video
|
11 |
sdk: gradio
|
12 |
+
sdk_version: 4.44.0
|
13 |
+
suggested_hardware: l40sx1
|
14 |
+
suggested_storage: large
|
15 |
app_file: app.py
|
16 |
+
models:
|
17 |
+
- tencent/HunyuanVideo
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
18 |
---
|
app.py
CHANGED
@@ -1,907 +1,209 @@
|
|
1 |
import os
|
2 |
-
import gradio as gr
|
3 |
-
import argparse
|
4 |
-
import numpy as np
|
5 |
-
import torch
|
6 |
-
import einops
|
7 |
-
import copy
|
8 |
-
import math
|
9 |
import time
|
|
|
|
|
|
|
|
|
10 |
import random
|
11 |
import spaces
|
12 |
-
import
|
13 |
-
import uuid
|
14 |
-
|
15 |
-
from gradio_imageslider import ImageSlider
|
16 |
-
from PIL import Image
|
17 |
-
from SUPIR.util import HWC3, upscale_image, fix_resize, convert_dtype, create_SUPIR_model, load_QF_ckpt
|
18 |
-
from huggingface_hub import hf_hub_download
|
19 |
-
from pillow_heif import register_heif_opener
|
20 |
-
|
21 |
-
register_heif_opener()
|
22 |
-
|
23 |
-
max_64_bit_int = np.iinfo(np.int32).max
|
24 |
|
25 |
-
|
26 |
-
|
27 |
-
|
28 |
-
|
29 |
-
|
30 |
|
31 |
-
|
32 |
-
parser.add_argument("--opt", type=str, default='options/SUPIR_v0.yaml')
|
33 |
-
parser.add_argument("--ip", type=str, default='127.0.0.1')
|
34 |
-
parser.add_argument("--port", type=int, default='6688')
|
35 |
-
parser.add_argument("--no_llava", action='store_true', default=True)#False
|
36 |
-
parser.add_argument("--use_image_slider", action='store_true', default=False)#False
|
37 |
-
parser.add_argument("--log_history", action='store_true', default=False)
|
38 |
-
parser.add_argument("--loading_half_params", action='store_true', default=False)#False
|
39 |
-
parser.add_argument("--use_tile_vae", action='store_true', default=True)#False
|
40 |
-
parser.add_argument("--encoder_tile_size", type=int, default=512)
|
41 |
-
parser.add_argument("--decoder_tile_size", type=int, default=64)
|
42 |
-
parser.add_argument("--load_8bit_llava", action='store_true', default=False)
|
43 |
-
args = parser.parse_args()
|
44 |
|
45 |
if torch.cuda.device_count() > 0:
|
46 |
-
|
|
|
47 |
|
48 |
-
|
49 |
-
|
50 |
-
|
51 |
-
|
52 |
-
if args.use_tile_vae:
|
53 |
-
model.init_tile_vae(encoder_tile_size=args.encoder_tile_size, decoder_tile_size=args.decoder_tile_size)
|
54 |
-
model = model.to(SUPIR_device)
|
55 |
-
model.first_stage_model.denoise_encoder_s1 = copy.deepcopy(model.first_stage_model.denoise_encoder)
|
56 |
-
model.current_model = 'v0-Q'
|
57 |
-
ckpt_Q, ckpt_F = load_QF_ckpt(args.opt)
|
58 |
|
59 |
-
|
60 |
-
|
61 |
-
|
62 |
-
|
63 |
|
64 |
-
def
|
65 |
-
|
66 |
-
return random.randint(0, max_64_bit_int)
|
67 |
-
return seed
|
68 |
-
|
69 |
-
def reset():
|
70 |
-
return [
|
71 |
-
None,
|
72 |
-
0,
|
73 |
-
None,
|
74 |
-
None,
|
75 |
-
"Cinematic, High Contrast, highly detailed, taken using a Canon EOS R camera, hyper detailed photo - realistic maximum detail, 32k, Color Grading, ultra HD, extreme meticulous detailing, skin pore detailing, hyper sharpness, perfect without deformations.",
|
76 |
-
"painting, oil painting, illustration, drawing, art, sketch, anime, cartoon, CG Style, 3D render, unreal engine, blurring, aliasing, unsharp, weird textures, ugly, dirty, messy, worst quality, low quality, frames, watermark, signature, jpeg artifacts, deformed, lowres, over-smooth",
|
77 |
-
1,
|
78 |
-
1024,
|
79 |
-
1,
|
80 |
-
2,
|
81 |
-
50,
|
82 |
-
-1.0,
|
83 |
-
1.,
|
84 |
-
default_setting.s_cfg_Quality if torch.cuda.device_count() > 0 else 1.0,
|
85 |
-
True,
|
86 |
-
random.randint(0, max_64_bit_int),
|
87 |
-
5,
|
88 |
-
1.003,
|
89 |
-
"Wavelet",
|
90 |
-
"fp32",
|
91 |
-
"fp32",
|
92 |
-
1.0,
|
93 |
-
True,
|
94 |
-
False,
|
95 |
-
default_setting.spt_linear_CFG_Quality if torch.cuda.device_count() > 0 else 1.0,
|
96 |
-
0.,
|
97 |
-
"v0-Q",
|
98 |
-
"input",
|
99 |
-
6
|
100 |
-
]
|
101 |
-
|
102 |
-
def check(input_image):
|
103 |
-
if input_image is None:
|
104 |
-
raise gr.Error("Please provide an image to restore.")
|
105 |
-
|
106 |
-
@spaces.GPU(duration=420)
|
107 |
-
def stage1_process(
|
108 |
-
input_image,
|
109 |
-
gamma_correction,
|
110 |
-
diff_dtype,
|
111 |
-
ae_dtype
|
112 |
-
):
|
113 |
-
print('stage1_process ==>>')
|
114 |
if torch.cuda.device_count() == 0:
|
115 |
-
|
116 |
-
|
117 |
-
|
118 |
-
|
119 |
-
|
120 |
-
|
121 |
-
|
122 |
-
|
123 |
-
|
124 |
-
|
125 |
-
|
126 |
-
|
127 |
-
LQ = model.batchify_denoise(LQ, is_stage1=True)
|
128 |
-
LQ = (LQ[0].permute(1, 2, 0) * 127.5 + 127.5).cpu().numpy().round().clip(0, 255).astype(np.uint8)
|
129 |
-
# gamma correction
|
130 |
-
LQ = LQ / 255.0
|
131 |
-
LQ = np.power(LQ, gamma_correction)
|
132 |
-
LQ *= 255.0
|
133 |
-
LQ = LQ.round().clip(0, 255).astype(np.uint8)
|
134 |
-
print('<<== stage1_process')
|
135 |
-
return LQ, gr.update(visible = True)
|
136 |
|
137 |
-
|
138 |
-
try:
|
139 |
-
return restore_in_Xmin(*args, **kwargs)
|
140 |
-
except Exception as e:
|
141 |
-
# NO_GPU_MESSAGE_INQUEUE
|
142 |
-
print("gradio.exceptions.Error 'No GPU is currently available for you after 60s'")
|
143 |
-
print('str(type(e)): ' + str(type(e))) # <class 'gradio.exceptions.Error'>
|
144 |
-
print('str(e): ' + str(e)) # You have exceeded your GPU quota...
|
145 |
-
try:
|
146 |
-
print('e.message: ' + e.message) # No GPU is currently available for you after 60s
|
147 |
-
except Exception as e2:
|
148 |
-
print('Failure')
|
149 |
-
if str(e).startswith("No GPU is currently available for you after 60s"):
|
150 |
-
print('Exception identified!!!')
|
151 |
-
#if str(type(e)) == "<class 'gradio.exceptions.Error'>":
|
152 |
-
#print('Exception of name ' + type(e).__name__)
|
153 |
-
raise e
|
154 |
|
155 |
-
def
|
156 |
-
noisy_image,
|
157 |
-
rotation,
|
158 |
-
denoise_image,
|
159 |
prompt,
|
160 |
-
|
161 |
-
|
162 |
-
num_samples,
|
163 |
-
min_size,
|
164 |
-
downscale,
|
165 |
-
upscale,
|
166 |
-
edm_steps,
|
167 |
-
s_stage1,
|
168 |
-
s_stage2,
|
169 |
-
s_cfg,
|
170 |
-
randomize_seed,
|
171 |
seed,
|
172 |
-
|
173 |
-
|
174 |
-
|
175 |
-
|
176 |
-
ae_dtype,
|
177 |
-
gamma_correction,
|
178 |
-
linear_CFG,
|
179 |
-
linear_s_stage2,
|
180 |
-
spt_linear_CFG,
|
181 |
-
spt_linear_s_stage2,
|
182 |
-
model_select,
|
183 |
-
output_format,
|
184 |
-
allocation
|
185 |
):
|
186 |
-
print("
|
187 |
-
|
188 |
-
|
189 |
-
|
190 |
-
|
191 |
-
|
192 |
-
|
193 |
-
|
194 |
-
|
195 |
-
|
196 |
-
|
197 |
-
|
198 |
-
print("s_stage2: " + str(s_stage2))
|
199 |
-
print("s_cfg: " + str(s_cfg))
|
200 |
-
print("randomize_seed: " + str(randomize_seed))
|
201 |
-
print("seed: " + str(seed))
|
202 |
-
print("s_churn: " + str(s_churn))
|
203 |
-
print("s_noise: " + str(s_noise))
|
204 |
-
print("color_fix_type: " + str(color_fix_type))
|
205 |
-
print("diff_dtype: " + str(diff_dtype))
|
206 |
-
print("ae_dtype: " + str(ae_dtype))
|
207 |
-
print("gamma_correction: " + str(gamma_correction))
|
208 |
-
print("linear_CFG: " + str(linear_CFG))
|
209 |
-
print("linear_s_stage2: " + str(linear_s_stage2))
|
210 |
-
print("spt_linear_CFG: " + str(spt_linear_CFG))
|
211 |
-
print("spt_linear_s_stage2: " + str(spt_linear_s_stage2))
|
212 |
-
print("model_select: " + str(model_select))
|
213 |
-
print("GPU time allocation: " + str(allocation) + " min")
|
214 |
-
print("output_format: " + str(output_format))
|
215 |
-
|
216 |
-
input_format = re.sub(r"^.*\.([^\.]+)$", r"\1", noisy_image)
|
217 |
-
|
218 |
-
if input_format not in ['png', 'webp', 'jpg', 'jpeg', 'gif', 'bmp', 'heic']:
|
219 |
-
gr.Warning('Invalid image format. Please first convert into *.png, *.webp, *.jpg, *.jpeg, *.gif, *.bmp or *.heic.')
|
220 |
-
return None, None, None, None
|
221 |
-
|
222 |
-
if output_format == "input":
|
223 |
-
if noisy_image is None:
|
224 |
-
output_format = "png"
|
225 |
-
else:
|
226 |
-
output_format = input_format
|
227 |
-
print("final output_format: " + str(output_format))
|
228 |
-
|
229 |
-
if prompt is None:
|
230 |
-
prompt = ""
|
231 |
-
|
232 |
-
if a_prompt is None:
|
233 |
-
a_prompt = ""
|
234 |
-
|
235 |
-
if n_prompt is None:
|
236 |
-
n_prompt = ""
|
237 |
-
|
238 |
-
if prompt != "" and a_prompt != "":
|
239 |
-
a_prompt = prompt + ", " + a_prompt
|
240 |
-
else:
|
241 |
-
a_prompt = prompt + a_prompt
|
242 |
-
print("Final prompt: " + str(a_prompt))
|
243 |
-
|
244 |
-
denoise_image = np.array(Image.open(noisy_image if denoise_image is None else denoise_image))
|
245 |
-
|
246 |
-
if rotation == 90:
|
247 |
-
denoise_image = np.array(list(zip(*denoise_image[::-1])))
|
248 |
-
elif rotation == 180:
|
249 |
-
denoise_image = np.array(list(zip(*denoise_image[::-1])))
|
250 |
-
denoise_image = np.array(list(zip(*denoise_image[::-1])))
|
251 |
-
elif rotation == -90:
|
252 |
-
denoise_image = np.array(list(zip(*denoise_image))[::-1])
|
253 |
-
|
254 |
-
if 1 < downscale:
|
255 |
-
input_height, input_width, input_channel = denoise_image.shape
|
256 |
-
denoise_image = np.array(Image.fromarray(denoise_image).resize((input_width // downscale, input_height // downscale), Image.LANCZOS))
|
257 |
-
|
258 |
-
denoise_image = HWC3(denoise_image)
|
259 |
-
|
260 |
-
if torch.cuda.device_count() == 0:
|
261 |
-
gr.Warning('Set this space to GPU config to make it work.')
|
262 |
-
return [noisy_image, denoise_image], gr.update(label="Downloadable results in *." + output_format + " format", format = output_format, value = [denoise_image]), None, gr.update(visible=True)
|
263 |
-
|
264 |
-
if model_select != model.current_model:
|
265 |
-
print('load ' + model_select)
|
266 |
-
if model_select == 'v0-Q':
|
267 |
-
model.load_state_dict(ckpt_Q, strict=False)
|
268 |
-
elif model_select == 'v0-F':
|
269 |
-
model.load_state_dict(ckpt_F, strict=False)
|
270 |
-
model.current_model = model_select
|
271 |
-
|
272 |
-
model.ae_dtype = convert_dtype(ae_dtype)
|
273 |
-
model.model.dtype = convert_dtype(diff_dtype)
|
274 |
-
|
275 |
-
# Allocation
|
276 |
-
if allocation == 1:
|
277 |
-
return restore_in_1min(
|
278 |
-
noisy_image, denoise_image, prompt, a_prompt, n_prompt, num_samples, min_size, downscale, upscale, edm_steps, s_stage1, s_stage2, s_cfg, randomize_seed, seed, s_churn, s_noise, color_fix_type, diff_dtype, ae_dtype, gamma_correction, linear_CFG, linear_s_stage2, spt_linear_CFG, spt_linear_s_stage2, model_select, output_format, allocation
|
279 |
-
)
|
280 |
-
if allocation == 2:
|
281 |
-
return restore_in_2min(
|
282 |
-
noisy_image, denoise_image, prompt, a_prompt, n_prompt, num_samples, min_size, downscale, upscale, edm_steps, s_stage1, s_stage2, s_cfg, randomize_seed, seed, s_churn, s_noise, color_fix_type, diff_dtype, ae_dtype, gamma_correction, linear_CFG, linear_s_stage2, spt_linear_CFG, spt_linear_s_stage2, model_select, output_format, allocation
|
283 |
-
)
|
284 |
-
if allocation == 3:
|
285 |
-
return restore_in_3min(
|
286 |
-
noisy_image, denoise_image, prompt, a_prompt, n_prompt, num_samples, min_size, downscale, upscale, edm_steps, s_stage1, s_stage2, s_cfg, randomize_seed, seed, s_churn, s_noise, color_fix_type, diff_dtype, ae_dtype, gamma_correction, linear_CFG, linear_s_stage2, spt_linear_CFG, spt_linear_s_stage2, model_select, output_format, allocation
|
287 |
-
)
|
288 |
-
if allocation == 4:
|
289 |
-
return restore_in_4min(
|
290 |
-
noisy_image, denoise_image, prompt, a_prompt, n_prompt, num_samples, min_size, downscale, upscale, edm_steps, s_stage1, s_stage2, s_cfg, randomize_seed, seed, s_churn, s_noise, color_fix_type, diff_dtype, ae_dtype, gamma_correction, linear_CFG, linear_s_stage2, spt_linear_CFG, spt_linear_s_stage2, model_select, output_format, allocation
|
291 |
-
)
|
292 |
-
if allocation == 5:
|
293 |
-
return restore_in_5min(
|
294 |
-
noisy_image, denoise_image, prompt, a_prompt, n_prompt, num_samples, min_size, downscale, upscale, edm_steps, s_stage1, s_stage2, s_cfg, randomize_seed, seed, s_churn, s_noise, color_fix_type, diff_dtype, ae_dtype, gamma_correction, linear_CFG, linear_s_stage2, spt_linear_CFG, spt_linear_s_stage2, model_select, output_format, allocation
|
295 |
-
)
|
296 |
-
if allocation == 7:
|
297 |
-
return restore_in_7min(
|
298 |
-
noisy_image, denoise_image, prompt, a_prompt, n_prompt, num_samples, min_size, downscale, upscale, edm_steps, s_stage1, s_stage2, s_cfg, randomize_seed, seed, s_churn, s_noise, color_fix_type, diff_dtype, ae_dtype, gamma_correction, linear_CFG, linear_s_stage2, spt_linear_CFG, spt_linear_s_stage2, model_select, output_format, allocation
|
299 |
-
)
|
300 |
-
if allocation == 8:
|
301 |
-
return restore_in_8min(
|
302 |
-
noisy_image, denoise_image, prompt, a_prompt, n_prompt, num_samples, min_size, downscale, upscale, edm_steps, s_stage1, s_stage2, s_cfg, randomize_seed, seed, s_churn, s_noise, color_fix_type, diff_dtype, ae_dtype, gamma_correction, linear_CFG, linear_s_stage2, spt_linear_CFG, spt_linear_s_stage2, model_select, output_format, allocation
|
303 |
-
)
|
304 |
-
if allocation == 9:
|
305 |
-
return restore_in_9min(
|
306 |
-
noisy_image, denoise_image, prompt, a_prompt, n_prompt, num_samples, min_size, downscale, upscale, edm_steps, s_stage1, s_stage2, s_cfg, randomize_seed, seed, s_churn, s_noise, color_fix_type, diff_dtype, ae_dtype, gamma_correction, linear_CFG, linear_s_stage2, spt_linear_CFG, spt_linear_s_stage2, model_select, output_format, allocation
|
307 |
-
)
|
308 |
-
if allocation == 10:
|
309 |
-
return restore_in_10min(
|
310 |
-
noisy_image, denoise_image, prompt, a_prompt, n_prompt, num_samples, min_size, downscale, upscale, edm_steps, s_stage1, s_stage2, s_cfg, randomize_seed, seed, s_churn, s_noise, color_fix_type, diff_dtype, ae_dtype, gamma_correction, linear_CFG, linear_s_stage2, spt_linear_CFG, spt_linear_s_stage2, model_select, output_format, allocation
|
311 |
-
)
|
312 |
-
else:
|
313 |
-
return restore_in_6min(
|
314 |
-
noisy_image, denoise_image, prompt, a_prompt, n_prompt, num_samples, min_size, downscale, upscale, edm_steps, s_stage1, s_stage2, s_cfg, randomize_seed, seed, s_churn, s_noise, color_fix_type, diff_dtype, ae_dtype, gamma_correction, linear_CFG, linear_s_stage2, spt_linear_CFG, spt_linear_s_stage2, model_select, output_format, allocation
|
315 |
-
)
|
316 |
-
|
317 |
-
@spaces.GPU(duration=59)
|
318 |
-
def restore_in_1min(*args, **kwargs):
|
319 |
-
return restore_on_gpu(*args, **kwargs)
|
320 |
-
|
321 |
-
@spaces.GPU(duration=119)
|
322 |
-
def restore_in_2min(*args, **kwargs):
|
323 |
-
return restore_on_gpu(*args, **kwargs)
|
324 |
-
|
325 |
-
@spaces.GPU(duration=179)
|
326 |
-
def restore_in_3min(*args, **kwargs):
|
327 |
-
return restore_on_gpu(*args, **kwargs)
|
328 |
-
|
329 |
-
@spaces.GPU(duration=239)
|
330 |
-
def restore_in_4min(*args, **kwargs):
|
331 |
-
return restore_on_gpu(*args, **kwargs)
|
332 |
-
|
333 |
-
@spaces.GPU(duration=299)
|
334 |
-
def restore_in_5min(*args, **kwargs):
|
335 |
-
return restore_on_gpu(*args, **kwargs)
|
336 |
-
|
337 |
-
@spaces.GPU(duration=359)
|
338 |
-
def restore_in_6min(*args, **kwargs):
|
339 |
-
return restore_on_gpu(*args, **kwargs)
|
340 |
-
|
341 |
-
@spaces.GPU(duration=419)
|
342 |
-
def restore_in_7min(*args, **kwargs):
|
343 |
-
return restore_on_gpu(*args, **kwargs)
|
344 |
-
|
345 |
-
@spaces.GPU(duration=479)
|
346 |
-
def restore_in_8min(*args, **kwargs):
|
347 |
-
return restore_on_gpu(*args, **kwargs)
|
348 |
-
|
349 |
-
@spaces.GPU(duration=539)
|
350 |
-
def restore_in_9min(*args, **kwargs):
|
351 |
-
return restore_on_gpu(*args, **kwargs)
|
352 |
-
|
353 |
-
@spaces.GPU(duration=599)
|
354 |
-
def restore_in_10min(*args, **kwargs):
|
355 |
-
return restore_on_gpu(*args, **kwargs)
|
356 |
|
357 |
-
|
358 |
-
|
359 |
-
|
360 |
prompt,
|
361 |
-
|
362 |
-
|
363 |
-
num_samples,
|
364 |
-
min_size,
|
365 |
-
downscale,
|
366 |
-
upscale,
|
367 |
-
edm_steps,
|
368 |
-
s_stage1,
|
369 |
-
s_stage2,
|
370 |
-
s_cfg,
|
371 |
-
randomize_seed,
|
372 |
seed,
|
373 |
-
|
374 |
-
|
375 |
-
|
376 |
-
|
377 |
-
ae_dtype,
|
378 |
-
gamma_correction,
|
379 |
-
linear_CFG,
|
380 |
-
linear_s_stage2,
|
381 |
-
spt_linear_CFG,
|
382 |
-
spt_linear_s_stage2,
|
383 |
-
model_select,
|
384 |
-
output_format,
|
385 |
-
allocation
|
386 |
):
|
387 |
-
|
388 |
-
|
389 |
-
|
390 |
-
|
391 |
-
|
392 |
-
|
393 |
-
|
394 |
-
|
395 |
-
|
396 |
-
|
397 |
-
|
398 |
-
|
399 |
-
|
400 |
-
|
401 |
-
|
402 |
-
|
403 |
-
|
404 |
-
|
405 |
-
|
406 |
-
|
407 |
-
|
408 |
-
|
409 |
-
|
410 |
-
|
411 |
-
|
412 |
-
|
413 |
-
#
|
414 |
-
|
415 |
-
|
416 |
-
|
417 |
-
|
418 |
-
|
419 |
-
|
420 |
-
|
421 |
-
|
422 |
-
|
423 |
-
|
424 |
-
|
425 |
-
"
|
426 |
-
|
427 |
-
|
428 |
-
|
429 |
-
|
430 |
-
|
431 |
-
|
432 |
-
|
433 |
-
|
434 |
-
|
435 |
-
try:
|
436 |
-
print("Initial resolution: " + f'{input_width * input_height:,}')
|
437 |
-
print("Final resolution: " + f'{result_width * result_height:,}')
|
438 |
-
print("edm_steps: " + str(edm_steps))
|
439 |
-
print("num_samples: " + str(num_samples))
|
440 |
-
print("downscale: " + str(downscale))
|
441 |
-
print("Estimated minutes: " + f'{(((result_width * result_height**(1/1.75)) * input_width * input_height * (edm_steps**(1/2)) * (num_samples**(1/2.5)))**(1/2.5)) / 25000:,}')
|
442 |
-
except Exception as e:
|
443 |
-
print('Exception of Estimation')
|
444 |
-
|
445 |
-
# Only one image can be shown in the slider
|
446 |
-
return [noisy_image] + [results[0]], gr.update(label="Downloadable results in *." + output_format + " format", format = output_format, value = results), gr.update(value = information, visible = True), gr.update(visible=True)
|
447 |
-
|
448 |
-
def load_and_reset(param_setting):
|
449 |
-
print('load_and_reset ==>>')
|
450 |
-
if torch.cuda.device_count() == 0:
|
451 |
-
gr.Warning('Set this space to GPU config to make it work.')
|
452 |
-
return None, None, None, None, None, None, None, None, None, None, None, None, None, None
|
453 |
-
edm_steps = default_setting.edm_steps
|
454 |
-
s_stage2 = 1.0
|
455 |
-
s_stage1 = -1.0
|
456 |
-
s_churn = 5
|
457 |
-
s_noise = 1.003
|
458 |
-
a_prompt = 'Cinematic, High Contrast, highly detailed, taken using a Canon EOS R camera, hyper detailed photo - ' \
|
459 |
-
'realistic maximum detail, 32k, Color Grading, ultra HD, extreme meticulous detailing, skin pore ' \
|
460 |
-
'detailing, hyper sharpness, perfect without deformations.'
|
461 |
-
n_prompt = 'painting, oil painting, illustration, drawing, art, sketch, anime, cartoon, CG Style, ' \
|
462 |
-
'3D render, unreal engine, blurring, dirty, messy, worst quality, low quality, frames, watermark, ' \
|
463 |
-
'signature, jpeg artifacts, deformed, lowres, over-smooth'
|
464 |
-
color_fix_type = 'Wavelet'
|
465 |
-
spt_linear_s_stage2 = 0.0
|
466 |
-
linear_s_stage2 = False
|
467 |
-
linear_CFG = True
|
468 |
-
if param_setting == "Quality":
|
469 |
-
s_cfg = default_setting.s_cfg_Quality
|
470 |
-
spt_linear_CFG = default_setting.spt_linear_CFG_Quality
|
471 |
-
model_select = "v0-Q"
|
472 |
-
elif param_setting == "Fidelity":
|
473 |
-
s_cfg = default_setting.s_cfg_Fidelity
|
474 |
-
spt_linear_CFG = default_setting.spt_linear_CFG_Fidelity
|
475 |
-
model_select = "v0-F"
|
476 |
-
else:
|
477 |
-
raise NotImplementedError
|
478 |
-
gr.Info('The parameters are reset.')
|
479 |
-
print('<<== load_and_reset')
|
480 |
-
return edm_steps, s_cfg, s_stage2, s_stage1, s_churn, s_noise, a_prompt, n_prompt, color_fix_type, linear_CFG, \
|
481 |
-
linear_s_stage2, spt_linear_CFG, spt_linear_s_stage2, model_select
|
482 |
-
|
483 |
-
def log_information(result_gallery):
|
484 |
-
print('log_information')
|
485 |
-
if result_gallery is not None:
|
486 |
-
for i, result in enumerate(result_gallery):
|
487 |
-
print(result[0])
|
488 |
-
|
489 |
-
def on_select_result(result_slider, result_gallery, evt: gr.SelectData):
|
490 |
-
print('on_select_result')
|
491 |
-
if result_gallery is not None:
|
492 |
-
for i, result in enumerate(result_gallery):
|
493 |
-
print(result[0])
|
494 |
-
return [result_slider[0], result_gallery[evt.index][0]]
|
495 |
-
|
496 |
-
title_html = """
|
497 |
-
<h1><center>SUPIR</center></h1>
|
498 |
-
<big><center>Upscale your images up to x10 freely, without account, without watermark and download it</center></big>
|
499 |
-
<center><big><big>🤸<big><big><big><big><big><big>🤸</big></big></big></big></big></big></big></big></center>
|
500 |
-
|
501 |
-
<p>This is an online demo of SUPIR, a practicing model scaling for photo-realistic image restoration.
|
502 |
-
The content added by SUPIR is <b><u>imagination, not real-world information</u></b>.
|
503 |
-
SUPIR is for beauty and illustration only.
|
504 |
-
Most of the processes last few minutes.
|
505 |
-
If you want to upscale AI-generated images, be noticed that <i>PixArt Sigma</i> space can directly generate 5984x5984 images.
|
506 |
-
Due to Gradio issues, the generated image is slightly less satured than the original.
|
507 |
-
Please leave a <a href="https://huggingface.co/spaces/Fabrice-TIERCELIN/SUPIR/discussions/new">message in discussion</a> if you encounter issues.
|
508 |
-
You can also use <a href="https://huggingface.co/spaces/gokaygokay/AuraSR">AuraSR</a> to upscale x4.
|
509 |
-
|
510 |
-
<p><center><a href="https://arxiv.org/abs/2401.13627">Paper</a>   <a href="http://supir.xpixel.group/">Project Page</a>   <a href="https://huggingface.co/blog/MonsterMMORPG/supir-sota-image-upscale-better-than-magnific-ai">Local Install Guide</a></center></p>
|
511 |
-
<p><center><a style="display:inline-block" href='https://github.com/Fanghua-Yu/SUPIR'><img alt="GitHub Repo stars" src="https://img.shields.io/github/stars/Fanghua-Yu/SUPIR?style=social"></a></center></p>
|
512 |
-
"""
|
513 |
-
|
514 |
-
|
515 |
-
claim_md = """
|
516 |
-
## **Piracy**
|
517 |
-
The images are not stored but the logs are saved during a month.
|
518 |
-
## **How to get SUPIR**
|
519 |
-
You can get SUPIR on HuggingFace by [duplicating this space](https://huggingface.co/spaces/Fabrice-TIERCELIN/SUPIR?duplicate=true) and set GPU.
|
520 |
-
You can also install SUPIR on your computer following [this tutorial](https://huggingface.co/blog/MonsterMMORPG/supir-sota-image-upscale-better-than-magnific-ai).
|
521 |
-
You can install _Pinokio_ on your computer and then install _SUPIR_ into it. It should be quite easy if you have an Nvidia GPU.
|
522 |
-
## **Terms of use**
|
523 |
-
By using this service, users are required to agree to the following terms: The service is a research preview intended for non-commercial use only. It only provides limited safety measures and may generate offensive content. It must not be used for any illegal, harmful, violent, racist, or sexual purposes. The service may collect user dialogue data for future research. Please submit a feedback to us if you get any inappropriate answer! We will collect those to keep improving our models. For an optimal experience, please use desktop computers for this demo, as mobile devices may compromise its quality.
|
524 |
-
## **License**
|
525 |
-
The service is a research preview intended for non-commercial use only, subject to the model [License](https://github.com/Fanghua-Yu/SUPIR) of SUPIR.
|
526 |
-
"""
|
527 |
-
|
528 |
-
# Gradio interface
|
529 |
-
with gr.Blocks() as interface:
|
530 |
-
if torch.cuda.device_count() == 0:
|
531 |
-
with gr.Row():
|
532 |
-
gr.HTML("""
|
533 |
-
<p style="background-color: red;"><big><big><big><b>⚠️To use SUPIR, <a href="https://huggingface.co/spaces/Fabrice-TIERCELIN/SUPIR?duplicate=true">duplicate this space</a> and set a GPU with 30 GB VRAM.</b>
|
534 |
|
535 |
-
|
536 |
-
|
537 |
-
|
538 |
-
|
539 |
-
|
540 |
-
|
541 |
-
rotation = gr.Radio([["No rotation", 0], ["⤵ Rotate +90°", 90], ["↩ Return 180°", 180], ["⤴ Rotate -90°", -90]], label="Orientation correction", info="Will apply the following rotation before restoring the image; the AI needs a good orientation to understand the content", value=0, interactive=True, visible=False)
|
542 |
-
with gr.Group():
|
543 |
-
prompt = gr.Textbox(label="Image description", info="Help the AI understand what the image represents; describe as much as possible, especially the details we can't see on the original image; you can write in any language", value="", placeholder="A 33 years old man, walking, in the street, Santiago, morning, Summer, photorealistic", lines=3)
|
544 |
-
prompt_hint = gr.HTML("You can use a <a href='"'https://huggingface.co/spaces/MaziyarPanahi/llava-llama-3-8b'"'>LlaVa space</a> to auto-generate the description of your image.")
|
545 |
-
upscale = gr.Radio([["x1", 1], ["x2", 2], ["x3", 3], ["x4", 4], ["x5", 5], ["x6", 6], ["x7", 7], ["x8", 8], ["x9", 9], ["x10", 10]], label="Upscale factor", info="Resolution x1 to x10", value=2, interactive=True)
|
546 |
-
output_format = gr.Radio([["As input", "input"], ["*.png", "png"], ["*.webp", "webp"], ["*.jpeg", "jpeg"], ["*.gif", "gif"], ["*.bmp", "bmp"]], label="Image format for result", info="File extention", value="input", interactive=True)
|
547 |
-
allocation = gr.Radio([["1 min", 1], ["2 min", 2], ["3 min", 3], ["4 min", 4], ["5 min", 5]], label="GPU allocation time", info="lower=May abort run, higher=Quota penalty for next runs", value=3, interactive=True)
|
548 |
-
|
549 |
-
with gr.Accordion("Pre-denoising (optional)", open=False):
|
550 |
-
gamma_correction = gr.Slider(label="Gamma Correction", info = "lower=lighter, higher=darker", minimum=0.1, maximum=2.0, value=1.0, step=0.1)
|
551 |
-
denoise_button = gr.Button(value="Pre-denoise")
|
552 |
-
denoise_image = gr.Image(label="Denoised image", show_label=True, type="filepath", sources=[], interactive = False, height=600, elem_id="image-s1")
|
553 |
-
denoise_information = gr.HTML(value="If present, the denoised image will be used for the restoration instead of the input image.", visible=False)
|
554 |
-
|
555 |
-
with gr.Accordion("Advanced options", open=False):
|
556 |
-
a_prompt = gr.Textbox(label="Additional image description",
|
557 |
-
info="Completes the main image description",
|
558 |
-
value='Cinematic, High Contrast, highly detailed, taken using a Canon EOS R '
|
559 |
-
'camera, hyper detailed photo - realistic maximum detail, 32k, Color '
|
560 |
-
'Grading, ultra HD, extreme meticulous detailing, skin pore detailing, '
|
561 |
-
'hyper sharpness, perfect without deformations.',
|
562 |
-
lines=3)
|
563 |
-
n_prompt = gr.Textbox(label="Negative image description",
|
564 |
-
info="Disambiguate by listing what the image does NOT represent",
|
565 |
-
value='painting, oil painting, illustration, drawing, art, sketch, anime, '
|
566 |
-
'cartoon, CG Style, 3D render, unreal engine, blurring, aliasing, unsharp, weird textures, ugly, dirty, messy, '
|
567 |
-
'worst quality, low quality, frames, watermark, signature, jpeg artifacts, '
|
568 |
-
'deformed, lowres, over-smooth',
|
569 |
-
lines=3)
|
570 |
-
edm_steps = gr.Slider(label="Steps", info="lower=faster, higher=more details", minimum=1, maximum=200, value=default_setting.edm_steps if torch.cuda.device_count() > 0 else 1, step=1)
|
571 |
-
num_samples = gr.Slider(label="Num Samples", info="Number of generated results", minimum=1, maximum=4 if not args.use_image_slider else 1
|
572 |
-
, value=1, step=1)
|
573 |
-
min_size = gr.Slider(label="Minimum size", info="Minimum height, minimum width of the result", minimum=32, maximum=4096, value=1024, step=32)
|
574 |
-
downscale = gr.Radio([["/1", 1], ["/2", 2], ["/3", 3], ["/4", 4], ["/5", 5], ["/6", 6], ["/7", 7], ["/8", 8], ["/9", 9], ["/10", 10]], label="Pre-downscale factor", info="Reducing blurred image reduce the process time", value=1, interactive=True)
|
575 |
-
with gr.Row():
|
576 |
-
with gr.Column():
|
577 |
-
model_select = gr.Radio([["💃 Quality (v0-Q)", "v0-Q"], ["🎯 Fidelity (v0-F)", "v0-F"]], label="Model Selection", info="Pretrained model", value="v0-Q",
|
578 |
-
interactive=True)
|
579 |
-
with gr.Column():
|
580 |
-
color_fix_type = gr.Radio([["None", "None"], ["AdaIn (improve as a photo)", "AdaIn"], ["Wavelet (for JPEG artifacts)", "Wavelet"]], label="Color-Fix Type", info="AdaIn=Improve following a style, Wavelet=For JPEG artifacts", value="AdaIn",
|
581 |
-
interactive=True)
|
582 |
-
s_cfg = gr.Slider(label="Text Guidance Scale", info="lower=follow the image, higher=follow the prompt", minimum=1.0, maximum=15.0,
|
583 |
-
value=default_setting.s_cfg_Quality if torch.cuda.device_count() > 0 else 1.0, step=0.1)
|
584 |
-
s_stage2 = gr.Slider(label="Restoring Guidance Strength", minimum=0., maximum=1., value=1., step=0.05)
|
585 |
-
s_stage1 = gr.Slider(label="Pre-denoising Guidance Strength", minimum=-1.0, maximum=6.0, value=-1.0, step=1.0)
|
586 |
-
s_churn = gr.Slider(label="S-Churn", minimum=0, maximum=40, value=5, step=1)
|
587 |
-
s_noise = gr.Slider(label="S-Noise", minimum=1.0, maximum=1.1, value=1.003, step=0.001)
|
588 |
with gr.Row():
|
589 |
with gr.Column():
|
590 |
-
|
591 |
-
|
592 |
-
|
593 |
-
|
594 |
-
|
595 |
-
|
596 |
-
|
597 |
-
|
598 |
-
|
599 |
-
|
600 |
-
|
601 |
-
|
602 |
-
|
603 |
-
|
604 |
-
|
605 |
-
|
606 |
-
|
607 |
-
|
608 |
-
|
609 |
-
|
610 |
-
|
611 |
-
|
612 |
-
|
613 |
-
|
614 |
-
|
615 |
-
|
616 |
-
|
617 |
-
|
618 |
-
|
619 |
-
|
620 |
-
|
621 |
-
|
622 |
-
|
623 |
-
|
624 |
-
|
625 |
-
|
626 |
-
|
627 |
-
|
628 |
-
|
629 |
-
|
630 |
-
|
631 |
-
|
632 |
-
|
633 |
-
|
634 |
-
|
635 |
-
|
636 |
-
|
637 |
-
|
638 |
-
|
639 |
-
|
640 |
-
|
641 |
-
|
642 |
-
|
643 |
-
|
644 |
-
|
645 |
-
|
646 |
-
|
647 |
-
|
648 |
-
3
|
649 |
-
],
|
650 |
-
[
|
651 |
-
"./Examples/Example2.jpeg",
|
652 |
-
0,
|
653 |
-
None,
|
654 |
-
"La cabeza de un gato atigrado, en una casa, fotorrealista, 8k, extremadamente detallada",
|
655 |
-
"Cinematic, High Contrast, highly detailed, taken using a Canon EOS R camera, hyper detailed photo - realistic maximum detail, 32k, Color Grading, ultra HD, extreme meticulous detailing, skin pore detailing, hyper sharpness, perfect without deformations.",
|
656 |
-
"painting, oil painting, illustration, drawing, art, sketch, anime, cartoon, CG Style, 3D render, unreal engine, blurring, aliasing, unsharp, weird textures, ugly, dirty, messy, worst quality, low quality, frames, watermark, signature, jpeg artifacts, deformed, lowres, over-smooth",
|
657 |
-
1,
|
658 |
-
1024,
|
659 |
-
1,
|
660 |
-
1,
|
661 |
-
200,
|
662 |
-
-1,
|
663 |
-
1,
|
664 |
-
7.5,
|
665 |
-
False,
|
666 |
-
42,
|
667 |
-
5,
|
668 |
-
1.003,
|
669 |
-
"Wavelet",
|
670 |
-
"fp16",
|
671 |
-
"bf16",
|
672 |
-
1.0,
|
673 |
-
True,
|
674 |
-
4,
|
675 |
-
False,
|
676 |
-
0.,
|
677 |
-
"v0-Q",
|
678 |
-
"input",
|
679 |
-
3
|
680 |
-
],
|
681 |
-
[
|
682 |
-
"./Examples/Example3.webp",
|
683 |
-
0,
|
684 |
-
None,
|
685 |
-
"A red apple",
|
686 |
-
"Cinematic, High Contrast, highly detailed, taken using a Canon EOS R camera, hyper detailed photo - realistic maximum detail, 32k, Color Grading, ultra HD, extreme meticulous detailing, skin pore detailing, hyper sharpness, perfect without deformations.",
|
687 |
-
"painting, oil painting, illustration, drawing, art, sketch, anime, cartoon, CG Style, 3D render, unreal engine, blurring, aliasing, unsharp, weird textures, ugly, dirty, messy, worst quality, low quality, frames, watermark, signature, jpeg artifacts, deformed, lowres, over-smooth",
|
688 |
-
1,
|
689 |
-
1024,
|
690 |
-
1,
|
691 |
-
1,
|
692 |
-
200,
|
693 |
-
-1,
|
694 |
-
1,
|
695 |
-
7.5,
|
696 |
-
False,
|
697 |
-
42,
|
698 |
-
5,
|
699 |
-
1.003,
|
700 |
-
"Wavelet",
|
701 |
-
"fp16",
|
702 |
-
"bf16",
|
703 |
-
1.0,
|
704 |
-
True,
|
705 |
-
4,
|
706 |
-
False,
|
707 |
-
0.,
|
708 |
-
"v0-Q",
|
709 |
-
"input",
|
710 |
-
3
|
711 |
-
],
|
712 |
-
[
|
713 |
-
"./Examples/Example3.webp",
|
714 |
-
0,
|
715 |
-
None,
|
716 |
-
"A red marble",
|
717 |
-
"Cinematic, High Contrast, highly detailed, taken using a Canon EOS R camera, hyper detailed photo - realistic maximum detail, 32k, Color Grading, ultra HD, extreme meticulous detailing, skin pore detailing, hyper sharpness, perfect without deformations.",
|
718 |
-
"painting, oil painting, illustration, drawing, art, sketch, anime, cartoon, CG Style, 3D render, unreal engine, blurring, aliasing, unsharp, weird textures, ugly, dirty, messy, worst quality, low quality, frames, watermark, signature, jpeg artifacts, deformed, lowres, over-smooth",
|
719 |
-
1,
|
720 |
-
1024,
|
721 |
-
1,
|
722 |
-
1,
|
723 |
-
200,
|
724 |
-
-1,
|
725 |
-
1,
|
726 |
-
7.5,
|
727 |
-
False,
|
728 |
-
42,
|
729 |
-
5,
|
730 |
-
1.003,
|
731 |
-
"Wavelet",
|
732 |
-
"fp16",
|
733 |
-
"bf16",
|
734 |
-
1.0,
|
735 |
-
True,
|
736 |
-
4,
|
737 |
-
False,
|
738 |
-
0.,
|
739 |
-
"v0-Q",
|
740 |
-
"input",
|
741 |
-
3
|
742 |
-
],
|
743 |
],
|
744 |
-
|
745 |
-
|
746 |
-
inputs = [
|
747 |
-
input_image,
|
748 |
-
rotation,
|
749 |
-
denoise_image,
|
750 |
-
prompt,
|
751 |
-
a_prompt,
|
752 |
-
n_prompt,
|
753 |
-
num_samples,
|
754 |
-
min_size,
|
755 |
-
downscale,
|
756 |
-
upscale,
|
757 |
-
edm_steps,
|
758 |
-
s_stage1,
|
759 |
-
s_stage2,
|
760 |
-
s_cfg,
|
761 |
-
randomize_seed,
|
762 |
-
seed,
|
763 |
-
s_churn,
|
764 |
-
s_noise,
|
765 |
-
color_fix_type,
|
766 |
-
diff_dtype,
|
767 |
-
ae_dtype,
|
768 |
-
gamma_correction,
|
769 |
-
linear_CFG,
|
770 |
-
linear_s_stage2,
|
771 |
-
spt_linear_CFG,
|
772 |
-
spt_linear_s_stage2,
|
773 |
-
model_select,
|
774 |
-
output_format,
|
775 |
-
allocation
|
776 |
-
],
|
777 |
-
outputs = [
|
778 |
-
result_slider,
|
779 |
-
result_gallery,
|
780 |
-
restore_information,
|
781 |
-
reset_btn
|
782 |
-
],
|
783 |
-
cache_examples = False,
|
784 |
-
)
|
785 |
-
|
786 |
-
with gr.Row():
|
787 |
-
gr.Markdown(claim_md)
|
788 |
|
789 |
-
|
790 |
-
input_image
|
791 |
-
], outputs = [
|
792 |
-
rotation
|
793 |
-
], queue = False, show_progress = False)
|
794 |
-
|
795 |
-
denoise_button.click(fn = check, inputs = [
|
796 |
-
input_image
|
797 |
-
], outputs = [], queue = False, show_progress = False).success(fn = stage1_process, inputs = [
|
798 |
-
input_image,
|
799 |
-
gamma_correction,
|
800 |
-
diff_dtype,
|
801 |
-
ae_dtype
|
802 |
-
], outputs=[
|
803 |
-
denoise_image,
|
804 |
-
denoise_information
|
805 |
-
])
|
806 |
|
807 |
-
|
808 |
-
|
809 |
-
|
810 |
-
|
811 |
-
seed
|
812 |
-
], queue = False, show_progress = False).then(fn = check, inputs = [
|
813 |
-
input_image
|
814 |
-
], outputs = [], queue = False, show_progress = False).success(fn=stage2_process, inputs = [
|
815 |
-
input_image,
|
816 |
-
rotation,
|
817 |
-
denoise_image,
|
818 |
-
prompt,
|
819 |
-
a_prompt,
|
820 |
-
n_prompt,
|
821 |
-
num_samples,
|
822 |
-
min_size,
|
823 |
-
downscale,
|
824 |
-
upscale,
|
825 |
-
edm_steps,
|
826 |
-
s_stage1,
|
827 |
-
s_stage2,
|
828 |
-
s_cfg,
|
829 |
-
randomize_seed,
|
830 |
-
seed,
|
831 |
-
s_churn,
|
832 |
-
s_noise,
|
833 |
-
color_fix_type,
|
834 |
-
diff_dtype,
|
835 |
-
ae_dtype,
|
836 |
-
gamma_correction,
|
837 |
-
linear_CFG,
|
838 |
-
linear_s_stage2,
|
839 |
-
spt_linear_CFG,
|
840 |
-
spt_linear_s_stage2,
|
841 |
-
model_select,
|
842 |
-
output_format,
|
843 |
-
allocation
|
844 |
-
], outputs = [
|
845 |
-
result_slider,
|
846 |
-
result_gallery,
|
847 |
-
restore_information,
|
848 |
-
reset_btn
|
849 |
-
]).success(fn = log_information, inputs = [
|
850 |
-
result_gallery
|
851 |
-
], outputs = [], queue = False, show_progress = False)
|
852 |
-
|
853 |
-
result_gallery.change(on_select_result, [result_slider, result_gallery], result_slider)
|
854 |
-
result_gallery.select(on_select_result, [result_slider, result_gallery], result_slider)
|
855 |
-
|
856 |
-
restart_button.click(fn = load_and_reset, inputs = [
|
857 |
-
param_setting
|
858 |
-
], outputs = [
|
859 |
-
edm_steps,
|
860 |
-
s_cfg,
|
861 |
-
s_stage2,
|
862 |
-
s_stage1,
|
863 |
-
s_churn,
|
864 |
-
s_noise,
|
865 |
-
a_prompt,
|
866 |
-
n_prompt,
|
867 |
-
color_fix_type,
|
868 |
-
linear_CFG,
|
869 |
-
linear_s_stage2,
|
870 |
-
spt_linear_CFG,
|
871 |
-
spt_linear_s_stage2,
|
872 |
-
model_select
|
873 |
-
])
|
874 |
-
|
875 |
-
reset_btn.click(fn = reset, inputs = [], outputs = [
|
876 |
-
input_image,
|
877 |
-
rotation,
|
878 |
-
denoise_image,
|
879 |
-
prompt,
|
880 |
-
a_prompt,
|
881 |
-
n_prompt,
|
882 |
-
num_samples,
|
883 |
-
min_size,
|
884 |
-
downscale,
|
885 |
-
upscale,
|
886 |
-
edm_steps,
|
887 |
-
s_stage1,
|
888 |
-
s_stage2,
|
889 |
-
s_cfg,
|
890 |
-
randomize_seed,
|
891 |
-
seed,
|
892 |
-
s_churn,
|
893 |
-
s_noise,
|
894 |
-
color_fix_type,
|
895 |
-
diff_dtype,
|
896 |
-
ae_dtype,
|
897 |
-
gamma_correction,
|
898 |
-
linear_CFG,
|
899 |
-
linear_s_stage2,
|
900 |
-
spt_linear_CFG,
|
901 |
-
spt_linear_s_stage2,
|
902 |
-
model_select,
|
903 |
-
output_format,
|
904 |
-
allocation
|
905 |
-
], queue = False, show_progress = False)
|
906 |
-
|
907 |
-
interface.queue(10).launch()
|
|
|
1 |
import os
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
2 |
import time
|
3 |
+
from pathlib import Path
|
4 |
+
from loguru import logger
|
5 |
+
from datetime import datetime
|
6 |
+
import gradio as gr
|
7 |
import random
|
8 |
import spaces
|
9 |
+
import torch
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
10 |
|
11 |
+
from hyvideo.utils.file_utils import save_videos_grid
|
12 |
+
from hyvideo.utils.preprocess_text_encoder_tokenizer_utils import preprocess_text_encoder_tokenizer
|
13 |
+
from hyvideo.config import parse_args
|
14 |
+
from hyvideo.inference import HunyuanVideoSampler
|
15 |
+
from hyvideo.constants import NEGATIVE_PROMPT
|
16 |
|
17 |
+
from huggingface_hub import snapshot_download
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
18 |
|
19 |
if torch.cuda.device_count() > 0:
|
20 |
+
snapshot_download(repo_id="tencent/HunyuanVideo", repo_type="model", local_dir="ckpts", force_download=True)
|
21 |
+
snapshot_download(repo_id="xtuner/llava-llama-3-8b-v1_1-transformers", repo_type="model", local_dir="ckpts/llava-llama-3-8b-v1_1-transformers", force_download=True)
|
22 |
|
23 |
+
class Args:
|
24 |
+
def __init__(self, input_dir, output_dir):
|
25 |
+
self.input_dir = input_dir
|
26 |
+
self.output_dir = output_dir
|
|
|
|
|
|
|
|
|
|
|
|
|
27 |
|
28 |
+
# Create the object
|
29 |
+
args = Args("ckpts/llava-llama-3-8b-v1_1-transformers", "ckpts/text_encoder")
|
30 |
+
preprocess_text_encoder_tokenizer(args)
|
31 |
+
snapshot_download(repo_id="openai/clip-vit-large-patch14", repo_type="model", local_dir="ckpts/text_encoder_2", force_download=True)
|
32 |
|
33 |
+
def initialize_model(model_path):
|
34 |
+
print("initialize_model: " + model_path)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
35 |
if torch.cuda.device_count() == 0:
|
36 |
+
return None
|
37 |
+
|
38 |
+
args = parse_args()
|
39 |
+
models_root_path = Path(model_path)
|
40 |
+
if not models_root_path.exists():
|
41 |
+
raise ValueError(f"`models_root` not exists: {models_root_path}")
|
42 |
+
|
43 |
+
print(f"`models_root` exists: {models_root_path}")
|
44 |
+
hunyuan_video_sampler = HunyuanVideoSampler.from_pretrained(models_root_path, args=args)
|
45 |
+
print("Model initialized: " + model_path)
|
46 |
+
return hunyuan_video_sampler
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
47 |
|
48 |
+
model = initialize_model("ckpts")
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
49 |
|
50 |
+
def generate_video(
|
|
|
|
|
|
|
51 |
prompt,
|
52 |
+
resolution,
|
53 |
+
video_length,
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
54 |
seed,
|
55 |
+
num_inference_steps,
|
56 |
+
guidance_scale,
|
57 |
+
flow_shift,
|
58 |
+
embedded_guidance_scale
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
59 |
):
|
60 |
+
print("generate_video (prompt: " + prompt + ")")
|
61 |
+
return generate_video_gpu(
|
62 |
+
model,
|
63 |
+
prompt,
|
64 |
+
resolution,
|
65 |
+
video_length,
|
66 |
+
seed,
|
67 |
+
num_inference_steps,
|
68 |
+
guidance_scale,
|
69 |
+
flow_shift,
|
70 |
+
embedded_guidance_scale
|
71 |
+
)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
72 |
|
73 |
+
@spaces.GPU(duration=120)
|
74 |
+
def generate_video_gpu(
|
75 |
+
model,
|
76 |
prompt,
|
77 |
+
resolution,
|
78 |
+
video_length,
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
79 |
seed,
|
80 |
+
num_inference_steps,
|
81 |
+
guidance_scale,
|
82 |
+
flow_shift,
|
83 |
+
embedded_guidance_scale
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
84 |
):
|
85 |
+
#print("generate_video_gpu (prompt: " + prompt + ")")
|
86 |
+
#if torch.cuda.device_count() == 0:
|
87 |
+
# gr.Warning("Set this space to GPU config to make it work.")
|
88 |
+
# return None
|
89 |
+
#
|
90 |
+
#seed = None if seed == -1 else seed
|
91 |
+
#width, height = resolution.split("x")
|
92 |
+
#width, height = int(width), int(height)
|
93 |
+
#negative_prompt = "" # not applicable in the inference
|
94 |
+
#print("Predicting video...")
|
95 |
+
#
|
96 |
+
#outputs = model.predict(
|
97 |
+
# prompt=prompt,
|
98 |
+
# height=height,
|
99 |
+
# width=width,
|
100 |
+
# video_length=video_length,
|
101 |
+
# seed=seed,
|
102 |
+
# negative_prompt=negative_prompt,
|
103 |
+
# infer_steps=num_inference_steps,
|
104 |
+
# guidance_scale=guidance_scale,
|
105 |
+
# num_videos_per_prompt=1,
|
106 |
+
# flow_shift=flow_shift,
|
107 |
+
# batch_size=1,
|
108 |
+
# embedded_guidance_scale=embedded_guidance_scale
|
109 |
+
#)
|
110 |
+
#
|
111 |
+
#print("Video predicted")
|
112 |
+
#samples = outputs["samples"]
|
113 |
+
#sample = samples[0].unsqueeze(0)
|
114 |
+
#
|
115 |
+
#save_path = "./gradio_outputs"
|
116 |
+
#os.makedirs(save_path, exist_ok=True)
|
117 |
+
#
|
118 |
+
#time_flag = datetime.fromtimestamp(time.time()).strftime("%Y-%m-%d-%H:%M:%S")
|
119 |
+
#video_path = f"{save_path}/{time_flag}_seed{outputs['seeds'][0]}_{outputs['prompts'][0][:100].replace('/','')}.mp4"
|
120 |
+
#save_videos_grid(sample, video_path, fps=24)
|
121 |
+
#logger.info(f"Sample saved to: {video_path}")
|
122 |
+
#
|
123 |
+
#print("Return the video")
|
124 |
+
#return video_path
|
125 |
+
return None
|
126 |
+
|
127 |
+
def create_demo(model_path):
|
128 |
+
with gr.Blocks() as demo:
|
129 |
+
if torch.cuda.device_count() == 0:
|
130 |
+
with gr.Row():
|
131 |
+
gr.HTML("""
|
132 |
+
<p style="background-color: red;"><big><big><big><b>⚠️To use <i>Hunyuan Video</i>, <a href="https://huggingface.co/spaces/Fabrice-TIERCELIN/HunyuanVideo?duplicate=true">duplicate this space</a> and set a GPU with 80 GB VRAM.</b>
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
133 |
|
134 |
+
You can't use <i>Hunyuan Video</i> directly here because this space runs on a CPU, which is not enough for <i>Hunyuan Video</i>. Please provide <a href="https://huggingface.co/spaces/Fabrice-TIERCELIN/HunyuanVideo/discussions/new">feedback</a> if you have issues.
|
135 |
+
</big></big></big></p><br/>
|
136 |
+
<p style="background-color: light-green;"><big>The space has been successfully deployed on A100 space on 2025-01-23. Synchronize your space to fix the errors.</big></p>
|
137 |
+
""")
|
138 |
+
gr.Markdown("# Hunyuan Video Generation")
|
139 |
+
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
140 |
with gr.Row():
|
141 |
with gr.Column():
|
142 |
+
prompt = gr.Textbox(label="Prompt", value="A cat walks on the grass, realistic style.")
|
143 |
+
with gr.Row():
|
144 |
+
resolution = gr.Dropdown(
|
145 |
+
choices=[
|
146 |
+
# 720p
|
147 |
+
("1280x720 (16:9, 720p)", "1280x720"),
|
148 |
+
("720x1280 (9:16, 720p)", "720x1280"),
|
149 |
+
("1104x832 (4:3, 720p)", "1104x832"),
|
150 |
+
("832x1104 (3:4, 720p)", "832x1104"),
|
151 |
+
("960x960 (1:1, 720p)", "960x960"),
|
152 |
+
# 540p
|
153 |
+
("960x544 (16:9, 540p)", "960x544"),
|
154 |
+
("544x960 (9:16, 540p)", "544x960"),
|
155 |
+
("832x624 (4:3, 540p)", "832x624"),
|
156 |
+
("624x832 (3:4, 540p)", "624x832"),
|
157 |
+
("720x720 (1:1, 540p)", "720x720"),
|
158 |
+
],
|
159 |
+
value="832x624",
|
160 |
+
label="Resolution"
|
161 |
+
)
|
162 |
+
video_length = gr.Dropdown(
|
163 |
+
label="Video Length",
|
164 |
+
choices=[
|
165 |
+
("2s(65f)", 65),
|
166 |
+
("5s(129f)", 129),
|
167 |
+
],
|
168 |
+
value=65,
|
169 |
+
)
|
170 |
+
num_inference_steps = gr.Slider(1, 100, value=5, step=1, label="Number of Inference Steps")
|
171 |
+
|
172 |
+
with gr.Accordion("Advanced Options", open=False):
|
173 |
+
with gr.Column():
|
174 |
+
seed = gr.Slider(label="Seed (-1 for random)", value=-1, minimum=-1, maximum=2**63 - 1, step=1)
|
175 |
+
guidance_scale = gr.Slider(1.0, 20.0, value=1.0, step=0.5, label="Guidance Scale")
|
176 |
+
flow_shift = gr.Slider(0.0, 10.0, value=7.0, step=0.1, label="Flow Shift")
|
177 |
+
embedded_guidance_scale = gr.Slider(1.0, 20.0, value=6.0, step=0.5, label="Embedded Guidance Scale")
|
178 |
+
|
179 |
+
generate_btn = gr.Button(value = "🚀 Generate Video", variant = "primary")
|
180 |
+
|
181 |
+
with gr.Row():
|
182 |
+
output = gr.Video(label = "Generated Video", autoplay = True)
|
183 |
+
|
184 |
+
gr.Markdown("""
|
185 |
+
## **Alternatives**
|
186 |
+
If you can't use _Hunyuan Video_, you can use _[CogVideoX](https://huggingface.co/spaces/THUDM/CogVideoX-5B-Space)_ or _[LTX Video Playground](https://huggingface.co/spaces/Lightricks/LTX-Video-Playground)_ instead.
|
187 |
+
""")
|
188 |
+
|
189 |
+
generate_btn.click(
|
190 |
+
fn=generate_video,
|
191 |
+
inputs=[
|
192 |
+
prompt,
|
193 |
+
resolution,
|
194 |
+
video_length,
|
195 |
+
seed,
|
196 |
+
num_inference_steps,
|
197 |
+
guidance_scale,
|
198 |
+
flow_shift,
|
199 |
+
embedded_guidance_scale
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
200 |
],
|
201 |
+
outputs=output
|
202 |
+
)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
203 |
|
204 |
+
return demo
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
205 |
|
206 |
+
if __name__ == "__main__":
|
207 |
+
os.environ["GRADIO_ANALYTICS_ENABLED"] = "False"
|
208 |
+
demo = create_demo("ckpts")
|
209 |
+
demo.queue(10).launch()
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
requirements.txt
CHANGED
@@ -1,41 +1,15 @@
|
|
1 |
-
|
2 |
-
|
3 |
-
|
4 |
-
|
5 |
-
|
6 |
-
|
7 |
-
|
8 |
-
|
9 |
-
|
10 |
-
|
11 |
-
|
12 |
-
|
13 |
-
|
14 |
-
|
15 |
-
|
16 |
-
einops==0.8.0
|
17 |
-
einops-exts==0.0.4
|
18 |
-
timm==1.0.7
|
19 |
-
openai-clip==1.0.1
|
20 |
-
fsspec==2024.6.1
|
21 |
-
kornia==0.7.3
|
22 |
-
matplotlib==3.9.1
|
23 |
-
ninja==1.11.1.1
|
24 |
-
omegaconf==2.3.0
|
25 |
-
opencv-python==4.10.0.84
|
26 |
-
pandas==2.2.2
|
27 |
-
pillow==10.4.0
|
28 |
-
pytorch-lightning==2.3.3
|
29 |
-
PyYAML==6.0.1
|
30 |
-
scipy==1.14.0
|
31 |
-
tqdm==4.66.4
|
32 |
-
triton==2.3.1
|
33 |
-
urllib3==2.2.2
|
34 |
-
webdataset==0.2.86
|
35 |
-
xformers==0.0.27
|
36 |
-
facexlib==0.3.0
|
37 |
-
k-diffusion==0.1.1.post1
|
38 |
-
diffusers==0.29.2
|
39 |
-
pillow-heif==0.18.0
|
40 |
-
|
41 |
-
open-clip-torch==2.24.0
|
|
|
1 |
+
opencv-python==4.9.0.80
|
2 |
+
diffusers==0.31.0
|
3 |
+
transformers==4.46.3
|
4 |
+
tokenizers==0.20.3
|
5 |
+
accelerate==1.1.1
|
6 |
+
pandas==2.0.3
|
7 |
+
numpy==1.24.4
|
8 |
+
einops==0.7.0
|
9 |
+
tqdm==4.66.2
|
10 |
+
loguru==0.7.2
|
11 |
+
imageio==2.34.0
|
12 |
+
imageio-ffmpeg==0.5.1
|
13 |
+
safetensors==0.4.3
|
14 |
+
gradio==5.0.0
|
15 |
+
torchvision==0.20.1
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|