feat: Convert `google.genai.types.Image` to `PIL.Image` for Gradio display using `io.BytesIO`.
Browse files
app.py
CHANGED
|
@@ -117,7 +117,18 @@ The output must be a single image that looks like the completed infographic, mai
|
|
| 117 |
# Extract and return the generated image
|
| 118 |
for part in response.parts:
|
| 119 |
if image := part.as_image():
|
| 120 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 121 |
|
| 122 |
raise gr.Error("The model completed but did not return an image. Check inputs or safety filters.")
|
| 123 |
|
|
|
|
| 117 |
# Extract and return the generated image
|
| 118 |
for part in response.parts:
|
| 119 |
if image := part.as_image():
|
| 120 |
+
# Convert google.genai.types.Image to PIL Image for Gradio
|
| 121 |
+
try:
|
| 122 |
+
# Try saving to a bytes buffer
|
| 123 |
+
img_buffer = io.BytesIO()
|
| 124 |
+
image.save(img_buffer)
|
| 125 |
+
img_buffer.seek(0)
|
| 126 |
+
return Image.open(img_buffer)
|
| 127 |
+
except Exception:
|
| 128 |
+
# Fallback: if save() doesn't accept buffer, try accessing bytes directly if possible
|
| 129 |
+
if hasattr(image, '_image_bytes'):
|
| 130 |
+
return Image.open(io.BytesIO(image._image_bytes))
|
| 131 |
+
raise
|
| 132 |
|
| 133 |
raise gr.Error("The model completed but did not return an image. Check inputs or safety filters.")
|
| 134 |
|