Testing text2img command (will be replaced, only for tests); added exception handling, queue and database table with generated images

This commit is contained in:
BarsTiger
2023-02-20 16:41:55 +02:00
parent 3bd34009ae
commit d56f1d386f
21 changed files with 219 additions and 34 deletions

View File

@@ -1,6 +1,6 @@
from bot.common import dp
from .txt2img import *
from .txt2img import txt2img_comand
def register():
dp.register_message_handler(txt2img, commands='txt2img')
dp.register_message_handler(txt2img.txt2img_comand, commands='txt2img')

View File

@@ -2,25 +2,50 @@ from aiogram import types
from bot.db import db, DBTables
from bot.utils.cooldown import throttle
from bot.modules.api.txt2img import txt2img
from bot.modules.api.objects.prompt_request import Prompt
from bot.modules.api.status import wait_for_status
from bot.keyboards.exception import get_exception_keyboard
from bot.utils.trace_exception import PrettyException
from aiohttp import ClientConnectorError
@throttle(cooldown=30, admin_ids=db[DBTables.config].get('admins'))
async def txt2img_comand(message: types.Message):
temp_message = await message.reply("Generating image...")
temp_message = await message.reply("Enqueued...")
if not message.get_args():
await temp_message.edit_text("Specify prompt for this command. Check /help txt2img")
await temp_message.edit_text("😶‍🌫️ Specify prompt for this command. Check /help txt2img")
return
try:
image = await txt2img(message.get_args())
await message.reply_photo(photo=image[0], caption=str(
image[1]["infotexts"][0]))
except Exception as e:
assert e
await message.reply("We ran into error while processing your request. StableDiffusion models may not be "
"configured on specified endpoint or server with StableDiffusion may be turned "
"off. Ask admins of this bot instance if you have contacts for further info")
db[DBTables.queue]['n'] = db[DBTables.queue].get('n', 0) + 1
await temp_message.edit_text(f"⏳ Enqueued in position {db[DBTables.queue].get('n', 0)}...")
await wait_for_status()
await temp_message.edit_text(f"⌛ Generating...")
prompt = Prompt(prompt=message.get_args(), creator=message.from_id)
image = await txt2img(prompt)
image_message = await message.reply_photo(photo=image[0])
db[DBTables.queue]['n'] = db[DBTables.queue].get('n', 1) - 1
db[DBTables.generated][image_message.photo[0].file_unique_id] = prompt
await temp_message.delete()
await db[DBTables.config].write()
except ClientConnectorError:
await message.reply('❌ Error! Maybe, StableDiffusion API endpoint is incorrect '
'or turned off')
await temp_message.delete()
db[DBTables.queue]['n'] = db[DBTables.queue].get('n', 1) - 1
return
await temp_message.delete()
except Exception as e:
exception_id = f'{message.message_thread_id}-{message.message_id}'
db[DBTables.exceptions][exception_id] = PrettyException(e)
await message.reply('❌ Error happened while processing your request', parse_mode='html',
reply_markup=get_exception_keyboard(exception_id))
await temp_message.delete()
db[DBTables.queue]['n'] = db[DBTables.queue].get('n', 1) - 1
return