import os
import time
import requests
# 1. Set up the API key and base URL
API_KEY = os.getenv("FASHN_API_KEY")
assert API_KEY, "Please set the FASHN_API_KEY environment variable."
BASE_URL = "https://api.fashn.ai/v1"
# 2. POST request to /run
input_data = {
"model_image": "https://v3.fal.media/files/panda/jRavCEb1D4OpZBjZKxaH7_image_2024-12-08_18-37-27%20Large.jpeg",
"garment_image": "https://v3.fal.media/files/elephant/qXMQpeM6fVOlg7bZs0dEh_fashn-tshirt-2.png",
"category": "tops",
}
headers = {"Content-Type": "application/json", "Authorization": f"Bearer {API_KEY}"}
run_response = requests.post(f"{BASE_URL}/run", json=input_data, headers=headers)
run_data = run_response.json()
prediction_id = run_data.get("id")
print("Prediction started, ID:", prediction_id)
# 3. Poll /status/<ID>
while True:
status_response = requests.get(f"{BASE_URL}/status/{prediction_id}", headers=headers)
status_data = status_response.json()
if status_data["status"] == "completed":
print("Prediction completed.")
# 4. The "output" field contains the final image URLs
print(status_data["output"])
break
elif status_data["status"] in ["starting", "in_queue", "processing"]:
print("Prediction status:", status_data["status"])
time.sleep(3)
else:
print("Prediction failed:", status_data.get("error"))
break