Fix Google image generation: model, response modality, data extraction
- NanoBanana Pro: gemini-2.0-flash-exp-image → gemini-3.1-flash-image-preview - Add response_modalities=["IMAGE","TEXT"] so Gemini returns image data - Replace part.as_image() with base64.b64decode(part.inline_data.data) Co-Authored-By: Claude Sonnet 4.6 <noreply@anthropic.com>
This commit is contained in:
parent
19222ab36d
commit
bbaef76fa7
1 changed files with 11 additions and 4 deletions
|
|
@ -169,19 +169,26 @@ class ImageGenerationService:
|
|||
self, prompt: str, output_directory: str, model: str
|
||||
) -> str:
|
||||
"""Base method for Google image generation models."""
|
||||
from google.genai import types
|
||||
|
||||
client = genai.Client()
|
||||
response = await asyncio.to_thread(
|
||||
client.models.generate_content,
|
||||
model=model,
|
||||
contents=[prompt],
|
||||
config=types.GenerateContentConfig(
|
||||
response_modalities=["IMAGE", "TEXT"],
|
||||
),
|
||||
)
|
||||
|
||||
image_path = None
|
||||
for part in response.candidates[0].content.parts:
|
||||
if part.inline_data is not None:
|
||||
image = part.as_image()
|
||||
image_data = base64.b64decode(part.inline_data.data)
|
||||
image_path = os.path.join(output_directory, f"{uuid.uuid4()}.jpg")
|
||||
image.save(image_path)
|
||||
with open(image_path, "wb") as f:
|
||||
f.write(image_data)
|
||||
break
|
||||
|
||||
if not image_path:
|
||||
raise HTTPException(
|
||||
|
|
@ -201,9 +208,9 @@ class ImageGenerationService:
|
|||
async def generate_image_nanobanana_pro(
|
||||
self, prompt: str, output_directory: str
|
||||
) -> str:
|
||||
"""Generate image using NanoBanana Pro (gemini-2.0-flash-exp-image)."""
|
||||
"""Generate image using NanoBanana Pro (gemini-3.1-flash-image-preview)."""
|
||||
return await self._generate_image_google(
|
||||
prompt, output_directory, "gemini-2.0-flash-exp-image"
|
||||
prompt, output_directory, "gemini-3.1-flash-image-preview"
|
||||
)
|
||||
|
||||
async def get_image_from_pexels(self, prompt: str) -> str:
|
||||
|
|
|
|||
Loading…
Add table
Reference in a new issue