You can interact with the service using any HTTP client (e.g., requests, httpx, curl). The service provides endpoints for swap, add_face, and enhance_face functionality.
1. Submit a request
First, load your image as bytes, then send a POST request to the desired endpoint. For example, to use add-face:
import requests
# load images from disc
with open("myimage.jpg", "rb") as image:
my_image = image.read()
submit = requests.post(
"http://localhost:8020/add-face",
files={"image": ("face.jpg", my_image, "image/jpeg")},
data={"face_name": "biden"},
)
my_job = submit.json()
2. Poll for results and parse the response
Use the job_id from the initial response to poll the /status/{job_id} endpoint. Once the job is finished, the response will contain the result (a media payload) and metrics.
import requests
# check status of job
status = requests.get(f"http://localhost:8020/status/{my_job['job_id']}").json()
# Access execution metrics
metrics = status.get("metrics", {})
print("execution_time_s:", metrics.get("execution_time_s"))
# Retrieve the result image
result = status.get("result", {})
image_url = result.get("content")
if image_url:
image_bytes = requests.get(image_url, timeout=60).content
with open("swapped_img.png", "wb") as f:
f.write(image_bytes)
import requests
# 1. Submit request
with open("myimage.jpg", "rb") as image:
my_image = image.read()
submit = requests.post(
"http://localhost:8020/add-face",
files={"image": ("face.jpg", my_image, "image/jpeg")},
data={"face_name": "biden"},
)
my_job = submit.json()
# 2. Poll status
status = requests.get(f"http://localhost:8020/status/{my_job['job_id']}").json()
# 3. Parse result
result = status.get("result", {})
image_url = result.get("content")
if image_url:
image_bytes = requests.get(image_url, timeout=60).content
with open("swapped_img.png", "wb") as f:
f.write(image_bytes)