[deepfloydif.py] black + ruff
Browse files- deepfloydif.py +112 -67
deepfloydif.py
CHANGED
|
@@ -1,46 +1,72 @@
|
|
| 1 |
import discord
|
| 2 |
-
from discord import app_commands
|
| 3 |
-
import gradio as gr
|
| 4 |
from gradio_client import Client
|
| 5 |
import os
|
| 6 |
-
import json
|
| 7 |
import random
|
| 8 |
from PIL import Image
|
| 9 |
import asyncio
|
| 10 |
import glob
|
| 11 |
import pathlib
|
| 12 |
|
| 13 |
-
HF_TOKEN = os.getenv(
|
| 14 |
deepfloydif_client = Client("huggingface-projects/IF", HF_TOKEN)
|
| 15 |
|
| 16 |
-
BOT_USER_ID =
|
| 17 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 18 |
|
| 19 |
def deepfloydif_stage_1_inference(prompt):
|
| 20 |
"""Generates an image based on a prompt"""
|
| 21 |
-
negative_prompt =
|
| 22 |
seed = random.randint(0, 1000)
|
| 23 |
number_of_images = 4
|
| 24 |
guidance_scale = 7
|
| 25 |
-
custom_timesteps_1 =
|
| 26 |
number_of_inference_steps = 50
|
| 27 |
-
|
| 28 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 29 |
|
| 30 |
def deepfloydif_stage_2_inference(index, stage_1_result_path):
|
| 31 |
"""Upscales one of the images from deepfloydif_stage_1_inference based on the chosen index"""
|
| 32 |
selected_index_for_stage_2 = index
|
| 33 |
seed_2 = 0
|
| 34 |
guidance_scale_2 = 4
|
| 35 |
-
custom_timesteps_2 =
|
| 36 |
number_of_inference_steps_2 = 50
|
| 37 |
-
result_path = deepfloydif_client.predict(
|
| 38 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 39 |
|
| 40 |
async def react_1234(reaction_emojis, combined_image_dfif):
|
| 41 |
"""Sets up 4 reaction emojis so the user can choose an image to upscale for deepfloydif"""
|
| 42 |
for emoji in reaction_emojis:
|
| 43 |
-
await combined_image_dfif.add_reaction(emoji)
|
|
|
|
| 44 |
|
| 45 |
def load_image(png_files, stage_1_results):
|
| 46 |
"""Opens images as variables so we can combine them later"""
|
|
@@ -49,77 +75,91 @@ def load_image(png_files, stage_1_results):
|
|
| 49 |
png_path = os.path.join(stage_1_results, file)
|
| 50 |
results.append(Image.open(png_path))
|
| 51 |
return results
|
| 52 |
-
|
|
|
|
| 53 |
async def deepfloydif_stage_1(interaction, prompt, client):
|
| 54 |
"""DeepfloydIF command (generate images with realistic text using slash commands)"""
|
| 55 |
try:
|
| 56 |
-
#global BOT_USER_ID
|
| 57 |
-
#global DEEPFLOYDIF_CHANNEL_ID
|
| 58 |
if interaction.user.id != BOT_USER_ID:
|
| 59 |
if interaction.channel.id == DEEPFLOYDIF_CHANNEL_ID:
|
| 60 |
-
if os.environ.get(
|
| 61 |
print("Safetychecks passed for deepfloydif_stage_1")
|
| 62 |
await interaction.response.send_message("Working on it!")
|
| 63 |
channel = interaction.channel
|
| 64 |
# interaction.response message can't be used to create a thread, so we create another message
|
| 65 |
message = await channel.send("DeepfloydIF Thread")
|
| 66 |
-
thread = await message.create_thread(
|
| 67 |
-
|
| 68 |
-
|
| 69 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 70 |
loop = asyncio.get_running_loop()
|
| 71 |
-
result = await loop.run_in_executor(
|
|
|
|
|
|
|
| 72 |
stage_1_results = result[0]
|
| 73 |
-
stage_1_result_path = result[2]
|
| 74 |
-
|
| 75 |
partial_path = pathlib.Path(stage_1_result_path).name
|
| 76 |
png_files = list(glob.glob(f"{stage_1_results}/**/*.png"))
|
| 77 |
-
|
| 78 |
if png_files:
|
| 79 |
# take all 4 images and combine them into one large 2x2 image (similar to Midjourney)
|
| 80 |
-
if os.environ.get(
|
| 81 |
print("Combining images for deepfloydif_stage_1")
|
| 82 |
-
images = load_image(png_files, stage_1_results)
|
| 83 |
-
combined_image = Image.new(
|
|
|
|
|
|
|
| 84 |
combined_image.paste(images[0], (0, 0))
|
| 85 |
combined_image.paste(images[1], (images[0].width, 0))
|
| 86 |
combined_image.paste(images[2], (0, images[0].height))
|
| 87 |
combined_image.paste(images[3], (images[0].width, images[0].height))
|
| 88 |
-
combined_image_path = os.path.join(
|
|
|
|
|
|
|
| 89 |
combined_image.save(combined_image_path)
|
| 90 |
-
if os.environ.get(
|
| 91 |
-
print("Images combined for deepfloydif_stage_1")
|
| 92 |
-
|
| 93 |
-
|
| 94 |
-
|
| 95 |
-
|
| 96 |
-
|
|
|
|
| 97 |
await react_1234(emoji_list, combined_image_dfif)
|
| 98 |
else:
|
| 99 |
-
await thread.send(
|
| 100 |
-
|
|
|
|
| 101 |
except Exception as e:
|
| 102 |
print(f"Error: {e}")
|
| 103 |
|
| 104 |
-
|
|
|
|
| 105 |
"""Checks for a reaction in order to call dfif2"""
|
| 106 |
try:
|
| 107 |
-
if os.environ.get(
|
| 108 |
-
print("Running deepfloydif_stage_2_react_check")
|
| 109 |
global BOT_USER_ID
|
| 110 |
global DEEPFLOYDIF_CHANNEL_ID
|
| 111 |
if user.id != BOT_USER_ID:
|
| 112 |
thread = reaction.message.channel
|
| 113 |
thread_parent_id = thread.parent.id
|
| 114 |
-
if thread_parent_id == DEEPFLOYDIF_CHANNEL_ID:
|
| 115 |
if reaction.message.attachments:
|
| 116 |
-
if user.id == reaction.message.mentions[0].id:
|
| 117 |
attachment = reaction.message.attachments[0]
|
| 118 |
-
image_name = attachment.filename
|
| 119 |
-
|
| 120 |
-
|
| 121 |
-
dfif_command_message_id = partial_path_message_id[11:]
|
| 122 |
-
full_path = "/tmp/" + partial_path
|
| 123 |
emoji = reaction.emoji
|
| 124 |
if emoji == "↖️":
|
| 125 |
index = 0
|
|
@@ -128,21 +168,23 @@ async def deepfloydif_stage_2_react_check(reaction, user):
|
|
| 128 |
elif emoji == "↙️":
|
| 129 |
index = 2
|
| 130 |
elif emoji == "↘️":
|
| 131 |
-
index = 3
|
| 132 |
stage_1_result_path = full_path
|
| 133 |
thread = reaction.message.channel
|
| 134 |
-
await deepfloydif_stage_2(
|
| 135 |
-
|
|
|
|
|
|
|
|
|
|
| 136 |
except Exception as e:
|
| 137 |
print(f"Error: {e} (known error, does not cause issues, low priority)")
|
| 138 |
-
|
| 139 |
-
|
|
|
|
| 140 |
"""upscaling function for images generated using /deepfloydif"""
|
| 141 |
try:
|
| 142 |
-
if os.environ.get(
|
| 143 |
-
print("Running deepfloydif_stage_2")
|
| 144 |
-
parent_channel = thread.parent
|
| 145 |
-
dfif_command_message = await parent_channel.fetch_message(dfif_command_message_id)
|
| 146 |
if index == 0:
|
| 147 |
position = "top left"
|
| 148 |
elif index == 1:
|
|
@@ -150,16 +192,19 @@ async def deepfloydif_stage_2(index: int, stage_1_result_path, thread, dfif_comm
|
|
| 150 |
elif index == 2:
|
| 151 |
position = "bottom left"
|
| 152 |
elif index == 3:
|
| 153 |
-
position = "bottom right"
|
| 154 |
-
await thread.send(f"Upscaling the {position} image...")
|
| 155 |
-
|
| 156 |
# run blocking function in executor
|
| 157 |
loop = asyncio.get_running_loop()
|
| 158 |
-
result_path = await loop.run_in_executor(
|
| 159 |
-
|
| 160 |
-
|
| 161 |
-
await thread.send('Here is the upscaled image!', file=discord.File(f, 'result.png'))
|
| 162 |
-
await thread.edit(archived=True)
|
| 163 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 164 |
except Exception as e:
|
| 165 |
-
print(f"Error: {e}")
|
|
|
|
| 1 |
import discord
|
|
|
|
|
|
|
| 2 |
from gradio_client import Client
|
| 3 |
import os
|
|
|
|
| 4 |
import random
|
| 5 |
from PIL import Image
|
| 6 |
import asyncio
|
| 7 |
import glob
|
| 8 |
import pathlib
|
| 9 |
|
| 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):
|
| 22 |
"""Generates an image based on a prompt"""
|
| 23 |
+
negative_prompt = ""
|
| 24 |
seed = random.randint(0, 1000)
|
| 25 |
number_of_images = 4
|
| 26 |
guidance_scale = 7
|
| 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,
|
| 36 |
+
seed,
|
| 37 |
+
number_of_images,
|
| 38 |
+
guidance_scale,
|
| 39 |
+
custom_timesteps_1,
|
| 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
|
| 50 |
guidance_scale_2 = 4
|
| 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,
|
| 58 |
+
custom_timesteps_2,
|
| 59 |
+
number_of_inference_steps_2,
|
| 60 |
+
api_name="/upscale256",
|
| 61 |
+
)
|
| 62 |
+
return result_path
|
| 63 |
+
|
| 64 |
|
| 65 |
async def react_1234(reaction_emojis, combined_image_dfif):
|
| 66 |
"""Sets up 4 reaction emojis so the user can choose an image to upscale for deepfloydif"""
|
| 67 |
for emoji in reaction_emojis:
|
| 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"""
|
|
|
|
| 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:
|
| 132 |
+
combined_image_dfif = await thread.send(
|
| 133 |
+
f"{interaction.user.mention} React with the image number you want to upscale!",
|
| 134 |
+
file=discord.File(f, f"{partial_path}.png"),
|
| 135 |
+
)
|
| 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 |
|
| 145 |
+
|
| 146 |
+
async def deepfloydif_stage_2_react_check(reaction, user):
|
| 147 |
"""Checks for a reaction in order to call dfif2"""
|
| 148 |
try:
|
| 149 |
+
if os.environ.get("TEST_ENV") == "True":
|
| 150 |
+
print("Running deepfloydif_stage_2_react_check")
|
| 151 |
global BOT_USER_ID
|
| 152 |
global DEEPFLOYDIF_CHANNEL_ID
|
| 153 |
if user.id != BOT_USER_ID:
|
| 154 |
thread = reaction.message.channel
|
| 155 |
thread_parent_id = thread.parent.id
|
| 156 |
+
if thread_parent_id == DEEPFLOYDIF_CHANNEL_ID:
|
| 157 |
if reaction.message.attachments:
|
| 158 |
+
if user.id == reaction.message.mentions[0].id:
|
| 159 |
attachment = reaction.message.attachments[0]
|
| 160 |
+
image_name = attachment.filename
|
| 161 |
+
partial_path = image_name[:-4]
|
| 162 |
+
full_path = "/tmp/" + partial_path
|
|
|
|
|
|
|
| 163 |
emoji = reaction.emoji
|
| 164 |
if emoji == "↖️":
|
| 165 |
index = 0
|
|
|
|
| 168 |
elif emoji == "↙️":
|
| 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":
|
| 187 |
+
print("Running deepfloydif_stage_2")
|
|
|
|
|
|
|
| 188 |
if index == 0:
|
| 189 |
position = "top left"
|
| 190 |
elif index == 1:
|
|
|
|
| 192 |
elif index == 2:
|
| 193 |
position = "bottom left"
|
| 194 |
elif index == 3:
|
| 195 |
+
position = "bottom right"
|
| 196 |
+
await thread.send(f"Upscaling the {position} image...")
|
| 197 |
+
|
| 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}")
|