Update app.py
Browse files
app.py
CHANGED
@@ -1 +1,96 @@
|
|
1 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
from PIL import Image
|
2 |
+
from IPython.display import display
|
3 |
+
import torch as th
|
4 |
+
|
5 |
+
from glide_text2im.download import load_checkpoint
|
6 |
+
from glide_text2im.model_creation import (
|
7 |
+
create_model_and_diffusion,
|
8 |
+
model_and_diffusion_defaults,
|
9 |
+
model_and_diffusion_defaults_upsampler
|
10 |
+
)
|
11 |
+
# Create base model.
|
12 |
+
options = model_and_diffusion_defaults()
|
13 |
+
options['use_fp16'] = has_cuda
|
14 |
+
options['timestep_respacing'] = '100' # use 100 diffusion steps for fast sampling
|
15 |
+
model, diffusion = create_model_and_diffusion(**options)
|
16 |
+
model.eval()
|
17 |
+
if has_cuda:
|
18 |
+
model.convert_to_fp16()
|
19 |
+
model.to(device)
|
20 |
+
model.load_state_dict(load_checkpoint('base', device))
|
21 |
+
print('total base parameters', sum(x.numel() for x in model.parameters()))has_cuda = th.cuda.is_available()
|
22 |
+
device = th.device('cpu' if not has_cuda else 'cuda')
|
23 |
+
def show_images(batch: th.Tensor):
|
24 |
+
""" Display a batch of images inline. """
|
25 |
+
scaled = ((batch + 1)*127.5).round().clamp(0,255).to(th.uint8).cpu()
|
26 |
+
reshaped = scaled.permute(2, 0, 3, 1).reshape([batch.shape[2], -1, 3])
|
27 |
+
display(Image.fromarray(reshaped.numpy()))
|
28 |
+
# Sampling parameters
|
29 |
+
prompt = ""
|
30 |
+
batch_size = 1
|
31 |
+
guidance_scale = 3.0
|
32 |
+
|
33 |
+
# Tune this parameter to control the sharpness of 256x256 images.
|
34 |
+
# A value of 1.0 is sharper, but sometimes results in grainy artifacts.
|
35 |
+
upsample_temp = 0.997
|
36 |
+
import gradio as gr
|
37 |
+
def generate_image_from_text(prompt):
|
38 |
+
# Set the prompt text
|
39 |
+
prompt = prompt
|
40 |
+
|
41 |
+
##############################
|
42 |
+
# Sample from the base model #
|
43 |
+
##############################
|
44 |
+
|
45 |
+
# Create the text tokens to feed to the model.
|
46 |
+
tokens = model.tokenizer.encode(prompt)
|
47 |
+
tokens, mask = model.tokenizer.padded_tokens_and_mask(
|
48 |
+
tokens, options['text_ctx']
|
49 |
+
)
|
50 |
+
|
51 |
+
# Create the classifier-free guidance tokens (empty)
|
52 |
+
full_batch_size = batch_size * 2
|
53 |
+
uncond_tokens, uncond_mask = model.tokenizer.padded_tokens_and_mask(
|
54 |
+
[], options['text_ctx']
|
55 |
+
)
|
56 |
+
|
57 |
+
# Pack the tokens together into model kwargs.
|
58 |
+
model_kwargs = dict(
|
59 |
+
tokens=th.tensor(
|
60 |
+
[tokens] * batch_size + [uncond_tokens] * batch_size, device=device
|
61 |
+
),
|
62 |
+
mask=th.tensor(
|
63 |
+
[mask] * batch_size + [uncond_mask] * batch_size,
|
64 |
+
dtype=th.bool,
|
65 |
+
device=device,
|
66 |
+
),
|
67 |
+
)
|
68 |
+
|
69 |
+
# Create a classifier-free guidance sampling function
|
70 |
+
def model_fn(x_t, ts, **kwargs):
|
71 |
+
half = x_t[: len(x_t) // 2]
|
72 |
+
combined = th.cat([half, half], dim=0)
|
73 |
+
model_out = model(combined, ts, **kwargs)
|
74 |
+
eps, rest = model_out[:, :3], model_out[:, 3:]
|
75 |
+
cond_eps, uncond_eps = th.split(eps, len(eps) // 2, dim=0)
|
76 |
+
half_eps = uncond_eps + guidance_scale * (cond_eps - uncond_eps)
|
77 |
+
eps = th.cat([half_eps, half_eps], dim=0)
|
78 |
+
return th.cat([eps, rest], dim=1)
|
79 |
+
|
80 |
+
# Sample from the base model.
|
81 |
+
model.del_cache()
|
82 |
+
samples = diffusion.p_sample_loop(
|
83 |
+
model_fn,
|
84 |
+
(full_batch_size, 3, options["image_size"], options["image_size"]),
|
85 |
+
device=device,
|
86 |
+
clip_denoised=True,
|
87 |
+
progress=True,
|
88 |
+
model_kwargs=model_kwargs,
|
89 |
+
cond_fn=None,
|
90 |
+
)[:batch_size]
|
91 |
+
model.del_cache()
|
92 |
+
|
93 |
+
# Show the output
|
94 |
+
show_images(samples)
|
95 |
+
demo = gr.Interface(fn =generate_image_from_text,inputs ="text",outputs ="image")
|
96 |
+
demo.launch()
|