From 89c413a612fcd89ccbb5291e4461a2decb35a159 Mon Sep 17 00:00:00 2001 From: Guillermo Rodriguez Date: Sat, 22 Jul 2023 16:53:00 -0300 Subject: [PATCH] Bump version number, also telegram max image limit and disable in private for now --- skynet/constants.py | 5 +- skynet/frontend/telegram/__init__.py | 71 ++++++++++++++++------------ skynet/frontend/telegram/handlers.py | 9 ++++ 3 files changed, 55 insertions(+), 30 deletions(-) diff --git a/skynet/constants.py b/skynet/constants.py index 4dc1c48..b4fcafa 100755 --- a/skynet/constants.py +++ b/skynet/constants.py @@ -1,6 +1,6 @@ #!/usr/bin/python -VERSION = '0.1a10' +VERSION = '0.1a11' DOCKER_RUNTIME_CUDA = 'skynet:runtime-cuda' @@ -172,3 +172,6 @@ CONFIG_ATTRS = [ DEFAULT_DOMAIN = 'skygpu.net' DEFAULT_IPFS_REMOTE = '/ip4/169.197.140.154/tcp/4001/p2p/12D3KooWKWogLFNEcNNMKnzU7Snrnuj84RZdMBg3sLiQSQc51oEv' + +TG_MAX_WIDTH = 1280 +TG_MAX_HEIGHT = 1280 diff --git a/skynet/frontend/telegram/__init__.py b/skynet/frontend/telegram/__init__.py index 33798c5..22d9420 100644 --- a/skynet/frontend/telegram/__init__.py +++ b/skynet/frontend/telegram/__init__.py @@ -1,10 +1,12 @@ #!/usr/bin/python -from json import JSONDecodeError +import io import random import logging import asyncio +from PIL import Image +from json import JSONDecodeError from decimal import Decimal from hashlib import sha256 from datetime import datetime @@ -66,7 +68,7 @@ class SkynetTelegramFrontend: self.ipfs_node = self._exit_stack.enter_context( open_ipfs_node()) - self.ipfs_node.connect(self.remote_ipfs_node) + # self.ipfs_node.connect(self.remote_ipfs_node) logging.info( f'connected to remote ipfs node: {self.remote_ipfs_node}') @@ -236,13 +238,13 @@ class SkynetTelegramFrontend: parse_mode='HTML' ) + caption = generate_reply_caption( + user, params, tx_hash, worker, reward) + # attempt to get the image and send it ipfs_link = f'https://ipfs.{DEFAULT_DOMAIN}/ipfs/{ipfs_hash}/image.png' resp = await get_ipfs_file(ipfs_link) - caption = generate_reply_caption( - user, params, tx_hash, worker, reward) - if not resp or resp.status_code != 200: logging.error(f'couldn\'t get ipfs hosted image at {ipfs_link}!') await self.update_status_message( @@ -251,29 +253,40 @@ class SkynetTelegramFrontend: reply_markup=build_redo_menu(), parse_mode='HTML' ) + return - else: - logging.info(f'success! sending generated image') - await self.bot.delete_message( - chat_id=status_msg.chat.id, message_id=status_msg.id) - if file_id: # img2img - await self.bot.send_media_group( - status_msg.chat.id, - media=[ - InputMediaPhoto(file_id), - InputMediaPhoto( - resp.raw, - caption=caption, - parse_mode='HTML' - ) - ], - ) + png_img = resp.raw + with Image.open(io.BytesIO(resp.raw)) as image: + w, h = image.size - else: # txt2img - await self.bot.send_photo( - status_msg.chat.id, - caption=caption, - photo=resp.raw, - reply_markup=build_redo_menu(), - parse_mode='HTML' - ) + if w > TG_MAX_WIDTH or h > TG_MAX_HEIGHT: + logging.warning(f'result is of size {image.size}') + image.thumbnail((TG_MAX_WIDTH, TG_MAX_HEIGHT)) + tmp_buf = io.BytesIO() + image.save(tmp_buf, format='PNG') + png_img = tmp_buf.getvalue() + + logging.info(f'success! sending generated image') + await self.bot.delete_message( + chat_id=status_msg.chat.id, message_id=status_msg.id) + if file_id: # img2img + await self.bot.send_media_group( + status_msg.chat.id, + media=[ + InputMediaPhoto(file_id), + InputMediaPhoto( + png_img, + caption=caption, + parse_mode='HTML' + ) + ], + ) + + else: # txt2img + await self.bot.send_photo( + status_msg.chat.id, + caption=caption, + photo=png_img, + reply_markup=build_redo_menu(), + parse_mode='HTML' + ) diff --git a/skynet/frontend/telegram/handlers.py b/skynet/frontend/telegram/handlers.py index 7e77880..c6982e5 100644 --- a/skynet/frontend/telegram/handlers.py +++ b/skynet/frontend/telegram/handlers.py @@ -118,6 +118,9 @@ def create_handler_context(frontend: 'SkynetTelegramFrontend'): user = message.from_user chat = message.chat + if chat.type == 'private': + return + reply_id = None if chat.type == 'group' and chat.id == GROUP_ID: reply_id = message.message_id @@ -174,6 +177,9 @@ def create_handler_context(frontend: 'SkynetTelegramFrontend'): user = message.from_user chat = message.chat + if chat.type == 'private': + return + reply_id = None if chat.type == 'group' and chat.id == GROUP_ID: reply_id = message.message_id @@ -263,6 +269,9 @@ def create_handler_context(frontend: 'SkynetTelegramFrontend'): user = message.from_user chat = message.chat + if chat.type == 'private': + return + init_msg = 'started processing redo request...' if is_query: status_msg = await bot.send_message(chat.id, init_msg)