Datasets:
EntropyDrop
feat: add Nano Banana Pro image API support and extract common image API utilities
72b170b | import os | |
| import argparse | |
| import requests | |
| from image_api_utils import ( | |
| IMAGE_API_BASE_URL, | |
| API_KEY, | |
| get_auth_headers, | |
| upload_file_to_s3, | |
| delete_file_from_s3, | |
| poll_task_status, | |
| download_image, | |
| run_media_generation_pipeline | |
| ) | |
| MODEL_NAME = "gemini-3-pro-image-preview" | |
| def generate_image(s3_urls=None, prompt="", aspect_ratio="1:1", image_size="2K", notify_url=None): | |
| """ | |
| Triggers the image generation task using the Nano Banana Pro (gemini-3-pro-image-preview) model. | |
| :param s3_urls: List of reference image URLs (up to 14) for img2img, or None for txt2img. | |
| :param prompt: Text prompt describing the desired image content. | |
| :param aspect_ratio: Image aspect ratio. Allowed: "1:1", "2:3", "3:2", "3:4", "4:3", "4:5", "5:4", "9:16", "16:9", "21:9" | |
| :param image_size: Image resolution/quality. Allowed: "1K", "2K", "4K" | |
| :param notify_url: Optional webhook URL for async callback. | |
| :return: task_id string/int | |
| """ | |
| url = f"{IMAGE_API_BASE_URL}/v1/media/generate" | |
| params = { | |
| "aspectRatio": aspect_ratio, | |
| "imageSize": image_size | |
| } | |
| if s3_urls: | |
| if isinstance(s3_urls, str): | |
| s3_urls = [s3_urls] | |
| params["images"] = s3_urls | |
| payload = { | |
| "model": MODEL_NAME, | |
| "params": params, | |
| "prompt": prompt | |
| } | |
| if notify_url: | |
| payload["notify_url"] = notify_url | |
| headers = get_auth_headers(api_key=API_KEY) | |
| print(f"[*] Sending Banana Pro ({MODEL_NAME}) generation request to: {url}") | |
| response = requests.post(url, json=payload, headers=headers) | |
| response.raise_for_status() | |
| res_json = response.json() | |
| # Try parsing task_id from root or nested "data" dictionary | |
| task_id = res_json.get("task_id") | |
| if not task_id and "data" in res_json and isinstance(res_json["data"], dict): | |
| task_id = res_json["data"].get("task_id") | |
| if not task_id: | |
| raise ValueError(f"Failed to obtain task_id from response: {res_json}") | |
| print(f"[+] Task created successfully. Task ID: {task_id}") | |
| return task_id | |
| def generate_txt2img(prompt, output_path=None, aspect_ratio="1:1", image_size="2K", notify_url=None): | |
| """ | |
| Text-to-image API for Nano Banana Pro (gemini-3-pro-image-preview). | |
| """ | |
| def task_fn(_): | |
| return generate_image( | |
| s3_urls=None, | |
| prompt=prompt, | |
| aspect_ratio=aspect_ratio, | |
| image_size=image_size, | |
| notify_url=notify_url | |
| ) | |
| return run_media_generation_pipeline( | |
| generate_task_fn=task_fn, | |
| local_image_paths=None, | |
| output_path=output_path, | |
| default_prefix="banana_pro_result" | |
| ) | |
| def generate_img2img(local_image_paths, prompt, output_path=None, aspect_ratio="1:1", image_size="2K", notify_url=None): | |
| """ | |
| Image-to-image API for Nano Banana Pro (gemini-3-pro-image-preview). | |
| """ | |
| def task_fn(s3_urls): | |
| return generate_image( | |
| s3_urls=s3_urls, | |
| prompt=prompt, | |
| aspect_ratio=aspect_ratio, | |
| image_size=image_size, | |
| notify_url=notify_url | |
| ) | |
| return run_media_generation_pipeline( | |
| generate_task_fn=task_fn, | |
| local_image_paths=local_image_paths, | |
| output_path=output_path, | |
| default_prefix="banana_pro_result" | |
| ) | |
| if __name__ == '__main__': | |
| parser = argparse.ArgumentParser(description="Generate image using Gemini 3 Pro (Nano Banana Pro) model.") | |
| parser.add_argument("prompt", help="Text prompt for image generation.") | |
| parser.add_argument("-i", "--images", nargs="+", help="Local reference image path(s) for img2img (up to 14).") | |
| parser.add_argument("-o", "--output", help="Output path for the generated image.") | |
| parser.add_argument("-a", "--aspect-ratio", default="1:1", | |
| choices=["1:1", "2:3", "3:2", "3:4", "4:3", "4:5", "5:4", "9:16", "16:9", "21:9"], | |
| help="Aspect ratio (default: 1:1).") | |
| parser.add_argument("-s", "--image-size", default="2K", choices=["1K", "2K", "4K"], help="Resolution size (default: 2K).") | |
| args = parser.parse_args() | |
| if args.images: | |
| generate_img2img( | |
| local_image_paths=args.images, | |
| prompt=args.prompt, | |
| output_path=args.output, | |
| aspect_ratio=args.aspect_ratio, | |
| image_size=args.image_size | |
| ) | |
| else: | |
| generate_txt2img( | |
| prompt=args.prompt, | |
| output_path=args.output, | |
| aspect_ratio=args.aspect_ratio, | |
| image_size=args.image_size | |
| ) | |