lunarflu HF Staff commited on
Commit
448f731
·
1 Parent(s): 668662c

FALCON -> FALCON/TRY_FALCON

Browse files
Files changed (1) hide show
  1. app.py +5 -253
app.py CHANGED
@@ -74,236 +74,18 @@ client = MyClient(intents=intents)
74
  async def on_ready():
75
  print(f'Logged in as {client.user} (ID: {client.user.id})')
76
  print('------')
77
- #-------------------------------------------------------------------------------------------------------------------------------------
78
- # deepfloydif stage 1 generation
79
- def inference(prompt):
80
- negative_prompt = ''
81
- seed = random.randint(0, 1000)
82
- #seed = 1
83
- number_of_images = 4
84
- guidance_scale = 7
85
- custom_timesteps_1 = 'smart50'
86
- number_of_inference_steps = 50
87
-
88
- stage_1_results, stage_1_param_path, stage_1_result_path = df.predict(
89
- prompt, negative_prompt, seed, number_of_images, guidance_scale, custom_timesteps_1, number_of_inference_steps, api_name='/generate64')
90
-
91
- return [stage_1_results, stage_1_param_path, stage_1_result_path]
92
  #-------------------------------------------------------------------------------------------------------------------------------------
93
- # deepfloydif stage 2 upscaling
94
- def inference2(index, stage_1_result_path):
95
- selected_index_for_stage_2 = index
96
- seed_2 = 0
97
- guidance_scale_2 = 4
98
- custom_timesteps_2 = 'smart50'
99
- number_of_inference_steps_2 = 50
100
- result_path = df.predict(stage_1_result_path, selected_index_for_stage_2, seed_2,
101
- guidance_scale_2, custom_timesteps_2, number_of_inference_steps_2, api_name='/upscale256')
102
-
103
- return result_path
104
- #-------------------------------------------------------------------------------------------------------------------------------------
105
- async def react1234(reaction_emojis, combined_image_dfif):
106
- for emoji in reaction_emojis:
107
- await combined_image_dfif.add_reaction(emoji)
108
- #-------------------------------------------------------------------------------------------------------------------------------------
109
- # deepfloydIF command (generate images with realistic text using slash commands)
110
  @client.tree.command()
111
  @app_commands.describe(
112
- prompt='Enter a prompt to generate an image! Can generate realistic text, too!')
113
- async def deepfloydif(interaction: discord.Interaction, prompt: str):
114
- thread = None
115
- try:
116
- global BOT_USER_ID
117
- global DEEPFLOYD_CHANNEL_ID
118
- if interaction.user.id != BOT_USER_ID:
119
- if interaction.channel.id == DEEPFLOYD_CHANNEL_ID:
120
- await interaction.response.send_message(f"Working on it!")
121
- channel = interaction.channel
122
- message = await channel.send(f"DeepfloydIF Thread")
123
- await message.add_reaction('🔁')
124
- thread = await message.create_thread(name=f'{prompt}', auto_archive_duration=60)
125
- await thread.send(f"[DISCLAIMER: HuggingBot is a **highly experimental** beta feature; Additional information" \
126
- f" on the DeepfloydIF model can be found here: https://huggingface.co/spaces/DeepFloyd/IF")
127
-
128
- dfif_command_message_id = message.id # used for updating the 'status' of our generations using reaction emojis
129
-
130
- await thread.send(f'{interaction.user.mention}Generating images in thread, can take ~1 minute...')
131
-
132
- # generation
133
- loop = asyncio.get_running_loop()
134
- result = await loop.run_in_executor(None, inference, prompt)
135
- stage_1_results = result[0]
136
- stage_1_result_path = result[2]
137
- partialpath = stage_1_result_path[5:]
138
- png_files = list(glob.glob(f"{stage_1_results}/**/*.png"))
139
-
140
- img1 = None
141
- img2 = None
142
- img3 = None
143
- img4 = None
144
-
145
- if png_files:
146
- png_file_index = 0
147
- images = load_image(png_files, stage_1_results, png_file_index)
148
- img1 = images[0]
149
- img2 = images[1]
150
- img3 = images[2]
151
- img4 = images[3]
152
-
153
-
154
- '''
155
- first_png = png_files[0]
156
- second_png = png_files[1]
157
- third_png = png_files[2]
158
- fourth_png = png_files[3]
159
-
160
- first_png_path = os.path.join(stage_1_results, first_png)
161
- second_png_path = os.path.join(stage_1_results, second_png)
162
- third_png_path = os.path.join(stage_1_results, third_png)
163
- fourth_png_path = os.path.join(stage_1_results, fourth_png)
164
-
165
- img1 = Image.open(first_png_path)
166
- img2 = Image.open(second_png_path)
167
- img3 = Image.open(third_png_path)
168
- img4 = Image.open(fourth_png_path)
169
- '''
170
- combined_image = Image.new('RGB', (img1.width * 2, img1.height * 2))
171
-
172
- combined_image.paste(img1, (0, 0))
173
- combined_image.paste(img2, (img1.width, 0))
174
- combined_image.paste(img3, (0, img1.height))
175
- combined_image.paste(img4, (img1.width, img1.height))
176
-
177
- combined_image_path = os.path.join(stage_1_results, f'{partialpath}{dfif_command_message_id}.png')
178
- combined_image.save(combined_image_path)
179
-
180
- with open(combined_image_path, 'rb') as f:
181
- combined_image_dfif = await thread.send(f'{interaction.user.mention}React with the image number you want to upscale!', file=discord.File(
182
- f, f'{partialpath}{dfif_command_message_id}.png')) # named something like: tmpgtv4qjix1111269940599738479.png
183
-
184
- emoji_list = ['↖️', '↗️', '↙️', '↘️']
185
- await react1234(emoji_list, combined_image_dfif)
186
-
187
- await message.remove_reaction('🔁', client.user)
188
- await message.add_reaction('✔️')
189
-
190
- except Exception as e:
191
- print(f"Error: {e}")
192
- await message.remove_reaction('🔁', client.user)
193
- await message.add_reaction('❌')
194
- #await thread.send(f"Error: {e} <@811235357663297546> (continue_falcon error)")
195
- #-------------------------------------------------------------------------------------------------------------------------------------
196
- def load_image(png_files, stage_1_results, png_file_index):
197
- for file in png_files:
198
- png_file = png_files[png_file_index]
199
- png_path = os.path.join(stage_1_results, png_file)
200
- if png_file_index == 0:
201
- img1 = Image.open(png_path)
202
- if png_file_index == 1:
203
- img2 = Image.open(png_path)
204
- if png_file_index == 2:
205
- img3 = Image.open(png_path)
206
- if png_file_index == 3:
207
- img4 = Image.open(png_path)
208
- png_file_index = png_file_index + 1
209
- return [img1, img2, img3, img4]
210
- #-------------------------------------------------------------------------------------------------------------------------------------
211
- # upscaling function for images generated using /deepfloydif
212
- async def dfif2(index: int, stage_1_result_path, thread, dfif_command_message_id):
213
- try:
214
- #await thread.send(f"inside dfif2")
215
- parent_channel = thread.parent
216
- dfif_command_message = await parent_channel.fetch_message(dfif_command_message_id)
217
- await dfif_command_message.remove_reaction('✔️', client.user)
218
- await dfif_command_message.add_reaction('🔁')
219
- #await thread.send(f"getting index")
220
- number = index + 1
221
- if number == 1:
222
- position = "top left"
223
- elif number == 2:
224
- position = "top right"
225
- elif number == 3:
226
- position = "bottom left"
227
- elif number == 4:
228
- position = "bottom right"
229
- await thread.send(f"Upscaling the {position} image...")
230
-
231
- # run blocking function in executor
232
- loop = asyncio.get_running_loop()
233
- result_path = await loop.run_in_executor(None, inference2, index, stage_1_result_path)
234
-
235
- #await thread.send(f"✅upscale done")
236
- with open(result_path, 'rb') as f:
237
- await thread.send(f'Here is the upscaled image!', file=discord.File(f, 'result.png'))
238
-
239
- await dfif_command_message.remove_reaction('🔁', client.user)
240
- await dfif_command_message.add_reaction('✔️')
241
- await thread.edit(archived=True)
242
 
243
  except Exception as e:
244
  print(f"Error: {e}")
245
- parent_channel = thread.parent
246
- dfif_command_message = await parent_channel.fetch_message(dfif_command_message_id)
247
- await dfif_command_message.remove_reaction('🔁', client.user)
248
- await dfif_command_message.add_reaction('❌')
249
- await thread.send(f"Error during stage 2 upscaling, {e}")
250
- await fullqueue(e, thread)
251
- await thread.edit(archived=True)
252
- #-------------------------------------------------------------------------------------------------------------------------------------
253
- @client.event
254
- async def on_reaction_add(reaction, user):
255
- try:
256
- global BOT_USER_ID
257
- global DEEPFLOYD_CHANNEL_ID
258
- channel = reaction.message.channel
259
- #await channel.send("reaction detected")
260
- if user.id != BOT_USER_ID: #
261
- thread = reaction.message.channel
262
- threadparentid = thread.parent.id
263
- #await channel.send(f"threadparentid = {threadparentid}")
264
- if threadparentid == DEEPFLOYD_CHANNEL_ID: # 1121834257959092234
265
- #await channel.send(f"right parent")
266
- if reaction.message.attachments:
267
- #await channel.send(f"has attachments")
268
- if user.id == reaction.message.mentions[0].id: # if user.id == reaction.message.mentions[0].id:
269
- #await channel.send("checks passed for on_react")
270
- #await reaction.message.channel.send("✅reaction detected")
271
- attachment = reaction.message.attachments[0]
272
- image_name = attachment.filename # named something like: tmpgtv4qjix1111269940599738479.png
273
- # remove .png first
274
- partialpathmessageid = image_name[:-4] # should be tmpgtv4qjix1111269940599738479
275
- # extract partialpath, messageid
276
- partialpath = partialpathmessageid[:11] # tmpgtv4qjix
277
- messageid = partialpathmessageid[11:] # 1111269940599738479
278
- # add /tmp/ to partialpath, save as new variable
279
- fullpath = "/tmp/" + partialpath # should be /tmp/tmpgtv4qjix
280
- #await reaction.message.channel.send(f"✅fullpath extracted, {fullpath}")
281
- emoji = reaction.emoji
282
- #await reaction.message.channel.send(f"emojis")
283
- if emoji == "↖️":
284
- index = 0
285
- elif emoji == "↗️":
286
- index = 1
287
- elif emoji == "↙️":
288
- index = 2
289
- elif emoji == "↘️":
290
- index = 3
291
-
292
- #await reaction.message.channel.send(f"✅index extracted, {index}")
293
- index = index
294
- stage_1_result_path = fullpath
295
- thread = reaction.message.channel
296
- dfif_command_message_id = messageid
297
- #await reaction.message.channel.send(f"✅calling dfif2")
298
- await dfif2(index, stage_1_result_path, thread, dfif_command_message_id)
299
-
300
- except Exception as e:
301
- print(f"Error: {e} (known error, does not cause issues, low priority)")
302
  #-------------------------------------------------------------------------------------------------------------------------------------
303
- @client.tree.command()
304
- @app_commands.describe(
305
- prompt='Enter some text to chat with the bot! Like this: /falcon Hello, how are you?')
306
- async def falcon(interaction: discord.Interaction, prompt: str):
307
  try:
308
  global falcon_userid_threadid_dictionary # tracks userid-thread existence
309
  global instructions
@@ -345,36 +127,6 @@ async def falcon(interaction: discord.Interaction, prompt: str):
345
  except Exception as e:
346
  print(f"Error: {e}")
347
  #await thread.send(f"{e} cc <@811235357663297546> (falconprivate error)")
348
- #-------------------------------------------------------------------------------------------------------------------------------------
349
- async def continue_falcon(message):
350
- try:
351
- global instructions
352
- global threadid_conversation
353
- await message.add_reaction('🔁')
354
- chathistory = threadid_conversation[message.channel.id]
355
- prompt = message.content
356
- # generation
357
- job = falconclient.submit(prompt, chathistory, instructions, 0.8, 0.9, fn_index=1) # This is not blocking, similar to run_in_executor (but better)
358
- while job.done() == False:
359
- status = job.status()
360
- #print(status)
361
- else:
362
- file_paths = job.outputs()
363
- full_generation = file_paths[-1] # tmp12345678.json
364
- with open(full_generation, 'r') as file:
365
- data = json.load(file)
366
- output_text = data[-1][-1] # we output this as the bot
367
-
368
- threadid_conversation[message.channel.id] = full_generation # overwrite the old file
369
- falcon_userid_threadid_dictionary[message.channel.id] = message.author.id
370
-
371
- print(output_text)
372
- await message.reply(f"{output_text}")
373
- await message.remove_reaction('🔁', client.user) # test=🔁 hf=🔁
374
-
375
- except Exception as e:
376
- print(f"Error: {e}")
377
- await message.reply(f"Error: {e} <@811235357663297546> (continue_falcon error)")
378
  #-------------------------------------------------------------------------------------------------------------------------------------
379
  @client.event
380
  async def on_message(message):
 
74
  async def on_ready():
75
  print(f'Logged in as {client.user} (ID: {client.user.id})')
76
  print('------')
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
77
  #-------------------------------------------------------------------------------------------------------------------------------------
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
78
  @client.tree.command()
79
  @app_commands.describe(
80
+ prompt='Enter some text to chat with the bot! Like this: /falcon Hello, how are you?')
81
+ async def falcon(interaction: discord.Interaction, prompt: str):
82
+ try:
83
+ await try_falcon(prompt)
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
84
 
85
  except Exception as e:
86
  print(f"Error: {e}")
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
87
  #-------------------------------------------------------------------------------------------------------------------------------------
88
+ async def try_falcon(prompt):
 
 
 
89
  try:
90
  global falcon_userid_threadid_dictionary # tracks userid-thread existence
91
  global instructions
 
127
  except Exception as e:
128
  print(f"Error: {e}")
129
  #await thread.send(f"{e} cc <@811235357663297546> (falconprivate error)")
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
130
  #-------------------------------------------------------------------------------------------------------------------------------------
131
  @client.event
132
  async def on_message(message):