vk_chat_bot/ai/tools/image_generation/generate_image.py
Kirill Kirilenko 4b265b5405 Устранено дублирование кода в AiAgent.
Добавлена возможность пересылки сообщений в личных чатах.
Обновлены зависимости.
Добавлен requirements.txt.
Исправлены предупреждения PyCharm 2026.1.
2026-04-07 01:01:34 +03:00

80 lines
3.1 KiB
Python
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

from typing import Any, Dict, List
from openrouter.components import ChatToolMessageContentTypedDict
from fal_client import AsyncClient as FalClient
from ai.tool import Tool
from ai.utils import *
FAL_MODEL = "fal-ai/bytedance/seedream/v4.5/text-to-image"
class GenerateImageTool(Tool):
def __init__(self, fal_token: str):
self._client = FalClient(key=fal_token)
@property
def name(self) -> str:
return "generate_image"
@property
def description(self) -> str:
return "Генерация изображения по описанию"
@property
def parameters(self) -> Dict[str, Any]:
return {
"type": "object",
"properties": {
"prompt": {
"type": "string",
"description": "Подробное описание сцены на английском языке БЕЗ технических параметров "
"(соотношение сторон, разрешение)"
},
"aspect_ratio": {
"type": "string",
"enum": ["1:1", "4:3", "3:4", "16:9", "9:16"],
"description": "Соотношение сторон"
}
},
"required": ["prompt"]
}
async def execute(self, args: Dict[str, Any], artifacts: Dict[str, Any]) -> List[ChatToolMessageContentTypedDict]:
prompt = args.get("prompt", "")
aspect_ratio = args.get("aspect_ratio", "4:3")
aspect_ratio_size_map = {
"1:1": "square",
"4:3": "landscape_4_3",
"3:4": "portrait_4_3",
"16:9": "landscape_16_9",
"9:16": "portrait_16_9",
"9:20": "portrait_16_9"
}
image_size = aspect_ratio_size_map.get(aspect_ratio, "landscape_4_3")
print(f"Генерация изображения {image_size}: {prompt}")
arguments = {
"prompt": prompt,
"image_size": image_size,
"enable_safety_checker": False
}
try:
result = await self._client.run(FAL_MODEL, arguments=arguments)
if "images" not in result:
raise RuntimeError("Неожиданный ответ от сервера.")
image_url = result["images"][0]["url"]
from utils import download_file
artifacts["generated_image_hires"] = await download_file(image_url)
artifacts["generated_image"] = compress_image(artifacts["generated_image_hires"], 1280)
return serialize_message_content(
text="Изображение сгенерировано и будет показано пользователю.",
image=None
)
except Exception as e:
print(f"Ошибка генерации изображения: {e}")
return serialize_message_content(text=f"Не удалось сгенерировать изображение: {e}")