[dfif2] >>> upscaling non-blockable, dfif2 >>> added safetychecks, safetychecks >>> added debugging messages
Browse files
app.py
CHANGED
|
@@ -21,10 +21,15 @@ import multiprocessing
|
|
| 21 |
import shutil # for doing image movement magic
|
| 22 |
|
| 23 |
#todo
|
| 24 |
-
#
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 25 |
# loading emoji? animated emojis?
|
| 26 |
# make success / fail emojis more consistent across both painter + dfif
|
| 27 |
-
|
| 28 |
# ratelimits
|
| 29 |
|
| 30 |
# enlarge each of 4 images?
|
|
@@ -67,27 +72,39 @@ async def safetychecks(ctx):
|
|
| 67 |
if any(role.id == offline_bot_role_id for role in bot_member.roles):
|
| 68 |
print(f"Error: The bot is offline or under maintenance. (Remove the offline-bot role to bring it online)")
|
| 69 |
return False
|
|
|
|
|
|
|
| 70 |
|
| 71 |
#✅✅ check if the command is in the allowed channel(s)
|
| 72 |
channel_id = 1100458786826747945
|
| 73 |
-
if ctx.channel.id !=
|
| 74 |
-
print(f"
|
| 75 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 76 |
|
| 77 |
#✅✅ check if the user has the required role(s)
|
| 78 |
guild_id = 879548962464493619
|
| 79 |
-
required_role_id =
|
| 80 |
guild = bot.get_guild(guild_id)
|
| 81 |
required_role = guild.get_role(required_role_id)
|
| 82 |
if required_role not in ctx.author.roles:
|
| 83 |
print(f"Error: The user does not have the required role to use that command. ({required_role} is the correct role)")
|
| 84 |
return False
|
|
|
|
|
|
|
| 85 |
|
| 86 |
return True
|
| 87 |
|
|
|
|
| 88 |
except Exception as e:
|
| 89 |
print(f"Error: safetychecks failed somewhere, command will not continue.")
|
| 90 |
-
await ctx.message.reply(f"<@811235357663297546> SC failed somewhere") # this will always ping, as long as the bot has access to the channel
|
| 91 |
#----------------------------------------------------------------------------------------------------------------------------------------------
|
| 92 |
# jojo ✅
|
| 93 |
@bot.command()
|
|
@@ -190,26 +207,39 @@ async def sketch(ctx):
|
|
| 190 |
await thread.send(f"{ctx.author.mention}Error: {e}")
|
| 191 |
await ctx.message.add_reaction('❌')
|
| 192 |
#----------------------------------------------------------------------------------------------------------------------------------------------
|
| 193 |
-
# deepfloydif stage 1 generation
|
| 194 |
def inference(prompt):
|
| 195 |
-
|
| 196 |
-
|
| 197 |
-
|
| 198 |
-
|
| 199 |
-
|
| 200 |
-
|
| 201 |
-
|
| 202 |
-
|
| 203 |
-
|
| 204 |
-
|
| 205 |
-
|
| 206 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 207 |
#----------------------------------------------------------------------------------------------------------------------------------------------
|
|
|
|
| 208 |
async def react1234(reaction_emojis, combined_image_dfif):
|
| 209 |
for emoji in reaction_emojis:
|
| 210 |
await combined_image_dfif.add_reaction(emoji)
|
| 211 |
#----------------------------------------------------------------------------------------------------------------------------------------------
|
| 212 |
-
# Stage 1
|
| 213 |
@bot.command()
|
| 214 |
async def deepfloydif(ctx, *, prompt: str):
|
| 215 |
try:
|
|
@@ -232,21 +262,23 @@ async def deepfloydif(ctx, *, prompt: str):
|
|
| 232 |
number_of_inference_steps = 50
|
| 233 |
api_name = '/generate64'
|
| 234 |
|
| 235 |
-
await thread.send(f'{ctx.author.mention}Generating images in thread, can take ~1 minute...')
|
| 236 |
|
| 237 |
except Exception as e:
|
| 238 |
print(f"Error: {e}")
|
| 239 |
await ctx.reply('stage 1 error -> pre generation')
|
| 240 |
await ctx.message.add_reaction('❌')
|
| 241 |
|
| 242 |
-
#generation
|
| 243 |
try:
|
| 244 |
#stage_1_results, stage_1_param_path, stage_1_result_path = df.predict(
|
| 245 |
# prompt, negative_prompt, seed, number_of_images, guidance_scale, custom_timesteps_1, number_of_inference_steps, api_name='/generate64')
|
| 246 |
|
| 247 |
# run blocking function in executor
|
|
|
|
| 248 |
loop = asyncio.get_running_loop()
|
| 249 |
result = await loop.run_in_executor(None, inference, prompt)
|
|
|
|
| 250 |
stage_1_results = result[0]
|
| 251 |
stage_1_result_path = result[2]
|
| 252 |
|
|
@@ -254,10 +286,11 @@ async def deepfloydif(ctx, *, prompt: str):
|
|
| 254 |
|
| 255 |
except Exception as e:
|
| 256 |
print(f"Error: {e}")
|
| 257 |
-
await ctx.reply('stage 1 error -> during generation')
|
| 258 |
await ctx.message.add_reaction('❌')
|
| 259 |
-
#posting images
|
| 260 |
try:
|
|
|
|
| 261 |
png_files = [f for f in os.listdir(stage_1_results) if f.endswith('.png')]
|
| 262 |
|
| 263 |
if png_files:
|
|
@@ -290,6 +323,7 @@ async def deepfloydif(ctx, *, prompt: str):
|
|
| 290 |
combined_image_dfif = await thread.send(f'{ctx.author.mention}React with the image number you want to upscale!', file=discord.File(
|
| 291 |
f, f'{partialpath}{dfif_command_message_id}.png')) # named something like: tmpgtv4qjix1111269940599738479.png
|
| 292 |
|
|
|
|
| 293 |
emoji_list = ['1️⃣', '2️⃣', '3️⃣', '4️⃣']
|
| 294 |
await react1234(emoji_list, combined_image_dfif)
|
| 295 |
|
|
@@ -308,47 +342,45 @@ async def deepfloydif(ctx, *, prompt: str):
|
|
| 308 |
|
| 309 |
except Exception as e:
|
| 310 |
print(f"Error: {e}")
|
| 311 |
-
await ctx.reply('stage 1 error -> posting images in thread')
|
| 312 |
await ctx.message.add_reaction('❌')
|
| 313 |
|
| 314 |
#deepfloydif try/except
|
| 315 |
except Exception as e:
|
| 316 |
print(f"Error: {e}")
|
| 317 |
-
await ctx.reply('An error occurred in stage 1 for deepfloydif')
|
| 318 |
await ctx.message.add_reaction('❌')
|
| 319 |
|
| 320 |
#----------------------------------------------------------------------------------------------------------------------------
|
| 321 |
-
# Stage 2
|
| 322 |
-
async def dfif2(index: int, stage_1_result_path, thread, dfif_command_message_id): # add safetychecks
|
| 323 |
try:
|
| 324 |
-
await
|
| 325 |
-
|
| 326 |
-
|
| 327 |
-
|
| 328 |
-
|
| 329 |
-
|
| 330 |
-
|
| 331 |
-
|
| 332 |
-
|
| 333 |
-
|
| 334 |
-
|
| 335 |
-
|
| 336 |
-
|
| 337 |
-
|
| 338 |
-
|
| 339 |
-
|
| 340 |
-
|
| 341 |
-
|
| 342 |
-
|
| 343 |
-
|
| 344 |
-
|
| 345 |
-
|
| 346 |
-
|
| 347 |
-
|
| 348 |
-
|
| 349 |
-
|
| 350 |
-
#await ctx.reply('Here is the result of the second stage', file=discord.File(f, 'result.png'))
|
| 351 |
-
#await ctx.message.add_reaction('✅') need to fix this
|
| 352 |
|
| 353 |
'''
|
| 354 |
try:
|
|
@@ -366,6 +398,7 @@ async def dfif2(index: int, stage_1_result_path, thread, dfif_command_message_id
|
|
| 366 |
@bot.event
|
| 367 |
async def on_reaction_add(reaction, user): # ctx = await bot.get_context(reaction.message)? could try later, might simplify
|
| 368 |
try:
|
|
|
|
| 369 |
# safety checks first ❌
|
| 370 |
thread = reaction.message.channel
|
| 371 |
threadparentid = thread.parent.id
|
|
@@ -403,8 +436,9 @@ async def on_reaction_add(reaction, user): # ctx = await bot.get_context(reac
|
|
| 403 |
stage_1_result_path = fullpath
|
| 404 |
thread = reaction.message.channel
|
| 405 |
dfif_command_message_id = messageid
|
|
|
|
| 406 |
await reaction.message.channel.send(f"calling dfif2")
|
| 407 |
-
await dfif2(index, stage_1_result_path, thread, dfif_command_message_id)
|
| 408 |
|
| 409 |
'''
|
| 410 |
|
|
|
|
| 21 |
import shutil # for doing image movement magic
|
| 22 |
|
| 23 |
#todo
|
| 24 |
+
# add safetychecks to all parts, test thoroughly
|
| 25 |
+
# add discord channels, configure for fellows
|
| 26 |
+
# add debugging messages, same as in deepfloydif, to the_painter
|
| 27 |
+
# experiment with animeGANv2
|
| 28 |
+
|
| 29 |
+
#✅ 4 -> combined image
|
| 30 |
# loading emoji? animated emojis?
|
| 31 |
# make success / fail emojis more consistent across both painter + dfif
|
| 32 |
+
#✅ tasks for concurrent coroutines
|
| 33 |
# ratelimits
|
| 34 |
|
| 35 |
# enlarge each of 4 images?
|
|
|
|
| 72 |
if any(role.id == offline_bot_role_id for role in bot_member.roles):
|
| 73 |
print(f"Error: The bot is offline or under maintenance. (Remove the offline-bot role to bring it online)")
|
| 74 |
return False
|
| 75 |
+
else:
|
| 76 |
+
await ctx.channel.send(f"✅{bot} is online")
|
| 77 |
|
| 78 |
#✅✅ check if the command is in the allowed channel(s)
|
| 79 |
channel_id = 1100458786826747945
|
| 80 |
+
if ctx.channel.id != channel_id: # #bot-test = 1100458786826747945
|
| 81 |
+
print(f"If using a command, commands are not permitted in {ctx.channel}")
|
| 82 |
+
thread = ctx.channel
|
| 83 |
+
if thread.parent_id != channel_id: # threads have different channel_ids than their parents
|
| 84 |
+
print(f"Error: {thread} is not a permitted thread for upscaling.")
|
| 85 |
+
return False
|
| 86 |
+
else:
|
| 87 |
+
await ctx.channel.send(f"✅{thread} is a valid thread for upscaling")
|
| 88 |
+
else:
|
| 89 |
+
await ctx.channel.send(f"✅{ctx.channel} is a valid channel for commands")
|
| 90 |
|
| 91 |
#✅✅ check if the user has the required role(s)
|
| 92 |
guild_id = 879548962464493619
|
| 93 |
+
required_role_id = 900063512829755413 # @verified = 900063512829755413, HF = 897376942817419265
|
| 94 |
guild = bot.get_guild(guild_id)
|
| 95 |
required_role = guild.get_role(required_role_id)
|
| 96 |
if required_role not in ctx.author.roles:
|
| 97 |
print(f"Error: The user does not have the required role to use that command. ({required_role} is the correct role)")
|
| 98 |
return False
|
| 99 |
+
else:
|
| 100 |
+
await ctx.channel.send(f"✅{ctx.author} has the required role {required_role}")
|
| 101 |
|
| 102 |
return True
|
| 103 |
|
| 104 |
+
# ping lunarflu if any safety check ever fails
|
| 105 |
except Exception as e:
|
| 106 |
print(f"Error: safetychecks failed somewhere, command will not continue.")
|
| 107 |
+
await ctx.message.reply(f"❌ <@811235357663297546> SC failed somewhere ❌") # this will always ping, as long as the bot has access to the channel
|
| 108 |
#----------------------------------------------------------------------------------------------------------------------------------------------
|
| 109 |
# jojo ✅
|
| 110 |
@bot.command()
|
|
|
|
| 207 |
await thread.send(f"{ctx.author.mention}Error: {e}")
|
| 208 |
await ctx.message.add_reaction('❌')
|
| 209 |
#----------------------------------------------------------------------------------------------------------------------------------------------
|
| 210 |
+
# deepfloydif stage 1 generation ✅
|
| 211 |
def inference(prompt):
|
| 212 |
+
negative_prompt = ''
|
| 213 |
+
seed = random.randint(0, 1000)
|
| 214 |
+
#seed = 1
|
| 215 |
+
number_of_images = 4
|
| 216 |
+
guidance_scale = 7
|
| 217 |
+
custom_timesteps_1 = 'smart50'
|
| 218 |
+
number_of_inference_steps = 50
|
| 219 |
+
|
| 220 |
+
stage_1_results, stage_1_param_path, stage_1_result_path = df.predict(
|
| 221 |
+
prompt, negative_prompt, seed, number_of_images, guidance_scale, custom_timesteps_1, number_of_inference_steps, api_name='/generate64')
|
| 222 |
+
|
| 223 |
+
return [stage_1_results, stage_1_param_path, stage_1_result_path]
|
| 224 |
+
#----------------------------------------------------------------------------------------------------------------------------------------------
|
| 225 |
+
# deepfloydif stage 2 upscaling ❌
|
| 226 |
+
def inference2(index):
|
| 227 |
+
selected_index_for_stage_2 = index
|
| 228 |
+
seed_2 = 0
|
| 229 |
+
guidance_scale_2 = 4
|
| 230 |
+
custom_timesteps_2 = 'smart50'
|
| 231 |
+
number_of_inference_steps_2 = 50
|
| 232 |
+
result_path = df.predict(stage_1_result_path, selected_index_for_stage_2, seed_2,
|
| 233 |
+
guidance_scale_2, custom_timesteps_2, number_of_inference_steps_2, api_name='/upscale256')
|
| 234 |
+
|
| 235 |
+
return [result_path]
|
| 236 |
#----------------------------------------------------------------------------------------------------------------------------------------------
|
| 237 |
+
# ✅
|
| 238 |
async def react1234(reaction_emojis, combined_image_dfif):
|
| 239 |
for emoji in reaction_emojis:
|
| 240 |
await combined_image_dfif.add_reaction(emoji)
|
| 241 |
#----------------------------------------------------------------------------------------------------------------------------------------------
|
| 242 |
+
# Stage 1 ✅
|
| 243 |
@bot.command()
|
| 244 |
async def deepfloydif(ctx, *, prompt: str):
|
| 245 |
try:
|
|
|
|
| 262 |
number_of_inference_steps = 50
|
| 263 |
api_name = '/generate64'
|
| 264 |
|
| 265 |
+
await thread.send(f'✅{ctx.author.mention}Generating images in thread, can take ~1 minute...')
|
| 266 |
|
| 267 |
except Exception as e:
|
| 268 |
print(f"Error: {e}")
|
| 269 |
await ctx.reply('stage 1 error -> pre generation')
|
| 270 |
await ctx.message.add_reaction('❌')
|
| 271 |
|
| 272 |
+
#generation✅-------------------------------------------------------
|
| 273 |
try:
|
| 274 |
#stage_1_results, stage_1_param_path, stage_1_result_path = df.predict(
|
| 275 |
# prompt, negative_prompt, seed, number_of_images, guidance_scale, custom_timesteps_1, number_of_inference_steps, api_name='/generate64')
|
| 276 |
|
| 277 |
# run blocking function in executor
|
| 278 |
+
await thread.send(f'✅running blocking function in executor')
|
| 279 |
loop = asyncio.get_running_loop()
|
| 280 |
result = await loop.run_in_executor(None, inference, prompt)
|
| 281 |
+
await thread.send(f'✅run_in_executor ran successfully')
|
| 282 |
stage_1_results = result[0]
|
| 283 |
stage_1_result_path = result[2]
|
| 284 |
|
|
|
|
| 286 |
|
| 287 |
except Exception as e:
|
| 288 |
print(f"Error: {e}")
|
| 289 |
+
await ctx.reply('❌stage 1 error -> during generation')
|
| 290 |
await ctx.message.add_reaction('❌')
|
| 291 |
+
#posting images✅----------------------------------------------------------------
|
| 292 |
try:
|
| 293 |
+
await thread.send(f'✅combining images...')
|
| 294 |
png_files = [f for f in os.listdir(stage_1_results) if f.endswith('.png')]
|
| 295 |
|
| 296 |
if png_files:
|
|
|
|
| 323 |
combined_image_dfif = await thread.send(f'{ctx.author.mention}React with the image number you want to upscale!', file=discord.File(
|
| 324 |
f, f'{partialpath}{dfif_command_message_id}.png')) # named something like: tmpgtv4qjix1111269940599738479.png
|
| 325 |
|
| 326 |
+
await thread.send(f'✅reacting with 1234...')
|
| 327 |
emoji_list = ['1️⃣', '2️⃣', '3️⃣', '4️⃣']
|
| 328 |
await react1234(emoji_list, combined_image_dfif)
|
| 329 |
|
|
|
|
| 342 |
|
| 343 |
except Exception as e:
|
| 344 |
print(f"Error: {e}")
|
| 345 |
+
await ctx.reply('❌stage 1 error -> posting images in thread')
|
| 346 |
await ctx.message.add_reaction('❌')
|
| 347 |
|
| 348 |
#deepfloydif try/except
|
| 349 |
except Exception as e:
|
| 350 |
print(f"Error: {e}")
|
| 351 |
+
await ctx.reply('❌An error occurred in stage 1 for deepfloydif')
|
| 352 |
await ctx.message.add_reaction('❌')
|
| 353 |
|
| 354 |
#----------------------------------------------------------------------------------------------------------------------------
|
| 355 |
+
# Stage 2 ✅
|
| 356 |
+
async def dfif2(ctx, index: int, stage_1_result_path, thread, dfif_command_message_id): # add safetychecks
|
| 357 |
try:
|
| 358 |
+
if await safetychecks(ctx):
|
| 359 |
+
await thread.send(f"✅inside dfif2, upscaling")
|
| 360 |
+
|
| 361 |
+
# run blocking function in executor
|
| 362 |
+
loop = asyncio.get_running_loop()
|
| 363 |
+
result_path = await loop.run_in_executor(None, inference2, index)
|
| 364 |
+
|
| 365 |
+
await thread.send(f"✅upscale done")
|
| 366 |
+
with open(result_path, 'rb') as f:
|
| 367 |
+
await thread.send(f'Here is the upscaled image! :) ', file=discord.File(f, 'result.png'))
|
| 368 |
+
|
| 369 |
+
# using custom emoji that looks nicer
|
| 370 |
+
emoji_guild = thread.guild
|
| 371 |
+
confirm_emoji_id = 1098629085955113011
|
| 372 |
+
confirm_emoji = discord.utils.get(emoji_guild.emojis, id=confirm_emoji_id)
|
| 373 |
+
|
| 374 |
+
# assuming dfif2 is always inside a thread, we can always exit the thread to find the channel with the original command,
|
| 375 |
+
# which allows us to react confirm on that message.
|
| 376 |
+
parent_channel = thread.parent
|
| 377 |
+
dfif_command_message = await parent_channel.fetch_message(dfif_command_message_id)
|
| 378 |
+
|
| 379 |
+
# reacting to original !deepfloydif command + using a custom emoji to do it
|
| 380 |
+
await dfif_command_message.add_reaction(confirm_emoji)
|
| 381 |
+
await thread.send(f"✅upscale posted")
|
| 382 |
+
#await ctx.reply('Here is the result of the second stage', file=discord.File(f, 'result.png'))
|
| 383 |
+
#await ctx.message.add_reaction('✅') need to fix this
|
|
|
|
|
|
|
| 384 |
|
| 385 |
'''
|
| 386 |
try:
|
|
|
|
| 398 |
@bot.event
|
| 399 |
async def on_reaction_add(reaction, user): # ctx = await bot.get_context(reaction.message)? could try later, might simplify
|
| 400 |
try:
|
| 401 |
+
ctx = await bot.get_context(reaction.message)
|
| 402 |
# safety checks first ❌
|
| 403 |
thread = reaction.message.channel
|
| 404 |
threadparentid = thread.parent.id
|
|
|
|
| 436 |
stage_1_result_path = fullpath
|
| 437 |
thread = reaction.message.channel
|
| 438 |
dfif_command_message_id = messageid
|
| 439 |
+
ctx = await bot.get_context(reaction.message)
|
| 440 |
await reaction.message.channel.send(f"calling dfif2")
|
| 441 |
+
await dfif2(ctx, index, stage_1_result_path, thread, dfif_command_message_id)
|
| 442 |
|
| 443 |
'''
|
| 444 |
|