Spaces:
Running
on
Zero
Running
on
Zero
Merge remote-tracking branch 'upstream/main'
Browse files
app.py
CHANGED
@@ -1,278 +0,0 @@
|
|
1 |
-
import logging
|
2 |
-
import gradio as gr
|
3 |
-
import numpy as np
|
4 |
-
import cv2
|
5 |
-
import os
|
6 |
-
import base64
|
7 |
-
|
8 |
-
from try_on_diffusion_client import TryOnDiffusionClient
|
9 |
-
|
10 |
-
LOG_LEVEL = logging.INFO
|
11 |
-
LOG_FORMAT = "%(asctime)s %(thread)-8s %(name)-16s %(levelname)-8s %(message)s"
|
12 |
-
LOG_DATE_FORMAT = "%Y-%m-%d %H:%M:%S"
|
13 |
-
|
14 |
-
EXAMPLE_PATH = os.path.join(os.path.dirname(__file__), "examples")
|
15 |
-
|
16 |
-
API_URL = os.getenv("TRY_ON_DIFFUSION_DEMO_API_URL", "http://localhost:8000")
|
17 |
-
API_KEY = os.getenv("TRY_ON_DIFFUSION_DEMO_API_KEY", "")
|
18 |
-
|
19 |
-
SHOW_RAPIDAPI_LINK = os.getenv("TRY_ON_DIFFUSION_DEMO_SHOW_RAPIDAPI_LINK", "1") == "1"
|
20 |
-
|
21 |
-
CONCURRENCY_LIMIT = int(os.getenv("TRY_ON_DIFFUSION_DEMO_CONCURRENCY_LIMIT", "2"))
|
22 |
-
|
23 |
-
logging.basicConfig(level=LOG_LEVEL, format=LOG_FORMAT, datefmt=LOG_DATE_FORMAT)
|
24 |
-
|
25 |
-
client = TryOnDiffusionClient(base_url=API_URL, api_key=API_KEY)
|
26 |
-
|
27 |
-
|
28 |
-
def get_image_base64(file_name: str) -> str:
|
29 |
-
_, ext = os.path.splitext(file_name.lower())
|
30 |
-
|
31 |
-
content_type = "image/jpeg"
|
32 |
-
|
33 |
-
if ext == ".png":
|
34 |
-
content_type = "image/png"
|
35 |
-
elif ext == ".webp":
|
36 |
-
content_type = "image/webp"
|
37 |
-
elif ext == ".gif":
|
38 |
-
content_type = "image/gif"
|
39 |
-
|
40 |
-
with open(file_name, "rb") as f:
|
41 |
-
return f"data:{content_type};base64," + base64.b64encode(f.read()).decode("utf-8")
|
42 |
-
|
43 |
-
|
44 |
-
def get_examples(example_dir: str) -> list[str]:
|
45 |
-
file_list = [f for f in os.listdir(os.path.join(EXAMPLE_PATH, example_dir)) if f.endswith(".jpg")]
|
46 |
-
file_list.sort()
|
47 |
-
|
48 |
-
return [os.path.join(EXAMPLE_PATH, example_dir, f) for f in file_list]
|
49 |
-
|
50 |
-
|
51 |
-
def try_on(
|
52 |
-
clothing_image: np.ndarray = None,
|
53 |
-
clothing_prompt: str = None,
|
54 |
-
avatar_image: np.ndarray = None,
|
55 |
-
avatar_prompt: str = None,
|
56 |
-
avatar_sex: str = None,
|
57 |
-
background_image: np.ndarray = None,
|
58 |
-
background_prompt: str = None,
|
59 |
-
seed: int = -1,
|
60 |
-
) -> tuple:
|
61 |
-
result = client.try_on_file(
|
62 |
-
clothing_image=cv2.cvtColor(clothing_image, cv2.COLOR_RGB2BGR) if clothing_image is not None else None,
|
63 |
-
clothing_prompt=clothing_prompt,
|
64 |
-
avatar_image=cv2.cvtColor(avatar_image, cv2.COLOR_RGB2BGR) if avatar_image is not None else None,
|
65 |
-
avatar_prompt=avatar_prompt,
|
66 |
-
avatar_sex=avatar_sex if avatar_sex in ["male", "female"] else None,
|
67 |
-
background_image=cv2.cvtColor(background_image, cv2.COLOR_RGB2BGR) if background_image is not None else None,
|
68 |
-
background_prompt=background_prompt,
|
69 |
-
seed=seed,
|
70 |
-
)
|
71 |
-
|
72 |
-
if result.status_code == 200:
|
73 |
-
return cv2.cvtColor(result.image, cv2.COLOR_BGR2RGB), f"<h3>Success</h3><p>Seed: {result.seed}</p>"
|
74 |
-
else:
|
75 |
-
error_message = f"<h3>Error {result.status_code}</h3>"
|
76 |
-
|
77 |
-
if result.error_details is not None:
|
78 |
-
error_message += f"<p>{result.error_details}</p>"
|
79 |
-
|
80 |
-
return None, error_message
|
81 |
-
|
82 |
-
|
83 |
-
with gr.Blocks(theme=gr.themes.Soft(), delete_cache=(3600, 3600)) as app:
|
84 |
-
gr.HTML(
|
85 |
-
f"""
|
86 |
-
|
87 |
-
"""
|
88 |
-
)
|
89 |
-
|
90 |
-
if SHOW_RAPIDAPI_LINK:
|
91 |
-
gr.Button(
|
92 |
-
|
93 |
-
)
|
94 |
-
|
95 |
-
gr.HTML(
|
96 |
-
"""
|
97 |
-
<center>
|
98 |
-
|
99 |
-
</center>
|
100 |
-
"""
|
101 |
-
)
|
102 |
-
|
103 |
-
gr.HTML("</p>")
|
104 |
-
|
105 |
-
with gr.Row():
|
106 |
-
with gr.Column():
|
107 |
-
gr.HTML(
|
108 |
-
"""
|
109 |
-
<h2>Clothing</h2>
|
110 |
-
<p>
|
111 |
-
Clothing may be specified with a reference image or a text prompt.
|
112 |
-
For more exotic use cases image and prompt can be also used together.
|
113 |
-
If both image and prompt are empty the model will generate random clothing.
|
114 |
-
<br/><br/>
|
115 |
-
</p>
|
116 |
-
"""
|
117 |
-
)
|
118 |
-
|
119 |
-
with gr.Tab("Image"):
|
120 |
-
clothing_image = gr.Image(label="Clothing Image", sources=["upload"], type="numpy")
|
121 |
-
|
122 |
-
clothing_image_examples = gr.Examples(
|
123 |
-
inputs=clothing_image, examples_per_page=18, examples=get_examples("clothing")
|
124 |
-
)
|
125 |
-
|
126 |
-
with gr.Tab("Prompt"):
|
127 |
-
clothing_prompt = gr.TextArea(
|
128 |
-
label="Clothing Prompt",
|
129 |
-
info='Compel weighting <a href="https://github.com/damian0815/compel/blob/main/doc/syntax.md">syntax</a> is supported.',
|
130 |
-
)
|
131 |
-
|
132 |
-
clothing_prompt_examples = gr.Examples(
|
133 |
-
inputs=clothing_prompt,
|
134 |
-
examples_per_page=8,
|
135 |
-
examples=[
|
136 |
-
"a sheer blue sleeveless mini dress",
|
137 |
-
"a beige woolen sweater and white pleated skirt",
|
138 |
-
"a black leather jacket and dark blue slim-fit jeans",
|
139 |
-
"a floral pattern blouse and leggings",
|
140 |
-
"a paisley pattern purple shirt and beige chinos",
|
141 |
-
"a striped white and blue polo shirt and blue jeans",
|
142 |
-
"a colorful t-shirt and black shorts",
|
143 |
-
"a checked pattern shirt and dark blue cargo pants",
|
144 |
-
],
|
145 |
-
)
|
146 |
-
|
147 |
-
with gr.Column():
|
148 |
-
gr.HTML(
|
149 |
-
"""
|
150 |
-
<h2>Avatar</h2>
|
151 |
-
<p>
|
152 |
-
Avatar may be specified with a subject photo or a text prompt.
|
153 |
-
Latter can be used, for example, to replace person while preserving clothing.
|
154 |
-
For more exotic use cases image and prompt can be also used together.
|
155 |
-
If both image and prompt are empty the model will generate random avatars.
|
156 |
-
</p>
|
157 |
-
"""
|
158 |
-
)
|
159 |
-
|
160 |
-
with gr.Tab("Image"):
|
161 |
-
avatar_image = gr.Image(label="Avatar Image", sources=["upload"], type="numpy")
|
162 |
-
|
163 |
-
avatar_image_examples = gr.Examples(
|
164 |
-
inputs=avatar_image,
|
165 |
-
examples_per_page=18,
|
166 |
-
examples=get_examples("avatar"),
|
167 |
-
)
|
168 |
-
|
169 |
-
with gr.Tab("Prompt"):
|
170 |
-
avatar_prompt = gr.TextArea(
|
171 |
-
label="Avatar Prompt",
|
172 |
-
info='Compel weighting <a href="https://github.com/damian0815/compel/blob/main/doc/syntax.md">syntax</a> is supported.',
|
173 |
-
)
|
174 |
-
|
175 |
-
avatar_prompt_examples = gr.Examples(
|
176 |
-
inputs=avatar_prompt,
|
177 |
-
examples_per_page=8,
|
178 |
-
examples=[
|
179 |
-
"a beautiful blond girl with long hair",
|
180 |
-
"a cute redhead girl with freckles",
|
181 |
-
"a plus size female model wearing sunglasses",
|
182 |
-
"a woman with dark hair and blue eyes",
|
183 |
-
"a fit man with dark beard and blue eyes",
|
184 |
-
"a young blond man posing for a photo",
|
185 |
-
"a gentleman with beard and mustache",
|
186 |
-
"a plus size man walking",
|
187 |
-
],
|
188 |
-
)
|
189 |
-
|
190 |
-
avatar_sex = gr.Dropdown(
|
191 |
-
label="Avatar Sex",
|
192 |
-
choices=[("Auto", ""), ("Male", "male"), ("Female", "female")],
|
193 |
-
value="",
|
194 |
-
info="Avatar sex selector can be used to enforce a specific sex of the avatar.",
|
195 |
-
)
|
196 |
-
|
197 |
-
with gr.Column():
|
198 |
-
gr.HTML(
|
199 |
-
"""
|
200 |
-
<h2>Background</h2>
|
201 |
-
<p>
|
202 |
-
Replacing the background is optional.
|
203 |
-
Resulting background may be specified with a reference image or a text prompt.
|
204 |
-
If omitted the original avatar background will be preserved.
|
205 |
-
<br/><br/><br/>
|
206 |
-
</p>
|
207 |
-
"""
|
208 |
-
)
|
209 |
-
|
210 |
-
with gr.Tab("Image"):
|
211 |
-
background_image = gr.Image(label="Background Image", sources=["upload"], type="numpy")
|
212 |
-
|
213 |
-
background_image_examples = gr.Examples(
|
214 |
-
inputs=background_image, examples_per_page=18, examples=get_examples("background")
|
215 |
-
)
|
216 |
-
|
217 |
-
with gr.Tab("Prompt"):
|
218 |
-
background_prompt = gr.TextArea(
|
219 |
-
label="Background Prompt",
|
220 |
-
info='Compel weighting <a href="https://github.com/damian0815/compel/blob/main/doc/syntax.md">syntax</a> is supported.',
|
221 |
-
)
|
222 |
-
|
223 |
-
background_prompt_examples = gr.Examples(
|
224 |
-
inputs=background_prompt,
|
225 |
-
examples_per_page=8,
|
226 |
-
examples=[
|
227 |
-
"in an autumn park",
|
228 |
-
"in front of a brick wall",
|
229 |
-
"near an old tree",
|
230 |
-
"on a busy city street",
|
231 |
-
"in front of a staircase",
|
232 |
-
"on an ocean beach with palm trees",
|
233 |
-
"in a shopping mall",
|
234 |
-
"in a modern office",
|
235 |
-
],
|
236 |
-
)
|
237 |
-
|
238 |
-
with gr.Column():
|
239 |
-
gr.HTML(
|
240 |
-
"""
|
241 |
-
<h2>Generation</h2>
|
242 |
-
"""
|
243 |
-
)
|
244 |
-
|
245 |
-
seed = gr.Number(
|
246 |
-
label="Seed",
|
247 |
-
value=-1,
|
248 |
-
minimum=-1,
|
249 |
-
info="Seed used for generation, specify -1 for random seed for each generation.",
|
250 |
-
)
|
251 |
-
|
252 |
-
generate_button = gr.Button(value="Generate", variant="primary")
|
253 |
-
|
254 |
-
result_image = gr.Image(label="Result", show_share_button=False, format="jpeg")
|
255 |
-
result_details = gr.HTML(label="Details")
|
256 |
-
|
257 |
-
generate_button.click(
|
258 |
-
fn=try_on,
|
259 |
-
inputs=[
|
260 |
-
clothing_image,
|
261 |
-
clothing_prompt,
|
262 |
-
avatar_image,
|
263 |
-
avatar_prompt,
|
264 |
-
avatar_sex,
|
265 |
-
background_image,
|
266 |
-
background_prompt,
|
267 |
-
seed,
|
268 |
-
],
|
269 |
-
outputs=[result_image, result_details],
|
270 |
-
api_name=False,
|
271 |
-
concurrency_limit=CONCURRENCY_LIMIT,
|
272 |
-
)
|
273 |
-
|
274 |
-
app.title = "Virtual Try-On Diffusion by Texel.Moda"
|
275 |
-
|
276 |
-
|
277 |
-
if __name__ == "__main__":
|
278 |
-
app.queue(api_open=False).launch(show_api=False, ssr_mode=False)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|