Update app.py
Browse files
app.py
CHANGED
@@ -35,7 +35,7 @@ guidance_scale = 3.0
|
|
35 |
# A value of 1.0 is sharper, but sometimes results in grainy artifacts.
|
36 |
upsample_temp = 0.997
|
37 |
import gradio as gr
|
38 |
-
def
|
39 |
# Set the prompt text
|
40 |
prompt = prompt
|
41 |
|
@@ -44,54 +44,43 @@ def generate_image_from_text(prompt):
|
|
44 |
##############################
|
45 |
|
46 |
# Create the text tokens to feed to the model.
|
47 |
-
tokens =
|
48 |
-
tokens, mask =
|
49 |
-
tokens,
|
50 |
)
|
51 |
|
52 |
-
# Create the
|
53 |
-
full_batch_size = batch_size * 2
|
54 |
-
uncond_tokens, uncond_mask = model.tokenizer.padded_tokens_and_mask(
|
55 |
-
[], options['text_ctx']
|
56 |
-
)
|
57 |
-
|
58 |
-
# Pack the tokens together into model kwargs.
|
59 |
model_kwargs = dict(
|
|
|
|
|
|
|
|
|
60 |
tokens=th.tensor(
|
61 |
-
[tokens] * batch_size
|
62 |
),
|
63 |
mask=th.tensor(
|
64 |
-
[mask] * batch_size
|
65 |
dtype=th.bool,
|
66 |
device=device,
|
67 |
),
|
68 |
)
|
69 |
|
70 |
-
# Create a classifier-free guidance sampling function
|
71 |
-
def model_fn(x_t, ts, **kwargs):
|
72 |
-
half = x_t[: len(x_t) // 2]
|
73 |
-
combined = th.cat([half, half], dim=0)
|
74 |
-
model_out = model(combined, ts, **kwargs)
|
75 |
-
eps, rest = model_out[:, :3], model_out[:, 3:]
|
76 |
-
cond_eps, uncond_eps = th.split(eps, len(eps) // 2, dim=0)
|
77 |
-
half_eps = uncond_eps + guidance_scale * (cond_eps - uncond_eps)
|
78 |
-
eps = th.cat([half_eps, half_eps], dim=0)
|
79 |
-
return th.cat([eps, rest], dim=1)
|
80 |
-
|
81 |
# Sample from the base model.
|
82 |
-
|
83 |
-
|
84 |
-
|
85 |
-
|
|
|
|
|
86 |
device=device,
|
87 |
clip_denoised=True,
|
88 |
progress=True,
|
89 |
model_kwargs=model_kwargs,
|
90 |
cond_fn=None,
|
91 |
)[:batch_size]
|
92 |
-
|
93 |
|
94 |
# Show the output
|
95 |
-
show_images(
|
96 |
-
demo = gr.Interface(fn =
|
97 |
demo.launch()
|
|
|
35 |
# A value of 1.0 is sharper, but sometimes results in grainy artifacts.
|
36 |
upsample_temp = 0.997
|
37 |
import gradio as gr
|
38 |
+
def generate_upsampled_image_from_text(prompt):
|
39 |
# Set the prompt text
|
40 |
prompt = prompt
|
41 |
|
|
|
44 |
##############################
|
45 |
|
46 |
# Create the text tokens to feed to the model.
|
47 |
+
tokens = model_up.tokenizer.encode(prompt)
|
48 |
+
tokens, mask = model_up.tokenizer.padded_tokens_and_mask(a
|
49 |
+
tokens, options_up['text_ctx']
|
50 |
)
|
51 |
|
52 |
+
# Create the model conditioning dict.
|
|
|
|
|
|
|
|
|
|
|
|
|
53 |
model_kwargs = dict(
|
54 |
+
# Low-res image to upsample.
|
55 |
+
low_res=((samples + 1) * 127.5).round() / 127.5 - 1,
|
56 |
+
|
57 |
+
# Text tokens
|
58 |
tokens=th.tensor(
|
59 |
+
[tokens] * batch_size, device=device
|
60 |
),
|
61 |
mask=th.tensor(
|
62 |
+
[mask] * batch_size,
|
63 |
dtype=th.bool,
|
64 |
device=device,
|
65 |
),
|
66 |
)
|
67 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
68 |
# Sample from the base model.
|
69 |
+
model_up.del_cache()
|
70 |
+
up_shape = (batch_size, 3, options_up["image_size"], options_up["image_size"])
|
71 |
+
up_samples = diffusion_up.ddim_sample_loop(
|
72 |
+
model_up,
|
73 |
+
up_shape,
|
74 |
+
noise=th.randn(up_shape, device=device) * upsample_temp,
|
75 |
device=device,
|
76 |
clip_denoised=True,
|
77 |
progress=True,
|
78 |
model_kwargs=model_kwargs,
|
79 |
cond_fn=None,
|
80 |
)[:batch_size]
|
81 |
+
model_up.del_cache()
|
82 |
|
83 |
# Show the output
|
84 |
+
show_images(up_samples)
|
85 |
+
demo = gr.Interface(fn =generate_upsampled_image_from_text,inputs ="text",outputs ="image")
|
86 |
demo.launch()
|