lunarflu HF Staff commited on
Commit
940edd3
·
1 Parent(s): 5c9ffae

[dfif] big change, removed inference, testing normal generation directly in dfif function

Browse files
Files changed (1) hide show
  1. app.py +238 -130
app.py CHANGED
@@ -9,7 +9,7 @@ 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
@@ -20,6 +20,12 @@ import multiprocessing
20
 
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
@@ -46,14 +52,19 @@ DISCORD_TOKEN = os.environ.get("GRADIOTEST_TOKEN", None)
46
 
47
  df = Client("huggingface-projects/IF", GRADIOTEST_TOKEN)
48
  jojogan = Client("akhaliq/JoJoGAN", GRADIOTEST_TOKEN)
49
-
50
 
51
  intents = discord.Intents.default()
52
  intents.message_content = True
53
 
54
  bot = commands.Bot(command_prefix='!', intents=intents)
55
 
56
-
 
 
 
 
 
57
  #---------------------------------------------------------------------------------------------------------------------------------------------
58
  @bot.event
59
  async def on_ready():
@@ -66,7 +77,7 @@ async def commands(ctx):
66
  if await safetychecks(ctx):
67
  await ctx.reply(f"Use !deepfloydif [prompt], !jojo !spidey or !sketch. Have fun! 🤗💖")
68
  except Exception as e:
69
- print(f"Error: unable to help :(")
70
  #---------------------------------------------------------------------------------------------------------------------------------------------
71
  async def safetychecks(ctx):
72
  failure_emoji = '<:disagree:1098628957521313892>'
@@ -88,8 +99,13 @@ async def safetychecks(ctx):
88
 
89
  #✅✅ check if the command is in the allowed channel(s)
90
  bot_test = 1100458786826747945
91
- testing_the_bot = 1113182673859518514
92
- channel_ids = [bot_test, testing_the_bot]
 
 
 
 
 
93
  if ctx.channel.id not in channel_ids:
94
  print(f"{ctx.author}, commands are not permitted in {ctx.channel}")
95
  thread = await ctx.message.create_thread(name=f'Channel Error')
@@ -121,8 +137,8 @@ async def safetychecks(ctx):
121
 
122
  # ping lunarflu if any safety check ever fails
123
  except Exception as e:
124
- print(f"Error: safetychecks failed somewhere, command will not continue.")
125
- await ctx.message.reply(f"❌ <@811235357663297546> SC failed somewhere ❌") # this will always ping, as long as the bot has access to the channel
126
  await ctx.message.add_reaction(failure_emoji)
127
  #----------------------------------------------------------------------------------------------------------------------------------------------
128
  @bot.command()
@@ -139,13 +155,41 @@ async def deepfloydifdemo(ctx):
139
  @bot.command()
140
  async def jojodemo(ctx):
141
  try:
142
- thread = await ctx.message.create_thread(name=f'{ctx.author} Demo Thread')
143
  await thread.send(f'{ctx.author.mention} Here is a demo for the !jojo command!')
144
  await asyncio.sleep(0.5)
145
- await thread.send(f'https://cdn.discordapp.com/attachments/932563860597121054/1113492260294770778/image.png')
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
146
  except Exception as e:
147
  print(f"Error: {e}")
148
  await ctx.message.add_reaction('<:disagree:1098628957521313892>')
 
149
  #----------------------------------------------------------------------------------------------------------------------------------------------
150
  # jojo ✅
151
  @bot.command()
@@ -159,114 +203,98 @@ async def jojo(ctx):
159
  # safety checks?✅
160
  # bot no crash ✅
161
  try:
162
- if await safetychecks(ctx): #✅
163
- await ctx.message.add_reaction('<a:loading:1114111677990981692>')
164
- thread = await ctx.message.create_thread(name=f'{ctx.author} Jojo Thread')
165
- if ctx.message.attachments:
166
- await thread.send(f'{ctx.author.mention} Generating images in thread, can take ~1 minute...yare yare, daze ...')
167
- attachment = ctx.message.attachments[0]
168
- style = 'JoJo'
169
- #im = jojogan.predict(attachment.url, style)
170
- im = await asyncio.get_running_loop().run_in_executor(None, jojogan.predict, attachment.url, style)
171
- #await ctx.message.reply(f'Here is the {style} version of it', file=discord.File(im))
172
- await thread.send(f'{ctx.author.mention} Here is the {style} version of it', file=discord.File(im))
173
-
174
- #testing animated
175
- # <a:hugging_spin:1102656012621713488>
176
- await ctx.message.add_reaction('<:agree:1098629085955113011>') # img + face
177
- await ctx.message.remove_reaction('<a:loading:1114111677990981692>', bot.user)
178
- else: # no image
179
- await thread.send(f"{ctx.author.mention} No attachments to be found...Can't animify dat! Try sending me an image 😉")
180
- await ctx.message.add_reaction('<:disagree:1098628957521313892>')
181
- await ctx.message.remove_reaction('<a:loading:1114111677990981692>', bot.user)
 
 
 
182
  except Exception as e: # no generation / img + no face
183
  await fullqueue(e, thread)
184
  print(f"Error: {e}")
185
  await thread.send(f"{ctx.author.mention} Error: {e}")
186
- await ctx.message.add_reaction('<:disagree:1098628957521313892>')
187
  await ctx.message.remove_reaction('<a:loading:1114111677990981692>', bot.user)
 
188
 
189
- #----------------------------------------------------------------------------------------------------------------------------------------------
190
- # Disney ❌
191
- @bot.command()
192
- async def disney(ctx):
193
- try:
194
- if await safetychecks(ctx): #✅
195
- await ctx.message.add_reaction('<a:loading:1114111677990981692>')
196
- thread = await ctx.message.create_thread(name=f'{ctx.author} disney Thread')
197
- if ctx.message.attachments:
198
- await thread.send(f'{ctx.author.mention} Generating images in thread, can take ~1 minute...')
199
- attachment = ctx.message.attachments[0]
200
- style = 'Disney'
201
- im = await asyncio.get_running_loop().run_in_executor(None, jojogan.predict, attachment.url, 'disney')
202
- await thread.send(f'{ctx.author.mention} Here is the {style} version of it', file=discord.File(im))
203
- await ctx.message.add_reaction('<:agree:1098629085955113011>') # img + face
204
- await ctx.message.remove_reaction('<a:loading:1114111677990981692>', bot.user)
205
- else: # no image
206
- await thread.send(f"{ctx.author.mention} No attachments to be found...Can't animify dat! Try sending me an image 😉")
207
- await ctx.message.add_reaction('<:disagree:1098628957521313892>')
208
- await ctx.message.remove_reaction('<a:loading:1114111677990981692>', bot.user)
209
-
210
- except Exception as e: # no generation / img + no face
211
- await fullqueue(e, thread)
212
- print(f"Error: {e}")
213
- await thread.send(f"{ctx.author.mention} Error: {e}")
214
- await ctx.message.add_reaction('<:disagree:1098628957521313892>')
215
- await ctx.message.remove_reaction('<a:loading:1114111677990981692>', bot.user)
216
-
217
  #----------------------------------------------------------------------------------------------------------------------------------------------
218
  # Spider-Verse ✅
219
  @bot.command()
220
  async def spidey(ctx):
221
  try:
222
  if await safetychecks(ctx): #✅
223
- await ctx.message.add_reaction('<a:loading:1114111677990981692>')
224
- thread = await ctx.message.create_thread(name=f'{ctx.author} spider-verse Thread')
225
- if ctx.message.attachments:
226
- await thread.send(f'{ctx.author.mention} Generating images in thread, can take ~1 minute...')
227
- attachment = ctx.message.attachments[0]
228
- style = 'Spider-Verse'
229
- im = await asyncio.get_running_loop().run_in_executor(None, jojogan.predict, attachment.url, style)
230
- await thread.send(f'{ctx.author.mention} Here is the {style} version of it', file=discord.File(im))
231
- await ctx.message.add_reaction('<:agree:1098629085955113011>') # img + face
232
- await ctx.message.remove_reaction('<a:loading:1114111677990981692>', bot.user)
233
- else: # no image
234
- await thread.send(f"{ctx.author.mention} No attachments to be found...Can't animify dat! Try sending me an image 😉")
235
- await ctx.message.add_reaction('<:disagree:1098628957521313892>')
236
- await ctx.message.remove_reaction('<a:loading:1114111677990981692>', bot.user)
 
 
 
237
  except Exception as e: # no generation / img + no face
238
  await fullqueue(e, thread)
239
  print(f"Error: {e}")
240
  await thread.send(f"{ctx.author.mention} Error: {e}")
241
  await ctx.message.add_reaction('<:disagree:1098628957521313892>')
242
  await ctx.message.remove_reaction('<a:loading:1114111677990981692>', bot.user)
243
-
244
  #----------------------------------------------------------------------------------------------------------------------------------------------
245
  # sketch ✅
246
  @bot.command()
247
  async def sketch(ctx):
248
  try:
249
  if await safetychecks(ctx): #✅
250
- await ctx.message.add_reaction('<a:loading:1114111677990981692>')
251
- thread = await ctx.message.create_thread(name=f'{ctx.author} sketch Thread')
252
- if ctx.message.attachments:
253
- await thread.send(f'{ctx.author.mention} Generating images in thread, can take ~1 minute...')
254
- attachment = ctx.message.attachments[0]
255
- #style = 'sketch'
256
- im = await asyncio.get_running_loop().run_in_executor(None, jojogan.predict, attachment.url, 'sketch')
257
- await thread.send(f'{ctx.author.mention} Here is the sketch version of it', file=discord.File(im))
258
- await ctx.message.add_reaction('<:agree:1098629085955113011>') # img + face
259
- await ctx.message.remove_reaction('<a:loading:1114111677990981692>', bot.user)
260
- else: # no image
261
- await thread.send(f"{ctx.author.mention} No attachments to be found...Can't animify dat! Try sending me an image 😉")
262
- await ctx.message.add_reaction('<:disagree:1098628957521313892>')
263
- await ctx.message.remove_reaction('<a:loading:1114111677990981692>', bot.user)
 
 
 
264
  except Exception as e: # no generation / img + no face
265
  await fullqueue(e, thread)
266
  print(f"Error: {e}")
267
  await thread.send(f"{ctx.author.mention} Error: {e}")
268
  await ctx.message.add_reaction('<:disagree:1098628957521313892>')
269
  await ctx.message.remove_reaction('<a:loading:1114111677990981692>', bot.user)
 
270
  #----------------------------------------------------------------------------------------------------------------------------------------------
271
  async def fullqueue(e, thread):
272
  error_message = str(e)
@@ -274,9 +302,13 @@ async def fullqueue(e, thread):
274
  await thread.send("Queue is full! Please try again.")
275
  elif "Error: Queue is full! Please try again." in error_message:
276
  await thread.send("Queue is full! Please try again.")
 
277
  elif "local variable 'stage_1_results' referenced before assignment" in error_message:
278
- await thread.send("Queue is full! Please try again.")
279
- #----------------------------------------------------------------------------------------------------------------------------------------------
 
 
 
280
  # deepfloydif stage 1 generation ✅
281
  def inference(prompt):
282
  negative_prompt = ''
@@ -312,60 +344,70 @@ async def react1234(reaction_emojis, combined_image_dfif):
312
  # Stage 1 ✅
313
  @bot.command()
314
  async def deepfloydif(ctx, *, prompt: str):
 
315
  try:
316
  try:
317
  if await safetychecks(ctx): #✅
318
- await ctx.message.add_reaction('<a:loading:1114111677990981692>')
319
- dfif_command_message_id = ctx.message.id # we will use this in some magic later on
320
- thread = await ctx.message.create_thread(name=f'{ctx.author} DeepfloydIF Image Upscaling Thread ')
321
- # create thread -> send new message inside thread + combined_image -> add reactions -> dfif2
322
-
323
- #current_time = int(time.time())
324
- #random.seed(current_time)
325
-
326
- negative_prompt = ''
327
- seed = random.randint(0, 1000)
328
- #seed = 1
329
- number_of_images = 4
330
- guidance_scale = 7
331
- custom_timesteps_1 = 'smart50'
332
- number_of_inference_steps = 50
333
- api_name = '/generate64'
334
- await thread.send(f'{ctx.author.mention}Generating images in thread, can take ~1 minute...')
 
335
 
336
  except Exception as e:
337
  print(f"Error: {e}")
338
- thread = await ctx.message.create_thread(name=f'DFIF1 Error')
339
- await thread.send(f"{ctx.author.mention} Error during stage 1 pre-generation.")
 
340
  await fullqueue(e, thread)
341
  await ctx.message.remove_reaction('<a:loading:1114111677990981692>', bot.user)
342
  await ctx.message.add_reaction('<:disagree:1098628957521313892>')
343
-
344
  #generation✅-------------------------------------------------------
345
  try:
346
  #stage_1_results, stage_1_param_path, stage_1_result_path = df.predict(
347
  # prompt, negative_prompt, seed, number_of_images, guidance_scale, custom_timesteps_1, number_of_inference_steps, api_name='/generate64')
348
 
349
  # run blocking function in executor
350
- #await thread.send(f'✅running blocking function in executor')
351
- loop = asyncio.get_running_loop()
352
- result = await loop.run_in_executor(None, inference, prompt)
353
- #await thread.send(f'✅run_in_executor ran successfully')
354
- stage_1_results = result[0]
355
- stage_1_result_path = result[2]
 
 
 
 
 
356
 
357
  partialpath = stage_1_result_path[5:] #magic for later
358
 
359
  except Exception as e:
360
  print(f"Error: {e}")
361
- thread = await ctx.message.create_thread(name=f'Generation Error')
362
- await thread.send(f"{ctx.author.mention} Error during stage 1 generation.")
 
363
  await fullqueue(e, thread)
364
  await ctx.message.remove_reaction('<a:loading:1114111677990981692>', bot.user)
365
- await ctx.message.add_reaction('<:disagree:1098628957521313892>')
 
366
  #posting images✅----------------------------------------------------------------
367
  try:
368
- #await thread.send(f'✅combining images...')
369
  png_files = [f for f in os.listdir(stage_1_results) if f.endswith('.png')]
370
 
371
  if png_files:
@@ -398,7 +440,7 @@ async def deepfloydif(ctx, *, prompt: str):
398
  combined_image_dfif = await thread.send(f'{ctx.author.mention}React with the image number you want to upscale!', file=discord.File(
399
  f, f'{partialpath}{dfif_command_message_id}.png')) # named something like: tmpgtv4qjix1111269940599738479.png
400
 
401
- #await thread.send(f'✅reacting with 1234...')
402
  emoji_list = ['↖️', '↗️', '↙️', '↘️']
403
  await react1234(emoji_list, combined_image_dfif)
404
 
@@ -417,21 +459,23 @@ async def deepfloydif(ctx, *, prompt: str):
417
 
418
  except Exception as e:
419
  print(f"Error: {e}")
420
- thread = await ctx.message.create_thread(name=f'Posting Error')
421
- await thread.send(f"{ctx.author.mention} Encountered error while posting combined image in thread.")
 
422
  await fullqueue(e, thread)
423
  await ctx.message.remove_reaction('<a:loading:1114111677990981692>', bot.user)
424
  await ctx.message.add_reaction('<:disagree:1098628957521313892>')
425
-
426
  #deepfloydif try/except
427
  except Exception as e:
428
  print(f"Error: {e}")
429
- thread = await ctx.message.create_thread(name=f'deepfloydif Error')
430
- await thread.send(f"{ctx.author.mention} Overall error with deepfloydif.")
 
431
  await fullqueue(e, thread)
432
  await ctx.message.remove_reaction('<a:loading:1114111677990981692>', bot.user)
433
  await ctx.message.add_reaction('<:disagree:1098628957521313892>')
434
-
435
  #----------------------------------------------------------------------------------------------------------------------------
436
  # Stage 2 ✅
437
  async def dfif2(index: int, stage_1_result_path, thread, dfif_command_message_id): # add safetychecks
@@ -459,6 +503,7 @@ async def dfif2(index: int, stage_1_result_path, thread, dfif_command_message_id
459
  dfif_command_message = await parent_channel.fetch_message(dfif_command_message_id)
460
  await dfif_command_message.remove_reaction('<a:loading:1114111677990981692>', bot.user)
461
  await dfif_command_message.add_reaction('<:agree:1098629085955113011>')
 
462
 
463
  except Exception as e:
464
  print(f"Error: {e}")
@@ -466,8 +511,9 @@ async def dfif2(index: int, stage_1_result_path, thread, dfif_command_message_id
466
  dfif_command_message = await parent_channel.fetch_message(dfif_command_message_id)
467
  await dfif_command_message.remove_reaction('<a:loading:1114111677990981692>', bot.user)
468
  await dfif_command_message.add_reaction('<:disagree:1098628957521313892>')
469
- await thread.send(f"Error during stage 2 upscaling.")
470
- await fullqueue(e, thread)
 
471
  #----------------------------------------------------------------------------------------------------------------------------
472
  # react detector for stage 2 ✅
473
  @bot.event
@@ -517,11 +563,73 @@ async def on_reaction_add(reaction, user): # ctx = await bot.get_context(reac
517
  await dfif2(index, stage_1_result_path, thread, dfif_command_message_id)
518
 
519
  except Exception as e:
520
- print(f"Error: {e} testing emoji behavior with reacts")
521
 
522
 
523
  #----------------------------------------------------------------------------------------------------------------------------
 
524
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
525
  def run_bot():
526
  bot.run(DISCORD_TOKEN)
527
 
 
9
  import re
10
  from discord import Embed, Color
11
  from discord.ext import commands
12
+ # a
13
  from gradio_client import Client
14
  from PIL import Image
15
  #from ratelimiter import RateLimiter
 
20
 
21
  import shutil # for doing image movement magic
22
 
23
+ #import tempfile
24
+ #import glob
25
+
26
+ import uuid
27
+
28
+
29
  #todo
30
  # add safetychecks to all parts, test thoroughly
31
  # add discord channels, configure for fellows
 
52
 
53
  df = Client("huggingface-projects/IF", GRADIOTEST_TOKEN)
54
  jojogan = Client("akhaliq/JoJoGAN", GRADIOTEST_TOKEN)
55
+ falconclient = Client("HuggingFaceH4/falcon-chat", GRADIOTEST_TOKEN)
56
 
57
  intents = discord.Intents.default()
58
  intents.message_content = True
59
 
60
  bot = commands.Bot(command_prefix='!', intents=intents)
61
 
62
+ #---------------------------------------------------------------------------------------------------------------------------------------------
63
+ @bot.command()
64
+ async def info(ctx):
65
+ current_directory = os.getcwd()
66
+ temp_directory = tempfile.gettempdir()
67
+ await ctx.reply(f"current_directory={current_directory}\n temp_directory={temp_directory}")
68
  #---------------------------------------------------------------------------------------------------------------------------------------------
69
  @bot.event
70
  async def on_ready():
 
77
  if await safetychecks(ctx):
78
  await ctx.reply(f"Use !deepfloydif [prompt], !jojo !spidey or !sketch. Have fun! 🤗💖")
79
  except Exception as e:
80
+ print(f"Error: unable to help :( {e}")
81
  #---------------------------------------------------------------------------------------------------------------------------------------------
82
  async def safetychecks(ctx):
83
  failure_emoji = '<:disagree:1098628957521313892>'
 
99
 
100
  #✅✅ check if the command is in the allowed channel(s)
101
  bot_test = 1100458786826747945
102
+ deepfloydif_channel = 1113182673859518514
103
+ jojo_channel = 1114217739473649764
104
+ sketch_channel = 1114218145343877180
105
+ spidey_channel = 1114218191594471514
106
+ falcon_channel = 1116089829147557999
107
+
108
+ channel_ids = [bot_test, deepfloydif_channel, jojo_channel, spidey_channel, sketch_channel, falcon_channel]
109
  if ctx.channel.id not in channel_ids:
110
  print(f"{ctx.author}, commands are not permitted in {ctx.channel}")
111
  thread = await ctx.message.create_thread(name=f'Channel Error')
 
137
 
138
  # ping lunarflu if any safety check ever fails
139
  except Exception as e:
140
+ print(f"Error: safetychecks failed somewhere, command will not continue, {e}")
141
+ await ctx.message.reply(f"❌ <@811235357663297546> SC failed somewhere ❌ {e}") # this will always ping, as long as the bot has access to the channel
142
  await ctx.message.add_reaction(failure_emoji)
143
  #----------------------------------------------------------------------------------------------------------------------------------------------
144
  @bot.command()
 
155
  @bot.command()
156
  async def jojodemo(ctx):
157
  try:
158
+ thread = await ctx.message.create_thread(name=f'JoJo Demo {ctx.author} ')
159
  await thread.send(f'{ctx.author.mention} Here is a demo for the !jojo command!')
160
  await asyncio.sleep(0.5)
161
+ await thread.send(f'https://cdn.discordapp.com/attachments/932563860597121054/1114220616199966810/image.png')
162
+ await thread.edit(archived=True)
163
+ except Exception as e:
164
+ print(f"Error: {e}")
165
+ await ctx.message.add_reaction('<:disagree:1098628957521313892>')
166
+ await thread.edit(archived=True)
167
+ #----------------------------------------------------------------------------------------------------------------------------------------------
168
+ @bot.command()
169
+ async def sketchdemo(ctx):
170
+ try:
171
+ thread = await ctx.message.create_thread(name=f'Sketch Demo {ctx.author} ')
172
+ await thread.send(f'{ctx.author.mention} Here is a demo for the !sketch command!')
173
+ await asyncio.sleep(0.5)
174
+ await thread.send(f'https://cdn.discordapp.com/attachments/932563860597121054/1114220716498370641/image.png')
175
+ await thread.edit(archived=True)
176
+ except Exception as e:
177
+ print(f"Error: {e}")
178
+ await ctx.message.add_reaction('<:disagree:1098628957521313892>')
179
+ await thread.edit(archived=True)
180
+ #----------------------------------------------------------------------------------------------------------------------------------------------
181
+ @bot.command()
182
+ async def spideydemo(ctx):
183
+ try:
184
+ thread = await ctx.message.create_thread(name=f'Spidey Demo {ctx.author} ')
185
+ await thread.send(f'{ctx.author.mention} Here is a demo for the !spidey command!')
186
+ await asyncio.sleep(0.5)
187
+ await thread.send(f'https://cdn.discordapp.com/attachments/932563860597121054/1114220798085959690/image.png')
188
+ await thread.edit(archived=True)
189
  except Exception as e:
190
  print(f"Error: {e}")
191
  await ctx.message.add_reaction('<:disagree:1098628957521313892>')
192
+ await thread.edit(archived=True)
193
  #----------------------------------------------------------------------------------------------------------------------------------------------
194
  # jojo ✅
195
  @bot.command()
 
203
  # safety checks?✅
204
  # bot no crash ✅
205
  try:
206
+ if await safetychecks(ctx): #✅
207
+ if ctx.channel.id == 1114217739473649764:
208
+ await ctx.message.add_reaction('<a:loading:1114111677990981692>')
209
+ thread = await ctx.message.create_thread(name=f'Jojo | {ctx.author}', auto_archive_duration=60)
210
+ if ctx.message.attachments:
211
+ await thread.send(f'{ctx.author.mention} Generating images in thread, can take ~1 minute...yare yare, daze ...')
212
+ attachment = ctx.message.attachments[0]
213
+ style = 'JoJo'
214
+ #im = jojogan.predict(attachment.url, style)
215
+ im = await asyncio.get_running_loop().run_in_executor(None, jojogan.predict, attachment.url, style)
216
+ #await ctx.message.reply(f'Here is the {style} version of it', file=discord.File(im))
217
+ await thread.send(f'{ctx.author.mention} Here is the {style} version of it', file=discord.File(im))
218
+
219
+ #testing animated
220
+ # <a:hugging_spin:1102656012621713488>
221
+ await ctx.message.add_reaction('<:agree:1098629085955113011>') # ✅
222
+ await ctx.message.remove_reaction('<a:loading:1114111677990981692>', bot.user)
223
+ await thread.edit(archived=True)
224
+ else: # no image
225
+ await thread.send(f"{ctx.author.mention} No attachments to be found...Can't animify dat! Try sending me an image 😉")
226
+ await ctx.message.add_reaction('<:disagree:1098628957521313892>') # ❌
227
+ await ctx.message.remove_reaction('<a:loading:1114111677990981692>', bot.user)
228
+ await thread.edit(archived=True)
229
  except Exception as e: # no generation / img + no face
230
  await fullqueue(e, thread)
231
  print(f"Error: {e}")
232
  await thread.send(f"{ctx.author.mention} Error: {e}")
233
+ await ctx.message.add_reaction('<:disagree:1098628957521313892>') # ❌
234
  await ctx.message.remove_reaction('<a:loading:1114111677990981692>', bot.user)
235
+ await thread.edit(archived=True)
236
 
237
+
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
238
  #----------------------------------------------------------------------------------------------------------------------------------------------
239
  # Spider-Verse ✅
240
  @bot.command()
241
  async def spidey(ctx):
242
  try:
243
  if await safetychecks(ctx): #✅
244
+ if ctx.channel.id == 1114218191594471514:
245
+ await ctx.message.add_reaction('<a:loading:1114111677990981692>')
246
+ thread = await ctx.message.create_thread(name=f'Spider-verse | {ctx.author}', auto_archive_duration=60)
247
+ if ctx.message.attachments:
248
+ await thread.send(f'{ctx.author.mention} Generating images in thread, can take ~1 minute...')
249
+ attachment = ctx.message.attachments[0]
250
+ style = 'Spider-Verse'
251
+ im = await asyncio.get_running_loop().run_in_executor(None, jojogan.predict, attachment.url, style)
252
+ await thread.send(f'{ctx.author.mention} Here is the {style} version of it', file=discord.File(im))
253
+ await ctx.message.add_reaction('<:agree:1098629085955113011>') # img + face
254
+ await ctx.message.remove_reaction('<a:loading:1114111677990981692>', bot.user)
255
+ await thread.edit(archived=True)
256
+ else: # no image
257
+ await thread.send(f"{ctx.author.mention} No attachments to be found...Can't animify dat! Try sending me an image 😉")
258
+ await ctx.message.add_reaction('<:disagree:1098628957521313892>')
259
+ await ctx.message.remove_reaction('<a:loading:1114111677990981692>', bot.user)
260
+ await thread.edit(archived=True)
261
  except Exception as e: # no generation / img + no face
262
  await fullqueue(e, thread)
263
  print(f"Error: {e}")
264
  await thread.send(f"{ctx.author.mention} Error: {e}")
265
  await ctx.message.add_reaction('<:disagree:1098628957521313892>')
266
  await ctx.message.remove_reaction('<a:loading:1114111677990981692>', bot.user)
267
+ await thread.edit(archived=True)
268
  #----------------------------------------------------------------------------------------------------------------------------------------------
269
  # sketch ✅
270
  @bot.command()
271
  async def sketch(ctx):
272
  try:
273
  if await safetychecks(ctx): #✅
274
+ if ctx.channel.id == 1114218145343877180:
275
+ await ctx.message.add_reaction('<a:loading:1114111677990981692>')
276
+ thread = await ctx.message.create_thread(name=f'Sketch | {ctx.author}', auto_archive_duration=60)
277
+ if ctx.message.attachments:
278
+ await thread.send(f'{ctx.author.mention} Generating images in thread, can take ~1 minute...')
279
+ attachment = ctx.message.attachments[0]
280
+ #style = 'sketch'
281
+ im = await asyncio.get_running_loop().run_in_executor(None, jojogan.predict, attachment.url, 'sketch')
282
+ await thread.send(f'{ctx.author.mention} Here is the sketch version of it', file=discord.File(im))
283
+ await ctx.message.add_reaction('<:agree:1098629085955113011>') # img + face
284
+ await ctx.message.remove_reaction('<a:loading:1114111677990981692>', bot.user)
285
+ await thread.edit(archived=True)
286
+ else: # no image
287
+ await thread.send(f"{ctx.author.mention} No attachments to be found...Can't animify dat! Try sending me an image 😉")
288
+ await ctx.message.add_reaction('<:disagree:1098628957521313892>')
289
+ await ctx.message.remove_reaction('<a:loading:1114111677990981692>', bot.user)
290
+ await thread.edit(archived=True)
291
  except Exception as e: # no generation / img + no face
292
  await fullqueue(e, thread)
293
  print(f"Error: {e}")
294
  await thread.send(f"{ctx.author.mention} Error: {e}")
295
  await ctx.message.add_reaction('<:disagree:1098628957521313892>')
296
  await ctx.message.remove_reaction('<a:loading:1114111677990981692>', bot.user)
297
+ await thread.edit(archived=True)
298
  #----------------------------------------------------------------------------------------------------------------------------------------------
299
  async def fullqueue(e, thread):
300
  error_message = str(e)
 
302
  await thread.send("Queue is full! Please try again.")
303
  elif "Error: Queue is full! Please try again." in error_message:
304
  await thread.send("Queue is full! Please try again.")
305
+ # raised when the space we depend on needs to build after some inactivity (~10h)
306
  elif "local variable 'stage_1_results' referenced before assignment" in error_message:
307
+ await thread.send("Space is building! Please try again after a few minutes.")
308
+
309
+ #----------------------------------------------------------------------------------------------------------------------------------------------
310
+
311
+
312
  # deepfloydif stage 1 generation ✅
313
  def inference(prompt):
314
  negative_prompt = ''
 
344
  # Stage 1 ✅
345
  @bot.command()
346
  async def deepfloydif(ctx, *, prompt: str):
347
+ thread = None
348
  try:
349
  try:
350
  if await safetychecks(ctx): #✅
351
+ if ctx.channel.id == 1113182673859518514:
352
+ await ctx.message.add_reaction('<a:loading:1114111677990981692>')
353
+ dfif_command_message_id = ctx.message.id # we will use this in some magic later on
354
+ thread = await ctx.message.create_thread(name=f'DeepfloydIF | {prompt}', auto_archive_duration=60) # could also just use prompt, no deepfloydif
355
+ # create thread -> send new message inside thread + combined_image -> add reactions -> dfif2
356
+
357
+ #current_time = int(time.time())
358
+ #random.seed(current_time)
359
+
360
+ negative_prompt = ''
361
+ seed = random.randint(0, 1000)
362
+ #seed = 1
363
+ number_of_images = 4
364
+ guidance_scale = 7
365
+ custom_timesteps_1 = 'smart50'
366
+ number_of_inference_steps = 50
367
+ api_name = '/generate64'
368
+ await thread.send(f'{ctx.author.mention}Generating images in thread, can take ~1 minute...')
369
 
370
  except Exception as e:
371
  print(f"Error: {e}")
372
+ if thread is None:
373
+ thread = await ctx.message.create_thread(name=f'DFIF1 Error')
374
+ await thread.send(f"{ctx.author.mention} Error before stage 1 generation, {e}. If error code: 50035, upscale can still work.")
375
  await fullqueue(e, thread)
376
  await ctx.message.remove_reaction('<a:loading:1114111677990981692>', bot.user)
377
  await ctx.message.add_reaction('<:disagree:1098628957521313892>')
378
+ await thread.edit(archived=True)
379
  #generation✅-------------------------------------------------------
380
  try:
381
  #stage_1_results, stage_1_param_path, stage_1_result_path = df.predict(
382
  # prompt, negative_prompt, seed, number_of_images, guidance_scale, custom_timesteps_1, number_of_inference_steps, api_name='/generate64')
383
 
384
  # run blocking function in executor
385
+ #await thread.send(f'✅running blocking function in executor')
386
+ #loop = asyncio.get_running_loop()
387
+ #result = await loop.run_in_executor(None, inference, prompt)
388
+
389
+ stage_1_results, stage_1_param_path, stage_1_result_path = df.predict(
390
+ prompt, negative_prompt, seed, number_of_images, guidance_scale, custom_timesteps_1, number_of_inference_steps, api_name='/generate64')
391
+
392
+ await thread.send(f'{ctx.author.mention}after executor')
393
+ await thread.send(f'✅run_in_executor ran successfully')
394
+ #stage_1_results = result[0]
395
+ #stage_1_result_path = result[2]
396
 
397
  partialpath = stage_1_result_path[5:] #magic for later
398
 
399
  except Exception as e:
400
  print(f"Error: {e}")
401
+ if thread is None:
402
+ thread = await ctx.message.create_thread(name=f'Generation Error')
403
+ await thread.send(f"{ctx.author.mention} Error during stage 1 generation, {e}")
404
  await fullqueue(e, thread)
405
  await ctx.message.remove_reaction('<a:loading:1114111677990981692>', bot.user)
406
+ await ctx.message.add_reaction('<:disagree:1098628957521313892>')
407
+ await thread.edit(archived=True)
408
  #posting images✅----------------------------------------------------------------
409
  try:
410
+ await thread.send(f'✅combining images...')
411
  png_files = [f for f in os.listdir(stage_1_results) if f.endswith('.png')]
412
 
413
  if png_files:
 
440
  combined_image_dfif = await thread.send(f'{ctx.author.mention}React with the image number you want to upscale!', file=discord.File(
441
  f, f'{partialpath}{dfif_command_message_id}.png')) # named something like: tmpgtv4qjix1111269940599738479.png
442
 
443
+ await thread.send(f'✅reacting with 1234...')
444
  emoji_list = ['↖️', '↗️', '↙️', '↘️']
445
  await react1234(emoji_list, combined_image_dfif)
446
 
 
459
 
460
  except Exception as e:
461
  print(f"Error: {e}")
462
+ if thread is None:
463
+ thread = await ctx.message.create_thread(name=f'Posting Error')
464
+ await thread.send(f"{ctx.author.mention} Encountered error while posting combined image in thread, {e}")
465
  await fullqueue(e, thread)
466
  await ctx.message.remove_reaction('<a:loading:1114111677990981692>', bot.user)
467
  await ctx.message.add_reaction('<:disagree:1098628957521313892>')
468
+ await thread.edit(archived=True)
469
  #deepfloydif try/except
470
  except Exception as e:
471
  print(f"Error: {e}")
472
+ if thread is None:
473
+ thread = await ctx.message.create_thread(name=f'deepfloydif Error')
474
+ await thread.send(f"{ctx.author.mention} Overall error with deepfloydif, {e}")
475
  await fullqueue(e, thread)
476
  await ctx.message.remove_reaction('<a:loading:1114111677990981692>', bot.user)
477
  await ctx.message.add_reaction('<:disagree:1098628957521313892>')
478
+ await thread.edit(archived=True)
479
  #----------------------------------------------------------------------------------------------------------------------------
480
  # Stage 2 ✅
481
  async def dfif2(index: int, stage_1_result_path, thread, dfif_command_message_id): # add safetychecks
 
503
  dfif_command_message = await parent_channel.fetch_message(dfif_command_message_id)
504
  await dfif_command_message.remove_reaction('<a:loading:1114111677990981692>', bot.user)
505
  await dfif_command_message.add_reaction('<:agree:1098629085955113011>')
506
+ await thread.edit(archived=True)
507
 
508
  except Exception as e:
509
  print(f"Error: {e}")
 
511
  dfif_command_message = await parent_channel.fetch_message(dfif_command_message_id)
512
  await dfif_command_message.remove_reaction('<a:loading:1114111677990981692>', bot.user)
513
  await dfif_command_message.add_reaction('<:disagree:1098628957521313892>')
514
+ await thread.send(f"Error during stage 2 upscaling, {e}")
515
+ await fullqueue(e, thread)
516
+ await thread.edit(archived=True)
517
  #----------------------------------------------------------------------------------------------------------------------------
518
  # react detector for stage 2 ✅
519
  @bot.event
 
563
  await dfif2(index, stage_1_result_path, thread, dfif_command_message_id)
564
 
565
  except Exception as e:
566
+ print(f"Error: {e} (known error, does not cause issues, fix later)")
567
 
568
 
569
  #----------------------------------------------------------------------------------------------------------------------------
570
+ chathistory = None
571
 
572
+ @bot.command()
573
+ async def falcon(ctx, *, prompt: str):
574
+ try:
575
+ if await safetychecks(ctx):
576
+ if ctx.channel.id == 1116089829147557999:
577
+ global chathistory
578
+
579
+ if chathistory is not None: # This handles all subsequent discussions/prompts to the chatbot/model
580
+ instructions = "The following is a conversation between a highly knowledgeable and intelligent AI assistant, called Falcon, and a human user, called User. In the following interactions, User and Falcon will converse in natural language, and Falcon will answer User's questions. Falcon was built to be respectful, polite and inclusive. Falcon was built by the Technology Innovation Institute in Abu Dhabi. Falcon will never decline to answer a question, and always attempts to give an answer that User would be satisfied with. It knows a lot, and always tells the truth. The conversation begins."
581
+ job = falconclient.submit(prompt, chathistory, instructions, 0.8, 0.9, fn_index=1) # This is not blocking
582
+ while job.done() == False:
583
+ status = job.status()
584
+ else:
585
+ file_paths = job.outputs()
586
+ full_generation = file_paths[-1]
587
+ chathistory = full_generation # we store this and pass it in the loop
588
+ with open(full_generation, 'r') as file:
589
+ data = json.load(file)
590
+ output_text = data[-1][-1] # we output this as the bot
591
+ print(output_text)
592
+ await ctx.reply(f"{output_text}")
593
+
594
+ if chathistory == None: # This handles the start of a conversation/chathistory file with the model
595
+ chathistory = falconclient.predict(
596
+ fn_index=5
597
+ )
598
+ instructions = "The following is a conversation between a highly knowledgeable and intelligent AI assistant, called Falcon, and a human user, called User. In the following interactions, User and Falcon will converse in natural language, and Falcon will answer User's questions. Falcon was built to be respectful, polite and inclusive. Falcon was built by the Technology Innovation Institute in Abu Dhabi. Falcon will never decline to answer a question, and always attempts to give an answer that User would be satisfied with. It knows a lot, and always tells the truth. The conversation begins."
599
+ job = falconclient.submit(prompt, chathistory, instructions, 0.8, 0.9, fn_index=1) # This is not blocking
600
+ while job.done() == False:
601
+ status = job.status()
602
+ else:
603
+ file_paths = job.outputs()
604
+ full_generation = file_paths[-1]
605
+ chathistory = full_generation # we store this and pass it in the loop
606
+ with open(full_generation, 'r') as file:
607
+ data = json.load(file)
608
+ output_text = data[-1][-1] # we output this as the bot
609
+ await ctx.reply(f"{output_text}")
610
+
611
+ except Exception as e:
612
+ print(f"Error: {e}")
613
+ await ctx.reply(f"{e} cc <@811235357663297546> (falcon error)")
614
+ #----------------------------------------------------------------------------------------------------------------------------
615
+ '''
616
+ @bot.event
617
+ async def on_message(message):
618
+ try:
619
+ if message.channel.id == 1116089829147557999:
620
+
621
+
622
+
623
+ except Exception as e:
624
+ print(f"Error: {e}")
625
+ await ctx.reply(f"{e} cc <@811235357663297546> (falcon error)")
626
+
627
+ '''
628
+ #----------------------------------------------------------------------------------------------------------------------------
629
+
630
+
631
+
632
+
633
  def run_bot():
634
  bot.run(DISCORD_TOKEN)
635