Spaces:
Running
Running
Cache examples
Browse files- .gitignore +2 -0
- app_generated_image.py +46 -23
- app_real_image.py +56 -22
- utils.py +0 -5
.gitignore
CHANGED
@@ -1,3 +1,5 @@
|
|
|
|
|
|
1 |
# Byte-compiled / optimized / DLL files
|
2 |
__pycache__/
|
3 |
*.py[cod]
|
|
|
1 |
+
gradio_cached_examples/
|
2 |
+
|
3 |
# Byte-compiled / optimized / DLL files
|
4 |
__pycache__/
|
5 |
*.py[cod]
|
app_generated_image.py
CHANGED
@@ -10,8 +10,6 @@ import tempfile
|
|
10 |
import gradio as gr
|
11 |
from omegaconf import OmegaConf
|
12 |
|
13 |
-
from utils import get_timestamp
|
14 |
-
|
15 |
|
16 |
def gen_feature_extraction_config(
|
17 |
exp_name: str,
|
@@ -39,17 +37,18 @@ def run_feature_extraction_command(
|
|
39 |
guidance_scale: float,
|
40 |
ddim_steps: int,
|
41 |
) -> tuple[str, str]:
|
42 |
-
exp_name =
|
43 |
-
|
44 |
-
|
45 |
-
|
46 |
-
|
47 |
-
|
48 |
-
|
49 |
-
|
50 |
-
|
51 |
-
|
52 |
-
|
|
|
53 |
return f'plug-and-play/experiments/{exp_name}/samples/0.png', exp_name
|
54 |
|
55 |
|
@@ -109,6 +108,21 @@ def run_pnp_command(
|
|
109 |
return out_path.as_posix()
|
110 |
|
111 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
112 |
def create_prompt_demo() -> gr.Blocks:
|
113 |
with gr.Blocks() as demo:
|
114 |
with gr.Box():
|
@@ -139,7 +153,7 @@ def create_prompt_demo() -> gr.Blocks:
|
|
139 |
with gr.Column():
|
140 |
generated_image = gr.Image(label='Generated image',
|
141 |
type='filepath')
|
142 |
-
exp_name = gr.
|
143 |
with gr.Box():
|
144 |
gr.Markdown(
|
145 |
'Step 2 (This step will take about 1.5 minutes on A10G.)')
|
@@ -180,15 +194,24 @@ def create_prompt_demo() -> gr.Blocks:
|
|
180 |
with gr.Column():
|
181 |
result = gr.Image(label='Result', type='filepath')
|
182 |
with gr.Row():
|
183 |
-
gr.Examples(
|
184 |
-
[
|
185 |
-
|
186 |
-
|
187 |
-
|
188 |
-
|
189 |
-
|
190 |
-
|
191 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
192 |
|
193 |
extract_feature_button.click(
|
194 |
fn=run_feature_extraction_command,
|
|
|
10 |
import gradio as gr
|
11 |
from omegaconf import OmegaConf
|
12 |
|
|
|
|
|
13 |
|
14 |
def gen_feature_extraction_config(
|
15 |
exp_name: str,
|
|
|
37 |
guidance_scale: float,
|
38 |
ddim_steps: int,
|
39 |
) -> tuple[str, str]:
|
40 |
+
exp_name = f'{prompt.replace(" ", "_")}_{seed}_{guidance_scale:.1f}_{ddim_steps}'
|
41 |
+
if not pathlib.Path(f'plug-and-play/experiments/{exp_name}').exists():
|
42 |
+
config_path = gen_feature_extraction_config(
|
43 |
+
exp_name,
|
44 |
+
prompt,
|
45 |
+
seed,
|
46 |
+
guidance_scale,
|
47 |
+
ddim_steps,
|
48 |
+
)
|
49 |
+
subprocess.run(shlex.split(
|
50 |
+
f'python run_features_extraction.py --config {config_path}'),
|
51 |
+
cwd='plug-and-play')
|
52 |
return f'plug-and-play/experiments/{exp_name}/samples/0.png', exp_name
|
53 |
|
54 |
|
|
|
108 |
return out_path.as_posix()
|
109 |
|
110 |
|
111 |
+
def process_example(source_prompt: str, seed: int,
|
112 |
+
translation_prompt: str) -> tuple[str, str, str]:
|
113 |
+
generated_image, exp_name = run_feature_extraction_command(
|
114 |
+
source_prompt, seed, guidance_scale=5, ddim_steps=50)
|
115 |
+
result = run_pnp_command(exp_name,
|
116 |
+
translation_prompt,
|
117 |
+
negative_prompt='',
|
118 |
+
guidance_scale=7.5,
|
119 |
+
ddim_steps=50,
|
120 |
+
feature_injection_threshold=40,
|
121 |
+
negative_prompt_alpha=0.75,
|
122 |
+
negative_prompt_schedule='linear')
|
123 |
+
return generated_image, exp_name, result
|
124 |
+
|
125 |
+
|
126 |
def create_prompt_demo() -> gr.Blocks:
|
127 |
with gr.Blocks() as demo:
|
128 |
with gr.Box():
|
|
|
153 |
with gr.Column():
|
154 |
generated_image = gr.Image(label='Generated image',
|
155 |
type='filepath')
|
156 |
+
exp_name = gr.Text(visible=False)
|
157 |
with gr.Box():
|
158 |
gr.Markdown(
|
159 |
'Step 2 (This step will take about 1.5 minutes on A10G.)')
|
|
|
194 |
with gr.Column():
|
195 |
result = gr.Image(label='Result', type='filepath')
|
196 |
with gr.Row():
|
197 |
+
gr.Examples(
|
198 |
+
examples=[
|
199 |
+
['horse in mud', 50, 'a photo of a zebra in the snow'],
|
200 |
+
['horse in mud', 50, 'a photo of a husky in the grass'],
|
201 |
+
],
|
202 |
+
inputs=[
|
203 |
+
source_prompt,
|
204 |
+
seed,
|
205 |
+
translation_prompt,
|
206 |
+
],
|
207 |
+
outputs=[
|
208 |
+
generated_image,
|
209 |
+
exp_name,
|
210 |
+
result,
|
211 |
+
],
|
212 |
+
fn=process_example,
|
213 |
+
cache_examples=True,
|
214 |
+
)
|
215 |
|
216 |
extract_feature_button.click(
|
217 |
fn=run_feature_extraction_command,
|
app_real_image.py
CHANGED
@@ -2,6 +2,7 @@
|
|
2 |
|
3 |
from __future__ import annotations
|
4 |
|
|
|
5 |
import pathlib
|
6 |
import shlex
|
7 |
import subprocess
|
@@ -10,7 +11,11 @@ import tempfile
|
|
10 |
import gradio as gr
|
11 |
from omegaconf import OmegaConf
|
12 |
|
13 |
-
|
|
|
|
|
|
|
|
|
14 |
|
15 |
|
16 |
def gen_feature_extraction_config(exp_name: str, init_image_path: str) -> str:
|
@@ -25,11 +30,12 @@ def gen_feature_extraction_config(exp_name: str, init_image_path: str) -> str:
|
|
25 |
|
26 |
|
27 |
def run_feature_extraction_command(init_image_path: str) -> tuple[str, str]:
|
28 |
-
exp_name =
|
29 |
-
|
30 |
-
|
31 |
-
|
32 |
-
|
|
|
33 |
return f'plug-and-play/experiments/{exp_name}/samples/0.png', exp_name
|
34 |
|
35 |
|
@@ -89,6 +95,20 @@ def run_pnp_command(
|
|
89 |
return out_path.as_posix()
|
90 |
|
91 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
92 |
def create_real_image_demo():
|
93 |
with gr.Blocks() as demo:
|
94 |
with gr.Box():
|
@@ -102,7 +122,7 @@ def create_real_image_demo():
|
|
102 |
with gr.Column():
|
103 |
reconstructed_image = gr.Image(label='Reconstructed image',
|
104 |
type='filepath')
|
105 |
-
exp_name = gr.
|
106 |
with gr.Box():
|
107 |
gr.Markdown(
|
108 |
'Step 2 (This step will take about 1.5 minutes on A10G.)')
|
@@ -144,23 +164,37 @@ def create_real_image_demo():
|
|
144 |
result = gr.Image(label='Result', type='filepath')
|
145 |
|
146 |
with gr.Row():
|
147 |
-
gr.Examples(
|
148 |
-
[
|
149 |
-
|
150 |
-
|
151 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
152 |
],
|
153 |
-
[
|
154 |
-
|
155 |
-
|
156 |
-
|
157 |
],
|
158 |
-
|
159 |
-
|
160 |
-
|
161 |
-
|
162 |
-
|
163 |
-
|
|
|
|
|
164 |
|
165 |
extract_feature_button.click(
|
166 |
fn=run_feature_extraction_command,
|
|
|
2 |
|
3 |
from __future__ import annotations
|
4 |
|
5 |
+
import hashlib
|
6 |
import pathlib
|
7 |
import shlex
|
8 |
import subprocess
|
|
|
11 |
import gradio as gr
|
12 |
from omegaconf import OmegaConf
|
13 |
|
14 |
+
|
15 |
+
def get_exp_name(path: str) -> str:
|
16 |
+
with open(path, 'rb') as f:
|
17 |
+
res = hashlib.md5(f.read()).hexdigest()
|
18 |
+
return res
|
19 |
|
20 |
|
21 |
def gen_feature_extraction_config(exp_name: str, init_image_path: str) -> str:
|
|
|
30 |
|
31 |
|
32 |
def run_feature_extraction_command(init_image_path: str) -> tuple[str, str]:
|
33 |
+
exp_name = get_exp_name(init_image_path)
|
34 |
+
if not pathlib.Path(f'plug-and-play/experiments/{exp_name}').exists():
|
35 |
+
config_path = gen_feature_extraction_config(exp_name, init_image_path)
|
36 |
+
subprocess.run(shlex.split(
|
37 |
+
f'python run_features_extraction.py --config {config_path}'),
|
38 |
+
cwd='plug-and-play')
|
39 |
return f'plug-and-play/experiments/{exp_name}/samples/0.png', exp_name
|
40 |
|
41 |
|
|
|
95 |
return out_path.as_posix()
|
96 |
|
97 |
|
98 |
+
def process_example(image: str, translation_prompt: str,
|
99 |
+
negative_prompt: str) -> tuple[str, str, str]:
|
100 |
+
reconstructed_image, exp_name = run_feature_extraction_command(image)
|
101 |
+
result = run_pnp_command(exp_name,
|
102 |
+
translation_prompt,
|
103 |
+
negative_prompt,
|
104 |
+
guidance_scale=10,
|
105 |
+
ddim_steps=50,
|
106 |
+
feature_injection_threshold=40,
|
107 |
+
negative_prompt_alpha=1,
|
108 |
+
negative_prompt_schedule='linear')
|
109 |
+
return reconstructed_image, exp_name, result
|
110 |
+
|
111 |
+
|
112 |
def create_real_image_demo():
|
113 |
with gr.Blocks() as demo:
|
114 |
with gr.Box():
|
|
|
122 |
with gr.Column():
|
123 |
reconstructed_image = gr.Image(label='Reconstructed image',
|
124 |
type='filepath')
|
125 |
+
exp_name = gr.Text(visible=False)
|
126 |
with gr.Box():
|
127 |
gr.Markdown(
|
128 |
'Step 2 (This step will take about 1.5 minutes on A10G.)')
|
|
|
164 |
result = gr.Image(label='Result', type='filepath')
|
165 |
|
166 |
with gr.Row():
|
167 |
+
gr.Examples(
|
168 |
+
examples=[
|
169 |
+
[
|
170 |
+
'plug-and-play/data/horse.png',
|
171 |
+
'a photo of a robot horse',
|
172 |
+
'a photo of a white horse',
|
173 |
+
],
|
174 |
+
[
|
175 |
+
'plug-and-play/data/horse.png',
|
176 |
+
'a photo of a bronze horse in a museum',
|
177 |
+
'a photo of a white horse',
|
178 |
+
],
|
179 |
+
[
|
180 |
+
'plug-and-play/data/horse.png',
|
181 |
+
'a photo of a pink horse on the beach',
|
182 |
+
'a photo of a white horse',
|
183 |
+
],
|
184 |
],
|
185 |
+
inputs=[
|
186 |
+
image,
|
187 |
+
translation_prompt,
|
188 |
+
negative_prompt,
|
189 |
],
|
190 |
+
outputs=[
|
191 |
+
reconstructed_image,
|
192 |
+
exp_name,
|
193 |
+
result,
|
194 |
+
],
|
195 |
+
fn=process_example,
|
196 |
+
cache_examples=True,
|
197 |
+
)
|
198 |
|
199 |
extract_feature_button.click(
|
200 |
fn=run_feature_extraction_command,
|
utils.py
DELETED
@@ -1,5 +0,0 @@
|
|
1 |
-
import datetime
|
2 |
-
|
3 |
-
|
4 |
-
def get_timestamp() -> str:
|
5 |
-
return datetime.datetime.now().strftime('%Y-%m-%d-%H-%M-%S-%f')
|
|
|
|
|
|
|
|
|
|
|
|