From 0742e0ab04519fb9e22ef5dd04ae68c2b782ae3b Mon Sep 17 00:00:00 2001 From: F33RNI Date: Sat, 9 Sep 2023 01:39:01 +0300 Subject: [PATCH] Make Bing image gen works (finally) --- BingImageGenModule.py | 31 +++++++++++++++++++++++----- BotHandler.py | 48 ++++++++++++++++++++++++++++++++++--------- QueueHandler.py | 27 +++++++++++++++--------- config.json | 9 +++++--- requirements.txt | 2 +- 5 files changed, 88 insertions(+), 29 deletions(-) diff --git a/BingImageGenModule.py b/BingImageGenModule.py index 0b1dbe3c..2092ee3d 100644 --- a/BingImageGenModule.py +++ b/BingImageGenModule.py @@ -24,6 +24,7 @@ import BotHandler import UsersHandler +from JSONReaderWriter import load_json from RequestResponseContainer import RequestResponseContainer @@ -60,8 +61,30 @@ def initialize(self, proxy=None) -> None: logging.warning("Bing ImageGen module disabled in config file!") raise Exception("Bing ImageGen module disabled in config file!") + # Parse cookies + auth_cookie = "" + auth_cookie_SRCHHPGUSR = "" + try: + cookies = load_json(self.config["bing_imagegen"]["cookies_file"]) + if not cookies or len(cookies) < 1: + raise "Error reading bing cookies!" + for cookie in cookies: + if cookie["name"] == "_U": + auth_cookie = cookie["value"] + elif cookie["name"] == "SRCHHPGUSR": + auth_cookie_SRCHHPGUSR = cookie["value"] + if not auth_cookie: + raise "No _U cookie!" + if not auth_cookie_SRCHHPGUSR: + raise "No SRCHHPGUSR cookie!" + except Exception as e: + raise e + # Initialize Bing ImageGen - self._image_generator = ImageGen(self.config["bing_imagegen"]["cookies_file"], quiet=True) + self._image_generator = ImageGen(auth_cookie=auth_cookie, + auth_cookie_SRCHHPGUSR=auth_cookie_SRCHHPGUSR, + quiet=True, + all_cookies=cookies) # Set proxy if proxy: @@ -99,19 +122,17 @@ def process_request(self, request_response: RequestResponseContainer) -> None: self.users_handler.save_user(request_response.user) # Generate images - # TODO: Make it work logging.info("Requesting images from Bing ImageGen") response_urls = self._image_generator.get_images(request_response.request) - print(response_urls) # Check response if not response_urls or len(response_urls) < 1: raise Exception("Wrong Bing ImageGen response!") - # TODO: Use all generated images (for now it's the first one) + # Use all generated images logging.info("Response successfully processed for user {0} ({1})" .format(request_response.user["user_name"], request_response.user["user_id"])) - request_response.response = response_urls[0] + request_response.response = response_urls # Exit requested except KeyboardInterrupt: diff --git a/BotHandler.py b/BotHandler.py index 9d5d7197..649a30e3 100644 --- a/BotHandler.py +++ b/BotHandler.py @@ -26,7 +26,7 @@ from typing import List, Dict import telegram -from telegram import Update, InlineKeyboardButton, InlineKeyboardMarkup +from telegram import Update, InlineKeyboardButton, InlineKeyboardMarkup, InputMediaPhoto from telegram.ext import ApplicationBuilder, ContextTypes, CommandHandler, MessageHandler, filters, CallbackQueryHandler import LoggingHandler @@ -128,8 +128,11 @@ async def send_message_async(config: dict, messages: List[Dict], lang = UsersHandler.get_key_or_none(request_response.user, "lang", 0) # Fix empty message - if len(request_response.response.strip()) <= 0 and end: - request_response.response = messages[lang]["empty_message"] + if end: + if not request_response.response \ + or (type(request_response.response) == list and len(request_response.response) == 0) \ + or (type(request_response.response) == str and len(request_response.response.strip()) <= 0): + request_response.response = messages[lang]["empty_message"] # The last message if end: @@ -180,13 +183,38 @@ async def send_message_async(config: dict, messages: List[Dict], if (request_response.request_type == RequestResponseContainer.REQUEST_TYPE_DALLE or request_response.request_type == RequestResponseContainer.REQUEST_TYPE_BING_IMAGEGEN) \ and not request_response.error: - request_response.message_id = (await (telegram.Bot(config["telegram"]["api_key"]).sendPhoto( - chat_id=request_response.user["user_id"], - photo=request_response.response, - reply_to_message_id=request_response - .reply_message_id, - reply_markup=request_response.reply_markup))) \ - .message_id + # Single photo + if type(request_response.response) == str: + request_response.message_id = (await (telegram.Bot(config["telegram"]["api_key"]).sendPhoto( + chat_id=request_response.user["user_id"], + photo=request_response.response, + reply_to_message_id=request_response + .reply_message_id, + reply_markup=request_response.reply_markup))) \ + .message_id + + # Multiple photos (send media group + markup as seperate messages) + else: + # Collect media group + media_group = [] + for url in request_response.response: + media_group.append(InputMediaPhoto(media=url)) + + # Send it + await (telegram.Bot(config["telegram"]["api_key"]).sendMediaGroup( + chat_id=request_response.user["user_id"], + media=media_group, + reply_to_message_id=request_response.reply_message_id)) + + # Send reply markup and get message ID + request_response.message_id = await send_reply(config["telegram"]["api_key"], + request_response.user["user_id"], + config["telegram"]["empty_message_symbol"], + request_response.reply_message_id, + markdown=False, + reply_markup=request_response.reply_markup, + edit_message_id=request_response.message_id) + # Send message as text else: request_response.message_id = await send_reply(config["telegram"]["api_key"], diff --git a/QueueHandler.py b/QueueHandler.py index 03bdf6eb..3b942b9e 100644 --- a/QueueHandler.py +++ b/QueueHandler.py @@ -675,16 +675,23 @@ def _collect_data(self, request_response: RequestResponseContainer, log_request= # Log response else: - # DALL-E or BingImageGen response without error - if (request_response.request_type == RequestResponseContainer.REQUEST_TYPE_DALLE - or request_response.request_type == RequestResponseContainer.REQUEST_TYPE_BING_IMAGEGEN) \ - and not request_response.error: - response = base64.b64encode(requests.get(request_response.response, timeout=120).content) \ - .decode("utf-8") - - # Text response (ChatGPT, EdgeGPT, Bard) - else: - response = request_response.response + response = "None" + try: + # DALL-E or BingImageGen response without error + if (request_response.request_type == RequestResponseContainer.REQUEST_TYPE_DALLE + or request_response.request_type == RequestResponseContainer.REQUEST_TYPE_BING_IMAGEGEN) \ + and not request_response.error: + response_url = request_response.response if type(request_response.response) == str\ + else request_response.response[0] + response = base64.b64encode(requests.get(response_url, timeout=120).content) \ + .decode("utf-8") + + # Text response (ChatGPT, EdgeGPT, Bard) + else: + response = request_response.response + except Exception as e: + logging.warning("Can't parse response for data logging!", exc_info=e) + response = str(response) # Log response response_str_to_format = self.config["data_collecting"]["response_format"].replace("\\n", "\n") \ diff --git a/config.json b/config.json index 97cab4d6..f7c409e7 100644 --- a/config.json +++ b/config.json @@ -5,7 +5,7 @@ "edgegpt": true, "dalle": true, "bard": true, - "bing_imagegen": false, + "bing_imagegen": true, "__comment04__": "Default (initial) module for handling user messages (see RequestResponseContainer.py)", "default_module": 0 @@ -95,7 +95,7 @@ "user_cooldown_seconds": 600 }, - "__comment05__": "BING IMAGEGEN SETTINGS (CURRENTLY NOT WORKING)", + "__comment05__": "BING IMAGEGEN SETTINGS", "bing_imagegen": { "__comment01__": "PATH TO COOKIES FILE (JSON) https://github.com/acheong08/EdgeGPT#collect-cookies", "cookies_file": "EdgeGPT_cookies.json", @@ -151,7 +151,10 @@ "add_cursor_symbol": true, "cursor_symbol": "▯", - "__comment07__": "SET TO true FOR THE BOT TO REPLY TO PLAIN MESSAGES AS WELL AS DIRECT MODULE COMMANDS", + "__comment07__": "EMPTY SYMBOL FOR REPLY MARKUP WITHOUT TEXT (FOR SENDING MULTIPLE PHOTOS)", + "empty_message_symbol": "ㅤ", + + "__comment08__": "SET TO true FOR THE BOT TO REPLY TO PLAIN MESSAGES AS WELL AS DIRECT MODULE COMMANDS", "reply_to_messages": true }, diff --git a/requirements.txt b/requirements.txt index 13b7fedd..89616f8f 100644 --- a/requirements.txt +++ b/requirements.txt @@ -9,4 +9,4 @@ asyncio>=3.4.3 requests>=2.28.1 telegram>=0.0.1 psutil>=5.9.4 -BingImageCreator>=0.4.4 \ No newline at end of file +BingImageCreator>=0.5.0 \ No newline at end of file