diff --git a/skynet/frontend/discord/__init__.py b/skynet/frontend/discord/__init__.py index 74675d7..4c5bf31 100644 --- a/skynet/frontend/discord/__init__.py +++ b/skynet/frontend/discord/__init__.py @@ -17,6 +17,7 @@ from leap.hyperion import HyperionAPI # from telebot.types import InputMediaPhoto import discord +import requests import io from PIL import Image, UnidentifiedImageError @@ -295,10 +296,20 @@ class SkynetDiscordFrontend: logging.info(f'success! sending generated image') await message.delete() if file_id: # img2img - embed.set_thumbnail( - url='https://ipfs.skygpu.net/ipfs/' + binary_data + '/image.png') embed.set_image(url=ipfs_link) - await send(embed=embed, view=SkynetView(self)) + orig_url = f'https://{self.ipfs_domain}/ipfs/' + binary_data + res = requests.get(orig_url, stream=True) + if res.status_code == 200: + with io.BytesIO(res.content) as img: + file = discord.File(img, filename='image.png') + embed.set_thumbnail(url='attachment://image.png') + await send(embed=embed, view=SkynetView(self), file=file) + # orig_url = f'https://{self.ipfs_domain}/ipfs/' \ + # + binary_data + '/image.png' + # embed.set_thumbnail( + # url=orig_url) + else: + await send(embed=embed, view=SkynetView(self)) else: # txt2img embed.set_image(url=ipfs_link) await send(embed=embed, view=SkynetView(self)) diff --git a/skynet/frontend/discord/bot.py b/skynet/frontend/discord/bot.py index accc926..3f1b654 100644 --- a/skynet/frontend/discord/bot.py +++ b/skynet/frontend/discord/bot.py @@ -44,7 +44,7 @@ class DiscordBot(commands.Bot): await channel.send('Skynet bot online', view=SkynetView(self.bot)) # intro_msg = await channel.send('Welcome to the Skynet discord bot.\nSkynet is a decentralized compute layer, focused on supporting AI paradigms. Skynet leverages blockchain technology to manage work requests and fills. We are currently featuring image generation and support 11 different models. Get started with the /help command, or just click on some buttons. Here is an example command to generate an image:\n/txt2img a big red tractor in a giant field of corn') intro_msg = await channel.send("Welcome to Skynet's Discord Bot,\n\nSkynet operates as a decentralized compute layer, offering a wide array of support for diverse AI paradigms through the use of blockchain technology. Our present focus is image generation, powered by 11 distinct models.\n\nTo begin exploring, use the '/help' command or directly interact with the provided buttons. Here is an example command to generate an image:\n\n'/txt2img a big red tractor in a giant field of corn'") - await intro_msg.pin() + # await intro_msg.pin() print("\n==============") print("Logged in as") diff --git a/skynet/frontend/discord/handlers.py b/skynet/frontend/discord/handlers.py index 5b68892..27da3cd 100644 --- a/skynet/frontend/discord/handlers.py +++ b/skynet/frontend/discord/handlers.py @@ -42,6 +42,7 @@ def create_handler_context(frontend: 'SkynetDiscordFrontend'): await ctx.reply(content=reply_txt, view=SkynetView(frontend)) bot.remove_command('help') + @bot.command(name='help', help='Responds with a help') async def help(ctx): splt_msg = ctx.message.content.split(' ') @@ -62,7 +63,7 @@ def create_handler_context(frontend: 'SkynetDiscordFrontend'): clean_cool_word = '\n'.join(CLEAN_COOL_WORDS) await ctx.send(content=f'```{clean_cool_word}```', view=SkynetView(frontend)) - @bot.command(name='stats', help='See user statistics' ) + @bot.command(name='stats', help='See user statistics') async def user_stats(ctx): user = ctx.author @@ -96,9 +97,8 @@ def create_handler_context(frontend: 'SkynetDiscordFrontend'): prompt = ' '.join(ctx.message.content.split(' ')[1:]) if len(prompt) == 0: - await status_msg.edit(content= - 'Empty text prompt ignored.' - ) + await status_msg.edit(content='Empty text prompt ignored.' + ) await db_call('update_user_request', status_msg.id, 'Empty text prompt ignored.') return @@ -209,14 +209,23 @@ def create_handler_context(frontend: 'SkynetDiscordFrontend'): file_id = str(file.id) # file bytes image_raw = await file.read() + + user_config = {**user_row} + del user_config['id'] with Image.open(io.BytesIO(image_raw)) as image: w, h = image.size - if w > 512 or h > 512: + if w > user_config['width'] or h > user_config['height']: logging.warning(f'user sent img of size {image.size}') - image.thumbnail((512, 512)) + image.thumbnail( + (user_config['width'], user_config['height'])) logging.warning(f'resized it to {image.size}') + # if w > 512 or h > 512: + # logging.warning(f'user sent img of size {image.size}') + # image.thumbnail((512, 512)) + # logging.warning(f'resized it to {image.size}') + # image.save(f'ipfs-docker-staging/image.png', format='PNG') image_loc = 'ipfs-staging/image.png' image.save(image_loc, format='PNG') @@ -228,9 +237,6 @@ def create_handler_context(frontend: 'SkynetDiscordFrontend'): logging.info(f'mid: {ctx.message.id}') - user_config = {**user_row} - del user_config['id'] - params = { 'prompt': prompt, **user_config @@ -240,8 +246,8 @@ def create_handler_context(frontend: 'SkynetDiscordFrontend'): 'update_user_stats', user.id, 'img2img', - last_file=file_id, last_prompt=prompt, + last_file=file_id, last_binary=ipfs_hash ) @@ -254,8 +260,6 @@ def create_handler_context(frontend: 'SkynetDiscordFrontend'): if success: await db_call('increment_generated', user.id) - - # TODO: DELETE BELOW # user = 'testworker3' # status_msg = 'status' @@ -314,7 +318,6 @@ def create_handler_context(frontend: 'SkynetDiscordFrontend'): # await bot.reply_to( # message, f'Total requests on skynet queue: {len(queue)}') - # @bot.message_handler(commands=['config']) # async def set_config(message): # user = message.from_user.id @@ -361,7 +364,6 @@ def create_handler_context(frontend: 'SkynetDiscordFrontend'): # # await bot.send_message(GROUP_ID, message.text[4:]) - # generic txt2img handler # async def _generic_txt2img(message_or_query): @@ -562,7 +564,6 @@ def create_handler_context(frontend: 'SkynetDiscordFrontend'): # binary_data=binary # ) - # "proxy" handlers just request routers # @bot.message_handler(commands=['txt2img']) @@ -594,7 +595,6 @@ def create_handler_context(frontend: 'SkynetDiscordFrontend'): # case 'redo': # await _redo(call) - # catch all handler for things we dont support # @bot.message_handler(func=lambda message: True) diff --git a/skynet/frontend/discord/ui.py b/skynet/frontend/discord/ui.py index 1e6fd97..2e6351a 100644 --- a/skynet/frontend/discord/ui.py +++ b/skynet/frontend/discord/ui.py @@ -11,14 +11,22 @@ class SkynetView(discord.ui.View): def __init__(self, bot): self.bot = bot super().__init__(timeout=None) - self.add_item(RedoButton('redo', discord.ButtonStyle.primary, self.bot)) - self.add_item(Txt2ImgButton('txt2img', discord.ButtonStyle.primary, self.bot)) - self.add_item(Img2ImgButton('img2img', discord.ButtonStyle.primary, self.bot)) - self.add_item(StatsButton('stats', discord.ButtonStyle.secondary, self.bot)) - self.add_item(DonateButton('donate', discord.ButtonStyle.secondary, self.bot)) - self.add_item(ConfigButton('config', discord.ButtonStyle.secondary, self.bot)) - self.add_item(HelpButton('help', discord.ButtonStyle.secondary, self.bot)) - self.add_item(CoolButton('cool', discord.ButtonStyle.secondary, self.bot)) + self.add_item(RedoButton( + 'redo', discord.ButtonStyle.primary, self.bot)) + self.add_item(Txt2ImgButton( + 'txt2img', discord.ButtonStyle.primary, self.bot)) + self.add_item(Img2ImgButton( + 'img2img', discord.ButtonStyle.primary, self.bot)) + self.add_item(StatsButton( + 'stats', discord.ButtonStyle.secondary, self.bot)) + self.add_item(DonateButton( + 'donate', discord.ButtonStyle.secondary, self.bot)) + self.add_item(ConfigButton( + 'config', discord.ButtonStyle.secondary, self.bot)) + self.add_item(HelpButton( + 'help', discord.ButtonStyle.secondary, self.bot)) + self.add_item(CoolButton( + 'cool', discord.ButtonStyle.secondary, self.bot)) class Txt2ImgButton(discord.ui.Button): @@ -44,9 +52,8 @@ class Txt2ImgButton(discord.ui.Button): prompt = msg.content if len(prompt) == 0: - await status_msg.edit(content= - 'Empty text prompt ignored.' - ) + await status_msg.edit(content='Empty text prompt ignored.' + ) await db_call('update_user_request', status_msg.id, 'Empty text prompt ignored.') return @@ -111,26 +118,35 @@ class Img2ImgButton(discord.ui.Button): file_id = str(file.id) # file bytes image_raw = await file.read() + + user_config = {**user_row} + del user_config['id'] + with Image.open(io.BytesIO(image_raw)) as image: w, h = image.size - if w > 512 or h > 512: + if w > user_config['width'] or h > user_config['height']: logging.warning(f'user sent img of size {image.size}') - image.thumbnail((512, 512)) + image.thumbnail( + (user_config['width'], user_config['height'])) logging.warning(f'resized it to {image.size}') - image.save(f'ipfs-docker-staging/image.png', format='PNG') + # if w > 512 or h > 512: + # logging.warning(f'user sent img of size {image.size}') + # image.thumbnail((512, 512)) + # logging.warning(f'resized it to {image.size}') + # image.save(f'ipfs-docker-staging/image.png', format='PNG') + image_loc = 'ipfs-staging/image.png' + image.save(image_loc, format='PNG') - ipfs_hash = ipfs_node.add('image.png') - ipfs_node.pin(ipfs_hash) + ipfs_info = await ipfs_node.add(image_loc) + ipfs_hash = ipfs_info['Hash'] + await ipfs_node.pin(ipfs_hash) logging.info(f'published input image {ipfs_hash} on ipfs') logging.info(f'mid: {msg.id}') - user_config = {**user_row} - del user_config['id'] - params = { 'prompt': prompt, **user_config @@ -140,8 +156,8 @@ class Img2ImgButton(discord.ui.Button): 'update_user_stats', user.id, 'img2img', - last_file=file_id, last_prompt=prompt, + last_file=file_id, last_binary=ipfs_hash ) @@ -307,5 +323,3 @@ async def grab(prompt, interaction): await interaction.response.send_message(prompt, ephemeral=True) message = await interaction.client.wait_for('message', check=vet) return message - -