()
| 11 | |
| 12 | |
| 13 | def main(): |
| 14 | parser = argparse.ArgumentParser(description="Call /v1/tasks/image/sync with base64 image inputs.") |
| 15 | parser.add_argument("--url", type=str, default="http://127.0.0.1:8000", help="Server base url") |
| 16 | parser.add_argument("--prompt", type=str, required=True, help="Prompt text") |
| 17 | parser.add_argument("--negative_prompt", type=str, default=None, help="Negative prompt text") |
| 18 | parser.add_argument("--seed", type=int, default=None, help="Random seed") |
| 19 | parser.add_argument("--aspect_ratio", type=str, default=None, help="Aspect ratio for image task") |
| 20 | parser.add_argument("--timeout_seconds", type=int, default=600, help="Sync API timeout_seconds") |
| 21 | parser.add_argument("--poll_interval_seconds", type=float, default=0.5, help="Sync API poll_interval_seconds") |
| 22 | parser.add_argument("--save_result_path", type=str, default=None, help="Server-side save_result_path") |
| 23 | parser.add_argument("--output", type=str, default="sync_result.png", help="Local output image path") |
| 24 | |
| 25 | # Base64 inputs (preferred) |
| 26 | parser.add_argument("--image_base64", type=str, default="", help="Base64 content for image_path") |
| 27 | parser.add_argument("--image_mask_base64", type=str, default="", help="Base64 content for image_mask_path") |
| 28 | |
| 29 | # Optional local files to convert to base64 |
| 30 | parser.add_argument("--image_path", type=str, default="", help="Local image file path; encoded if image_base64 is empty") |
| 31 | parser.add_argument("--image_mask_path", type=str, default="", help="Local mask file path; encoded if image_mask_base64 is empty") |
| 32 | |
| 33 | args = parser.parse_args() |
| 34 | |
| 35 | image_base64 = args.image_base64 |
| 36 | if not image_base64: |
| 37 | if not args.image_path: |
| 38 | raise ValueError("Either --image_base64 or --image_path must be provided") |
| 39 | image_base64 = image_file_to_base64(args.image_path) |
| 40 | |
| 41 | image_mask_base64 = args.image_mask_base64 |
| 42 | if not image_mask_base64 and args.image_mask_path: |
| 43 | image_mask_base64 = image_file_to_base64(args.image_mask_path) |
| 44 | |
| 45 | payload = { |
| 46 | "prompt": args.prompt, |
| 47 | "negative_prompt": args.negative_prompt, |
| 48 | "image_path": image_base64, |
| 49 | "seed": args.seed, |
| 50 | "aspect_ratio": args.aspect_ratio, |
| 51 | "save_result_path": args.save_result_path, |
| 52 | } |
| 53 | payload = {key: value for key, value in payload.items() if value is not None} |
| 54 | if image_mask_base64: |
| 55 | payload["image_mask_path"] = image_mask_base64 |
| 56 | |
| 57 | endpoint = f"{args.url.rstrip('/')}/v1/tasks/image/sync?timeout_seconds={args.timeout_seconds}&poll_interval_seconds={args.poll_interval_seconds}" |
| 58 | resp = requests.post(endpoint, json=payload, timeout=args.timeout_seconds + 30) |
| 59 | |
| 60 | if resp.status_code != 200: |
| 61 | raise RuntimeError(f"Request failed ({resp.status_code}): {resp.text}") |
| 62 | |
| 63 | output_path = Path(args.output) |
| 64 | output_path.parent.mkdir(parents=True, exist_ok=True) |
| 65 | output_path.write_bytes(resp.content) |
| 66 | print(f"Saved result to: {output_path}") |
| 67 | |
| 68 | |
| 69 | if __name__ == "__main__": |
| 70 | main() |
no test coverage detected