lunarflu HF Staff commited on
Commit
ef4b786
·
1 Parent(s): d20c7c3

Update deepfloydif.py

Browse files
Files changed (1) hide show
  1. deepfloydif.py +39 -55
deepfloydif.py CHANGED
@@ -10,12 +10,8 @@ import pathlib
10
  HF_TOKEN = os.getenv("HF_TOKEN")
11
  deepfloydif_client = Client("huggingface-projects/IF", HF_TOKEN)
12
 
13
- BOT_USER_ID = (
14
- 1086256910572986469 if os.getenv("TEST_ENV", False) else 1102236653545861151
15
- )
16
- DEEPFLOYDIF_CHANNEL_ID = (
17
- 1121834257959092234 if os.getenv("TEST_ENV", False) else 1119313215675973714
18
- )
19
 
20
 
21
  def deepfloydif_stage_1_inference(prompt):
@@ -27,9 +23,9 @@ def deepfloydif_stage_1_inference(prompt):
27
  custom_timesteps_1 = "smart50"
28
  number_of_inference_steps = 50
29
  (
30
- stage_1_results,
31
  stage_1_param_path,
32
- stage_1_result_path,
33
  ) = deepfloydif_client.predict(
34
  prompt,
35
  negative_prompt,
@@ -40,10 +36,10 @@ def deepfloydif_stage_1_inference(prompt):
40
  number_of_inference_steps,
41
  api_name="/generate64",
42
  )
43
- return [stage_1_results, stage_1_param_path, stage_1_result_path]
44
 
45
 
46
- def deepfloydif_stage_2_inference(index, stage_1_result_path):
47
  """Upscales one of the images from deepfloydif_stage_1_inference based on the chosen index"""
48
  selected_index_for_stage_2 = index
49
  seed_2 = 0
@@ -51,7 +47,7 @@ def deepfloydif_stage_2_inference(index, stage_1_result_path):
51
  custom_timesteps_2 = "smart50"
52
  number_of_inference_steps_2 = 50
53
  result_path = deepfloydif_client.predict(
54
- stage_1_result_path,
55
  selected_index_for_stage_2,
56
  seed_2,
57
  guidance_scale_2,
@@ -68,64 +64,56 @@ async def react_1234(reaction_emojis, combined_image_dfif):
68
  await combined_image_dfif.add_reaction(emoji)
69
 
70
 
71
- def load_image(png_files, stage_1_results):
72
  """Opens images as variables so we can combine them later"""
73
  results = []
74
  for file in png_files:
75
- png_path = os.path.join(stage_1_results, file)
76
  results.append(Image.open(png_path))
77
  return results
78
 
79
 
 
 
 
 
 
 
 
 
 
 
 
 
 
80
  async def deepfloydif_stage_1(interaction, prompt, client):
81
  """DeepfloydIF command (generate images with realistic text using slash commands)"""
82
  try:
83
- # global BOT_USER_ID
84
- # global DEEPFLOYDIF_CHANNEL_ID
85
  if interaction.user.id != BOT_USER_ID:
86
  if interaction.channel.id == DEEPFLOYDIF_CHANNEL_ID:
87
  if os.environ.get("TEST_ENV") == "True":
88
- print("Safetychecks passed for deepfloydif_stage_1")
89
  await interaction.response.send_message("Working on it!")
90
  channel = interaction.channel
91
  # interaction.response message can't be used to create a thread, so we create another message
92
  message = await channel.send("DeepfloydIF Thread")
93
- thread = await message.create_thread(
94
- name=f"{prompt}", auto_archive_duration=60
95
- )
96
  await thread.send(
97
- "[DISCLAIMER: HuggingBot is a **highly experimental** beta feature; Additional information on the DeepfloydIF model can be found here: https://huggingface.co/spaces/DeepFloyd/IF"
98
- )
99
- await thread.send(
100
- f"{interaction.user.mention} Generating images in thread, can take ~1 minute..."
101
  )
 
102
 
103
  loop = asyncio.get_running_loop()
104
- result = await loop.run_in_executor(
105
- None, deepfloydif_stage_1_inference, prompt
106
- )
107
- stage_1_results = result[0]
108
- stage_1_result_path = result[2]
109
 
110
- partial_path = pathlib.Path(stage_1_result_path).name
111
- png_files = list(glob.glob(f"{stage_1_results}/**/*.png"))
112
 
113
  if png_files:
114
- # take all 4 images and combine them into one large 2x2 image (similar to Midjourney)
115
- if os.environ.get("TEST_ENV") == "True":
116
- print("Combining images for deepfloydif_stage_1")
117
- images = load_image(png_files, stage_1_results)
118
- combined_image = Image.new(
119
- "RGB", (images[0].width * 2, images[0].height * 2)
120
- )
121
- combined_image.paste(images[0], (0, 0))
122
- combined_image.paste(images[1], (images[0].width, 0))
123
- combined_image.paste(images[2], (0, images[0].height))
124
- combined_image.paste(images[3], (images[0].width, images[0].height))
125
- combined_image_path = os.path.join(
126
- stage_1_results, f"{partial_path}.png"
127
- )
128
- combined_image.save(combined_image_path)
129
  if os.environ.get("TEST_ENV") == "True":
130
  print("Images combined for deepfloydif_stage_1")
131
  with open(combined_image_path, "rb") as f:
@@ -136,9 +124,7 @@ async def deepfloydif_stage_1(interaction, prompt, client):
136
  emoji_list = ["↖️", "↗️", "↙️", "↘️"]
137
  await react_1234(emoji_list, combined_image_dfif)
138
  else:
139
- await thread.send(
140
- f"{interaction.user.mention} No PNG files were found, cannot post them!"
141
- )
142
  except Exception as e:
143
  print(f"Error: {e}")
144
 
@@ -169,18 +155,18 @@ async def deepfloydif_stage_2_react_check(reaction, user):
169
  index = 2
170
  elif emoji == "↘️":
171
  index = 3
172
- stage_1_result_path = full_path
173
  thread = reaction.message.channel
174
  await deepfloydif_stage_2(
175
  index,
176
- stage_1_result_path,
177
  thread,
178
  )
179
  except Exception as e:
180
  print(f"Error: {e} (known error, does not cause issues, low priority)")
181
 
182
 
183
- async def deepfloydif_stage_2(index: int, stage_1_result_path, thread):
184
  """upscaling function for images generated using /deepfloydif"""
185
  try:
186
  if os.environ.get("TEST_ENV") == "True":
@@ -198,13 +184,11 @@ async def deepfloydif_stage_2(index: int, stage_1_result_path, thread):
198
  # run blocking function in executor
199
  loop = asyncio.get_running_loop()
200
  result_path = await loop.run_in_executor(
201
- None, deepfloydif_stage_2_inference, index, stage_1_result_path
202
  )
203
 
204
  with open(result_path, "rb") as f:
205
- await thread.send(
206
- "Here is the upscaled image!", file=discord.File(f, "result.png")
207
- )
208
  await thread.edit(archived=True)
209
  except Exception as e:
210
  print(f"Error: {e}")
 
10
  HF_TOKEN = os.getenv("HF_TOKEN")
11
  deepfloydif_client = Client("huggingface-projects/IF", HF_TOKEN)
12
 
13
+ BOT_USER_ID = 1086256910572986469 if os.getenv("TEST_ENV", False) else 1102236653545861151
14
+ DEEPFLOYDIF_CHANNEL_ID = 1121834257959092234 if os.getenv("TEST_ENV", False) else 1119313215675973714
 
 
 
 
15
 
16
 
17
  def deepfloydif_stage_1_inference(prompt):
 
23
  custom_timesteps_1 = "smart50"
24
  number_of_inference_steps = 50
25
  (
26
+ stage_1_images,
27
  stage_1_param_path,
28
+ path_for_stage_2_upscaling,
29
  ) = deepfloydif_client.predict(
30
  prompt,
31
  negative_prompt,
 
36
  number_of_inference_steps,
37
  api_name="/generate64",
38
  )
39
+ return [stage_1_images, stage_1_param_path, path_for_stage_2_upscaling]
40
 
41
 
42
+ def deepfloydif_stage_2_inference(index, path_for_stage_2_upscaling):
43
  """Upscales one of the images from deepfloydif_stage_1_inference based on the chosen index"""
44
  selected_index_for_stage_2 = index
45
  seed_2 = 0
 
47
  custom_timesteps_2 = "smart50"
48
  number_of_inference_steps_2 = 50
49
  result_path = deepfloydif_client.predict(
50
+ path_for_stage_2_upscaling,
51
  selected_index_for_stage_2,
52
  seed_2,
53
  guidance_scale_2,
 
64
  await combined_image_dfif.add_reaction(emoji)
65
 
66
 
67
+ def load_image(png_files, stage_1_images):
68
  """Opens images as variables so we can combine them later"""
69
  results = []
70
  for file in png_files:
71
+ png_path = os.path.join(stage_1_images, file)
72
  results.append(Image.open(png_path))
73
  return results
74
 
75
 
76
+ def combine_images(png_files, stage_1_images, partial_path):
77
+ if os.environ.get("TEST_ENV") == "True":
78
+ print("Combining images for deepfloydif_stage_1")
79
+ images = load_image(png_files, stage_1_images)
80
+ combined_image = Image.new("RGB", (images[0].width * 2, images[0].height * 2))
81
+ combined_image.paste(images[0], (0, 0))
82
+ combined_image.paste(images[1], (images[0].width, 0))
83
+ combined_image.paste(images[2], (0, images[0].height))
84
+ combined_image.paste(images[3], (images[0].width, images[0].height))
85
+ combined_image_path = os.path.join(stage_1_images, f"{partial_path}.png")
86
+ combined_image.save(combined_image_path)
87
+
88
+
89
  async def deepfloydif_stage_1(interaction, prompt, client):
90
  """DeepfloydIF command (generate images with realistic text using slash commands)"""
91
  try:
 
 
92
  if interaction.user.id != BOT_USER_ID:
93
  if interaction.channel.id == DEEPFLOYDIF_CHANNEL_ID:
94
  if os.environ.get("TEST_ENV") == "True":
95
+ print("Safety checks passed for deepfloydif_stage_1")
96
  await interaction.response.send_message("Working on it!")
97
  channel = interaction.channel
98
  # interaction.response message can't be used to create a thread, so we create another message
99
  message = await channel.send("DeepfloydIF Thread")
100
+ thread = await message.create_thread(name=f"{prompt}", auto_archive_duration=60)
 
 
101
  await thread.send(
102
+ "[DISCLAIMER: HuggingBot is a **highly experimental** beta feature; Additional information on the"
103
+ " DeepfloydIF model can be found here: https://huggingface.co/spaces/DeepFloyd/IF"
 
 
104
  )
105
+ await thread.send(f"{interaction.user.mention} Generating images in thread, can take ~1 minute...")
106
 
107
  loop = asyncio.get_running_loop()
108
+ result = await loop.run_in_executor(None, deepfloydif_stage_1_inference, prompt)
109
+ stage_1_images = result[0]
110
+ path_for_stage_2_upscaling = result[2]
 
 
111
 
112
+ partial_path = pathlib.Path(path_for_stage_2_upscaling).name
113
+ png_files = list(glob.glob(f"{stage_1_images}/**/*.png"))
114
 
115
  if png_files:
116
+ combine_images(png_files, stage_1_images, partial_path)
 
 
 
 
 
 
 
 
 
 
 
 
 
 
117
  if os.environ.get("TEST_ENV") == "True":
118
  print("Images combined for deepfloydif_stage_1")
119
  with open(combined_image_path, "rb") as f:
 
124
  emoji_list = ["↖️", "↗️", "↙️", "↘️"]
125
  await react_1234(emoji_list, combined_image_dfif)
126
  else:
127
+ await thread.send(f"{interaction.user.mention} No PNG files were found, cannot post them!")
 
 
128
  except Exception as e:
129
  print(f"Error: {e}")
130
 
 
155
  index = 2
156
  elif emoji == "↘️":
157
  index = 3
158
+ path_for_stage_2_upscaling = full_path
159
  thread = reaction.message.channel
160
  await deepfloydif_stage_2(
161
  index,
162
+ path_for_stage_2_upscaling,
163
  thread,
164
  )
165
  except Exception as e:
166
  print(f"Error: {e} (known error, does not cause issues, low priority)")
167
 
168
 
169
+ async def deepfloydif_stage_2(index: int, path_for_stage_2_upscaling, thread):
170
  """upscaling function for images generated using /deepfloydif"""
171
  try:
172
  if os.environ.get("TEST_ENV") == "True":
 
184
  # run blocking function in executor
185
  loop = asyncio.get_running_loop()
186
  result_path = await loop.run_in_executor(
187
+ None, deepfloydif_stage_2_inference, index, path_for_stage_2_upscaling
188
  )
189
 
190
  with open(result_path, "rb") as f:
191
+ await thread.send("Here is the upscaled image!", file=discord.File(f, "result.png"))
 
 
192
  await thread.edit(archived=True)
193
  except Exception as e:
194
  print(f"Error: {e}")