From 09cffdbac9ae85f4f4df273f3bf53e4853fec396 Mon Sep 17 00:00:00 2001 From: Mikhail Guseletov Date: Wed, 12 Jul 2023 23:08:19 +0700 Subject: [PATCH] dont work translate from long message add button send file to VK album --- README.md | 5 ++++ bot.py | 70 ++++++++++++++++++++++++++++++++++++++++++++++++------- 2 files changed, 66 insertions(+), 9 deletions(-) diff --git a/README.md b/README.md index eacbfcf..e1749e9 100644 --- a/README.md +++ b/README.md @@ -10,6 +10,7 @@ pip install aiogram pip install webuiapi pip install translate pip install transformers +pip install vk_api cd C:\stable-diffusion-webui\extensions\sd_telegram\ python bot.py `````` @@ -76,6 +77,9 @@ Please pay attention to the file size limit in Telegram API: _**Chat History** We go into any Telegram chat with prompts (individual messages), click three dots in the upper right corner, upload only text messages in json format. We get the result.json file, which we throw into the bot and select "Chat History". We get a random prompt, which we can save in data +If you chose _/img_real = true_, the document will be unloaded for you, and after it the social network **VK** upload button. +Before that, you need to set up a token and enter the ID of the album where the photo will be sent. + drawing @@ -86,6 +90,7 @@ We go into any Telegram chat with prompts (individual messages), click three dot 4. Ability to send everything with one command with settings 5. Preloading photos when waiting for a long time so that you can skip 6. Progress in script +7. Translator capable of translating up to 4000 characters and detecting the language **TNX** [AUTOMATIC1111](https://github.com/AUTOMATIC1111/stable-diffusion-webui) diff --git a/bot.py b/bot.py index 65b084e..91f3f83 100644 --- a/bot.py +++ b/bot.py @@ -31,6 +31,8 @@ from translate import Translator import base64 from pathlib import Path import logging +import vk_api +from vk_api import VkUpload # Настройка логгера logging.basicConfig(format="[%(asctime)s] %(levelname)s : %(name)s : %(message)s", @@ -41,9 +43,16 @@ logging.getLogger('aiogram').setLevel(logging.DEBUG) logger = logging.getLogger(__name__) # from https://t.me/BotFather -API_TOKEN = "TOKEN_HERE" +API_BOT_TOKEN = "TOKEN_HERE" +#заходим в https://oauth.vk.com/authorize?client_id=123&scope=photos&redirect_uri=http%3A%2F%2Foauth.vk.com%2Fblank.html&display=page&response_type=token, +# где 123 - номер вашего включенного приложения, созданного в https://vk.com/apps?act=manage, +# photos - зона доступа. +# После перехода и подтверждения выцепляем access_token из адресной строки +# TODO auto requests +VK_TOKEN = 'TOKEN_HERE' +vk_album_id = '789' # брать с адресной строки, когда открываешь ВК. Пример https://vk.com/album123_789 -bot = Bot(token=API_TOKEN) +bot = Bot(token=API_BOT_TOKEN) storage = MemoryStorage() dp = Dispatcher(bot, storage=storage) @@ -198,7 +207,7 @@ def rnd_prmt_lxc(): return txt # get settings. TODO - cut 4000 symbols -def get_prompt_settings(): +def get_prompt_settings(typeCode = 'HTML'): global sd prompt = data['prompt'].replace('<', '<').replace('>', '>') cfg_scale = data['cfg_scale'] @@ -211,7 +220,10 @@ def get_prompt_settings(): sd_model_checkpoint = dataParams['sd_model_checkpoint'] else: sd_model_checkpoint = api.get_options()['sd_model_checkpoint'] - txt = f"prompt = {prompt}\nsteps = {steps} \ncfg_scale = {cfg_scale} \nwidth = {width} \nheight = {height} \nsampler_name = {sampler_name} \nsd_model_checkpoint = {sd_model_checkpoint} \nnegative_prompt = {negative_prompt} " + if typeCode == 'HTML': + txt = f"prompt = {prompt}\nsteps = {steps} \ncfg_scale = {cfg_scale} \nwidth = {width} \nheight = {height} \nsampler_name = {sampler_name} \nsd_model_checkpoint = {sd_model_checkpoint} \nnegative_prompt = {negative_prompt} " + else: + txt = f"prompt = {prompt}\n\nsteps = {steps} cfg_scale = {cfg_scale} width = {width} height = {height} sampler_name = {sampler_name} sd_model_checkpoint = {sd_model_checkpoint} \n\nnegative_prompt = {negative_prompt} " return txt # Translate @@ -606,12 +618,12 @@ async def inl_uplchat(callback: types.CallbackQuery) -> None: t = t2 break - chatHistoryPrompt = translateRuToEng(t) + chatHistoryPrompt = t#translateRuToEng(t) keyboard = InlineKeyboardMarkup(inline_keyboard=[getPromptFromJson(0), getStart(0)]) await bot.edit_message_text( chat_id=callback.message.chat.id, message_id=callback.message.message_id, - text=translateRuToEng(t).replace('<', '<').replace('>', '>'), + text=t.replace('<', '<').replace('>', '>'),#translateRuToEng(t).replace('<', '<').replace('>', '>'), reply_markup=keyboard, parse_mode = types.ParseMode.HTML ) @@ -736,8 +748,17 @@ async def inl_gen(message: Union[types.Message, types.CallbackQuery]) -> None: chat_id=chatId, media=pilToImages(res, "tg") ) if dataParams["img_real"] == "true" or dataParams["img_real"] == "True": - await bot.send_media_group( - chat_id=chatId, media=pilToImages(res, "real") + mes_file = await bot.send_media_group( + chat_id=chatId, + media=pilToImages(res, "real") + ) + # send button load in VK + # TODO long message + await bot.send_message( + chat_id=chatId, + text="↓ send to VK", + reply_markup=InlineKeyboardMarkup( + inline_keyboard=[[InlineKeyboardButton(mes_file[0].document.file_id, callback_data='send_vk')]]) ) await bot.send_message( chat_id=chatId, @@ -763,6 +784,37 @@ async def inl_gen(message: Union[types.Message, types.CallbackQuery]) -> None: keyboard = InlineKeyboardMarkup(inline_keyboard=[getSet(0), getOpt(0), getStart(0)]) await getKeyboardUnion("Turn on SD"+sd, message, keyboard) +# upload in VK +@dp.callback_query_handler(text="send_vk") +async def inl_samplers(callback: types.CallbackQuery) -> None: + try: + global VK_TOKEN, vk_album_id + file_id = callback.message.reply_markup.inline_keyboard[0][0].text #TODO + file_obj = await bot.get_file(file_id) + vk_session = vk_api.VkApi(token=VK_TOKEN) + vk_upload = VkUpload(vk_session) + file_url = f'https://api.telegram.org/file/bot{API_BOT_TOKEN}/{file_obj.file_path}' + + #TODO optimize + with open('temp.png', 'wb') as file: + file.write(requests.get(file_url).content) + vk_upload.photo( + photos='temp.png', + album_id=vk_album_id, + caption=get_prompt_settings(0) + ) + # clear garbage + os.remove('temp.png') + await callback.message.edit_text( + 'Фотка в VK загружена' + ) + except Exception as e: + await bot.send_message( + chat_id=callback.message.chat.id, + text=e, + parse_mode=types.ParseMode.HTML + ) + # Получить меню действий с промптами @dp.callback_query_handler(text="prompt") async def cmd_prompt(message: Union[types.Message, types.CallbackQuery]) -> None: @@ -1104,7 +1156,7 @@ async def change_json(message: types.Message): f"JSON параметры:\n{getJson()}\n{getJson(1)}", reply_markup=keyboard ) else: - data["prompt"] = translateRuToEng(message.text) + data["prompt"] = message.text#translateRuToEng(message.text) await bot.delete_message(chat_id=message.chat.id, message_id=message.message_id) await message.answer( f"Записали промпт. JSON параметры:\n{getJson()}\n{getJson(1)}",