Spaces:
Sleeping
Sleeping
hmmmm
Browse files
tools/image_generation_tools.py
CHANGED
|
@@ -88,8 +88,10 @@ class GenerateImageTool(Tool):
|
|
| 88 |
"""
|
| 89 |
try:
|
| 90 |
# Правильный импорт библиотеки
|
| 91 |
-
|
| 92 |
-
from google.
|
|
|
|
|
|
|
| 93 |
|
| 94 |
# Get API key from environment variable
|
| 95 |
api_key = os.environ.get("GEMINI_API_KEY")
|
|
@@ -98,8 +100,8 @@ class GenerateImageTool(Tool):
|
|
| 98 |
return "<p>Error: Gemini API key not found. Please set the GEMINI_API_KEY environment variable.</p>"
|
| 99 |
|
| 100 |
logger.info("Initializing Gemini client")
|
| 101 |
-
|
| 102 |
-
client = genai.Client()
|
| 103 |
|
| 104 |
logger.info("Generating image with Gemini")
|
| 105 |
response = client.models.generate_content(
|
|
@@ -114,10 +116,17 @@ class GenerateImageTool(Tool):
|
|
| 114 |
for part in response.candidates[0].content.parts:
|
| 115 |
if part.text is not None:
|
| 116 |
logger.info(f"Gemini response text: {part.text[:100]}...")
|
| 117 |
-
elif
|
| 118 |
-
#
|
| 119 |
-
|
| 120 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 121 |
img_html = f'<img src="data:image/png;base64,{image_b64}" alt="Generated image based on song analysis" style="max-width:100%; border-radius:10px; box-shadow:0 4px 8px rgba(0,0,0,0.1);">'
|
| 122 |
return img_html
|
| 123 |
|
|
|
|
| 88 |
"""
|
| 89 |
try:
|
| 90 |
# Правильный импорт библиотеки
|
| 91 |
+
from google import genai
|
| 92 |
+
from google.genai import types
|
| 93 |
+
from io import BytesIO
|
| 94 |
+
import base64
|
| 95 |
|
| 96 |
# Get API key from environment variable
|
| 97 |
api_key = os.environ.get("GEMINI_API_KEY")
|
|
|
|
| 100 |
return "<p>Error: Gemini API key not found. Please set the GEMINI_API_KEY environment variable.</p>"
|
| 101 |
|
| 102 |
logger.info("Initializing Gemini client")
|
| 103 |
+
# Новый способ настройки клиента
|
| 104 |
+
client = genai.Client(api_key=api_key)
|
| 105 |
|
| 106 |
logger.info("Generating image with Gemini")
|
| 107 |
response = client.models.generate_content(
|
|
|
|
| 116 |
for part in response.candidates[0].content.parts:
|
| 117 |
if part.text is not None:
|
| 118 |
logger.info(f"Gemini response text: {part.text[:100]}...")
|
| 119 |
+
elif part.inline_data is not None:
|
| 120 |
+
# Извлекаем данные изображения
|
| 121 |
+
image_data = part.inline_data.data
|
| 122 |
+
|
| 123 |
+
# Преобразуем в base64 для HTML, если нужно
|
| 124 |
+
if isinstance(image_data, bytes):
|
| 125 |
+
image_b64 = base64.b64encode(image_data).decode('utf-8')
|
| 126 |
+
else:
|
| 127 |
+
# Если данные уже в base64
|
| 128 |
+
image_b64 = image_data
|
| 129 |
+
|
| 130 |
img_html = f'<img src="data:image/png;base64,{image_b64}" alt="Generated image based on song analysis" style="max-width:100%; border-radius:10px; box-shadow:0 4px 8px rgba(0,0,0,0.1);">'
|
| 131 |
return img_html
|
| 132 |
|