| 17 | |
| 18 | |
| 19 | class APIImageGenServing(VLMServingABC): |
| 20 | API_FORMAT_OPENAI = "openai" |
| 21 | API_FORMAT_GEMINI = "gemini" |
| 22 | |
| 23 | def __init__( |
| 24 | self, |
| 25 | api_url: str, |
| 26 | image_io, |
| 27 | Image_gen_task: str = "text2image", |
| 28 | batch_size: int = 4, |
| 29 | timeout: int = 300, |
| 30 | connect_timeout: int = 30, |
| 31 | api_format: str = "openai", |
| 32 | api_key: Optional[str] = None, |
| 33 | model_name: str = "dall-e-3", |
| 34 | ): |
| 35 | """ |
| 36 | :param api_url: Base URL of the cloud API (e.g. "https://api.openai.com/v1") |
| 37 | :param image_io: ImageIO instance, for saving generated images |
| 38 | :param Image_gen_task: Task type, "text2image" or "imageedit" |
| 39 | :param batch_size: Batch size |
| 40 | :param timeout: Request timeout (seconds) |
| 41 | :param api_format: API format type, "openai" or "gemini" (default is "openai") |
| 42 | :param api_key: API key (directly from parameters, not from environment variables) |
| 43 | :param model_name: Model name (OpenAI: "dall-e-3", Gemini: "gemini-2.5-flash-image", "gemini-3-pro-image", etc. (default is "dall-e-3")) |
| 44 | """ |
| 45 | self.api_url = api_url.rstrip("/") |
| 46 | self.image_io = image_io |
| 47 | self.image_gen_task = Image_gen_task |
| 48 | self.batch_size = batch_size |
| 49 | self.timeout = timeout |
| 50 | self.connect_timeout = connect_timeout |
| 51 | self.api_format = api_format |
| 52 | self.model_name = model_name |
| 53 | self.logger = get_logger() |
| 54 | |
| 55 | self.api_key = api_key |
| 56 | |
| 57 | if not self.api_key: |
| 58 | self.logger.warning("API key not provided. Some APIs may require authentication.") |
| 59 | |
| 60 | if api_format == "gemini": |
| 61 | if not GEMINI_AVAILABLE: |
| 62 | raise ImportError( |
| 63 | "google.genai library is required for Gemini API. " |
| 64 | "Please install it: pip install google-genai" |
| 65 | ) |
| 66 | |
| 67 | if not self.api_key: |
| 68 | raise ValueError("Gemini API key is required! Please provide it via --api_key parameter.") |
| 69 | |
| 70 | if self.api_url: |
| 71 | http_options = types.HttpOptions( |
| 72 | base_url=self.api_url, |
| 73 | timeout=None |
| 74 | ) |
| 75 | else: |
| 76 | http_options = types.HttpOptions(timeout=None) |