Skip to content

Commit

Permalink
Merge pull request #102 from handsome0hell/bard-images
Browse files Browse the repository at this point in the history
feat: show images from Bard's reply
  • Loading branch information
F33RNI authored Dec 9, 2023
2 parents 04358fb + 84a7a34 commit fe97feb
Show file tree
Hide file tree
Showing 7 changed files with 244 additions and 190 deletions.
2 changes: 2 additions & 0 deletions BardModule.py
Original file line number Diff line number Diff line change
Expand Up @@ -158,6 +158,8 @@ def process_request(self, request_response: RequestResponseContainer) -> None:
logging.info("Response successfully processed for user {0} ({1})"
.format(request_response.user["user_name"], request_response.user["user_id"]))
request_response.response = bard_response["content"]
if "images" in bard_response and len(bard_response["images"]) > 0:
request_response.response_images = bard_response["images"]

# Save conversation
logging.info("Saving conversation_id as {} and response_id as {} and choice_id as {}".
Expand Down
2 changes: 1 addition & 1 deletion BingImageGenModule.py
Original file line number Diff line number Diff line change
Expand Up @@ -132,7 +132,7 @@ def process_request(self, request_response: RequestResponseContainer) -> None:
# 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
request_response.response_images = response_urls

# Exit requested
except KeyboardInterrupt:
Expand Down
349 changes: 190 additions & 159 deletions BotHandler.py

Large diffs are not rendered by default.

65 changes: 38 additions & 27 deletions QueueHandler.py
Original file line number Diff line number Diff line change
Expand Up @@ -685,7 +685,7 @@ def _collect_data(self, request_response: RequestResponseContainer, log_request=
request_str_to_format = self.config["data_collecting"]["request_format"].replace("\\n", "\n") \
.replace("\\t", "\t").replace("\\r", "\r")

# Log image
# Log image request
try:
if request_response.image_url:
logging.info("Downloading user image")
Expand All @@ -712,34 +712,45 @@ def _collect_data(self, request_response: RequestResponseContainer, log_request=

# Log response
else:
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
# Get formatter
response_str_to_format = self.config["data_collecting"]["response_format"].replace("\\n", "\n") \
.replace("\\t", "\t").replace("\\r", "\r")
log_file.write(response_str_to_format.format(request_response.response_timestamp,
request_response.id,
request_response.user["user_name"],
request_response.user["user_id"],
RequestResponseContainer
.REQUEST_NAMES[request_response.request_type],
response))

# Text
if request_response.response and type(request_response.response) == str:
log_file.write(response_str_to_format.format(request_response.response_timestamp,
request_response.id,
request_response.user["user_name"],
request_response.user["user_id"],
RequestResponseContainer
.REQUEST_NAMES[request_response.request_type],
request_response.response))

# Images
for image_url in request_response.response_images:
try:
response = base64.b64encode(requests.get(image_url, timeout=120).content).decode("utf-8")
log_file.write(response_str_to_format.format(request_response.response_timestamp,
request_response.id,
request_response.user["user_name"],
request_response.user["user_id"],
RequestResponseContainer
.REQUEST_NAMES[request_response.request_type],
response))
# Error logging image
except Exception as e:
logging.warning("Error logging image: {}".format(image_url), exc_info=e)

if request_response.response and type(request_response.response) == str:
response_str_to_format = self.config["data_collecting"]["response_format"].replace("\\n", "\n") \
.replace("\\t", "\t").replace("\\r", "\r")
log_file.write(response_str_to_format.format(request_response.response_timestamp,
request_response.id,
request_response.user["user_name"],
request_response.user["user_id"],
RequestResponseContainer
.REQUEST_NAMES[request_response.request_type],
request_response.response))

# Log confirmation
logging.info("The {0} were written to the file: {1}".format("request" if log_request else "response",
Expand Down
7 changes: 4 additions & 3 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -39,9 +39,10 @@ Or message me if you would like to donate 💰
## 🤗 Contributors

- 1️⃣ [Sprav04ka](https://github.com/Sprav04ka) - *Tofii'skovyi' language, Testing, [Super beautiful DIY jack o'lantern (for poster)](Banner.png), [Project Logo](Logo.png), Motivation*
- 2️⃣ [Sergey Krashevich](https://github.com/skrashevich) - *Docker, GitHub Actions*
- 3️⃣ [Wahit Fitriyanto](https://github.com/wahitftry) - *Indonesian language*
- 4️⃣ [Alexander Fadeyev](https://github.com/alfsoft) - *EdgeGPT Fix*
- 2️⃣ [Hanssen](https://github.com/handsome0hell) - *Markdown parsing, bard images, Chinese language, /chat command*
- 3️⃣ [Sergey Krashevich](https://github.com/skrashevich) - *Docker, GitHub Actions*
- 4️⃣ [Wahit Fitriyanto](https://github.com/wahitftry) - *Indonesian language*
- 5️⃣ [Alexander Fadeyev](https://github.com/alfsoft) - *EdgeGPT Fix*

----------

Expand Down
8 changes: 8 additions & 0 deletions RequestResponseContainer.py
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,7 @@ def __init__(self,
request="",
response="",
response_len_last=0,
response_images=None,
request_type=REQUEST_TYPE_CHATGPT,
request_timestamp="",
response_timestamp="",
Expand All @@ -59,6 +60,7 @@ def __init__(self,
:param request: text request
:param response: text response
:param response_len_last: length of last response (for editing aka live replying)
:param response_images: images in the responses
:param request_type: REQUEST_TYPE_CHATGPT / REQUEST_TYPE_DALLE / ...
:param request_timestamp: timestamp of request (for data collecting)
:param response_timestamp: timestamp of response (for data collecting)
Expand All @@ -83,6 +85,12 @@ def __init__(self,
self.pid = pid
self.image_url = image_url

# Empty or response_images
if response_images is None:
self.response_images = []
else:
self.response_images = response_images

self.processing_start_timestamp = 0.
self.error = False

Expand Down
1 change: 1 addition & 0 deletions requirements.txt
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
revChatGPT==6.8.6
git+https://github.com/F33RNI/EdgeGPT@main#egg=EdgeGPT
git+https://github.com/dsdanielpark/Bard-API@main
git+https://github.com/handsome0hell/md2tgmd.git@main
python-telegram-bot==20.3
openai>=0.26.4
tiktoken>=0.2.0
Expand Down

0 comments on commit fe97feb

Please sign in to comment.