-
Notifications
You must be signed in to change notification settings - Fork 0
/
stable_bot.py
385 lines (326 loc) · 13.4 KB
/
stable_bot.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
# -*- coding: utf-8 -*-
"""
Created on Sun Feb 25 15:20:32 2024
@author: user
"""
from dotenv import dotenv_values
from stable_diffusion import *
from interactions import OptionType, slash_option, slash_command, SlashContext, Embed, SlashCommandChoice
import interactions
from interactions import Client, Intents, listen
from discord import File
import asyncio
import random
import re
from interactions import Button, ButtonStyle, ActionRow, StringSelectMenu, StringSelectOption
from interactions.api.events import Component
from interactions import Modal, ParagraphText, ShortText, SlashContext, slash_command, modal_callback, ModalContext
import mask
import localization as l
import detect_nsfw
waiting_messages = ["waiting_message."+str(i) for i in range(10)]
bot = Client(intents=Intents.DEFAULT)
TOKEN = dotenv_values(".env")["TOKEN"]
bot = interactions.Client(token=TOKEN)
MAX_BATCH_SIZE = str(dotenv_values(".env")["MAX_BATCH_SIZE"])
DEFAULT_IMG_RATIO = dotenv_values(".env")["DEFAULT_IMG_RATIO"]
SQUARE_IMG_SIZE = str(dotenv_values(".env")["SQUARE_IMG_SIZE"])+"x"+str(dotenv_values(".env")["SQUARE_IMG_SIZE"])
LANDSCAPE_IMG_SIZE = dotenv_values(".env")["LANDSCAPE_IMG_SIZE"]
PORTRAIT_IMG_SIZE = dotenv_values(".env")["PORTRAIT_IMG_SIZE"]
MAX_WIDTH = int(dotenv_values(".env")["MAX_WIDTH"])
MAX_HEIGHT = int(dotenv_values(".env")["MAX_HEIGHT"])
PROMPT_REQUIRED = int(dotenv_values(".env")["PROMPT_REQUIRED"])
FIXED_SIZE_TXT2IMG = int(dotenv_values(".env")["FIXED_SIZE_TXT2IMG"])
@listen() # this decorator tells snek that it needs to listen for the corresponding event, and run this coroutine
async def on_ready():
# This event is called when the bot is ready to respond to commands
print("Ready")
print(f"This bot is owned by {bot.owner}")
@slash_command(name="imagine", description="Generate a picture using StableDiffusion")
@slash_option(
name="prompt",
description="What you want to see",
required=PROMPT_REQUIRED,
opt_type=OptionType.STRING
)
@slash_option(
name="negative_prompt",
description="What you don't want to see",
required=False,
opt_type=OptionType.STRING
)
@slash_option(
name="size",
description="Choose a size [10, "+str(MAX_WIDTH)+"]x[10, "+str(MAX_WIDTH)+"]",
required=False,
opt_type=OptionType.STRING,
choices=[
SlashCommandChoice(name=SQUARE_IMG_SIZE, value=SQUARE_IMG_SIZE),
SlashCommandChoice(name=LANDSCAPE_IMG_SIZE, value=LANDSCAPE_IMG_SIZE),
SlashCommandChoice(name=PORTRAIT_IMG_SIZE, value=PORTRAIT_IMG_SIZE)
] if FIXED_SIZE_TXT2IMG else []
)
@slash_option(
name="batch_size",
description="How many image ? [1-"+MAX_BATCH_SIZE+"]",
required=False,
opt_type=OptionType.INTEGER
)
async def generate(ctx: SlashContext, prompt: str = "", negative_prompt: str = "", size: str = "",batch_size: int =1):
if not 1<=batch_size<=int(MAX_BATCH_SIZE):
await ctx.send(content=l.get(ctx.locale, "invalid_batch_size", MAX_BATCH_SIZE),ephemeral=True)
return
if size=="":
size = {"SQUARE":SQUARE_IMG_SIZE, "LANDSCAPE":LANDSCAPE_IMG_SIZE, "PORTRAIT":PORTRAIT_IMG_SIZE}.get("DEFAULT_IMG_RATIO", SQUARE_IMG_SIZE)
size = size.split("x")
size[0] = int(size[0])
size[1] = int(size[1])
if not (10<=size[0]<=MAX_WIDTH and 10<=size[1]<=MAX_HEIGHT):
await ctx.send(content=l.get(ctx.locale, "invalid_size", MAX_BATCH_SIZE),ephemeral=True)
return
if ctx.channel is None:
await ctx.send(content=l.get(ctx.locale, "no_private_message", MAX_BATCH_SIZE),ephemeral=True)
neg_prompt=""
if not ctx.channel.nsfw:
neg_prompt = negative_prompt + "NSFW, nude, explicit, sexual, nudity"
await ctx.send(l.get(ctx.locale, random.choice(waiting_messages)), ephemeral=True)
for i in range(batch_size):
image = await get_image(ctx.author,prompt,neg_prompt, size)
is_nsfw = detect_nsfw.detect_nsfw(image)
if is_nsfw and not ctx.channel.nsfw:
await ctx.send(l.get(ctx.locale, "nsfw_detected"), ephemeral=True)
else:
embed = Embed(
title= l.get(ctx.locale, "processing_complete") if batch_size==1 else l.get(ctx.locale, "processing_complete_batch",i+1, batch_size),
description= l.get(ctx.locale, "description", prompt, negative_prompt) if negative_prompt!="" else l.get(ctx.locale, "description_neg", prompt),
footer=l.get(ctx.locale, "footer")
)
# Send the response after processing is complete
await ctx.send(embed=embed, files=[image])
os.remove(image)
@slash_command(name="transform", description="Transform a picture using StableDiffusion")
@slash_option(
name="image_url",
description="Original image URL to transform (PNG, JPG, GIF)",
required=True,
opt_type=OptionType.STRING
)
@slash_option(
name="prompt",
description="What you want to see",
required=False,
opt_type=OptionType.STRING
)
@slash_option(
name="negative_prompt",
description="What you don't want to see",
required=False,
opt_type=OptionType.STRING
)
@slash_option(
name="denoising",
description="How similar should it be (0.0 MAX - 1.0 MIN)",
required=False,
opt_type=OptionType.NUMBER
)
@slash_option(
name="batch_size",
description="How many image ? [1-"+MAX_BATCH_SIZE+"]",
required=False,
opt_type=OptionType.INTEGER
)
async def transform(ctx: SlashContext, image_url: str, prompt: str = "", negative_prompt: str = "", denoising: float = 0.5, batch_size=1):
if not 1<=batch_size<=int(MAX_BATCH_SIZE):
await ctx.send(content=l.get(ctx.locale, "invalid_batch_size", MAX_BATCH_SIZE),ephemeral=True)
return
await ctx.send(random.choice(waiting_messages), ephemeral=True)
if not 0<=denoising<=1:
await ctx.send("Incorrect denoising",ephemeral=True)
return
for i in range(batch_size):
try:
image = await img2img(ctx.author,image_url,prompt,negative_prompt, denoising)
except Exception as e:
await ctx.send(l.get(ctx.locale, "invalid_image"),ephemeral=True)
return
embed = Embed(
title= l.get(ctx.locale, "processing_complete") if batch_size==1 else l.get(ctx.locale, "processing_complete_batch",i+1, batch_size),
description= l.get(ctx.locale, "description", prompt, negative_prompt) if negative_prompt!="" else l.get(ctx.locale, "description_neg", prompt),
footer=l.get(ctx.locale, "footer"),
images=[image_url]
)
# Send the response after processing is complete
await ctx.send(embed=embed, files=[image])
os.remove(image)
inpaint_action_menu: list[ActionRow] = [
ActionRow(
StringSelectMenu(
StringSelectOption(
label="Bigger",
emoji="<:zoomin:972506443938934814>",
value="bigger"
),
StringSelectOption(
label="Smaller",
emoji="<:zoomout:972511245112586240>",
value="smaller"
),
StringSelectOption(
label="UP",
emoji="<:uparrow:972510553497014282>",
value="move_up"
),
StringSelectOption(
label="DOWN",
emoji="<:downarrow:972510590847315988>",
value="move_down"
),
StringSelectOption(
label="LEFT",
emoji="<:leftarrow:972504681257500682>",
value="move_left"
),
StringSelectOption(
label="RIGHT",
emoji="<:rightarrow:972510572883087380>",
value="move_right"
),
StringSelectOption(
label="Enter MaskCode",
emoji="<:pen:972506421767847946>",
value="maskcode"
),
StringSelectOption(
label="Validate",
emoji="<:check:972503604592250940>",
value="validate"
),
custom_id="inpaint_act",
placeholder="Agrandir ou Rétrécir",
min_values=1,
max_values=1,
)
)
]
@slash_command(name="inpaint", description="Inpaint a picture using StableDiffusion")
@slash_option(
name="image_url",
description="Original image URL to transform (PNG, JPG, GIF)",
required=True,
opt_type=OptionType.STRING
)
@slash_option(
name="mask_code",
description="size;x;y:maskcode",
required=True,
opt_type=OptionType.STRING
)
@slash_option(
name="prompt",
description="What you want to see",
required=False,
opt_type=OptionType.STRING
)
@slash_option(
name="negative_prompt",
description="What you don't want to see",
required=False,
opt_type=OptionType.STRING
)
@slash_option(
name="denoising",
description="How similar should it be (0.0 MAX - 1.0 MIN)",
required=False,
opt_type=OptionType.NUMBER
)
@slash_option(
name="mask_blur",
description="How similar should it be (0.0 MIN - 64 MAX)",
required=False,
opt_type=OptionType.NUMBER
)
async def inpaint(ctx: SlashContext, image_url: str, mask_code:str, prompt: str = "", negative_prompt: str = "", denoising: float = 0.5):
#mask_code = "50;0;0:maskcode"#size;x;y
maskcode_l = mask_code.replace(":maskcode","").split(";")
try:
iid, mid = mask.create_image_mask(image_url, int(maskcode_l[1]), int(maskcode_l[2]), int(maskcode_l[0]))
except Exception as e:
await ctx.send(l.get(ctx.locale, "invalid_image"),ephemeral=True)
return
await ctx.send(mask_code+"\n`"+image_url+"`\n[Prompt] "+prompt+"\n[Negatives] "+negative_prompt, components=inpaint_action_menu, ephemeral=True, files=[iid, mid])
os.remove(iid)
os.remove(mid)
ask_maskcode = Modal(
ShortText(label="size;x;y (End with :maskcode)", custom_id="maskcode_text"),
title="Enter your maskcode",
custom_id="maskcode_modal",
)
@listen(Component)
async def on_component(event: Component):
ctx = event.ctx
value = str(ctx.values[0])
mask_code = ctx.message.content.split(":maskcode\n")[0]
maskcode_l = mask_code.split(";")
image_url = ctx.message.content.split("`")[1]
prompt = ctx.message.content.split("[Prompt] ")[1]
negative_prompt = prompt.split("[Negatives]")[1]
prompt = prompt.split("[Negatives]")[0]
if ctx.custom_id == str("inpaint_act"):
if value == "maskcode":
await ctx.send_modal(ask_maskcode)
if value == "bigger":
maskcode_l[0] = str(int(maskcode_l[0]) + 5)
if value == "smaller":
maskcode_l[0] = str(int(maskcode_l[0]) - 5)
if value == "move_up":
maskcode_l[2] = str(int(maskcode_l[2]) - 5)
if value == "move_down":
maskcode_l[2] = str(int(maskcode_l[2]) + 5)
if value == "move_left":
maskcode_l[1] = str(int(maskcode_l[1]) - 5)
if value == "move_right":
maskcode_l[1] = str(int(maskcode_l[1]) + 5)
if value == "validate":
await ctx.send(content=random.choice(waiting_messages), ephemeral=True)
iid, mid = mask.create_image_mask(image_url, int(maskcode_l[1]), int(maskcode_l[2]), int(maskcode_l[0]))
image = await sd_inpaint(ctx.author,image_url, mid,prompt,negative_prompt)
embed = Embed(
title= l.get(ctx.locale, "processing_complete") if batch_size==1 else l.get(ctx.locale, "processing_complete_batch",i+1, batch_size),
description= l.get(ctx.locale, "description", prompt, negative_prompt) if negative_prompt!="" else l.get(ctx.locale, "description_neg", prompt),
footer=l.get(ctx.locale, "footer"),
images=[image_url]
)
await ctx.send(embed=embed,files=[image])
os.remove(image)
os.remove(iid)
os.remove(mid)
return
iid, mid = mask.create_image_mask(image_url, int(maskcode_l[1]), int(maskcode_l[2]), int(maskcode_l[0]))
mask_code = str(";".join(maskcode_l))+":maskcode"
await ctx.edit_origin(content=mask_code+"\n`"+image_url+"`\n[Prompt] "+prompt+"\n[Negatives] "+negative_prompt, components=inpaint_action_menu, files=[iid, mid])
os.remove(iid)
os.remove(mid)
@modal_callback("maskcode_modal")
async def on_modal_answer(ctx: ModalContext, maskcode_text: str):
image_url = ctx.message.content.split("`")[1]
prompt = ctx.message.content.split("[Prompt] ")[1]
negative_prompt = prompt.split("[Negatives]")[1]
prompt = prompt.split("[Negatives] ")[0]
if re.match(r"^[0-9]+;[0-9]+;[0-9]+:maskcode$", maskcode_text) is not None:
mask_code = maskcode_text.split(":maskcode")[0]
maskcode_l = mask_code.split(";")
iid, mid = mask.create_image_mask(image_url, int(maskcode_l[1]), int(maskcode_l[2]), int(maskcode_l[0]))
await ctx.send(maskcode_text+"\n`"+image_url+"`\n[Prompt] "+prompt+"\n[Negatives] "+negative_prompt, components=inpaint_action_menu, ephemeral=True, files=[iid, mid])
os.remove(iid)
os.remove(mid)
else:
await ctx.send(l.get(ctx.locale,"invalid_maskcode",maskcode_text), ephemeral=True)
@slash_command(name="credit", description="See contributors")
async def credit(ctx: SlashContext):
embed = Embed(
title="Credit",
description=l.get(ctx.locale,"credit"),
color=0xff0000 # RED color
)
await ctx.send(embed=embed)
bot.start()