图片生成非流式响应处理器
| 535 | |
| 536 | |
| 537 | class ImageCollectProcessor(BaseProcessor): |
| 538 | """图片生成非流式响应处理器""" |
| 539 | |
| 540 | def __init__( |
| 541 | self, |
| 542 | model: str, |
| 543 | token: str = "", |
| 544 | response_format: str = "b64_json", |
| 545 | ): |
| 546 | super().__init__(model, token) |
| 547 | self.response_format = (response_format or "b64_json").lower() |
| 548 | |
| 549 | async def process(self, response: AsyncIterable[bytes]) -> List[str]: |
| 550 | """处理并收集图片""" |
| 551 | images = [] |
| 552 | |
| 553 | try: |
| 554 | async for line in response: |
| 555 | if not line: |
| 556 | continue |
| 557 | try: |
| 558 | data = orjson.loads(line) |
| 559 | except orjson.JSONDecodeError: |
| 560 | continue |
| 561 | |
| 562 | resp = data.get("result", {}).get("response", {}) |
| 563 | |
| 564 | if mr := resp.get("modelResponse"): |
| 565 | if urls := mr.get("generatedImageUrls"): |
| 566 | for url in urls: |
| 567 | if self.response_format == "url": |
| 568 | processed = await self.process_url(url, "image") |
| 569 | if processed: |
| 570 | images.append(processed) |
| 571 | continue |
| 572 | dl_service = self._get_dl() |
| 573 | base64_data = await dl_service.to_base64(url, self.token, "image") |
| 574 | if base64_data: |
| 575 | if "," in base64_data: |
| 576 | b64 = base64_data.split(",", 1)[1] |
| 577 | else: |
| 578 | b64 = base64_data |
| 579 | images.append(b64) |
| 580 | |
| 581 | except Exception as e: |
| 582 | logger.error(f"Image collect processing error: {e}") |
| 583 | finally: |
| 584 | await self.close() |
| 585 | |
| 586 | return images |
| 587 | |
| 588 | |
| 589 | __all__ = [ |
no outgoing calls
no test coverage detected