0.6 some cleanup, testing original DFIF
Browse files
app.py
CHANGED
@@ -22,15 +22,18 @@ import time
|
|
22 |
#fix error on first command on bot startup
|
23 |
#stable diffusion upscale
|
24 |
#buttons for deepfloydIF (1,2,3,4)
|
|
|
25 |
|
26 |
|
27 |
|
28 |
|
29 |
-
#token update
|
30 |
DFIF_TOKEN = os.getenv('DFIF_TOKEN')
|
31 |
-
|
32 |
-
#
|
33 |
-
df = Client("
|
|
|
|
|
|
|
34 |
sdlu = Client("huggingface-projects/stable-diffusion-latent-upscaler", DFIF_TOKEN)
|
35 |
|
36 |
# Set up discord bot
|
@@ -38,28 +41,29 @@ class MyClient(discord.Client):
|
|
38 |
async def on_ready(self):
|
39 |
print('Logged on as', self.user)
|
40 |
|
41 |
-
|
42 |
async def on_message(self, message):
|
43 |
|
44 |
-
#
|
|
|
|
|
45 |
#1) it does not have @offline role
|
46 |
#2) user has @verified role
|
47 |
#3) bot is in #bot-test channel
|
48 |
|
|
|
|
|
|
|
49 |
|
50 |
# if the bot has this role, it won't run
|
51 |
-
OFFLINE_ROLE_ID = 1103676632667017266 # 1103676632667017266 = @offline
|
52 |
guild = message.guild
|
53 |
bot_member = guild.get_member(self.user.id)
|
54 |
if any(role.id == OFFLINE_ROLE_ID for role in bot_member.roles):
|
55 |
return
|
56 |
|
57 |
-
#
|
58 |
-
|
59 |
-
return
|
60 |
-
|
61 |
-
# if the message author doesn't have this role, the bot won't run
|
62 |
-
REQUIRED_ROLE_ID = 897376942817419265 # 900063512829755413 = @verified 897376942817419265 = @huggingfolks
|
63 |
if not any(role.id == REQUIRED_ROLE_ID for role in message.author.roles):
|
64 |
return
|
65 |
|
@@ -68,18 +72,14 @@ class MyClient(discord.Client):
|
|
68 |
if message.channel.id not in ALLOWED_CHANNEL_IDS:
|
69 |
return
|
70 |
|
71 |
-
|
72 |
-
|
73 |
-
#
|
74 |
-
if message.content.startswith('!deepfloydif'):
|
75 |
|
76 |
-
#predict
|
77 |
-
#was: 1,4,7.0
|
78 |
-
#predict
|
79 |
#(prompt, negative_prompt, seed, number_of_images, guidance_scale,custom_timesteps_1, number_of_inference_steps, api_name="/generate64")
|
80 |
#-> (stage_1_results, stage_1_param_path, stage_1_result_path)
|
81 |
|
82 |
-
#
|
83 |
prompt = message.content[12:].strip()
|
84 |
number_of_images = 4
|
85 |
|
@@ -92,43 +92,46 @@ class MyClient(discord.Client):
|
|
92 |
|
93 |
#stage_1_results, stage_1_param_path, stage_1_result_path = df.predict("gradio written on a wall", "blur", 1,1,7.0, 'smart100',50, api_name="/generate64")
|
94 |
|
95 |
-
#
|
96 |
png_files = [f for f in os.listdir(stage_1_results) if f.endswith('.png')]
|
97 |
|
98 |
-
|
99 |
-
# image merge test
|
100 |
-
# Assuming png_files contains the list of all PNG files in the directory
|
101 |
if png_files:
|
102 |
first_png = png_files[0]
|
103 |
second_png = png_files[1]
|
104 |
third_png = png_files[2]
|
105 |
fourth_png = png_files[3]
|
|
|
|
|
|
|
|
|
|
|
|
|
106 |
|
107 |
first_png_path = os.path.join(stage_1_results, first_png)
|
108 |
second_png_path = os.path.join(stage_1_results, second_png)
|
109 |
third_png_path = os.path.join(stage_1_results, third_png)
|
110 |
fourth_png_path = os.path.join(stage_1_results, fourth_png)
|
111 |
|
112 |
-
# Open the images using Pillow
|
113 |
img1 = Image.open(first_png_path)
|
114 |
img2 = Image.open(second_png_path)
|
115 |
img3 = Image.open(third_png_path)
|
116 |
img4 = Image.open(fourth_png_path)
|
117 |
|
118 |
-
#
|
119 |
combined_image = Image.new('RGB', (img1.width * 2, img1.height * 2))
|
120 |
|
121 |
-
#
|
122 |
combined_image.paste(img1, (0, 0))
|
123 |
combined_image.paste(img2, (img1.width, 0))
|
124 |
combined_image.paste(img3, (0, img1.height))
|
125 |
combined_image.paste(img4, (img1.width, img1.height))
|
126 |
|
127 |
-
#
|
128 |
combined_image_path = os.path.join(stage_1_results, 'combined_image.png')
|
129 |
combined_image.save(combined_image_path)
|
130 |
|
131 |
-
#
|
132 |
with open(combined_image_path, 'rb') as f:
|
133 |
await message.reply('Here is the combined image', file=discord.File(f, 'combined_image.png'))
|
134 |
|
@@ -156,11 +159,6 @@ class MyClient(discord.Client):
|
|
156 |
# Send the image file as a Discord attachment
|
157 |
with open(img_path, 'rb') as f:
|
158 |
await message.reply(f'Here is the generated image', file=discord.File(f, 'generated_image.png'))
|
159 |
-
|
160 |
-
|
161 |
-
|
162 |
-
|
163 |
-
|
164 |
'''
|
165 |
|
166 |
|
|
|
22 |
#fix error on first command on bot startup
|
23 |
#stable diffusion upscale
|
24 |
#buttons for deepfloydIF (1,2,3,4)
|
25 |
+
#application commands instead of message content checks (more user-friendly)
|
26 |
|
27 |
|
28 |
|
29 |
|
|
|
30 |
DFIF_TOKEN = os.getenv('DFIF_TOKEN')
|
31 |
+
|
32 |
+
#deepfloydIF
|
33 |
+
df = Client("DeepFloyd/IF", DFIF_TOKEN) #not reliable at the moment
|
34 |
+
#df = Client("huggingface-projects/IF", DFIF_TOKEN)
|
35 |
+
|
36 |
+
#stable diffusion upscaler
|
37 |
sdlu = Client("huggingface-projects/stable-diffusion-latent-upscaler", DFIF_TOKEN)
|
38 |
|
39 |
# Set up discord bot
|
|
|
41 |
async def on_ready(self):
|
42 |
print('Logged on as', self.user)
|
43 |
|
44 |
+
|
45 |
async def on_message(self, message):
|
46 |
|
47 |
+
#safety checks----------------------------------------------------------------------------------------------------
|
48 |
+
|
49 |
+
# tldr, bot should run if
|
50 |
#1) it does not have @offline role
|
51 |
#2) user has @verified role
|
52 |
#3) bot is in #bot-test channel
|
53 |
|
54 |
+
# bot won't respond to itself, prevents feedback loop + API spam
|
55 |
+
if message.author == self.user:
|
56 |
+
return
|
57 |
|
58 |
# if the bot has this role, it won't run
|
59 |
+
OFFLINE_ROLE_ID = 1103676632667017266 # 1103676632667017266 = @offline / under maintenance
|
60 |
guild = message.guild
|
61 |
bot_member = guild.get_member(self.user.id)
|
62 |
if any(role.id == OFFLINE_ROLE_ID for role in bot_member.roles):
|
63 |
return
|
64 |
|
65 |
+
# the message author needs this role in order to use the bot
|
66 |
+
REQUIRED_ROLE_ID = 897376942817419265 # 900063512829755413 = @verified, 897376942817419265 = @huggingfolks
|
|
|
|
|
|
|
|
|
67 |
if not any(role.id == REQUIRED_ROLE_ID for role in message.author.roles):
|
68 |
return
|
69 |
|
|
|
72 |
if message.channel.id not in ALLOWED_CHANNEL_IDS:
|
73 |
return
|
74 |
|
75 |
+
#deepfloydif----------------------------------------------------------------------------------------------------
|
76 |
+
|
77 |
+
if message.content.startswith('!deepfloydif'): # change to application commands, more intuitive
|
|
|
78 |
|
|
|
|
|
|
|
79 |
#(prompt, negative_prompt, seed, number_of_images, guidance_scale,custom_timesteps_1, number_of_inference_steps, api_name="/generate64")
|
80 |
#-> (stage_1_results, stage_1_param_path, stage_1_result_path)
|
81 |
|
82 |
+
# input prompt
|
83 |
prompt = message.content[12:].strip()
|
84 |
number_of_images = 4
|
85 |
|
|
|
92 |
|
93 |
#stage_1_results, stage_1_param_path, stage_1_result_path = df.predict("gradio written on a wall", "blur", 1,1,7.0, 'smart100',50, api_name="/generate64")
|
94 |
|
95 |
+
# stage_1_results -> path to directory with png files, so we isolate those
|
96 |
png_files = [f for f in os.listdir(stage_1_results) if f.endswith('.png')]
|
97 |
|
98 |
+
# merge images into larger, 2x2 image the way midjourney does it
|
|
|
|
|
99 |
if png_files:
|
100 |
first_png = png_files[0]
|
101 |
second_png = png_files[1]
|
102 |
third_png = png_files[2]
|
103 |
fourth_png = png_files[3]
|
104 |
+
|
105 |
+
'''
|
106 |
+
[],[],[],[] -> [][]
|
107 |
+
[][]
|
108 |
+
|
109 |
+
'''
|
110 |
|
111 |
first_png_path = os.path.join(stage_1_results, first_png)
|
112 |
second_png_path = os.path.join(stage_1_results, second_png)
|
113 |
third_png_path = os.path.join(stage_1_results, third_png)
|
114 |
fourth_png_path = os.path.join(stage_1_results, fourth_png)
|
115 |
|
|
|
116 |
img1 = Image.open(first_png_path)
|
117 |
img2 = Image.open(second_png_path)
|
118 |
img3 = Image.open(third_png_path)
|
119 |
img4 = Image.open(fourth_png_path)
|
120 |
|
121 |
+
# create a new blank image with the size of the combined images (2x2)
|
122 |
combined_image = Image.new('RGB', (img1.width * 2, img1.height * 2))
|
123 |
|
124 |
+
# paste the individual images into the combined image
|
125 |
combined_image.paste(img1, (0, 0))
|
126 |
combined_image.paste(img2, (img1.width, 0))
|
127 |
combined_image.paste(img3, (0, img1.height))
|
128 |
combined_image.paste(img4, (img1.width, img1.height))
|
129 |
|
130 |
+
# save the combined image
|
131 |
combined_image_path = os.path.join(stage_1_results, 'combined_image.png')
|
132 |
combined_image.save(combined_image_path)
|
133 |
|
134 |
+
# send the combined image file as a discord attachment
|
135 |
with open(combined_image_path, 'rb') as f:
|
136 |
await message.reply('Here is the combined image', file=discord.File(f, 'combined_image.png'))
|
137 |
|
|
|
159 |
# Send the image file as a Discord attachment
|
160 |
with open(img_path, 'rb') as f:
|
161 |
await message.reply(f'Here is the generated image', file=discord.File(f, 'generated_image.png'))
|
|
|
|
|
|
|
|
|
|
|
162 |
'''
|
163 |
|
164 |
|