callum-canavan
commited on
Commit
•
45c0347
1
Parent(s):
cca580a
Update app filename
Browse files- app.py +76 -52
- bapp.py +0 -92
- test_app.py +68 -0
app.py
CHANGED
@@ -1,68 +1,92 @@
|
|
1 |
-
|
2 |
-
from
|
|
|
3 |
import gradio as gr
|
4 |
import torch
|
5 |
-
|
|
|
6 |
|
|
|
|
|
|
|
|
|
7 |
|
8 |
stage_1 = DiffusionPipeline.from_pretrained(
|
9 |
-
|
10 |
-
|
11 |
-
|
12 |
-
stage_1.enable_model_cpu_offload()
|
13 |
stage_2 = DiffusionPipeline.from_pretrained(
|
14 |
-
|
15 |
-
|
16 |
-
|
17 |
-
|
18 |
-
)
|
19 |
-
|
20 |
stage_2.enable_model_cpu_offload()
|
21 |
|
22 |
-
# stage 3
|
23 |
-
safety_modules = {
|
24 |
-
"feature_extractor": stage_1.feature_extractor,
|
25 |
-
"safety_checker": stage_1.safety_checker,
|
26 |
-
"watermarker": stage_1.watermarker,
|
27 |
-
}
|
28 |
-
stage_3 = DiffusionPipeline.from_pretrained(
|
29 |
-
"stabilityai/stable-diffusion-x4-upscaler",
|
30 |
-
**safety_modules,
|
31 |
-
torch_dtype=torch.float16
|
32 |
-
)
|
33 |
-
stage_3.enable_xformers_memory_efficient_attention() # remove line if torch.__version__ >= 2.0.0
|
34 |
-
stage_3.enable_model_cpu_offload()
|
35 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
36 |
|
37 |
-
|
38 |
-
prompt_embeds, negative_embeds = stage_1.encode_prompt(prompt)
|
39 |
-
generator = torch.manual_seed(0)
|
40 |
-
image = stage_1(
|
41 |
-
prompt_embeds=prompt_embeds,
|
42 |
-
negative_prompt_embeds=negative_embeds,
|
43 |
-
generator=generator,
|
44 |
-
output_type="pt",
|
45 |
-
).images
|
46 |
-
image = stage_2(
|
47 |
-
image=image,
|
48 |
-
prompt_embeds=prompt_embeds,
|
49 |
-
negative_prompt_embeds=negative_embeds,
|
50 |
-
generator=generator,
|
51 |
-
output_type="pt",
|
52 |
-
).images
|
53 |
-
image = stage_3(
|
54 |
-
prompt=prompt, image=image, generator=generator, noise_level=100
|
55 |
-
).images[0]
|
56 |
-
return image
|
57 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
58 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
59 |
gradio_app = gr.Interface(
|
60 |
-
fn=
|
61 |
-
|
62 |
-
|
63 |
-
|
64 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
65 |
)
|
66 |
|
|
|
67 |
if __name__ == "__main__":
|
68 |
-
gradio_app.launch(
|
|
|
1 |
+
import argparse
|
2 |
+
from pathlib import Path
|
3 |
+
|
4 |
import gradio as gr
|
5 |
import torch
|
6 |
+
from diffusers import DiffusionPipeline
|
7 |
+
from icecream import ic
|
8 |
|
9 |
+
from visual_anagrams.views import get_views, VIEW_MAP_NAMES
|
10 |
+
from visual_anagrams.samplers import sample_stage_1, sample_stage_2
|
11 |
+
from visual_anagrams.utils import add_args, save_illusion, save_metadata
|
12 |
+
from visual_anagrams.animate import animate_two_view
|
13 |
|
14 |
stage_1 = DiffusionPipeline.from_pretrained(
|
15 |
+
"DeepFloyd/IF-I-M-v1.0",
|
16 |
+
variant="fp16",
|
17 |
+
torch_dtype=torch.float16)
|
|
|
18 |
stage_2 = DiffusionPipeline.from_pretrained(
|
19 |
+
"DeepFloyd/IF-II-M-v1.0",
|
20 |
+
text_encoder=None,
|
21 |
+
variant="fp16",
|
22 |
+
torch_dtype=torch.float16,
|
23 |
+
)
|
24 |
+
stage_1.enable_model_cpu_offload()
|
25 |
stage_2.enable_model_cpu_offload()
|
26 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
27 |
|
28 |
+
def generate_content(
|
29 |
+
style,
|
30 |
+
prompt_for_original,
|
31 |
+
prompt_for_transformed,
|
32 |
+
transformation,
|
33 |
+
num_inference_steps,
|
34 |
+
seed
|
35 |
+
):
|
36 |
+
prompts = [f'{style} {p}'.strip() for p in [prompt_for_original, prompt_for_transformed]]
|
37 |
+
prompt_embeds = [stage_1.encode_prompt(p) for p in prompts]
|
38 |
+
prompt_embeds, negative_prompt_embeds = zip(*prompt_embeds)
|
39 |
+
prompt_embeds = torch.cat(prompt_embeds)
|
40 |
+
negative_prompt_embeds = torch.cat(negative_prompt_embeds)
|
41 |
+
|
42 |
+
views = ['identity', VIEW_MAP_NAMES[transformation]]
|
43 |
+
views = get_views(views)
|
44 |
|
45 |
+
generator = torch.manual_seed(seed)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
46 |
|
47 |
+
print("Sample stage 1")
|
48 |
+
image = sample_stage_1(stage_1,
|
49 |
+
prompt_embeds,
|
50 |
+
negative_prompt_embeds,
|
51 |
+
views,
|
52 |
+
num_inference_steps=num_inference_steps,
|
53 |
+
generator=generator)
|
54 |
|
55 |
+
print("Sample stage 2")
|
56 |
+
image = sample_stage_2(stage_2,
|
57 |
+
image,
|
58 |
+
prompt_embeds,
|
59 |
+
negative_prompt_embeds,
|
60 |
+
views,
|
61 |
+
num_inference_steps=num_inference_steps,
|
62 |
+
generator=generator)
|
63 |
+
save_illusion(image, views, Path(""))
|
64 |
+
|
65 |
+
size = image.shape[-1]
|
66 |
+
animate_two_view(
|
67 |
+
f"sample_{size}.png",
|
68 |
+
views[1],
|
69 |
+
prompts[0],
|
70 |
+
prompts[1],
|
71 |
+
)
|
72 |
+
return 'tmp.mp4', f"sample_{size}.png", f"sample_{size}.views.png"
|
73 |
+
|
74 |
+
|
75 |
+
choices = list(VIEW_MAP_NAMES.keys())
|
76 |
gradio_app = gr.Interface(
|
77 |
+
fn=generate_content,
|
78 |
+
title="Multi-View Illusion Diffusion",
|
79 |
+
inputs=[
|
80 |
+
gr.Textbox(label="Style", placeholder="an oil painting of"),
|
81 |
+
gr.Textbox(label="Prompt for original view", placeholder="a dress"),
|
82 |
+
gr.Textbox(label="Prompt for transformed view", placeholder="an old man"),
|
83 |
+
gr.Dropdown(label="View transformation", choices=choices, value=choices[0]),
|
84 |
+
gr.Number(label="Number of diffusion steps", value=75, step=1, minimum=1, maximum=300),
|
85 |
+
gr.Number(label="Random seed", value=0, step=1, minimum=0, maximum=100000)
|
86 |
+
],
|
87 |
+
outputs=[gr.Video(label="Illusion"), gr.Image(label="Original"), gr.Image(label="Transformed")],
|
88 |
)
|
89 |
|
90 |
+
|
91 |
if __name__ == "__main__":
|
92 |
+
gradio_app.launch() # server_name="0.0.0.0"
|
bapp.py
DELETED
@@ -1,92 +0,0 @@
|
|
1 |
-
import argparse
|
2 |
-
from pathlib import Path
|
3 |
-
|
4 |
-
import gradio as gr
|
5 |
-
import torch
|
6 |
-
from diffusers import DiffusionPipeline
|
7 |
-
from icecream import ic
|
8 |
-
|
9 |
-
from visual_anagrams.views import get_views, VIEW_MAP_NAMES
|
10 |
-
from visual_anagrams.samplers import sample_stage_1, sample_stage_2
|
11 |
-
from visual_anagrams.utils import add_args, save_illusion, save_metadata
|
12 |
-
from visual_anagrams.animate import animate_two_view
|
13 |
-
|
14 |
-
stage_1 = DiffusionPipeline.from_pretrained(
|
15 |
-
"DeepFloyd/IF-I-M-v1.0",
|
16 |
-
variant="fp16",
|
17 |
-
torch_dtype=torch.float16)
|
18 |
-
stage_2 = DiffusionPipeline.from_pretrained(
|
19 |
-
"DeepFloyd/IF-II-M-v1.0",
|
20 |
-
text_encoder=None,
|
21 |
-
variant="fp16",
|
22 |
-
torch_dtype=torch.float16,
|
23 |
-
)
|
24 |
-
stage_1.enable_model_cpu_offload()
|
25 |
-
stage_2.enable_model_cpu_offload()
|
26 |
-
|
27 |
-
|
28 |
-
def generate_content(
|
29 |
-
style,
|
30 |
-
prompt_for_original,
|
31 |
-
prompt_for_transformed,
|
32 |
-
transformation,
|
33 |
-
num_inference_steps,
|
34 |
-
seed
|
35 |
-
):
|
36 |
-
prompts = [f'{style} {p}'.strip() for p in [prompt_for_original, prompt_for_transformed]]
|
37 |
-
prompt_embeds = [stage_1.encode_prompt(p) for p in prompts]
|
38 |
-
prompt_embeds, negative_prompt_embeds = zip(*prompt_embeds)
|
39 |
-
prompt_embeds = torch.cat(prompt_embeds)
|
40 |
-
negative_prompt_embeds = torch.cat(negative_prompt_embeds)
|
41 |
-
|
42 |
-
views = ['identity', VIEW_MAP_NAMES[transformation]]
|
43 |
-
views = get_views(views)
|
44 |
-
|
45 |
-
generator = torch.manual_seed(seed)
|
46 |
-
|
47 |
-
print("Sample stage 1")
|
48 |
-
image = sample_stage_1(stage_1,
|
49 |
-
prompt_embeds,
|
50 |
-
negative_prompt_embeds,
|
51 |
-
views,
|
52 |
-
num_inference_steps=num_inference_steps,
|
53 |
-
generator=generator)
|
54 |
-
|
55 |
-
print("Sample stage 2")
|
56 |
-
image = sample_stage_2(stage_2,
|
57 |
-
image,
|
58 |
-
prompt_embeds,
|
59 |
-
negative_prompt_embeds,
|
60 |
-
views,
|
61 |
-
num_inference_steps=num_inference_steps,
|
62 |
-
generator=generator)
|
63 |
-
save_illusion(image, views, Path(""))
|
64 |
-
|
65 |
-
size = image.shape[-1]
|
66 |
-
animate_two_view(
|
67 |
-
f"sample_{size}.png",
|
68 |
-
views[1],
|
69 |
-
prompts[0],
|
70 |
-
prompts[1],
|
71 |
-
)
|
72 |
-
return 'tmp.mp4', f"sample_{size}.png", f"sample_{size}.views.png"
|
73 |
-
|
74 |
-
|
75 |
-
choices = list(VIEW_MAP_NAMES.keys())
|
76 |
-
gradio_app = gr.Interface(
|
77 |
-
fn=generate_content,
|
78 |
-
title="Multi-View Illusion Diffusion",
|
79 |
-
inputs=[
|
80 |
-
gr.Textbox(label="Style", placeholder="an oil painting of"),
|
81 |
-
gr.Textbox(label="Prompt for original view", placeholder="a dress"),
|
82 |
-
gr.Textbox(label="Prompt for transformed view", placeholder="an old man"),
|
83 |
-
gr.Dropdown(label="View transformation", choices=choices, value=choices[0]),
|
84 |
-
gr.Number(label="Number of diffusion steps", value=50, step=1, minimum=1, maximum=300),
|
85 |
-
gr.Number(label="Random seed", value=0, step=1, minimum=0, maximum=100000)
|
86 |
-
],
|
87 |
-
outputs=[gr.Video(label="Illusion"), gr.Image(label="Original"), gr.Image(label="Transformed")],
|
88 |
-
)
|
89 |
-
|
90 |
-
|
91 |
-
if __name__ == "__main__":
|
92 |
-
gradio_app.launch(server_name="0.0.0.0") # server_name="0.0.0.0"
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
test_app.py
ADDED
@@ -0,0 +1,68 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
from diffusers import DiffusionPipeline
|
2 |
+
from diffusers.utils import pt_to_pil
|
3 |
+
import gradio as gr
|
4 |
+
import torch
|
5 |
+
import numpy as np
|
6 |
+
|
7 |
+
|
8 |
+
stage_1 = DiffusionPipeline.from_pretrained(
|
9 |
+
"DeepFloyd/IF-I-M-v1.0", variant="fp16", torch_dtype=torch.float16
|
10 |
+
)
|
11 |
+
stage_1.enable_xformers_memory_efficient_attention() # remove line if torch.__version__ >= 2.0.0
|
12 |
+
stage_1.enable_model_cpu_offload()
|
13 |
+
stage_2 = DiffusionPipeline.from_pretrained(
|
14 |
+
"DeepFloyd/IF-II-M-v1.0",
|
15 |
+
text_encoder=None,
|
16 |
+
variant="fp16",
|
17 |
+
torch_dtype=torch.float16,
|
18 |
+
)
|
19 |
+
stage_2.enable_xformers_memory_efficient_attention() # remove line if torch.__version__ >= 2.0.0
|
20 |
+
stage_2.enable_model_cpu_offload()
|
21 |
+
|
22 |
+
# stage 3
|
23 |
+
safety_modules = {
|
24 |
+
"feature_extractor": stage_1.feature_extractor,
|
25 |
+
"safety_checker": stage_1.safety_checker,
|
26 |
+
"watermarker": stage_1.watermarker,
|
27 |
+
}
|
28 |
+
stage_3 = DiffusionPipeline.from_pretrained(
|
29 |
+
"stabilityai/stable-diffusion-x4-upscaler",
|
30 |
+
**safety_modules,
|
31 |
+
torch_dtype=torch.float16
|
32 |
+
)
|
33 |
+
stage_3.enable_xformers_memory_efficient_attention() # remove line if torch.__version__ >= 2.0.0
|
34 |
+
stage_3.enable_model_cpu_offload()
|
35 |
+
|
36 |
+
|
37 |
+
def predict(prompt):
|
38 |
+
prompt_embeds, negative_embeds = stage_1.encode_prompt(prompt)
|
39 |
+
generator = torch.manual_seed(0)
|
40 |
+
image = stage_1(
|
41 |
+
prompt_embeds=prompt_embeds,
|
42 |
+
negative_prompt_embeds=negative_embeds,
|
43 |
+
generator=generator,
|
44 |
+
output_type="pt",
|
45 |
+
).images
|
46 |
+
image = stage_2(
|
47 |
+
image=image,
|
48 |
+
prompt_embeds=prompt_embeds,
|
49 |
+
negative_prompt_embeds=negative_embeds,
|
50 |
+
generator=generator,
|
51 |
+
output_type="pt",
|
52 |
+
).images
|
53 |
+
image = stage_3(
|
54 |
+
prompt=prompt, image=image, generator=generator, noise_level=100
|
55 |
+
).images[0]
|
56 |
+
return image
|
57 |
+
|
58 |
+
|
59 |
+
gradio_app = gr.Interface(
|
60 |
+
fn=predict,
|
61 |
+
inputs="text",
|
62 |
+
outputs="image",
|
63 |
+
title="Text to Image Generator",
|
64 |
+
description="Enter a text string to generate an image.",
|
65 |
+
)
|
66 |
+
|
67 |
+
if __name__ == "__main__":
|
68 |
+
gradio_app.launch(server_name="0.0.0.0") # server_name="0.0.0.0"
|