Skip to content

Commit

Permalink
Make Bing image gen works (finally)
Browse files Browse the repository at this point in the history
  • Loading branch information
F33RNI committed Sep 8, 2023
1 parent 432e2f0 commit 0742e0a
Show file tree
Hide file tree
Showing 5 changed files with 88 additions and 29 deletions.
31 changes: 26 additions & 5 deletions BingImageGenModule.py
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@

import BotHandler
import UsersHandler
from JSONReaderWriter import load_json
from RequestResponseContainer import RequestResponseContainer


Expand Down Expand Up @@ -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:
Expand Down Expand Up @@ -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:
Expand Down
48 changes: 38 additions & 10 deletions BotHandler.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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:
Expand Down Expand Up @@ -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"],
Expand Down
27 changes: 17 additions & 10 deletions QueueHandler.py
Original file line number Diff line number Diff line change
Expand Up @@ -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") \
Expand Down
9 changes: 6 additions & 3 deletions config.json
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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",
Expand Down Expand Up @@ -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
},

Expand Down
2 changes: 1 addition & 1 deletion requirements.txt
Original file line number Diff line number Diff line change
Expand Up @@ -9,4 +9,4 @@ asyncio>=3.4.3
requests>=2.28.1
telegram>=0.0.1
psutil>=5.9.4
BingImageCreator>=0.4.4
BingImageCreator>=0.5.0

0 comments on commit 0742e0a

Please sign in to comment.