Updated the API client to support RapidAPI, small tweaks and fixes
Browse files- app.py +23 -8
- docs/API.md +2 -2
- try_on_diffusion_client.py +18 -7
app.py
CHANGED
@@ -16,6 +16,8 @@ EXAMPLE_PATH = os.path.join(os.path.dirname(__file__), "examples")
|
|
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 |
CONCURRENCY_LIMIT = int(os.getenv("TRY_ON_DIFFUSION_DEMO_CONCURRENCY_LIMIT", "2"))
|
20 |
|
21 |
logging.basicConfig(level=LOG_LEVEL, format=LOG_FORMAT, datefmt=LOG_DATE_FORMAT)
|
@@ -68,7 +70,7 @@ def try_on(
|
|
68 |
)
|
69 |
|
70 |
if result.status_code == 200:
|
71 |
-
return cv2.cvtColor(result.image, cv2.
|
72 |
else:
|
73 |
error_message = f"<h3>Error {result.status_code}</h3>"
|
74 |
|
@@ -78,7 +80,7 @@ def try_on(
|
|
78 |
return None, error_message
|
79 |
|
80 |
|
81 |
-
with gr.Blocks(theme=gr.themes.Soft()) as app:
|
82 |
gr.HTML(
|
83 |
f"""
|
84 |
<div style="width: 100%; background-color: #001537; border-radius: 10px; padding-left: 10px">
|
@@ -93,13 +95,26 @@ with gr.Blocks(theme=gr.themes.Soft()) as app:
|
|
93 |
custom diffusion-based pipeline for fast and flexible multi-modal virtual try-on.
|
94 |
Clothing, avatar and background can be specified by reference images or text prompts allowing for clothing
|
95 |
transfer, avatar replacement, fashion image generation and other virtual try-on related tasks.
|
96 |
-
Feel free to <a href="https://texelmoda.com/contact/" target="_blank">contact us</a> if you are interested
|
97 |
-
in commercial API usage or partnership.
|
98 |
-
</p>
|
99 |
-
<br/>
|
100 |
"""
|
101 |
)
|
102 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
103 |
with gr.Row():
|
104 |
with gr.Column():
|
105 |
gr.HTML(
|
@@ -247,9 +262,9 @@ with gr.Blocks(theme=gr.themes.Soft()) as app:
|
|
247 |
info="Seed used for generation, specify -1 for random seed for each generation.",
|
248 |
)
|
249 |
|
250 |
-
generate_button = gr.Button(value="Generate",
|
251 |
|
252 |
-
result_image = gr.Image(label="Result", show_share_button=False)
|
253 |
result_details = gr.HTML(label="Details")
|
254 |
|
255 |
generate_button.click(
|
|
|
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)
|
|
|
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 |
|
|
|
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 |
<div style="width: 100%; background-color: #001537; border-radius: 10px; padding-left: 10px">
|
|
|
95 |
custom diffusion-based pipeline for fast and flexible multi-modal virtual try-on.
|
96 |
Clothing, avatar and background can be specified by reference images or text prompts allowing for clothing
|
97 |
transfer, avatar replacement, fashion image generation and other virtual try-on related tasks.
|
|
|
|
|
|
|
|
|
98 |
"""
|
99 |
)
|
100 |
|
101 |
+
if SHOW_RAPIDAPI_LINK:
|
102 |
+
gr.Button(
|
103 |
+
value="Check out the API @ RapidAPI.com",
|
104 |
+
link="https://rapidapi.com/texelmoda-texelmoda-apis/api/try-on-diffusion",
|
105 |
+
icon="https://files.readme.io/9336831-small-rapid-logo-favicon.png",
|
106 |
+
)
|
107 |
+
|
108 |
+
gr.HTML(
|
109 |
+
"""
|
110 |
+
<center>
|
111 |
+
<a href="https://huggingface.co/spaces/texelmoda/try-on-diffusion/blob/main/docs/API.md">API Documentation</a>
|
112 |
+
</center>
|
113 |
+
"""
|
114 |
+
)
|
115 |
+
|
116 |
+
gr.HTML("</p>")
|
117 |
+
|
118 |
with gr.Row():
|
119 |
with gr.Column():
|
120 |
gr.HTML(
|
|
|
262 |
info="Seed used for generation, specify -1 for random seed for each generation.",
|
263 |
)
|
264 |
|
265 |
+
generate_button = gr.Button(value="Generate", variant="primary")
|
266 |
|
267 |
+
result_image = gr.Image(label="Result", show_share_button=False, format="jpeg")
|
268 |
result_details = gr.HTML(label="Details")
|
269 |
|
270 |
generate_button.click(
|
docs/API.md
CHANGED
@@ -33,7 +33,7 @@
|
|
33 |
Virtual Try-On Diffusion [VTON-D] by [Texel.Moda](https://texelmoda.com) is a custom diffusion-based pipeline for fast
|
34 |
and flexible multi-modal virtual try-on. Clothing, avatar and background can be specified by reference images or text
|
35 |
prompts allowing for clothing transfer, avatar replacement, fashion image generation and other virtual try-on related
|
36 |
-
tasks. Check out the [demo on
|
37 |
a user-friendly way.
|
38 |
|
39 |
## Consuming the API
|
@@ -60,7 +60,7 @@ curl --request POST \
|
|
60 |
```
|
61 |
|
62 |
For a simple Python client implementation please see the
|
63 |
-
[
|
64 |
|
65 |
## Try-On Endpoints
|
66 |
|
|
|
33 |
Virtual Try-On Diffusion [VTON-D] by [Texel.Moda](https://texelmoda.com) is a custom diffusion-based pipeline for fast
|
34 |
and flexible multi-modal virtual try-on. Clothing, avatar and background can be specified by reference images or text
|
35 |
prompts allowing for clothing transfer, avatar replacement, fashion image generation and other virtual try-on related
|
36 |
+
tasks. Check out the [demo on Hugging Face](https://huggingface.co/spaces/texelmoda/try-on-diffusion) to try the API in
|
37 |
a user-friendly way.
|
38 |
|
39 |
## Consuming the API
|
|
|
60 |
```
|
61 |
|
62 |
For a simple Python client implementation please see the
|
63 |
+
[Hugging Face demo application source](https://huggingface.co/spaces/texelmoda/try-on-diffusion/blob/main/try_on_diffusion_client.py).
|
64 |
|
65 |
## Try-On Endpoints
|
66 |
|
try_on_diffusion_client.py
CHANGED
@@ -2,6 +2,7 @@ import cv2
|
|
2 |
import numpy as np
|
3 |
import requests
|
4 |
from requests_toolbelt.multipart.encoder import MultipartEncoder
|
|
|
5 |
import logging
|
6 |
import json
|
7 |
from io import BytesIO
|
@@ -26,6 +27,13 @@ class TryOnDiffusionClient:
|
|
26 |
if self._base_url[-1] == "/":
|
27 |
self._base_url = self._base_url[:-1]
|
28 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
29 |
@staticmethod
|
30 |
def _image_to_upload_file(image: np.ndarray) -> tuple:
|
31 |
_, jpeg_data = cv2.imencode(".jpg", image, [int(cv2.IMWRITE_JPEG_QUALITY), 99])
|
@@ -44,14 +52,12 @@ class TryOnDiffusionClient:
|
|
44 |
avatar_sex: str = None,
|
45 |
background_image: np.ndarray = None,
|
46 |
background_prompt: str = None,
|
47 |
-
negative_prompt: str = None,
|
48 |
-
num_images: int = 1,
|
49 |
seed: int = -1,
|
50 |
raw_response: bool = False,
|
51 |
) -> TryOnDiffusionAPIResponse:
|
52 |
url = self._base_url + "/try-on-file"
|
53 |
|
54 |
-
request_data = {"
|
55 |
|
56 |
if clothing_image is not None:
|
57 |
request_data["clothing_image"] = self._image_to_upload_file(clothing_image)
|
@@ -74,16 +80,21 @@ class TryOnDiffusionClient:
|
|
74 |
if background_prompt is not None:
|
75 |
request_data["background_prompt"] = background_prompt
|
76 |
|
77 |
-
if negative_prompt is not None:
|
78 |
-
request_data["negative_prompt"] = negative_prompt
|
79 |
-
|
80 |
multipart_data = MultipartEncoder(fields=request_data)
|
81 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
82 |
try:
|
83 |
response = requests.post(
|
84 |
url,
|
85 |
data=multipart_data,
|
86 |
-
headers=
|
87 |
)
|
88 |
except Exception as e:
|
89 |
self._logger.error(e, exc_info=True)
|
|
|
2 |
import numpy as np
|
3 |
import requests
|
4 |
from requests_toolbelt.multipart.encoder import MultipartEncoder
|
5 |
+
from urllib.parse import urlparse
|
6 |
import logging
|
7 |
import json
|
8 |
from io import BytesIO
|
|
|
27 |
if self._base_url[-1] == "/":
|
28 |
self._base_url = self._base_url[:-1]
|
29 |
|
30 |
+
parsed_url = urlparse(self._base_url)
|
31 |
+
|
32 |
+
self._rapidapi_host = parsed_url.netloc if parsed_url.netloc.endswith(".rapidapi.com") else None
|
33 |
+
|
34 |
+
if self._rapidapi_host is not None:
|
35 |
+
self._logger.info(f"Using RapidAPI proxy: {self._rapidapi_host}")
|
36 |
+
|
37 |
@staticmethod
|
38 |
def _image_to_upload_file(image: np.ndarray) -> tuple:
|
39 |
_, jpeg_data = cv2.imencode(".jpg", image, [int(cv2.IMWRITE_JPEG_QUALITY), 99])
|
|
|
52 |
avatar_sex: str = None,
|
53 |
background_image: np.ndarray = None,
|
54 |
background_prompt: str = None,
|
|
|
|
|
55 |
seed: int = -1,
|
56 |
raw_response: bool = False,
|
57 |
) -> TryOnDiffusionAPIResponse:
|
58 |
url = self._base_url + "/try-on-file"
|
59 |
|
60 |
+
request_data = {"seed": str(seed)}
|
61 |
|
62 |
if clothing_image is not None:
|
63 |
request_data["clothing_image"] = self._image_to_upload_file(clothing_image)
|
|
|
80 |
if background_prompt is not None:
|
81 |
request_data["background_prompt"] = background_prompt
|
82 |
|
|
|
|
|
|
|
83 |
multipart_data = MultipartEncoder(fields=request_data)
|
84 |
|
85 |
+
headers = {"Content-Type": multipart_data.content_type}
|
86 |
+
|
87 |
+
if self._rapidapi_host is not None:
|
88 |
+
headers["X-RapidAPI-Key"] = self._api_key
|
89 |
+
headers["X-RapidAPI-Host"] = self._rapidapi_host
|
90 |
+
else:
|
91 |
+
headers["X-API-Key"] = self._api_key
|
92 |
+
|
93 |
try:
|
94 |
response = requests.post(
|
95 |
url,
|
96 |
data=multipart_data,
|
97 |
+
headers=headers,
|
98 |
)
|
99 |
except Exception as e:
|
100 |
self._logger.error(e, exc_info=True)
|