Spaces:
Running
Running
Amit
commited on
Commit
·
94db145
1
Parent(s):
46f47c4
Fix: Update docstring and Imagen 4.0 API usage
Browse files- services/gemini_client.py +21 -30
services/gemini_client.py
CHANGED
|
@@ -1,12 +1,11 @@
|
|
| 1 |
"""
|
| 2 |
Google Gemini client for Pip.
|
| 3 |
Handles: Text generation (emotion analysis, conversation) and image generation.
|
| 4 |
-
Uses
|
| 5 |
"""
|
| 6 |
|
| 7 |
import os
|
| 8 |
import json
|
| 9 |
-
import asyncio
|
| 10 |
from typing import Optional, AsyncGenerator
|
| 11 |
import google.generativeai as genai
|
| 12 |
from google.generativeai import types
|
|
@@ -333,50 +332,42 @@ Generate the enhanced image prompt only, no explanation."""
|
|
| 333 |
|
| 334 |
async def generate_image(self, prompt: str) -> Optional[str]:
|
| 335 |
"""
|
| 336 |
-
Generate an image using
|
| 337 |
-
Note: Imagen requires specific API access - this may not be available to all users.
|
| 338 |
Returns base64 encoded image or None.
|
| 339 |
"""
|
| 340 |
if not self.available:
|
| 341 |
return None
|
| 342 |
|
| 343 |
try:
|
| 344 |
-
#
|
| 345 |
-
|
| 346 |
|
| 347 |
-
|
| 348 |
-
|
| 349 |
-
|
| 350 |
-
|
| 351 |
-
lambda: imagen.generate_images(
|
| 352 |
-
prompt=prompt,
|
| 353 |
-
number_of_images=1,
|
| 354 |
-
aspect_ratio="1:1"
|
| 355 |
)
|
| 356 |
)
|
| 357 |
|
| 358 |
-
|
| 359 |
-
|
| 360 |
-
|
| 361 |
-
|
| 362 |
-
|
| 363 |
-
|
| 364 |
-
|
| 365 |
-
|
| 366 |
|
| 367 |
-
print("
|
| 368 |
return None
|
| 369 |
|
| 370 |
-
except ImportError:
|
| 371 |
-
print("Gemini: ImageGenerationModel not available in this version")
|
| 372 |
-
return None
|
| 373 |
except Exception as e:
|
| 374 |
error_str = str(e)
|
| 375 |
if "429" in error_str or "quota" in error_str.lower():
|
| 376 |
-
print(f"
|
| 377 |
elif "not found" in error_str.lower() or "not supported" in error_str.lower():
|
| 378 |
-
print(f"
|
| 379 |
return None
|
| 380 |
else:
|
| 381 |
-
print(f"
|
| 382 |
-
|
|
|
|
| 1 |
"""
|
| 2 |
Google Gemini client for Pip.
|
| 3 |
Handles: Text generation (emotion analysis, conversation) and image generation.
|
| 4 |
+
Uses gemini-flash-lite-latest for text, imagen-4.0-fast-generate-001 for images.
|
| 5 |
"""
|
| 6 |
|
| 7 |
import os
|
| 8 |
import json
|
|
|
|
| 9 |
from typing import Optional, AsyncGenerator
|
| 10 |
import google.generativeai as genai
|
| 11 |
from google.generativeai import types
|
|
|
|
| 332 |
|
| 333 |
async def generate_image(self, prompt: str) -> Optional[str]:
|
| 334 |
"""
|
| 335 |
+
Generate an image using Imagen 4.0.
|
|
|
|
| 336 |
Returns base64 encoded image or None.
|
| 337 |
"""
|
| 338 |
if not self.available:
|
| 339 |
return None
|
| 340 |
|
| 341 |
try:
|
| 342 |
+
# Use GenerativeModel with Imagen 4.0
|
| 343 |
+
imagen_model = genai.GenerativeModel(self.IMAGE_MODEL)
|
| 344 |
|
| 345 |
+
response = await imagen_model.generate_content_async(
|
| 346 |
+
prompt,
|
| 347 |
+
generation_config=types.GenerationConfig(
|
| 348 |
+
temperature=1.0,
|
|
|
|
|
|
|
|
|
|
|
|
|
| 349 |
)
|
| 350 |
)
|
| 351 |
|
| 352 |
+
# Check for image in response
|
| 353 |
+
if response.candidates:
|
| 354 |
+
for candidate in response.candidates:
|
| 355 |
+
if hasattr(candidate, 'content') and candidate.content.parts:
|
| 356 |
+
for part in candidate.content.parts:
|
| 357 |
+
if hasattr(part, 'inline_data') and part.inline_data:
|
| 358 |
+
# Return base64 encoded image
|
| 359 |
+
return base64.b64encode(part.inline_data.data).decode('utf-8')
|
| 360 |
|
| 361 |
+
print("Imagen: No image in response")
|
| 362 |
return None
|
| 363 |
|
|
|
|
|
|
|
|
|
|
| 364 |
except Exception as e:
|
| 365 |
error_str = str(e)
|
| 366 |
if "429" in error_str or "quota" in error_str.lower():
|
| 367 |
+
print(f"Imagen rate limit exceeded: {e}")
|
| 368 |
elif "not found" in error_str.lower() or "not supported" in error_str.lower():
|
| 369 |
+
print(f"Imagen not available: {e}")
|
| 370 |
return None
|
| 371 |
else:
|
| 372 |
+
print(f"Imagen generation error: {e}")
|
| 373 |
+
return None
|