[asyncio.queue] testing
Browse files
app.py
CHANGED
|
@@ -9,37 +9,17 @@ import time
|
|
| 9 |
import re
|
| 10 |
from discord import Embed, Color
|
| 11 |
from discord.ext import commands
|
| 12 |
-
# test # fix? # unstick # fix
|
| 13 |
from gradio_client import Client
|
| 14 |
from PIL import Image
|
| 15 |
#from ratelimiter import RateLimiter
|
| 16 |
-
#
|
| 17 |
-
from datetime import datetime
|
| 18 |
-
from pytz import timezone
|
| 19 |
-
#
|
| 20 |
-
import asyncio
|
| 21 |
-
|
| 22 |
-
import shutil # for doing image movement magic
|
| 23 |
-
|
| 24 |
-
#todo
|
| 25 |
-
# 4 -> combined image
|
| 26 |
-
# loading emoji? animated emojis?
|
| 27 |
-
# tasks for concurrent coroutines
|
| 28 |
-
# ratelimits
|
| 29 |
-
|
| 30 |
-
# enlarge each of 4 images?
|
| 31 |
-
# Error: [Errno 104] Connection reset by peer?
|
| 32 |
|
| 33 |
-
|
| 34 |
-
|
| 35 |
-
|
| 36 |
-
# lock generation after ~120s, can change
|
| 37 |
-
# restructure using slash commands? generate -> deepfloydif -> prompt -> thread -> combined -> upscale -> thread
|
| 38 |
|
| 39 |
DFIF_TOKEN = os.getenv('HF_TOKEN')
|
| 40 |
DISCORD_TOKEN = os.environ.get("GRADIOTEST_TOKEN", None)
|
| 41 |
|
| 42 |
-
df = Client("huggingface-projects/IF", DFIF_TOKEN)
|
| 43 |
jojogan = Client("akhaliq/JoJoGAN", DFIF_TOKEN)
|
| 44 |
|
| 45 |
|
|
@@ -49,385 +29,54 @@ intents.message_content = True
|
|
| 49 |
bot = commands.Bot(command_prefix='!', intents=intents)
|
| 50 |
|
| 51 |
|
| 52 |
-
|
| 53 |
-
@bot.event
|
| 54 |
-
async def on_ready():
|
| 55 |
-
print('Logged on as', bot.user)
|
| 56 |
-
bot.log_channel = bot.get_channel(1100458786826747945) # 1100458786826747945 = bot-test, 1107006391547342910 = lunarbot server
|
| 57 |
-
#---------------------------------------------------------------------------------------------------------------------------------------------
|
| 58 |
-
async def safetychecks(ctx):
|
| 59 |
-
try:
|
| 60 |
-
if ctx.author.bot:
|
| 61 |
-
print(f"Error: The bot is not allowed to use its own commands.")
|
| 62 |
-
return False
|
| 63 |
-
|
| 64 |
-
#β
β
check if the bot is offline
|
| 65 |
-
offline_bot_role_id = 1103676632667017266
|
| 66 |
-
bot_member = ctx.guild.get_member(bot.user.id)
|
| 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 != 1100458786826747945:
|
| 74 |
-
print(f"Error: This is not a permitted channel for that command. (bot-test is the correct channel)")
|
| 75 |
-
return False
|
| 76 |
-
|
| 77 |
-
#β
β
check if the user has the required role(s)
|
| 78 |
-
guild_id = 879548962464493619
|
| 79 |
-
required_role_id = 900063512829755413 # @verified for now
|
| 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()
|
| 94 |
async def jojo(ctx):
|
| 95 |
-
|
| 96 |
-
|
| 97 |
-
|
| 98 |
-
|
| 99 |
-
|
| 100 |
-
|
| 101 |
-
|
| 102 |
-
|
| 103 |
-
try:
|
| 104 |
-
if await safetychecks(ctx): #β
|
| 105 |
-
await ctx.message.add_reaction('π€')
|
| 106 |
-
thread = await ctx.message.create_thread(name=f'{ctx.author} Jojo Thread')
|
| 107 |
-
if ctx.message.attachments:
|
| 108 |
-
await thread.send(f'{ctx.author.mention}Generating images in thread, can take ~1 minute...yare yare, daze ...')
|
| 109 |
-
attachment = ctx.message.attachments[0]
|
| 110 |
-
style = 'JoJo'
|
| 111 |
-
im = jojogan.predict(attachment.url, style)
|
| 112 |
-
#await ctx.message.reply(f'Here is the {style} version of it', file=discord.File(im))
|
| 113 |
-
await thread.send(f'{ctx.author.mention}Here is the {style} version of it', file=discord.File(im))
|
| 114 |
-
await ctx.message.add_reaction('β
') # img + face
|
| 115 |
-
else: # no image
|
| 116 |
-
await thread.send(f"{ctx.author.mention}No attachments to be found...Can't animify dat! Try sending me an image π")
|
| 117 |
-
await ctx.message.add_reaction('β')
|
| 118 |
-
except Exception as e: # no generation / img + no face
|
| 119 |
-
print(f"Error: {e}")
|
| 120 |
-
await thread.send(f"{ctx.author.mention}Error: {e}")
|
| 121 |
-
await ctx.message.add_reaction('β')
|
| 122 |
-
|
| 123 |
-
#----------------------------------------------------------------------------------------------------------------------------------------------
|
| 124 |
-
# Disney
|
| 125 |
-
@bot.command()
|
| 126 |
-
async def disney(ctx):
|
| 127 |
-
try:
|
| 128 |
-
if await safetychecks(ctx): #β
|
| 129 |
-
await ctx.message.add_reaction('π€')
|
| 130 |
-
thread = await ctx.message.create_thread(name=f'{ctx.author} disney Thread')
|
| 131 |
-
if ctx.message.attachments:
|
| 132 |
-
await thread.send(f'{ctx.author.mention}Generating images in thread, can take ~1 minute...')
|
| 133 |
-
attachment = ctx.message.attachments[0]
|
| 134 |
-
style = 'disney'
|
| 135 |
-
im = jojogan.predict(attachment.url, style)
|
| 136 |
-
await thread.send(f'{ctx.author.mention}Here is the {style} version of it', file=discord.File(im))
|
| 137 |
-
await ctx.message.add_reaction('β
') # img + face
|
| 138 |
-
else: # no image
|
| 139 |
-
await thread.send(f"{ctx.author.mention}No attachments to be found...Can't animify dat! Try sending me an image π")
|
| 140 |
-
await ctx.message.add_reaction('β')
|
| 141 |
-
except Exception as e: # no generation / img + no face
|
| 142 |
-
print(f"Error: {e}")
|
| 143 |
-
await thread.send(f"{ctx.author.mention}Error: {e}")
|
| 144 |
-
await ctx.message.add_reaction('β')
|
| 145 |
-
|
| 146 |
-
#----------------------------------------------------------------------------------------------------------------------------------------------
|
| 147 |
-
# Spider-Verse
|
| 148 |
-
@bot.command()
|
| 149 |
-
async def spiderverse(ctx):
|
| 150 |
-
try:
|
| 151 |
-
if await safetychecks(ctx): #β
|
| 152 |
-
await ctx.message.add_reaction('π€')
|
| 153 |
-
thread = await ctx.message.create_thread(name=f'{ctx.author} spider-verse Thread')
|
| 154 |
-
if ctx.message.attachments:
|
| 155 |
-
await thread.send(f'{ctx.author.mention}Generating images in thread, can take ~1 minute...')
|
| 156 |
-
attachment = ctx.message.attachments[0]
|
| 157 |
-
style = 'Spider-Verse'
|
| 158 |
-
im = jojogan.predict(attachment.url, style)
|
| 159 |
-
await thread.send(f'{ctx.author.mention}Here is the {style} version of it', file=discord.File(im))
|
| 160 |
-
await ctx.message.add_reaction('β
') # img + face
|
| 161 |
-
else: # no image
|
| 162 |
-
await thread.send(f"{ctx.author.mention}No attachments to be found...Can't animify dat! Try sending me an image π")
|
| 163 |
-
await ctx.message.add_reaction('β')
|
| 164 |
-
except Exception as e: # no generation / img + no face
|
| 165 |
-
print(f"Error: {e}")
|
| 166 |
-
await thread.send(f"{ctx.author.mention}Error: {e}")
|
| 167 |
-
await ctx.message.add_reaction('β')
|
| 168 |
-
|
| 169 |
-
#----------------------------------------------------------------------------------------------------------------------------------------------
|
| 170 |
-
# sketch
|
| 171 |
-
@bot.command()
|
| 172 |
-
async def sketch(ctx):
|
| 173 |
-
try:
|
| 174 |
-
if await safetychecks(ctx): #β
|
| 175 |
-
await ctx.message.add_reaction('π€')
|
| 176 |
-
thread = await ctx.message.create_thread(name=f'{ctx.author} sketch Thread')
|
| 177 |
-
if ctx.message.attachments:
|
| 178 |
-
await thread.send(f'{ctx.author.mention}Generating images in thread, can take ~1 minute...')
|
| 179 |
-
attachment = ctx.message.attachments[0]
|
| 180 |
-
style = 'sketch'
|
| 181 |
-
im = jojogan.predict(attachment.url, style)
|
| 182 |
-
await thread.send(f'{ctx.author.mention}Here is the {style} version of it', file=discord.File(im))
|
| 183 |
-
await ctx.message.add_reaction('β
') # img + face
|
| 184 |
-
else: # no image
|
| 185 |
-
await thread.send(f"{ctx.author.mention}No attachments to be found...Can't animify dat! Try sending me an image π")
|
| 186 |
-
await ctx.message.add_reaction('β')
|
| 187 |
-
except Exception as e: # no generation / img + no face
|
| 188 |
-
print(f"Error: {e}")
|
| 189 |
-
await thread.send(f"{ctx.author.mention}Error: {e}")
|
| 190 |
-
await ctx.message.add_reaction('β')
|
| 191 |
-
#----------------------------------------------------------------------------------------------------------------------------------------------
|
| 192 |
-
# Stage 1
|
| 193 |
-
@bot.command()
|
| 194 |
-
async def deepfloydif(ctx, *, prompt: str):
|
| 195 |
-
try:
|
| 196 |
-
try:
|
| 197 |
-
if await safetychecks(ctx): #β
|
| 198 |
-
await ctx.message.add_reaction('π€') # loading emoji?
|
| 199 |
-
dfif_command_message_id = ctx.message.id # we will use this in some magic later on
|
| 200 |
-
thread = await ctx.message.create_thread(name=f'{ctx.author} DeepfloydIF Image Upscaling Thread ')
|
| 201 |
-
# create thread -> send new message inside thread + combined_image -> add reactions -> dfif2
|
| 202 |
-
|
| 203 |
-
#current_time = int(time.time())
|
| 204 |
-
#random.seed(current_time)
|
| 205 |
-
|
| 206 |
-
negative_prompt = ''
|
| 207 |
-
seed = random.randint(0, 1000)
|
| 208 |
-
#seed = 1
|
| 209 |
-
number_of_images = 4
|
| 210 |
-
guidance_scale = 7
|
| 211 |
-
custom_timesteps_1 = 'smart50'
|
| 212 |
-
number_of_inference_steps = 50
|
| 213 |
-
|
| 214 |
-
await thread.send(f'{ctx.author.mention}Generating images in thread, can take ~1 minute...')
|
| 215 |
-
|
| 216 |
-
except Exception as e:
|
| 217 |
-
print(f"Error: {e}")
|
| 218 |
-
await ctx.reply('stage 1 error -> pre generation')
|
| 219 |
-
await ctx.message.add_reaction('β')
|
| 220 |
-
|
| 221 |
-
try:
|
| 222 |
-
stage_1_results, stage_1_param_path, stage_1_result_path = df.predict(
|
| 223 |
-
prompt, negative_prompt, seed, number_of_images, guidance_scale, custom_timesteps_1, number_of_inference_steps, api_name='/generate64')
|
| 224 |
-
|
| 225 |
-
partialpath = stage_1_result_path[5:] #magic for later
|
| 226 |
-
except Exception as e:
|
| 227 |
-
print(f"Error: {e}")
|
| 228 |
-
await ctx.reply('stage 1 error -> during generation')
|
| 229 |
-
await ctx.message.add_reaction('β')
|
| 230 |
-
|
| 231 |
-
try:
|
| 232 |
-
png_files = [f for f in os.listdir(stage_1_results) if f.endswith('.png')]
|
| 233 |
-
|
| 234 |
-
if png_files:
|
| 235 |
-
first_png = png_files[0]
|
| 236 |
-
second_png = png_files[1]
|
| 237 |
-
third_png = png_files[2]
|
| 238 |
-
fourth_png = png_files[3]
|
| 239 |
-
|
| 240 |
-
first_png_path = os.path.join(stage_1_results, first_png)
|
| 241 |
-
second_png_path = os.path.join(stage_1_results, second_png)
|
| 242 |
-
third_png_path = os.path.join(stage_1_results, third_png)
|
| 243 |
-
fourth_png_path = os.path.join(stage_1_results, fourth_png)
|
| 244 |
-
|
| 245 |
-
img1 = Image.open(first_png_path)
|
| 246 |
-
img2 = Image.open(second_png_path)
|
| 247 |
-
img3 = Image.open(third_png_path)
|
| 248 |
-
img4 = Image.open(fourth_png_path)
|
| 249 |
-
|
| 250 |
-
combined_image = Image.new('RGB', (img1.width * 2, img1.height * 2))
|
| 251 |
-
|
| 252 |
-
combined_image.paste(img1, (0, 0))
|
| 253 |
-
combined_image.paste(img2, (img1.width, 0))
|
| 254 |
-
combined_image.paste(img3, (0, img1.height))
|
| 255 |
-
combined_image.paste(img4, (img1.width, img1.height))
|
| 256 |
-
|
| 257 |
-
combined_image_path = os.path.join(stage_1_results, f'{partialpath}{dfif_command_message_id}.png')
|
| 258 |
-
combined_image.save(combined_image_path)
|
| 259 |
-
|
| 260 |
-
with open(combined_image_path, 'rb') as f:
|
| 261 |
-
combined_image_dfif = await thread.send(f'{ctx.author.mention}React with the image number you want to upscale!', file=discord.File(
|
| 262 |
-
f, f'{partialpath}{dfif_command_message_id}.png')) # named something like: tmpgtv4qjix1111269940599738479.png
|
| 263 |
-
|
| 264 |
-
for emoji in ['1οΈβ£', '2οΈβ£', '3οΈβ£', '4οΈβ£']:
|
| 265 |
-
await combined_image_dfif.add_reaction(emoji)
|
| 266 |
-
''' individual images
|
| 267 |
-
if png_files:
|
| 268 |
-
for i, png_file in enumerate(png_files):
|
| 269 |
-
png_file_path = os.path.join(stage_1_results, png_file)
|
| 270 |
-
img = Image.open(png_file_path)
|
| 271 |
-
image_path = os.path.join(stage_1_results, f'{i+1}{partialpath}.png')
|
| 272 |
-
img.save(image_path)
|
| 273 |
-
with open(image_path, 'rb') as f:
|
| 274 |
-
await thread.send(f'{ctx.author.mention}Image {i+1}', file=discord.File(f, f'{i+1}{partialpath}.png'))
|
| 275 |
-
await asyncio.sleep(1)
|
| 276 |
-
|
| 277 |
-
'''
|
| 278 |
-
|
| 279 |
-
except Exception as e:
|
| 280 |
-
print(f"Error: {e}")
|
| 281 |
-
await ctx.reply('stage 1 error -> posting images in thread')
|
| 282 |
-
await ctx.message.add_reaction('β')
|
| 283 |
-
|
| 284 |
-
#deepfloydif try/except
|
| 285 |
-
except Exception as e:
|
| 286 |
-
print(f"Error: {e}")
|
| 287 |
-
await ctx.reply('An error occurred in stage 1 for deepfloydif')
|
| 288 |
-
await ctx.message.add_reaction('β')
|
| 289 |
-
|
| 290 |
#----------------------------------------------------------------------------------------------------------------------------
|
| 291 |
-
|
| 292 |
-
|
| 293 |
-
|
| 294 |
-
|
| 295 |
-
selected_index_for_stage_2 = index
|
| 296 |
-
seed_2 = 0
|
| 297 |
-
guidance_scale_2 = 4
|
| 298 |
-
custom_timesteps_2 = 'smart50'
|
| 299 |
-
number_of_inference_steps_2 = 50
|
| 300 |
-
result_path = df.predict(stage_1_result_path, selected_index_for_stage_2, seed_2,
|
| 301 |
-
guidance_scale_2, custom_timesteps_2, number_of_inference_steps_2, api_name='/upscale256')
|
| 302 |
|
| 303 |
-
|
| 304 |
-
|
| 305 |
-
|
|
|
|
|
|
|
| 306 |
|
| 307 |
-
|
| 308 |
-
|
| 309 |
-
|
| 310 |
-
|
| 311 |
-
|
| 312 |
-
# assuming dfif2 is always inside a thread, we can always exit the thread to find the channel with the original command,
|
| 313 |
-
# which allows us to react confirm on that message.
|
| 314 |
-
parent_channel = thread.parent
|
| 315 |
-
dfif_command_message = await parent_channel.fetch_message(dfif_command_message_id)
|
| 316 |
|
| 317 |
-
#
|
| 318 |
-
|
| 319 |
-
|
| 320 |
-
|
| 321 |
-
|
| 322 |
|
| 323 |
-
|
| 324 |
-
|
| 325 |
-
|
| 326 |
-
await dfif_command_message.add_reaction('β
')
|
| 327 |
-
'''
|
| 328 |
-
|
| 329 |
-
except Exception as e:
|
| 330 |
-
print(f"Error: {e}")
|
| 331 |
-
|
| 332 |
-
#await ctx.reply('An error occured in stage 2') need to fix
|
| 333 |
-
#await ctx.message.add_reaction('β')
|
| 334 |
-
#----------------------------------------------------------------------------------------------------------------------------
|
| 335 |
-
@bot.event
|
| 336 |
-
async def on_reaction_add(reaction, user): # ctx = await bot.get_context(reaction.message)? could try later, might simplify
|
| 337 |
-
try:
|
| 338 |
-
# safety checks first β
|
| 339 |
-
'''
|
| 340 |
-
if user.bot:
|
| 341 |
-
return
|
| 342 |
|
| 343 |
-
#
|
| 344 |
-
|
| 345 |
-
bot_member = reaction.message.guild.get_member(bot.user.id)
|
| 346 |
-
if any(role.id == offline_bot_role_id for role in bot_member.roles):
|
| 347 |
-
return
|
| 348 |
-
|
| 349 |
-
# verified role check β
|
| 350 |
-
guild = reaction.message.guild
|
| 351 |
-
required_role_id = 900063512829755413 # @verified for now
|
| 352 |
-
required_role = guild.get_role(required_role_id)
|
| 353 |
-
if required_role not in user.roles:
|
| 354 |
-
return
|
| 355 |
|
| 356 |
-
#
|
| 357 |
-
|
| 358 |
-
|
| 359 |
-
|
| 360 |
-
|
| 361 |
|
| 362 |
-
thread = reaction.message.channel
|
| 363 |
-
threadparentid = thread.parent.id
|
| 364 |
-
|
| 365 |
-
if not user.bot:
|
| 366 |
-
if threadparentid == 1100458786826747945: # bot-test
|
| 367 |
-
# 811235357663297546 = lunarflu
|
| 368 |
-
if reaction.message.attachments:
|
| 369 |
-
if user.id == reaction.message.mentions[0].id: # if user.id == reaction.message.mentions[0].id:
|
| 370 |
-
# magic begins
|
| 371 |
-
await reaction.message.channel.send("reaction detected")
|
| 372 |
-
attachment = reaction.message.attachments[0]
|
| 373 |
-
image_name = attachment.filename # named something like: tmpgtv4qjix1111269940599738479.png
|
| 374 |
-
# remove .png first
|
| 375 |
-
partialpathmessageid = image_name[:-4] # should be tmpgtv4qjix1111269940599738479
|
| 376 |
-
# extract partialpath, messageid
|
| 377 |
-
partialpath = partialpathmessageid[:11] # tmpgtv4qjix
|
| 378 |
-
messageid = partialpathmessageid[11:] # 1111269940599738479
|
| 379 |
-
# add /tmp/ to partialpath, save as new variable
|
| 380 |
-
fullpath = "/tmp/" + partialpath # should be /tmp/tmpgtv4qjix
|
| 381 |
-
await reaction.message.channel.send(f"fullpath extracted, {fullpath}")
|
| 382 |
-
emoji = reaction.emoji
|
| 383 |
-
|
| 384 |
-
if emoji == "1οΈβ£":
|
| 385 |
-
index = 0
|
| 386 |
-
elif emoji == "2οΈβ£":
|
| 387 |
-
index = 1
|
| 388 |
-
elif emoji == "3οΈβ£":
|
| 389 |
-
index = 2
|
| 390 |
-
elif emoji == "4οΈβ£":
|
| 391 |
-
index = 3
|
| 392 |
-
await reaction.message.channel.send(f"index extracted, {index}")
|
| 393 |
-
index = index
|
| 394 |
-
stage_1_result_path = fullpath
|
| 395 |
-
thread = reaction.message.channel
|
| 396 |
-
dfif_command_message_id = messageid
|
| 397 |
-
await reaction.message.channel.send(f"calling dfif2")
|
| 398 |
-
await dfif2(index, stage_1_result_path, thread, dfif_command_message_id)
|
| 399 |
-
|
| 400 |
-
'''
|
| 401 |
|
| 402 |
-
if reaction.message.attachments:
|
| 403 |
-
if user.id == reaction.message.mentions[0].id: # all we care about is upscaling whatever image this is
|
| 404 |
-
|
| 405 |
-
# magic begins
|
| 406 |
-
attachment = reaction.message.attachments[0]
|
| 407 |
-
image_name = attachment.filename
|
| 408 |
-
# we know image_name will be something like 1tmpgtv4qjix.png
|
| 409 |
-
# remove .png first
|
| 410 |
-
indexpartialpath = image_name[:-4] # should be 1tmpgtv4qjix
|
| 411 |
-
# extract index as an integer (dfif2 needs integer)
|
| 412 |
-
index = int(indexpartialpath[0]) - 1# should be 1
|
| 413 |
-
# extract partialpath
|
| 414 |
-
partialpath = indexpartialpath[1:] # should be tmpgtv4qjix
|
| 415 |
-
# add /tmp/ to partialpath, save as new variable
|
| 416 |
-
fullpath = "/tmp/" + partialpath # should be /tmp/tmpgtv4qjix
|
| 417 |
-
|
| 418 |
-
stage_1_result_path = fullpath
|
| 419 |
-
index = index
|
| 420 |
-
await dfif2(index, stage_1_result_path, thread)
|
| 421 |
-
|
| 422 |
-
'''
|
| 423 |
|
| 424 |
-
|
| 425 |
-
except Exception as e:
|
| 426 |
-
print(f"Error: {e}")
|
| 427 |
-
|
| 428 |
-
#await ctx.reply('An error occured in stage 2') need to fix
|
| 429 |
-
#await ctx.message.add_reaction('β')
|
| 430 |
-
#----------------------------------------------------------------------------------------------------------------------------
|
| 431 |
|
| 432 |
|
| 433 |
def run_bot():
|
|
|
|
| 9 |
import re
|
| 10 |
from discord import Embed, Color
|
| 11 |
from discord.ext import commands
|
|
|
|
| 12 |
from gradio_client import Client
|
| 13 |
from PIL import Image
|
| 14 |
#from ratelimiter import RateLimiter
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 15 |
|
| 16 |
+
import asyncio
|
| 17 |
+
import httpx
|
| 18 |
+
from tasks import make_request_async
|
|
|
|
|
|
|
| 19 |
|
| 20 |
DFIF_TOKEN = os.getenv('HF_TOKEN')
|
| 21 |
DISCORD_TOKEN = os.environ.get("GRADIOTEST_TOKEN", None)
|
| 22 |
|
|
|
|
| 23 |
jojogan = Client("akhaliq/JoJoGAN", DFIF_TOKEN)
|
| 24 |
|
| 25 |
|
|
|
|
| 29 |
bot = commands.Bot(command_prefix='!', intents=intents)
|
| 30 |
|
| 31 |
|
| 32 |
+
#---------------------------------------------------------------------------------------------------------------------------
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 33 |
async def jojo(ctx):
|
| 34 |
+
start_time = time.time()
|
| 35 |
+
style = 'JoJo'
|
| 36 |
+
atchurl = 'https://cdn.discordapp.com/attachments/1100458786826747945/1111746037640601610/image.png'
|
| 37 |
+
im = jojogan.predict(atchurl, style)
|
| 38 |
+
end_time = time.time()
|
| 39 |
+
generation_time = end_time - start_time
|
| 40 |
+
await ctx.send(f"{style} image generated in {generation_time:.2f} seconds.")
|
| 41 |
+
await ctx.send(file=discord.File(im))
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 42 |
#----------------------------------------------------------------------------------------------------------------------------
|
| 43 |
+
async def make_request_async(num, client):
|
| 44 |
+
# Make an asynchronous HTTP request
|
| 45 |
+
response = await jojo(ctx)
|
| 46 |
+
print(f"Response for request {num}: {response.status_code}")
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 47 |
|
| 48 |
+
async def process_requests(queue, client):
|
| 49 |
+
while True:
|
| 50 |
+
num = await queue.get()
|
| 51 |
+
await make_request_async(num, client)
|
| 52 |
+
queue.task_done()
|
| 53 |
|
| 54 |
+
@bot.command()
|
| 55 |
+
async def command(ctx, num_requests: int):
|
| 56 |
+
async with httpx.AsyncClient() as client:
|
| 57 |
+
queue = asyncio.Queue()
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 58 |
|
| 59 |
+
# Start the request processing tasks
|
| 60 |
+
request_tasks = []
|
| 61 |
+
for _ in range(num_requests):
|
| 62 |
+
task = asyncio.create_task(process_requests(queue, client))
|
| 63 |
+
request_tasks.append(task)
|
| 64 |
|
| 65 |
+
# Add the requests to the queue
|
| 66 |
+
for num in range(1, num_requests + 1):
|
| 67 |
+
await queue.put(num)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 68 |
|
| 69 |
+
# Wait for all requests to be processed
|
| 70 |
+
await queue.join()
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 71 |
|
| 72 |
+
# Cancel the request processing tasks
|
| 73 |
+
for task in request_tasks:
|
| 74 |
+
task.cancel()
|
| 75 |
+
|
| 76 |
+
await ctx.send("Command executed.")
|
| 77 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 78 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 79 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 80 |
|
| 81 |
|
| 82 |
def run_bot():
|