fix img to img bug

discord-upate
zoltan 2024-01-25 19:06:04 +00:00
parent 9821aac16a
commit d3cf3bc6f2
4 changed files with 67 additions and 42 deletions

View File

@ -17,6 +17,7 @@ from leap.hyperion import HyperionAPI
# from telebot.types import InputMediaPhoto # from telebot.types import InputMediaPhoto
import discord import discord
import requests
import io import io
from PIL import Image, UnidentifiedImageError from PIL import Image, UnidentifiedImageError
@ -295,9 +296,19 @@ class SkynetDiscordFrontend:
logging.info(f'success! sending generated image') logging.info(f'success! sending generated image')
await message.delete() await message.delete()
if file_id: # img2img if file_id: # img2img
embed.set_thumbnail(
url='https://ipfs.skygpu.net/ipfs/' + binary_data + '/image.png')
embed.set_image(url=ipfs_link) embed.set_image(url=ipfs_link)
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)) await send(embed=embed, view=SkynetView(self))
else: # txt2img else: # txt2img
embed.set_image(url=ipfs_link) embed.set_image(url=ipfs_link)

View File

@ -44,7 +44,7 @@ class DiscordBot(commands.Bot):
await channel.send('Skynet bot online', view=SkynetView(self.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 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'") 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("\n==============")
print("Logged in as") print("Logged in as")

View File

@ -42,6 +42,7 @@ def create_handler_context(frontend: 'SkynetDiscordFrontend'):
await ctx.reply(content=reply_txt, view=SkynetView(frontend)) await ctx.reply(content=reply_txt, view=SkynetView(frontend))
bot.remove_command('help') bot.remove_command('help')
@bot.command(name='help', help='Responds with a help') @bot.command(name='help', help='Responds with a help')
async def help(ctx): async def help(ctx):
splt_msg = ctx.message.content.split(' ') splt_msg = ctx.message.content.split(' ')
@ -62,7 +63,7 @@ def create_handler_context(frontend: 'SkynetDiscordFrontend'):
clean_cool_word = '\n'.join(CLEAN_COOL_WORDS) clean_cool_word = '\n'.join(CLEAN_COOL_WORDS)
await ctx.send(content=f'```{clean_cool_word}```', view=SkynetView(frontend)) 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): async def user_stats(ctx):
user = ctx.author user = ctx.author
@ -96,8 +97,7 @@ def create_handler_context(frontend: 'SkynetDiscordFrontend'):
prompt = ' '.join(ctx.message.content.split(' ')[1:]) prompt = ' '.join(ctx.message.content.split(' ')[1:])
if len(prompt) == 0: if len(prompt) == 0:
await status_msg.edit(content= await status_msg.edit(content='Empty text prompt ignored.'
'Empty text prompt ignored.'
) )
await db_call('update_user_request', status_msg.id, 'Empty text prompt ignored.') await db_call('update_user_request', status_msg.id, 'Empty text prompt ignored.')
return return
@ -209,14 +209,23 @@ def create_handler_context(frontend: 'SkynetDiscordFrontend'):
file_id = str(file.id) file_id = str(file.id)
# file bytes # file bytes
image_raw = await file.read() image_raw = await file.read()
user_config = {**user_row}
del user_config['id']
with Image.open(io.BytesIO(image_raw)) as image: with Image.open(io.BytesIO(image_raw)) as image:
w, h = image.size 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}') 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}') 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_loc = 'ipfs-staging/image.png'
image.save(image_loc, format='PNG') image.save(image_loc, format='PNG')
@ -228,9 +237,6 @@ def create_handler_context(frontend: 'SkynetDiscordFrontend'):
logging.info(f'mid: {ctx.message.id}') logging.info(f'mid: {ctx.message.id}')
user_config = {**user_row}
del user_config['id']
params = { params = {
'prompt': prompt, 'prompt': prompt,
**user_config **user_config
@ -240,8 +246,8 @@ def create_handler_context(frontend: 'SkynetDiscordFrontend'):
'update_user_stats', 'update_user_stats',
user.id, user.id,
'img2img', 'img2img',
last_file=file_id,
last_prompt=prompt, last_prompt=prompt,
last_file=file_id,
last_binary=ipfs_hash last_binary=ipfs_hash
) )
@ -254,8 +260,6 @@ def create_handler_context(frontend: 'SkynetDiscordFrontend'):
if success: if success:
await db_call('increment_generated', user.id) await db_call('increment_generated', user.id)
# TODO: DELETE BELOW # TODO: DELETE BELOW
# user = 'testworker3' # user = 'testworker3'
# status_msg = 'status' # status_msg = 'status'
@ -314,7 +318,6 @@ def create_handler_context(frontend: 'SkynetDiscordFrontend'):
# await bot.reply_to( # await bot.reply_to(
# message, f'Total requests on skynet queue: {len(queue)}') # message, f'Total requests on skynet queue: {len(queue)}')
# @bot.message_handler(commands=['config']) # @bot.message_handler(commands=['config'])
# async def set_config(message): # async def set_config(message):
# user = message.from_user.id # user = message.from_user.id
@ -361,7 +364,6 @@ def create_handler_context(frontend: 'SkynetDiscordFrontend'):
# #
# await bot.send_message(GROUP_ID, message.text[4:]) # await bot.send_message(GROUP_ID, message.text[4:])
# generic txt2img handler # generic txt2img handler
# async def _generic_txt2img(message_or_query): # async def _generic_txt2img(message_or_query):
@ -562,7 +564,6 @@ def create_handler_context(frontend: 'SkynetDiscordFrontend'):
# binary_data=binary # binary_data=binary
# ) # )
# "proxy" handlers just request routers # "proxy" handlers just request routers
# @bot.message_handler(commands=['txt2img']) # @bot.message_handler(commands=['txt2img'])
@ -594,7 +595,6 @@ def create_handler_context(frontend: 'SkynetDiscordFrontend'):
# case 'redo': # case 'redo':
# await _redo(call) # await _redo(call)
# catch all handler for things we dont support # catch all handler for things we dont support
# @bot.message_handler(func=lambda message: True) # @bot.message_handler(func=lambda message: True)

View File

@ -11,14 +11,22 @@ class SkynetView(discord.ui.View):
def __init__(self, bot): def __init__(self, bot):
self.bot = bot self.bot = bot
super().__init__(timeout=None) super().__init__(timeout=None)
self.add_item(RedoButton('redo', discord.ButtonStyle.primary, self.bot)) self.add_item(RedoButton(
self.add_item(Txt2ImgButton('txt2img', discord.ButtonStyle.primary, self.bot)) 'redo', discord.ButtonStyle.primary, self.bot))
self.add_item(Img2ImgButton('img2img', discord.ButtonStyle.primary, self.bot)) self.add_item(Txt2ImgButton(
self.add_item(StatsButton('stats', discord.ButtonStyle.secondary, self.bot)) 'txt2img', discord.ButtonStyle.primary, self.bot))
self.add_item(DonateButton('donate', discord.ButtonStyle.secondary, self.bot)) self.add_item(Img2ImgButton(
self.add_item(ConfigButton('config', discord.ButtonStyle.secondary, self.bot)) 'img2img', discord.ButtonStyle.primary, self.bot))
self.add_item(HelpButton('help', discord.ButtonStyle.secondary, self.bot)) self.add_item(StatsButton(
self.add_item(CoolButton('cool', discord.ButtonStyle.secondary, self.bot)) '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): class Txt2ImgButton(discord.ui.Button):
@ -44,8 +52,7 @@ class Txt2ImgButton(discord.ui.Button):
prompt = msg.content prompt = msg.content
if len(prompt) == 0: if len(prompt) == 0:
await status_msg.edit(content= await status_msg.edit(content='Empty text prompt ignored.'
'Empty text prompt ignored.'
) )
await db_call('update_user_request', status_msg.id, 'Empty text prompt ignored.') await db_call('update_user_request', status_msg.id, 'Empty text prompt ignored.')
return return
@ -111,26 +118,35 @@ class Img2ImgButton(discord.ui.Button):
file_id = str(file.id) file_id = str(file.id)
# file bytes # file bytes
image_raw = await file.read() image_raw = await file.read()
user_config = {**user_row}
del user_config['id']
with Image.open(io.BytesIO(image_raw)) as image: with Image.open(io.BytesIO(image_raw)) as image:
w, h = image.size 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}') 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}') 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_info = await ipfs_node.add(image_loc)
ipfs_node.pin(ipfs_hash) ipfs_hash = ipfs_info['Hash']
await ipfs_node.pin(ipfs_hash)
logging.info(f'published input image {ipfs_hash} on ipfs') logging.info(f'published input image {ipfs_hash} on ipfs')
logging.info(f'mid: {msg.id}') logging.info(f'mid: {msg.id}')
user_config = {**user_row}
del user_config['id']
params = { params = {
'prompt': prompt, 'prompt': prompt,
**user_config **user_config
@ -140,8 +156,8 @@ class Img2ImgButton(discord.ui.Button):
'update_user_stats', 'update_user_stats',
user.id, user.id,
'img2img', 'img2img',
last_file=file_id,
last_prompt=prompt, last_prompt=prompt,
last_file=file_id,
last_binary=ipfs_hash last_binary=ipfs_hash
) )
@ -307,5 +323,3 @@ async def grab(prompt, interaction):
await interaction.response.send_message(prompt, ephemeral=True) await interaction.response.send_message(prompt, ephemeral=True)
message = await interaction.client.wait_for('message', check=vet) message = await interaction.client.wait_for('message', check=vet)
return message return message