Spaces:
Running on Zero
Running on Zero
update app.py
Browse files
app.py
CHANGED
|
@@ -16,37 +16,54 @@ from model import AlignDRAW
|
|
| 16 |
REPO = "mediasynthesismuseum/aligndraw"
|
| 17 |
HERE = os.path.dirname(os.path.abspath(__file__))
|
| 18 |
|
| 19 |
-
#
|
| 20 |
-
|
| 21 |
-
model
|
| 22 |
-
|
| 23 |
-
|
| 24 |
-
|
| 25 |
-
|
| 26 |
)
|
| 27 |
-
model.eval().to("cuda")
|
| 28 |
|
| 29 |
-
|
| 30 |
-
|
| 31 |
-
|
| 32 |
)
|
| 33 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 34 |
|
| 35 |
def _tokens(caption):
|
| 36 |
words = caption.strip().lower().replace(".", " .").split()
|
|
|
|
| 37 |
unknown = sorted({
|
| 38 |
-
w for w in words
|
|
|
|
| 39 |
})
|
|
|
|
| 40 |
toks = [
|
| 41 |
-
DICT.get(w, DICT["UNK"])
|
|
|
|
| 42 |
] or [DICT["UNK"]]
|
|
|
|
| 43 |
return toks, unknown
|
| 44 |
|
| 45 |
|
| 46 |
-
#
|
| 47 |
def _up(t, size=512):
|
| 48 |
a = (
|
| 49 |
-
t.detach()
|
|
|
|
|
|
|
|
|
|
| 50 |
).clip(0, 255).astype(np.uint8)
|
| 51 |
|
| 52 |
return Image.fromarray(a).resize(
|
|
@@ -57,43 +74,59 @@ def _up(t, size=512):
|
|
| 57 |
|
| 58 |
@spaces.GPU(duration=120)
|
| 59 |
def generate(caption, n_samples):
|
| 60 |
-
|
|
|
|
| 61 |
raise gr.Error("Type a caption first.")
|
| 62 |
|
| 63 |
toks, unknown = _tokens(caption)
|
|
|
|
| 64 |
y = torch.tensor(
|
| 65 |
-
[toks],
|
|
|
|
|
|
|
| 66 |
)
|
| 67 |
|
| 68 |
with torch.inference_mode():
|
|
|
|
| 69 |
imgs = model.generate(
|
| 70 |
y.expand(int(n_samples), -1)
|
| 71 |
)
|
| 72 |
|
| 73 |
gallery = [
|
| 74 |
-
_up(imgs[i])
|
|
|
|
| 75 |
]
|
| 76 |
|
| 77 |
-
_, seq = model.generate(
|
|
|
|
|
|
|
|
|
|
| 78 |
|
| 79 |
-
#
|
| 80 |
frames = [
|
| 81 |
-
np.array(_up(s[0]))
|
|
|
|
| 82 |
]
|
| 83 |
|
| 84 |
-
#
|
| 85 |
if frames:
|
| 86 |
while len(frames) < 69:
|
| 87 |
frames.append(frames[-1])
|
| 88 |
|
| 89 |
-
|
| 90 |
|
| 91 |
gif = os.path.join(HERE, "draw.gif")
|
| 92 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 93 |
|
| 94 |
note = (
|
| 95 |
-
"💡 Not in the 2015 vocabulary
|
| 96 |
-
+ ", ".join(unknown)
|
| 97 |
) if unknown else ""
|
| 98 |
|
| 99 |
return gallery, gif, note
|
|
@@ -122,15 +155,16 @@ CSS = """
|
|
| 122 |
"""
|
| 123 |
|
| 124 |
with gr.Blocks(title="AlignDRAW") as demo:
|
|
|
|
| 125 |
gr.Markdown(
|
| 126 |
"# 🖍️ AlignDRAW\n"
|
| 127 |
"One of the **first text-to-image models** "
|
| 128 |
"([Mansimov et al., 2015](https://arxiv.org/abs/1511.02793)) — "
|
| 129 |
"resurrected.\n\n"
|
| 130 |
-
"Original COCO-trained
|
| 131 |
-
"
|
| 132 |
-
"The original
|
| 133 |
-
"
|
| 134 |
"Weights: [mediasynthesismuseum/aligndraw]"
|
| 135 |
"(https://huggingface.co/mediasynthesismuseum/aligndraw).",
|
| 136 |
elem_id="ad-title"
|
|
@@ -142,6 +176,7 @@ with gr.Blocks(title="AlignDRAW") as demo:
|
|
| 142 |
value=EXAMPLES[0],
|
| 143 |
scale=4
|
| 144 |
)
|
|
|
|
| 145 |
n = gr.Slider(
|
| 146 |
4, 12, 8,
|
| 147 |
step=1,
|
|
@@ -158,8 +193,9 @@ with gr.Blocks(title="AlignDRAW") as demo:
|
|
| 158 |
columns=4,
|
| 159 |
height=380
|
| 160 |
)
|
|
|
|
| 161 |
anim = gr.Image(
|
| 162 |
-
label="Drawing process (69
|
| 163 |
height=300
|
| 164 |
)
|
| 165 |
|
|
@@ -169,7 +205,10 @@ with gr.Blocks(title="AlignDRAW") as demo:
|
|
| 169 |
[gallery, anim, note]
|
| 170 |
)
|
| 171 |
|
| 172 |
-
gr.Examples(
|
|
|
|
|
|
|
|
|
|
| 173 |
|
| 174 |
|
| 175 |
if __name__ == "__main__":
|
|
|
|
| 16 |
REPO = "mediasynthesismuseum/aligndraw"
|
| 17 |
HERE = os.path.dirname(os.path.abspath(__file__))
|
| 18 |
|
| 19 |
+
# Original architecture dimensions, matching the original checkpoint.
|
| 20 |
+
# 69 recurrent drawing steps.
|
| 21 |
+
model = AlignDRAW(imgSize=32, runSteps=69)
|
| 22 |
+
|
| 23 |
+
checkpoint_path = hf_hub_download(
|
| 24 |
+
REPO,
|
| 25 |
+
"aligndraw_coco.pt"
|
| 26 |
)
|
|
|
|
| 27 |
|
| 28 |
+
checkpoint = torch.load(
|
| 29 |
+
checkpoint_path,
|
| 30 |
+
map_location="cpu"
|
| 31 |
)
|
| 32 |
|
| 33 |
+
model.load_state_dict(checkpoint)
|
| 34 |
+
model.eval().to("cuda")
|
| 35 |
+
|
| 36 |
+
# Original vocabulary
|
| 37 |
+
with open(
|
| 38 |
+
hf_hub_download(REPO, "dictionary.pkl"),
|
| 39 |
+
"rb"
|
| 40 |
+
) as f:
|
| 41 |
+
DICT = pickle.load(f, encoding="latin1")
|
| 42 |
+
|
| 43 |
|
| 44 |
def _tokens(caption):
|
| 45 |
words = caption.strip().lower().replace(".", " .").split()
|
| 46 |
+
|
| 47 |
unknown = sorted({
|
| 48 |
+
w for w in words
|
| 49 |
+
if w not in DICT and w != "."
|
| 50 |
})
|
| 51 |
+
|
| 52 |
toks = [
|
| 53 |
+
DICT.get(w, DICT["UNK"])
|
| 54 |
+
for w in words
|
| 55 |
] or [DICT["UNK"]]
|
| 56 |
+
|
| 57 |
return toks, unknown
|
| 58 |
|
| 59 |
|
| 60 |
+
# Enlarge original 32x32 output to 512x512
|
| 61 |
def _up(t, size=512):
|
| 62 |
a = (
|
| 63 |
+
t.detach()
|
| 64 |
+
.cpu()
|
| 65 |
+
.permute(1, 2, 0)
|
| 66 |
+
.numpy() * 255
|
| 67 |
).clip(0, 255).astype(np.uint8)
|
| 68 |
|
| 69 |
return Image.fromarray(a).resize(
|
|
|
|
| 74 |
|
| 75 |
@spaces.GPU(duration=120)
|
| 76 |
def generate(caption, n_samples):
|
| 77 |
+
|
| 78 |
+
if not caption or not caption.strip():
|
| 79 |
raise gr.Error("Type a caption first.")
|
| 80 |
|
| 81 |
toks, unknown = _tokens(caption)
|
| 82 |
+
|
| 83 |
y = torch.tensor(
|
| 84 |
+
[toks],
|
| 85 |
+
dtype=torch.long,
|
| 86 |
+
device="cuda"
|
| 87 |
)
|
| 88 |
|
| 89 |
with torch.inference_mode():
|
| 90 |
+
|
| 91 |
imgs = model.generate(
|
| 92 |
y.expand(int(n_samples), -1)
|
| 93 |
)
|
| 94 |
|
| 95 |
gallery = [
|
| 96 |
+
_up(imgs[i])
|
| 97 |
+
for i in range(int(n_samples))
|
| 98 |
]
|
| 99 |
|
| 100 |
+
_, seq = model.generate(
|
| 101 |
+
y,
|
| 102 |
+
return_seq=True
|
| 103 |
+
)
|
| 104 |
|
| 105 |
+
# Original drawing sequence
|
| 106 |
frames = [
|
| 107 |
+
np.array(_up(s[0]))
|
| 108 |
+
for s in seq
|
| 109 |
]
|
| 110 |
|
| 111 |
+
# Keep the GIF at 69 displayed frames
|
| 112 |
if frames:
|
| 113 |
while len(frames) < 69:
|
| 114 |
frames.append(frames[-1])
|
| 115 |
|
| 116 |
+
frames.extend([frames[-1]] * 8)
|
| 117 |
|
| 118 |
gif = os.path.join(HERE, "draw.gif")
|
| 119 |
+
|
| 120 |
+
imageio.mimsave(
|
| 121 |
+
gif,
|
| 122 |
+
frames,
|
| 123 |
+
duration=0.09,
|
| 124 |
+
loop=0
|
| 125 |
+
)
|
| 126 |
|
| 127 |
note = (
|
| 128 |
+
"💡 Not in the 2015 vocabulary "
|
| 129 |
+
"(treated as UNK): " + ", ".join(unknown)
|
| 130 |
) if unknown else ""
|
| 131 |
|
| 132 |
return gallery, gif, note
|
|
|
|
| 155 |
"""
|
| 156 |
|
| 157 |
with gr.Blocks(title="AlignDRAW") as demo:
|
| 158 |
+
|
| 159 |
gr.Markdown(
|
| 160 |
"# 🖍️ AlignDRAW\n"
|
| 161 |
"One of the **first text-to-image models** "
|
| 162 |
"([Mansimov et al., 2015](https://arxiv.org/abs/1511.02793)) — "
|
| 163 |
"resurrected.\n\n"
|
| 164 |
+
"Original COCO-trained weights and vocabulary. "
|
| 165 |
+
"Images are displayed at **512×512**.\n\n"
|
| 166 |
+
"The original 32×32 model is preserved internally. "
|
| 167 |
+
"The drawing sequence uses 69 recurrent steps.\n\n"
|
| 168 |
"Weights: [mediasynthesismuseum/aligndraw]"
|
| 169 |
"(https://huggingface.co/mediasynthesismuseum/aligndraw).",
|
| 170 |
elem_id="ad-title"
|
|
|
|
| 176 |
value=EXAMPLES[0],
|
| 177 |
scale=4
|
| 178 |
)
|
| 179 |
+
|
| 180 |
n = gr.Slider(
|
| 181 |
4, 12, 8,
|
| 182 |
step=1,
|
|
|
|
| 193 |
columns=4,
|
| 194 |
height=380
|
| 195 |
)
|
| 196 |
+
|
| 197 |
anim = gr.Image(
|
| 198 |
+
label="Drawing process (69 steps)",
|
| 199 |
height=300
|
| 200 |
)
|
| 201 |
|
|
|
|
| 205 |
[gallery, anim, note]
|
| 206 |
)
|
| 207 |
|
| 208 |
+
gr.Examples(
|
| 209 |
+
EXAMPLES,
|
| 210 |
+
inputs=cap
|
| 211 |
+
)
|
| 212 |
|
| 213 |
|
| 214 |
if __name__ == "__main__":
|