Created
May 4, 2023 23:50
-
-
Save vasmarfas/54c82316951e31dda678df737b20821e to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| import re | |
| from bot.common import bot | |
| 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.get_prompt import get_prompt | |
| from bot.modules.api.objects.prompt_request import Generated | |
| from bot.modules.api.status import wait_for_status | |
| from bot.keyboards.image_info import get_img_info_keyboard | |
| from bot.utils.errorable_command import wrap_exception | |
| from bot.callbacks.factories.image_info import (prompt_only, full_prompt, import_prompt, back) | |
| @wrap_exception([ValueError], custom_loading=True) | |
| @throttle(cooldown=30, admin_ids=db[DBTables.config].get('admins')) | |
| async def generate_command(message: types.Message): | |
| temp_message = await message.reply("β³ Enqueued...") | |
| if not db[DBTables.config]['enabled']: | |
| await message.reply('π Generation is disabled by admins now. Try again later') | |
| await temp_message.delete() | |
| return | |
| elif (message.chat.id not in db[DBTables.config]['whitelist'] and message.from_id not in db[DBTables.config]['whitelist']): | |
| await message.reply('βYou are not on the white list, access denied. Contact admin @kilisauros for details') | |
| await temp_message.delete() | |
| return | |
| try: | |
| prompt = get_prompt(user_id=message.from_id, | |
| prompt_string=message.get_args()) | |
| except AttributeError: | |
| await temp_message.edit_text(f"You didn't created any prompt. Specify prompt text at least first time. " | |
| f"For example, it can be: <code>masterpiece, best quality, 1girl, white hair, " | |
| f"medium hair, cat ears, closed eyes, looking at viewer, :3, cute, scarf, jacket, " | |
| f"outdoors, streets</code>", parse_mode='HTML') | |
| return | |
| try: | |
| 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...") | |
| db[DBTables.queue]['n'] = db[DBTables.queue].get('n', 1) - 1 | |
| image = await txt2img(prompt) | |
| image_message = await message.reply_photo(photo=image[0]) | |
| #Send photo to SD Image Archive | |
| message_data = get_full_info(image_message.photo[0].file_unique_id) | |
| archive_message = f'User ID: {message.from_id} \n \ | |
| User nickname: {message.from_user.full_name} \n \ | |
| User username: @{message.from_user.username} \n \ | |
| Chat ID: {message.chat.id} \n \ | |
| Chat title: {message.chat.title} \n \ | |
| {get_full_info(image_message.photo[0].file_unique_id)}' | |
| # archive_message = f'{callback_data=full_prompt.new(p_id=f"{image_message.photo[0].file_unique_id}")} + "User ID: " + message.from_id + " User nickname: " + message.from_user.full_name + " User username: " + message.from_user.username' | |
| await bot.send_photo(-929754401, photo=image[0], caption=archive_message, reply_markup=get_img_info_keyboard(image_message.photo[0].file_unique_id)) | |
| db[DBTables.generated][image_message.photo[0].file_unique_id] = Generated( | |
| prompt=prompt, | |
| seed=image[1]['seed'], | |
| model=re.search(r", Model: ([^,]+),", image[1]['infotexts'][0]).groups()[0] | |
| ) | |
| await message.reply('Here is your image', | |
| reply_markup=get_img_info_keyboard(image_message.photo[0].file_unique_id)) | |
| await temp_message.delete() | |
| await db[DBTables.config].write() | |
| except ValueError as e: | |
| await message.reply(f'β Error! {e.args[0]}') | |
| await temp_message.delete() | |
| db[DBTables.queue]['n'] = db[DBTables.queue].get('n', 1) - 1 | |
| return | |
| async def get_full_info(p_id: str): | |
| prompt: Generated = db[DBTables.generated].get(p_id) | |
| outstr: str | |
| outstr = f"π€ Prompt: {prompt.prompt.prompt} \n \ | |
| π Negative: {prompt.prompt.negative_prompt} \n \ | |
| π« Model: {prompt.model} \n \ | |
| πͺ Steps: {prompt.prompt.steps} \n \ | |
| π§βπ¨ CFG Scale: {prompt.prompt.cfg_scale} \n \ | |
| π₯οΈ Size: {prompt.prompt.width}x{prompt.prompt.height} \n \ | |
| π Restore faces: {'on' if prompt.prompt.restore_faces else 'off'} \n \ | |
| βοΈ Sampler: {prompt.prompt.sampler} \n \ | |
| π± Seed: {prompt.seed}" | |
| return outstr | |
| # parse_mode='html', | |
| # reply_markup=get_img_back_keyboard(p_id) | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment