import os
import time
import requests
# 1. Set up the API key and base URL
API_KEY = os.getenv("AYNA_API_KEY")
assert API_KEY, "Please set the AYNA_API_KEY environment variable."
BASE_URL = "https://api.getayna.com/v1"
# 2. POST request to /try-on/run
input_data = {
"model": "ayna-2.0-preview",
"input": {
"model_image": "YOUR_MODEL_IMAGE_URL_OR_BASE64_HERE",
"garment_image": "YOUR_GARMENT_IMAGE_URL_OR_BASE64_HERE",
"category": "auto"
}
}
headers = {"Content-Type": "application/json", "Authorization": f"Bearer {API_KEY}"}
run_response = requests.post(f"{BASE_URL}/try-on/run", json=input_data, headers=headers)
run_data = run_response.json()
# Check if the request was successful
if run_data.get("status") == "failed":
print("Request failed:", run_data.get("error"))
exit(1)
polling_url = run_data.get("pollingUrl")
print("Generation started, polling URL:", polling_url)
# 3. Poll the status endpoint using the polling URL
while True:
status_response = requests.get(polling_url, headers=headers)
status_data = status_response.json()
if status_data["status"] == "completed":
print("Generation completed.")
# 4. The "output" field contains the final image URL
print("Result image:", status_data["output"]["image"])
break
elif status_data["status"] in ["queued", "processing"]:
print("Generation status:", status_data["status"])
time.sleep(3)
elif status_data["status"] == "failed":
print("Generation failed:", status_data.get("error"))
break
else:
print("Unknown status:", status_data["status"])
break