Compare commits
2 Commits
28c8fb103e
...
e79b191463
| Author | SHA1 | Date | |
|---|---|---|---|
| e79b191463 | |||
| 8fd9f9b8c8 |
@@ -317,8 +317,9 @@ async def generate_image(
|
||||
char: Character = await dao.chars.get_character(char_id)
|
||||
|
||||
# Начинаем список с фото персонажа
|
||||
media_group_bytes = [await bot.download(char.character_image_doc_tg_id)]
|
||||
|
||||
file_byes = await bot.download(char.character_image_doc_tg_id)
|
||||
media_group_bytes = [file_byes.read()]
|
||||
file_byes.close()
|
||||
if media:
|
||||
# Скачиваем файлы
|
||||
# tasks вернут список объектов BytesIO
|
||||
@@ -345,18 +346,22 @@ async def generate_image(
|
||||
quality=Quality[data['quality']],
|
||||
)
|
||||
|
||||
|
||||
images = []
|
||||
if generated_images_io:
|
||||
for img_io in generated_images_io:
|
||||
# Читаем байты
|
||||
content = img_io.read()
|
||||
|
||||
# Сразу закрываем поток от адаптера, освобождая память
|
||||
img_io.close()
|
||||
|
||||
images.append(
|
||||
BufferedInputFile(
|
||||
img_io.read(),
|
||||
content,
|
||||
filename=f"img_{random.randint(1000, 9999)}.png"
|
||||
)
|
||||
)
|
||||
# Важно: img_io здесь тоже BytesIO. После отправки aiogram закроет его сам,
|
||||
# либо он удалится GC. Но если список generated_images_io большой,
|
||||
# он висит в памяти до выхода из функции.
|
||||
|
||||
return images
|
||||
|
||||
@@ -367,8 +372,13 @@ async def handle_text(message: Message, gemini: GoogleAdapter, state: FSMContext
|
||||
await wait_msg.edit_text(await gen_start_text(message=message, gemini=gemini, state=state, dao=dao, bot=bot))
|
||||
|
||||
|
||||
async def gen_start_text(message: Message, gemini: GoogleAdapter, state: FSMContext, dao: DAO, bot: Bot) -> str:
|
||||
async def gen_start_text(message: Message, gemini: GoogleAdapter, state: FSMContext, dao: DAO, bot: Bot,
|
||||
char_id: str = None) -> str:
|
||||
await bot.send_chat_action(message.chat.id, "typing")
|
||||
prompt = message.text
|
||||
if char_id:
|
||||
char = await dao.chars.get_character(message.chat.id)
|
||||
prompt += char.character_bio
|
||||
result = await asyncio.to_thread(gemini.generate_text, prompt=message.text)
|
||||
if result:
|
||||
return result
|
||||
|
||||
Reference in New Issue
Block a user