-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathbot.py
1149 lines (988 loc) · 50.5 KB
/
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
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
import disnake
from typing import Optional
from disnake.ext import commands
import config as settings
import nest_asyncio
# above line is weird spyder workaround. DW about it.
import asyncio
import aiohttp
import time
import logging
from PIL import Image, ImageDraw
from nsfw_detector import predict
import base64
import io
import ast
import aiofiles
import re
import argparse
from random import randint
import os
import json
import views
try:
import intents
except:
print('ERROR: CP checker not imported')
model = predict.load_model('./checker.h5')
# gets model
logging.basicConfig(level=logging.INFO)
nest_asyncio.apply()
# weird spyder thing I don't get it
class UpscaleView(disnake.ui.View):
def __init__(self, codeid, number):
super().__init__(timeout=None)
with open('textcache/' + codeid + '.txt', 'r') as f:
text = f.read()
self.codeid = codeid
self.vars = ast.literal_eval(text)
self.number = number
filepath = asyncio.run(views.upscale_code(codeid, number))
self.filepath = filepath
self.color = asyncio.run(views.average_color(self.filepath))
self.moreinfo.custom_id = f'{codeid}@{number}@m'
self.facial.custom_id = f'{codeid}@{number}@f'
self.variate.custom_id = f'{codeid}@{number}@v'
@disnake.ui.button(custom_id='info', label='More Info!', style=disnake.ButtonStyle.blurple, emoji=settings.thinking_emoji, row=1)
async def moreinfo(self, button: disnake.ui.Button, inter: disnake.MessageInteraction):
embed=disnake.Embed(title='Status Sheet', color=self.color)
embed.add_field(name='Seed:', value=self.vars.get(str(self.number-1), -1).get('seed', -1), inline=False)
embed.add_field(name='Seed:', value=self.vars['0']['seed'], inline=False)
embed.add_field(name='Sampler:', value=self.vars['sampler'], inline=False)
embed.add_field(name='Prompt:', value=self.vars['prompt'][:1024], inline=False)
embed.add_field(name='Negative Prompt:', value=self.vars['neg_prompt'][:1024], inline=False)
embed.add_field(name='Model:', value=self.vars['model'], inline=False)
embed.add_field(name='Content Filter', value=str(self.vars['filter']), inline=False)
embed.add_field(name='CFG Scale:', value=self.vars['cfg_scale'], inline=False)
embed.add_field(name='Steps:', value=str(self.vars['steps']), inline=False)
embed.add_field(name='Dimensions:', value=str(self.vars['width']) + 'x' + str(self.vars['height']), inline=False)
embed.add_field(name='Upscaler:', value=str(self.vars['upscalers']), inline=False)
embed.add_field(name='Author:', value='<@'+str(self.vars['author'])+'>', inline=False)
embed.set_image(file=disnake.File(self.filepath))
await inter.send(embed=embed)
@disnake.ui.button(custom_id='esrgan', label='x4 Upscale', style=disnake.ButtonStyle.blurple, emoji=settings.eyes_emoji, row=1)
async def esrgan(self, button: disnake.ui.Button, inter: disnake.MessageInteraction):
await inter.send('Your Upscale is on the way!', ephemeral=True)
dm = False
try:
nsfw = not inter.channel.nsfw or settings.nsfw_filter
except AttributeError:
dm = True
nsfw = True
image_info = await pil2base64(Image.open(self.filepath))
source_image, width, height = image_info
author = inter.user
inter_response = await inter.original_response()
vvars = {
'prompt': self.vars['prompt'],
'raw_prompt': ' **x4 Upscale** ',
'neg_prompt': self.vars['neg_prompt'],
'width': width,
'height': height,
'upscalers': 'RealESRGAN_x4plus',
'model': 'stable_diffusion',
'cfg_scale': self.vars['cfg_scale'],
'userid': author.mention,
'channelid': inter.channel_id,
'sampler': self.vars['sampler'],
'karras': True,
'steps': 70,
'source_image': source_image,
'source_processing': 'img2img',
'source_mask': None,
'filter':nsfw,
'dm': dm,
'author': author,
'denoising_strength': 0.05,
'seed': -1,
'inter_response': inter_response,
'amount' : 1,
'tiling' : False,
'hires_fix' : False,
'english' : True,
'midj' : False,
}
await query_api(settings.url + settings.endpoint, vvars, inter)
@disnake.ui.button(custom_id='facial', label='Facial Redo', style=disnake.ButtonStyle.blurple, emoji=settings.pleading_emoji, row=1)
async def facial(self, button: disnake.ui.Button, inter: disnake.MessageInteraction):
await inter.send('Your Facial Redo is on the way!', ephemeral=True)
dm = False
try:
nsfw = not inter.channel.nsfw or settings.nsfw_filter
except AttributeError:
dm = True
nsfw = True
image_info = await pil2base64(Image.open(self.filepath))
source_image, width, height = image_info
author = inter.user
inter_response = await inter.original_response()
vvars = {
'prompt': self.vars['prompt'],
'raw_prompt': ' **Facial Redo** ',
'neg_prompt': self.vars['neg_prompt'],
'width': self.vars['width'],
'height': self.vars['height'],
'upscalers': 'CodeFormers',
'model': self.vars['model'],
'cfg_scale': self.vars['cfg_scale'],
'userid': author.mention,
'channelid': inter.channel_id,
'sampler': self.vars['sampler'],
'karras': True,
'steps': 10,
'source_image': source_image,
'source_processing': 'img2img',
'source_mask': None,
'filter':nsfw,
'dm': dm,
'author': author,
'denoising_strength': 0.05,
'seed': -1,
'inter_response': inter_response,
'amount' : 1,
'tiling' : False,
'hires_fix' : False,
'english' : False,
'midj' : False,
}
await query_api(settings.url + settings.endpoint, vvars, inter)
@disnake.ui.button(custom_id='variate', label='Make Variations', style=disnake.ButtonStyle.green)
async def variate(self, button: disnake.ui.Button, inter: disnake.MessageInteraction):
await inter.send('Your Variation is on the way!', ephemeral=True)
dm = False
try:
nsfw = not inter.channel.nsfw or settings.nsfw_filter
except AttributeError:
dm = True
nsfw = True
image_info = await pil2base64(Image.open(self.filepath))
source_image, width, height = image_info
author = inter.user
inter_response = await inter.original_response()
vvars = {
'prompt': self.vars['prompt'],
'raw_prompt': self.vars['raw_prompt'],
'neg_prompt': self.vars['neg_prompt'],
'width': self.vars['width'],
'height': self.vars['height'],
'upscalers': 'GFPGAN',
'model': self.vars['model'],
'cfg_scale': self.vars['cfg_scale'],
'userid': author.mention,
'channelid': inter.channel_id,
'sampler': self.vars['sampler'],
'karras': True,
'steps': 10,
'source_image': source_image,
'source_processing': 'img2img',
'source_mask': None,
'filter':nsfw,
'dm': dm,
'author': author,
'denoising_strength': 0.4,
'seed': -1,
'inter_response': inter_response,
'amount': settings.default_images,
'tiling' : False,
'hires_fix' : True,
'english' : False,
'midj' : self.vars.get('midj', False),
}
await query_api(settings.url + settings.endpoint, vvars, inter)
class GenView(disnake.ui.View):
def make_view(self, amount, codeid, midj=False):
u1 = disnake.ui.Button(custom_id=codeid+'_u1', label='U1', style=disnake.ButtonStyle.blurple, row=0)
u2 = disnake.ui.Button(custom_id=codeid+'_u2', label='U2', style=disnake.ButtonStyle.blurple, row=0)
u3 = disnake.ui.Button(custom_id=codeid+'_u3', label='U3', style=disnake.ButtonStyle.blurple, row=0)
u4 = disnake.ui.Button(custom_id=codeid+'_u4', label='U4', style=disnake.ButtonStyle.blurple, row=0)
u5 = disnake.ui.Button(custom_id=codeid+'_u5', label='U5', style=disnake.ButtonStyle.blurple, row=1)
u6 = disnake.ui.Button(custom_id=codeid+'_u6', label='U6', style=disnake.ButtonStyle.blurple, row=1)
u7 = disnake.ui.Button(custom_id=codeid+'_u7', label='U7', style=disnake.ButtonStyle.blurple, row=1)
u8 = disnake.ui.Button(custom_id=codeid+'_u8', label='U8', style=disnake.ButtonStyle.blurple, row=1)
u9 = disnake.ui.Button(custom_id=codeid+'_u9', label='U9', style=disnake.ButtonStyle.blurple, row=1)
if amount == 1:
u1 = disnake.ui.Button(custom_id=codeid+'_u1', label='U1', style=disnake.ButtonStyle.blurple, row=0, disabled=True)
elif amount == 4:
u3 = disnake.ui.Button(custom_id=codeid+'_u3', label='U3', style=disnake.ButtonStyle.blurple, row=1)
u4 = disnake.ui.Button(custom_id=codeid+'_u4', label='U4', style=disnake.ButtonStyle.blurple, row=1)
elif amount == 6:
u4 = disnake.ui.Button(custom_id=codeid+'_u4', label='U4', style=disnake.ButtonStyle.blurple, row=1)
elif amount == 9:
u4 = disnake.ui.Button(custom_id=codeid+'_u4', label='U4', style=disnake.ButtonStyle.blurple, row=1)
u7 = disnake.ui.Button(custom_id=codeid+'_u7', label='U7', style=disnake.ButtonStyle.blurple, row=2)
u8 = disnake.ui.Button(custom_id=codeid+'_u8', label='U8', style=disnake.ButtonStyle.blurple, row=2)
u9 = disnake.ui.Button(custom_id=codeid+'_u9', label='U9', style=disnake.ButtonStyle.blurple, row=2)
regen = disnake.ui.Button(custom_id=codeid+'_regen', emoji=settings.redo_emoji, style=disnake.ButtonStyle.blurple, row=1)
async def regencallback(interaction):
with open('textcache/' + codeid + '.txt', 'r') as f:
text = f.read()
rvars = ast.literal_eval(text)
await imagine(interaction, rvars['raw_prompt'])
async def u1callback(interaction):
await upscale(interaction, number=1, code=codeid)
async def u2callback(interaction):
await upscale(interaction, number=2, code=codeid)
async def u3callback(interaction):
await upscale(interaction, number=3, code=codeid)
async def u4callback(interaction):
await upscale(interaction, number=4, code=codeid)
async def u5callback(interaction):
await upscale(interaction, number=5, code=codeid)
async def u6callback(interaction):
await upscale(interaction, number=6, code=codeid)
async def u7callback(interaction):
await upscale(interaction, number=7, code=codeid)
async def u8callback(interaction):
await upscale(interaction, number=8, code=codeid)
async def u9callback(interaction):
await upscale(interaction, number=9, code=codeid)
regen.callback = regencallback
u1.callback = u1callback
u2.callback = u2callback
u3.callback = u3callback
u4.callback = u4callback
u5.callback = u5callback
u6.callback = u6callback
u7.callback = u7callback
u8.callback = u8callback
u9.callback = u9callback
view = disnake.ui.View(timeout=None)
if amount >= 1:
view.add_item(u1)
if amount >= 2:
view.add_item(u2)
if amount >= 4:
view.add_item(u3)
view.add_item(u4)
if amount >= 6:
view.add_item(u5)
view.add_item(u6)
if amount >= 8:
view.add_item(u7)
view.add_item(u8)
if amount >= 9:
view.add_item(u9)
if midj:
view.add_item(regen)
return view
class PersistentViewBot(commands.AutoShardedBot):
def __init__(self):
super().__init__(command_prefix=commands.when_mentioned)
async def on_ready(self):
print(f"Logged in as {self.user} (ID: {self.user.id})\n------")
bot = PersistentViewBot()
@bot.slash_command(description='Help menu!!!')
async def see_help(inter):
embed=disnake.Embed(title='Help menu!', color=0x00ff40)
embed.set_image(url='https://i.imgur.com/Ke5vRBr.png')
await inter.response.send_message(embed=embed)
@bot.slash_command(description='See total amount of servers the bot is in!')
async def total_servers(inter):
await inter.response.send_message(f'I am in {len(bot.guilds)} servers.', ephemeral=True)
@bot.slash_command(description='Caption image with CLIP interrogator')
async def caption(
inter: disnake.ApplicationCommandInteraction,
image: disnake.Attachment = commands.Param(description='Image to caption'),
):
if not str(image.content_type) == image.content_type or not image.content_type.endswith(settings.input_types):
await inter.response.send_message('Attachment is not valid image of types: WebP, PNG, JPG, JPEG', ephemeral=True)
return
await inter.response.defer(with_message = True)
image_bytes = await image.read()
base64_bytes = base64.b64encode(image_bytes)
base64_string = base64_bytes.decode('utf-8')
json_data = {
"forms": [{"name": "caption",}],
"source_image": base64_string}
headers = {
# Already added when you pass json= but not when you pass data=
# 'Content-Type': 'application/json',
'apikey': settings.sd_api_key,
}
try:
async with aiohttp.ClientSession() as session:
async with session.post('https://stablehorde.net/api/v2/interrogate/async', json=json_data, headers=headers) as response:
rawjson = await response.json()
print(str(rawjson))
id = rawjson['id']
await asyncio.sleep(5)
done = False
while not done:
response = await session.get('https://stablehorde.net/api/v2/interrogate/status/' + id)
jsondata = await response.json()
print('jsondata, I am your father:' + str(jsondata))
try:
done = jsondata['state'] == 'done'
except:
done = False
await asyncio.sleep(settings.wait_time)
buffer = io.BytesIO(image_bytes)
buffer.seek(0)
color = await views.average_color(Image.open(buffer))
embed = disnake.Embed(title='Image Captioned!', color=color)
embed.set_thumbnail(url=image.url)
embed.add_field(name='Caption:', value=jsondata['forms'][0]['result']['caption'], inline=False)
await inter.send('Interrogation done for ' + inter.author.mention + '!', embed=embed)
except:
await inter.send('Interrogation failed', ephemeral=True)
@bot.slash_command(description='Alternate upscale endpoint if the buttons don\'t work')
async def upscale(
inter: disnake.ApplicationCommandInteraction,
code: str = commands.Param(description = 'Code for upscaling', min_length=36, max_length=36),
number: int = commands.Param(le=8, ge=1,description = 'Number to upscale'),
):
try:
await inter.response.defer(with_message=True)
except:
pass
with open('textcache/' + code + '.txt', 'r') as f:
text = f.read()
vars = ast.literal_eval(text)
if not number == 200:
filepath = await views.upscale_code(code, number)
else:
filepath = 'imagecache/' + code + settings.img_type
#try:
color = await views.average_color(filepath)
if not number == 200:
embed=disnake.Embed(title='Upscale Done!', color=color)
embed.add_field(name='Seed:', value=str(vars[str(number-1)]['seed']), inline=False)
else:
embed=disnake.Embed(title='Generation Done!', color=color)
embed.add_field(name='Seed:', value=str(vars['0']['seed']), inline=False)
embed.set_image(file=disnake.File(filepath))
if not number == 200:
view = UpscaleView(code, number)
if number == 200:
message = await inter.followup.send('Image for ' + inter.author.mention + '!',file=disnake.File(filepath))
else:
message = await inter.followup.send('Image for ' + inter.author.mention + '!',file=disnake.File(filepath), view=view)
#with open('textcache/' + code + '-url.txt', 'w') as f:
# f.write(message.attachments[0].url)
await views.add_data(code + '@' + str(number))
@bot.slash_command(description='Generates images using Stable Horde!')
async def generate(
inter: disnake.ApplicationCommandInteraction,
prompt: str = commands.Param(description='What the AI-generated image should be of.'),
neg_prompt: str = commands.Param(default = '2D, grid, text', description='What the AI image model should avoid. Default: \'2D, grid, text\''),
upscalers: str = commands.Param(choices=settings.processor_list, default='GFPGAN', description='Which Post-Processing to use for the images. Default: GFPGAN'),
model: str = commands.Param(default=settings.default_model,autocomplete=settings.autocomp_models, description='Which model to generate the image with. Default: ' + str(settings.default_model)),
cfg_scale: Optional[float] = commands.Param(default=8, le=30, ge=-40, description='How much the image should look like your prompt. Default: 8'),
#imageurl: str = commands.Param(name = 'init_image', default = None, description='Initial image for img2img.'),
width: int = commands.Param(default = settings.default_width, le=settings.max_size, ge=64, description='Width of the final image. Default: ' + str(settings.default_width)),
height: int = commands.Param(default = settings.default_height, le=settings.max_size, ge=64, description='Height of the final image. Default: ' + str(settings.default_height)),
sampler: str = commands.Param(default = settings.default_sampler, description = 'ADVANCED: Which stable diffusion sampler to use. Default: ' + settings.default_sampler, choices=settings.sampler_list),
steps: int = commands.Param(default=settings.default_steps, le=50, ge=1, description='Greater: Higher Image Quality but takes longer. Default: ' + str(settings.default_steps)),
seed: int = commands.Param(default=-1, description='Seed for the image.'),
amount: int = commands.Param(default=settings.default_images, choices = [1,2,4,6,8,9], description='Amount of images to generate. Default: ' + str(settings.default_images)),
tiling: bool = commands.Param(default = False, description='Whether to have the image be repeating and tileable. Default: False'),
hires_fix: bool = commands.Param(default = True, description='Improves Image Quality at high resolutions. Default: True'),
english: bool = commands.Param(default = True, description='Set to false if prompt language is not English'),
):
raw_prompt = prompt
if not model in settings.model_list:
await inter.response.send_message('Invalid Model: ```'+model+'```', ephemeral=True)
return
karras = True
dm = False
imageurl = None
userid = inter.author.mention
cfg_scale = round(cfg_scale * 2)/2
width = round(width/64)*64
height = round(height/64)*64
try:
nsfw = not inter.channel.nsfw or settings.nsfw_filter
except AttributeError:
dm = True
nsfw = True
embed=disnake.Embed(title='Generation queued with following parameters:', color=0xf0e000)
embed.set_author(name='Your generation request is on its way!')
embed.set_thumbnail(url=settings.embed_icon)
embed.add_field(name='Prompt:', value=prompt[:512], inline=False)
embed.add_field(name='Negative Prompt:', value=neg_prompt[:512], inline=False)
embed.add_field(name='CFG Scale:', value=cfg_scale, inline=True)
embed.add_field(name='Steps:', value=steps, inline=True)
embed.add_field(name='Model:', value=model, inline=True)
embed.add_field(name='Content Filter:', value=str(nsfw), inline=True)
embed.add_field(name='Dimensions:', value=str(width) + 'x' + str(height), inline=True)
print(nsfw)
if settings.use_embeds:
await inter.response.send_message(embed=embed)
else:
user_response = 'Generating with following parameters:\nPrompt: ```' + prompt + '```Negative Prompt: ```' + neg_prompt + '```CFG Scale: ```' + str(cfg_scale) + '```Steps: ```' + str(steps) + '```for ' + userid + '. Please be patient'
await inter.response.send_message(user_response)
inter_response = await inter.original_response()
vars = {
'prompt': prompt,
'neg_prompt': neg_prompt,
'width': width,
'height': height,
'upscalers': upscalers,
'model': model,
'cfg_scale': cfg_scale,
'imageurl': imageurl,
'userid': userid,
'channelid': inter.channel_id,
'sampler': sampler,
'karras': karras,
'steps': steps,
'source_image': None,
'source_processing': 'img2img',
'source_mask': None,
'filter':nsfw,
'dm': dm,
'author': inter.author,
'denoising_strength': 0.75,
'seed': seed,
'inter_response': inter_response,
'amount': amount,
'tiling' : tiling,
'hires_fix' : hires_fix,
'english' : english,
'midj' : False,
'raw_prompt' : raw_prompt,
}
global url
global endpoint
await query_api(settings.url + settings.endpoint, vars, inter)
@bot.slash_command(description='Midjourney for brokies')
async def imagine(
inter: disnake.ApplicationCommandInteraction,
prompt: str = commands.Param(description='The prompt to imagine (supports midj args)'),
):
model = settings.default_model
raw_prompt = prompt
# grab raw prompt
width = settings.default_width
height = settings.default_height
if 'https://' in raw_prompt:
image_urls = re.findall(r'\b((?:https?://)?(?:(?:www\.)?(?:[\da-z\.-]+)\.(?:[a-z]{2,6})|(?:(?:25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)\.){3}(?:25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)|(?:(?:[0-9a-fA-F]{1,4}:){7,7}[0-9a-fA-F]{1,4}|(?:[0-9a-fA-F]{1,4}:){1,7}:|(?:[0-9a-fA-F]{1,4}:){1,6}:[0-9a-fA-F]{1,4}|(?:[0-9a-fA-F]{1,4}:){1,5}(?::[0-9a-fA-F]{1,4}){1,2}|(?:[0-9a-fA-F]{1,4}:){1,4}(?::[0-9a-fA-F]{1,4}){1,3}|(?:[0-9a-fA-F]{1,4}:){1,3}(?::[0-9a-fA-F]{1,4}){1,4}|(?:[0-9a-fA-F]{1,4}:){1,2}(?::[0-9a-fA-F]{1,4}){1,5}|[0-9a-fA-F]{1,4}:(?:(?::[0-9a-fA-F]{1,4}){1,6})|:(?:(?::[0-9a-fA-F]{1,4}){1,7}|:)|fe80:(?::[0-9a-fA-F]{0,4}){0,4}%[0-9a-zA-Z]{1,}|::(?:ffff(?::0{1,4}){0,1}:){0,1}(?:(?:25[0-5]|(?:2[0-4]|1{0,1}[0-9]){0,1}[0-9])\.){3,3}(?:25[0-5]|(?:2[0-4]|1{0,1}[0-9]){0,1}[0-9])|(?:[0-9a-fA-F]{1,4}:){1,4}:(?:(?:25[0-5]|(?:2[0-4]|1{0,1}[0-9]){0,1}[0-9])\.){3,3}(?:25[0-5]|(?:2[0-4]|1{0,1}[0-9]){0,1}[0-9])))(?::[0-9]{1,4}|[1-5][0-9]{4}|6[0-4][0-9]{3}|65[0-4][0-9]{2}|655[0-2][0-9]|6553[0-5])?(?:/[\w\.-]*)*/?)\b', raw_prompt)
if len(image_urls) != 1:
await inter.response.send_message('Error, only one image allowed', ephemeral=True)
return
image_url = image_urls[0]
print(f'Image found: {image_url}')
prompt = prompt.split(image_url)[1]
try:
# turn init_image attachment data to pil image with mult of 64 width/height
init_image = await url2pil(image_url)
new_width = round(init_image.width/64)*64
new_height = round(init_image.height/64)*64
for i in range(5):
if max(new_width, new_height) < 400:
new_width *= 2
new_height *= 2
else:
break
init_image = init_image.resize((new_width, new_height), Image.Resampling.BICUBIC)
try:
image_info = await pil2base64(init_image)
source_image, width, height = image_info
except:
await inter.response.send_message('Error, Image file does not have valid encoding', ephemeral=True)
return
except:
source_image = None
print('Image load failed')
await inter.response.send_message('Error, Invalid Image URL', ephemeral=True)
return
else:
source_image = None
# set anime model if anime-ish prompt
if 'anime' in prompt or 'waifu' in prompt:
model = 'Anything v3'
neg_prompt = '2d, grid, text'
if not model in settings.model_list:
await inter.response.send_message('Invalid Model: ```'+model+'```', ephemeral=True)
return
steps = 30
cfg_scale = 8
tiling = False
#try:
if 1:
arguments = prompt.split('-')
for i in range(len(arguments)):
try:
if 'http' in arguments[i]:
arguments.pop(i)
except:
break
prompt = arguments.pop(0)
if not len(arguments) == 0:
arguments = ('-' + '-'.join(arguments)).split(' ')
print(arguments)
for i, argument in enumerate(arguments):
try:
keyvalue = arguments[i+1]
except:
keyvalue = None
print(f'{argument} : {keyvalue}')
if argument in ['--ar', '-ar', '-aspect' ,'--aspect']:
width, height = await views.ar2size(keyvalue, settings.default_size)
elif argument in ['--chaos', '-chaos']:
cfg_scale = int(keyvalue)//3.5
elif argument in ['--no', '-no']:
neg_prompt = neg_prompt + ', ' + keyvalue
elif argument in ['--quality', '-quality']:
steps = round(float(keyvalue)*35)
elif argument in ['--niji', '-niji']:
model = 'Anything v3'
elif argument in ['--tile', '-tile']:
tiling = True
elif '-' in argument and len(argument) < 20:
await inter.response.send_message('Invalid argument: ```' + argument + '```')
await inter.response.send_message('You got it, boss :saluting_face:', ephemeral=True)
#except:
# await inter.response.send_message('Invalid Args', ephemeral=True)
# return
karras = True
dm = False
imageurl = None
userid = inter.author.mention
try:
nsfw = not inter.channel.nsfw or settings.nsfw_filter
except AttributeError:
dm = True
nsfw = True
vars = {
'prompt': prompt,
'neg_prompt': neg_prompt,
'width': width,
'height': height,
'upscalers': 'GFPGAN',
'model': model,
'cfg_scale': cfg_scale,
'imageurl': imageurl,
'userid': userid,
'channelid': inter.channel_id,
'sampler': 'k_euler_a',
'karras': karras,
'steps': steps,
'source_image': source_image,
'source_processing': 'img2img',
'source_mask': None,
'filter':nsfw,
'dm': dm,
'author': inter.author,
'denoising_strength': 0.50,
'seed': -1,
'inter_response': None,
'amount': 4,
'tiling' : tiling,
'hires_fix' : True,
'english' : True,
'midj' : True,
'raw_prompt' : raw_prompt,
}
global url
global endpoint
await query_api(settings.url + settings.endpoint, vars, inter)
@bot.slash_command(description='Generates images using Stable Horde img2img!')
async def riff(
inter: disnake.ApplicationCommandInteraction,
init_image: disnake.Attachment = commands.Param(description='Initial Image'),
prompt: str = commands.Param(description='Command for the AI, e.g. \'Make her hair green\''),
neg_prompt: str = commands.Param(default = '2D, grid, text', description='What the AI image model should avoid. Default: \'2D, grid, text\''),
upscalers: str = commands.Param(choices=settings.processor_list, default='GFPGAN', description='Which Post-Processing to use for the images. Default: GFPGAN'),
model: str = commands.Param(default=settings.default_riff,autocomplete=settings.autocomp_models, description='Which model to generate the image with. Default: ' + str(settings.default_riff)),
cfg_scale: Optional[float] = commands.Param(default=8, le=30, ge=-40, description='How much the image should look like your prompt. Default: 8'),
denoising_strength: int = commands.Param(default=50, name = 'image_guidance',le=100, ge=0, description = 'How much the image should match the provided, 100 = clone, 0 = no effect'),
sampler: str = commands.Param(default = settings.default_sampler, description = 'ADVANCED: Which stable diffusion sampler to use. Default: ' + settings.default_sampler, choices=settings.sampler_list),
steps: int = commands.Param(default=settings.default_steps, le=50, ge=1, description='Greater: Higher Image Quality but takes longer. Default: ' + str(settings.default_steps)),
seed: int = commands.Param(default=-1, description='Seed for the image.'),
amount: int = commands.Param(default=settings.default_images, choices = [1,2,4,6,8,9], description='Amount of images to generate. Default: ' + str(settings.default_images)),
tiling: bool = commands.Param(default = False, description='Whether to have the image be repeating and tileable. Default: False'),
hires_fix: bool = commands.Param(default = True, description='Improves Image Quality at high resolutions. Default: True'),
english: bool = commands.Param(default = True, description='Set to false if prompt language is not English'),
control_type: str = commands.param(default = None, choices=settings.acceptable_controls, description='ControlNet Parameter type'),
):
raw_prompt = prompt
if not model in settings.model_list and not model == 'pix2pix':
await inter.response.send_message('Invalid Model: ```'+model+'```', ephemeral=True)
return
if not str(init_image.content_type) == init_image.content_type or not init_image.content_type.endswith(settings.input_types):
await inter.response.send_message('Attachment is not valid image of types: WebP, PNG, JPG, JPEG', ephemeral=True)
return
print(f'Media Type: {init_image.content_type}')
denoising_strength = 1-(denoising_strength/100)
karras = True
dm = False
userid = inter.author.mention
cfg_scale = round(cfg_scale * 2)/2
# save init_image url for vars
image_url = init_image.url
# turn init_image attachment data to pil image with mult of 64 width/height
init_image = await url2pil(image_url)
new_width = round(init_image.width/64)*64
new_height = round(init_image.height/64)*64
for i in range(5):
if max(new_width, new_height) < 400:
new_width *= 2
new_height *= 2
else:
break
init_image = init_image.resize((new_width, new_height), Image.Resampling.BICUBIC)
try:
image_info = await pil2base64(init_image)
source_image, width, height = image_info
except:
await inter.response.send_message('Error, Image file does not have valid encoding', ephemeral=True)
return
try:
nsfw = not inter.channel.nsfw or settings.nsfw_filter
except AttributeError:
dm = True
nsfw = True
embed=disnake.Embed(title='img2img generation queued with following parameters:', color=0xf0e000)
embed.set_author(name='Your generation request is on its way!')
embed.set_thumbnail(url=settings.embed_icon)
embed.add_field(name='Prompt:', value=prompt[:512], inline=False)
embed.add_field(name='Negative Prompt:', value=neg_prompt[:512], inline=False)
embed.add_field(name='CFG Scale:', value=cfg_scale, inline=True)
embed.add_field(name='Steps:', value=steps, inline=True)
embed.add_field(name='Model:', value=model, inline=True)
embed.add_field(name='Content Filter:', value=str(nsfw), inline=True)
embed.add_field(name='Dimensions:', value=str(width) + 'x' + str(height), inline=True)
print(nsfw)
if settings.use_embeds:
await inter.response.send_message(embed=embed)
else:
user_response = 'Generating with following parameters:\nPrompt: ```' + prompt + '```Negative Prompt: ```' + neg_prompt + '```CFG Scale: ```' + str(cfg_scale) + '```Steps: ```' + str(steps) + '```for ' + userid + '. Please be patient'
await inter.response.send_message(user_response)
inter_response = await inter.original_response()
vars = {
'prompt': prompt,
'neg_prompt': neg_prompt,
'width': width,
'height': height,
'upscalers': upscalers,
'model': model,
'cfg_scale': cfg_scale,
'imageurl': image_url,
'userid': userid,
'channelid': inter.channel_id,
'sampler': sampler,
'karras': karras,
'steps': steps,
'source_image': source_image,
'source_processing': 'img2img',
'source_mask': None,
'filter':nsfw,
'dm': dm,
'author': inter.author,
'denoising_strength': denoising_strength,
'seed': seed,
'inter_response': inter_response,
'amount': amount,
'tiling' : tiling,
'hires_fix' : hires_fix,
'english' : english,
'control_type' : control_type,
'midj' : False,
'raw_prompt' : raw_prompt,
}
global url
global endpoint
await query_api(settings.url + settings.endpoint, vars, inter)
async def pil2base64(image):
width, height = image.size
rounded_width = 0
rounded_height = 0
if width <= height:
rounded_width = settings.default_width
rounded_height = (height/width*settings.default_width)
else:
rounded_height = settings.default_height
rounded_width = (width/height*settings.default_width)
rounded_width = round(rounded_width / 64) * 64
rounded_height = round(rounded_height / 64) * 64
rounded_width = min(max(rounded_width, 64), settings.max_size)
rounded_height = min(max(rounded_height, 64), settings.max_size)
image_resized = image.resize((rounded_width, rounded_height))
bytesio = io.BytesIO()
image_resized.save(bytesio, format = settings.format_type)
bytesio.seek(0)
image_string = base64.b64encode(bytesio.read()).decode()
return [image_string, width, height]
async def url2base64(url):
async with aiohttp.ClientSession() as session:
async with session.get(url) as response:
image_data = await response.read()
image = Image.open(io.BytesIO(image_data))
data = await pil2base64(image)
return data
async def url2pil(url):
async with aiohttp.ClientSession() as session:
async with session.get(url) as response:
image_data = await response.read()
image = Image.open(io.BytesIO(image_data))
return image
async def attach2base64(image):
image_data = await image.read()
image = Image.open(io.BytesIO(image_data))
data = await pil2base64(image)
return data
async def shade_cells(width, height, gens, amount):
rounded_width = rounded_height = 0
if width <= height:
rounded_width = 512
rounded_height = (height/width*512)
else:
rounded_height = 512
rounded_width = (width/height*512)
width = round(rounded_width)
height = round(rounded_height)
dst = Image.new('RGB', (width*4, height*2))
finished = gens['finished']
processing = gens['processing']
restarted = gens['restarted']
waiting = gens['waiting']
imagename = str(finished) + str(processing) + str(restarted) + str(waiting) + '_' + str(width) + 'x' + str(height) + 'n' + str(amount) + settings.img_type
if os.path.isfile('waitcache/' + imagename):
return 'waitcache/' + imagename
generations = []
for i in range(finished):
generations.append(settings.finished_color)
for i in range(restarted):
generations.append(settings.restarted_color)
for i in range(waiting):
generations.append(settings.waiting_color)
for i in range(processing):
generations.append(settings.processing_color)
for i in range(amount-len(generations)):
generations.append(settings.error_color)
images = []
for color in generations:
images.append(Image.new('RGB', (width, height), color=color))
dst = await views.make_grid(images, amount, width, height)
dst.save('waitcache/' + imagename)
return 'waitcache/' + imagename
async def fetch_image(session, url):
async with session.get(url) as response:
return await response.read()
async def url_to_pil(url):
async with aiohttp.ClientSession() as session:
image_data = await fetch_image(session, url)
with io.BytesIO(image_data) as f:
with Image.open(f) as img:
return img
async def query_api(url, vars, interaction):
if vars['midj']:
prompt = await settings.enhance_prompt(vars['prompt'])
else:
prompt = vars['prompt']
# temporary, remove later
if randint(1,20) == 1:
settings.autocomp_models = settings.redefine_autocomp()
msg_channel = bot.get_channel(vars['channelid'])
if vars['dm']:
msg_channel = vars['author']
try:
checkprompt = intents.check_cp(vars['prompt'])
if checkprompt[0]:
await msg_channel.send(vars['userid'] +', Suspected Child Porn prompt has been blocked and reported. If repeated multiple times, risk being banned.')
return
except:
print('WARNING: Unfiltered Prompt')
try:
amount = vars['amount']
except:
amount = settings.default_images
if vars['model'] == 'Midjourney Diffusion' and not vars['prompt'].startswith('midjrny-v4 style'):
await msg_channel.send('Auto-adding \'midjrny-v4 style\' to start of prompt!')
vars['prompt'] = 'midjrny-v4 style ' + vars['prompt']
headers = {
# Already added when you pass json= but not when you pass data=
# 'Content-Type': 'application/json',
'apikey': settings.sd_api_key,
}
params = {
'sampler_name': vars['sampler'],
#'seed_variation': 1,
'cfg_scale': 7.5,
'height': vars['height'],
'width': vars['width'],
'post_processing': [
vars['upscalers']
],
'karras': vars['karras'],
'tiling': vars['tiling'],
'hires_fix': vars['hires_fix'],
'steps': vars['steps'],
'n': amount,
'denoising_strength': vars['denoising_strength'],
}
if 'control_type' in vars:
if not vars['control_type'] == None:
params.update({'control_type': vars['control_type']})
if not vars.get('seed', -1) == -1:
params.update({'seed': str(vars.get('seed', -1))})
if not vars['english']:
vars['prompt'] = await views.trans_query(vars['prompt'])
vars['neg_prompt'] = await views.trans_query(vars['neg_prompt'])
json_data = {
'prompt': prompt + '###' + vars['neg_prompt'],
'params': params,
'nsfw': True,
'trusted_workers': True,
'censor_nsfw': False,
'models': [
vars['model']
],
'source_processing': vars['source_processing'],
}
if vars['upscalers'] == 'RealESRGAN_x4plus':
vars['width'] *= 4
vars['height'] *= 4
if not vars['source_image'] == None:
json_data.update({'source_image': vars['source_image']})
if not vars['source_mask'] == None:
json_data.update({'source_mask': vars['source_mask']})
vars.pop('source_image')
vars.pop('source_mask')
vars['midj'] = True
# Initialize a variable to track whether the desired status has been received, failed, and a list to capture results
done = False
failed = False
results = []
message = await msg_channel.send('Querying job for user ' + vars['userid'])
if not settings.accept_dm:
await message.edit('Sorry, but generating in DM\'s is disabled')
return
# Keep querying the API until the desired status is received
async with aiohttp.ClientSession() as session:
async with session.post(url + 'async', json=json_data, headers=headers) as response:
codeid = await response.json()
if not 'id' in codeid:
await msg_channel.send('Error: Job for user ' + vars['userid'] + ' failed. Full Error Report: ' + str(codeid))
return
codeid = codeid['id']
print(codeid)
start = time.time()
while not done: