| 1 | import os |
| 2 | import time |
| 3 | import requests |
| 4 | |
| 5 | BASE = "https://api.apimarketplace.com" |
| 6 | headers = {"Authorization": "Bearer " + os.environ["API_KEY"]} |
| 7 | |
| 8 | def call(method, path, **kwargs): |
| 9 | response = requests.request(method, BASE + path, headers=headers, timeout=300, **kwargs) |
| 10 | response.raise_for_status() |
| 11 | return response.json() if response.content else None |
| 12 | |
| 13 | def wait(path, success): |
| 14 | deadline = time.monotonic() + 900 |
| 15 | while time.monotonic() < deadline: |
| 16 | result = call("GET", path) |
| 17 | if result["status"] == success: |
| 18 | return result |
| 19 | if result["status"] == "failed": |
| 20 | raise RuntimeError(result.get("errorMessage") or "Generation failed") |
| 21 | time.sleep(3) |
| 22 | raise TimeoutError("Still pending; keep the returned ID and check later") |
| 23 | |
| 24 | character = call("POST", "/v1/characters", json={'name': 'Example character', |
| 25 | 'model_alias': 'photoreal-v1', |
| 26 | 'subject': 'A 30-year-old adult with brown eyes and short brown hair', |
| 27 | 'action': 'Wearing a blue shirt, facing the camera', |
| 28 | 'environment': 'A plain studio'}) |
| 29 | print("Character ID:", character["id"]) |
| 30 | wait("/v1/characters/" + character["id"], "ready") |
| 31 | payload = {'model_alias': 'photoreal-v1', |
| 32 | 'action': 'Wearing a green jacket, standing by a window', |
| 33 | 'environment': 'A modern apartment in soft morning light', |
| 34 | 'aspect_ratio': '2:3'} |
| 35 | payload["character_id"] = character["id"] |
| 36 | generation = call("POST", "/v1/generations", json=payload) |
| 37 | print("Generation ID:", generation["id"]) |
| 38 | completed = wait("/v1/generations/" + generation["id"], "completed") |
| 39 | print("Temporary output URL:", completed["outputUrl"]) |