-
Notifications
You must be signed in to change notification settings - Fork 12
/
simulacra.py
1153 lines (1041 loc) · 51.6 KB
/
simulacra.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 os
import time
import secrets
import io
import json
import torch
from torch import multiprocessing as mp
import logging
import sqlite3
import asyncio
import threading
import queue
import nextcord
from nextcord.ext import commands
from simulacra_glide_sample import main as gen
from yfcc_upscale import main as upscale
from collections import namedtuple
intents = nextcord.Intents.default()
intents.members = True
intents.message_content = True
bot = commands.Bot(command_prefix='.', intents=intents)
logging.basicConfig(filename='simulacra.log', encoding='utf-8', level=logging.WARNING)
if not os.path.exists("db.sqlite"):
db = sqlite3.connect('db.sqlite')
cursor = db.cursor()
cursor.execute('''CREATE TABLE users
(id INTEGER PRIMARY KEY, admin INTEGER, banned INTEGER,
verified INTEGER, name TEXT)''')
cursor.execute('''CREATE TABLE survey
(uid INTEGER, qid INTEGER, rating INTEGER,
FOREIGN KEY(uid) REFERENCES users(id)
PRIMARY KEY(uid, qid))''')
# cursor.execute("INSERT INTO users VALUES (621583764174143488, 1, 0, 1)")
cursor.execute('''CREATE TABLE generations
(id INTEGER PRIMARY KEY, uid INTEGER, mid INTEGER,
method INTEGER, prompt TEXT,
FOREIGN KEY(uid) REFERENCES users(id))''')
cursor.execute('''CREATE TABLE images
(id INTEGER PRIMARY KEY AUTOINCREMENT,
gid INTEGER,
idx INTEGER,
FOREIGN KEY(gid) REFERENCES generations(id),
UNIQUE(gid,idx))''')
cursor.execute('''CREATE TABLE ratings
(uid INTEGER, iid INTEGER, rating INTEGER,
FOREIGN KEY(uid) REFERENCES users(id),
FOREIGN KEY(iid) REFERENCES images(id),
PRIMARY KEY(uid, iid))''')
cursor.execute('''CREATE TABLE flags
(uid INTEGER, iid INTEGER,
FOREIGN KEY(uid) REFERENCES users(id),
FOREIGN KEY(iid) REFERENCES images(id),
PRIMARY KEY(uid, iid))''')
cursor.execute('''CREATE TABLE upscales
(iid INTEGER, method INTEGER,
FOREIGN KEY(iid) REFERENCES images(id),
PRIMARY KEY(iid))''')
db.commit()
cursor.close()
class Users:
def __init__(self, dbpath):
self.dbpath = dbpath
def add_user(self, uid, name, admin=0, verified=1):
db = sqlite3.connect(self.dbpath)
cursor = db.cursor()
user = cursor.execute("INSERT INTO users VALUES (?,?,?,?,?)",
(uid, admin, 0, verified, name))
db.commit()
cursor.close()
db.close()
def is_user(self, uid):
db = sqlite3.connect(self.dbpath)
cursor = db.cursor()
user = cursor.execute("SELECT * FROM users WHERE id=?", (uid,)).fetchone()
survey = cursor.execute("SELECT COUNT(*) FROM survey WHERE uid=?", (uid,)).fetchone()[0]
if user and (survey >= 20):
return user
else:
return False
class Generations:
def __init__(self, dbpath):
self.dbpath = dbpath
db = sqlite3.connect(dbpath)
cursor = db.cursor()
try:
self._next_seed = cursor.execute('SELECT id FROM generations ORDER BY id DESC LIMIT 1').fetchone()[0] + 1
except TypeError:
self._next_seed = 0
def get_next_seed(self):
self._next_seed += 1
return self._next_seed - 1
def get_gen_from_mid(self, mid):
"""Get generation data from message ID."""
db = sqlite3.connect(self.dbpath)
cursor = db.cursor()
return cursor.execute("SELECT * FROM generations WHERE mid=?", (mid,)).fetchone()
class Ratings:
def __init__(self, dbpath):
self.dbpath = dbpath
def record_rating(self, uid: int, gid: int, rating: int, index: int = 1):
db = sqlite3.connect(self.dbpath)
cursor = db.cursor()
cursor.execute("SELECT id FROM images WHERE gid=? AND idx=?",
(gid, index))
image_id = cursor.fetchone()[0]
cursor.execute("INSERT INTO ratings VALUES (?,?,?)", (uid, image_id, rating))
db.commit()
db.close()
class Flags:
def __init__(self, dbpath):
self.dbpath = dbpath
def get_flags(self):
db = sqlite3.connect(self.dbpath)
cursor = db.cursor()
cursor.execute("SELECT * FROM flags;")
return cursor.fetchall()
def record_flag(self, uid: int, gid: int, index: int = 1):
db = sqlite3.connect(self.dbpath)
cursor = db.cursor()
cursor.execute("SELECT id FROM images WHERE gid=? AND idx=?",
(gid, index))
image_id = cursor.fetchone()[0]
cursor.execute("INSERT INTO flags VALUES (?,?)", (uid, image_id))
db.commit()
db.close()
def do_gen_job(messages, interaction, job):
gen(job)
messages.put((interaction,job))
def do_upscale_job(messages, interaction, job):
upscale(job)
messages.put((interaction,job))
class Jobs:
"""Job controller for SimulacraBot"""
def __init__(self, channels, dbpath='db.sqlite'):
self._gpu_table = [None for i in range(torch.cuda.device_count())]
self._pending = queue.Queue()
self._scheduled = {}
self._pending_messages = mp.Queue()
self._dbpath = dbpath
self._channels = channels
async def main(self):
last_rate_prompt = time.time()
while 1:
if not self._pending_messages.empty():
db = sqlite3.connect(self._dbpath)
cursor = db.cursor()
response = self._pending_messages.get()
if type(response[1]) == UpscaleJob:
try:
query, params = await self.finish_upscale_job(response[0],
response[1])
cursor.execute(query, params)
except sqlite3.IntegrityError as e:
logging.warning("Duplicate upscale occurred - {}".format(str(e)))
cursor.close()
db.close()
continue
# TODO: Change this class to 'GenJob' or some such
elif type(response[1]) == Job:
query, params = await self.finish_gen_job(response[0],
response[1])
cursor.execute(query, params)
for i in range(1,9):
cursor.execute("INSERT INTO images(gid, idx) VALUES (?,?)",
(params[0], i))
db.commit()
cursor.close()
db.close()
if (time.time() - last_rate_prompt) >= 120:
await self.rate_prompt()
last_rate_prompt = time.time()
if None not in self._gpu_table:
for job_time in enumerate(self._gpu_table):
# Clear out stuck job registers/timeout
i,t = job_time
if (time.time() - t) >= 120:
self._gpu_table[i] = None
time.sleep(0.25)
continue
# Job dispatch
try:
next_job = self._pending.get(block=False)
except:
time.sleep(0.25)
continue
device_index = self._gpu_table.index(None)
self._gpu_table[device_index] = time.time()
interaction, job = self._scheduled[next_job].pop()
job.device = str(device_index)
os.environ['CUDA_VISIBLE_DEVICES'] = job.device
if type(job) == UpscaleJob:
# Make values picklable
interaction = {"cid":interaction.channel.id,
"mention":interaction.user.mention}
upscale_job = mp.Process(
target=do_upscale_job,
args=(self._pending_messages,interaction,job)
)
upscale_job.start()
else:
# Make values picklable
interaction = {"cid":interaction.channel.id,
"mention":interaction.message.author.mention,
"uid":interaction.message.author.id}
gen_job = mp.Process(
target=do_gen_job,
args=(self._pending_messages,interaction,job)
)
gen_job.start()
async def rate_prompt(self):
db = sqlite3.connect('db.sqlite')
cursor = db.cursor()
# Try finding images with no rating
cursor.execute('''SELECT generations.id, images.id, prompt, images.idx, COUNT(rating) as num_ratings FROM
generations LEFT OUTER JOIN images ON images.gid=generations.id
LEFT OUTER JOIN ratings ON images.id=ratings.iid
GROUP BY images.id
HAVING num_ratings == 0;''')
to_rate = cursor.fetchall()
for cid in self._channels:
try:
gen = secrets.choice(to_rate)
except IndexError:
return
view = RatingButtons(gen[0], index=gen[3])
embed = nextcord.Embed(title="Feedback", description="")
cursor.execute("SELECT COUNT(*) from ratings where iid=?", (gen[1],))
ratings = cursor.fetchone()[0]
cursor.execute("SELECT COUNT(*) from flags where iid=?", (gen[1],))
flags = cursor.fetchone()[0]
embed.add_field(name="Ratings", value=ratings)
embed.add_field(name="Flags", value=flags)
upload = nextcord.File(str(gen[0]) + "_" + gen[2].replace(" ", "_").replace("/","_") +
"_" + str(gen[3]) + ".png")
channel = bot.get_channel(cid)
coroutine = channel.send(
gen[2],
file=upload,
embed=embed,
view=view)
asyncio.run_coroutine_threadsafe(coroutine, bot.loop).result()
def submit(self, interaction, job):
try:
uid = interaction.user.id
except AttributeError:
uid = interaction.message.author
try:
self._scheduled[uid]
except KeyError:
self._scheduled[uid] = []
if len(self._scheduled[uid]) < 2:
self._pending.put(uid)
self._scheduled[uid].append((interaction, job))
return True
else:
return False
async def finish_gen_job(self, interaction, job):
view = GenerationButtons()
embed = nextcord.Embed(title="Feedback", description="")
embed.add_field(name="Ratings", value=0)
embed.add_field(name="Flags", value=0)
upload = nextcord.File(str(job.seed) + "_" + job.prompt.replace(" ", "_").replace("/","_") +
"_1" + ".png")
channel = bot.get_channel(interaction["cid"])
coroutine = channel.send(
job.prompt + f" by {interaction['mention']}",
embed=embed,
file=upload, view=view)
response = asyncio.run_coroutine_threadsafe(coroutine, bot.loop).result()
self._gpu_table[int(job.device)] = None
return ("INSERT INTO generations VALUES (?, ?, ?, ?, ?)",
(job.seed, interaction["uid"], response.id, 2, job.prompt))
async def finish_upscale_job(self, interaction, job):
upload_path = job.input.replace(".png", "_4x_upscale.png")
upload = nextcord.File(upload_path)
channel = bot.get_channel(interaction["cid"])
coroutine = channel.send(
"'" + job.prompt + f"' upscaled by {interaction['mention']}",
file=upload
)
response = asyncio.run_coroutine_threadsafe(coroutine, bot.loop).result()
self._gpu_table[int(job.device)] = None
return ("INSERT INTO upscales VALUES (?,?)", (job.iid, 0))
@bot.event
async def on_ready():
print(f'We have logged in as {bot.user}')
class UpscaleJob:
def __init__(self, input, iid, checkpoint, eta, output, seed, steps):
self.input = input
self.iid = iid
self.checkpoint = checkpoint
self.eta = eta
self.output = output
self.seed = seed
self.steps = steps
class GridButtons(nextcord.ui.View):
def __init__(self, parent_msg):
super().__init__(timeout=None)
self.parent_msg = parent_msg
self.value = None
async def upscale(self, button, interaction, index):
gen = generations.get_gen_from_mid(self.parent_msg.id)
if interaction.user.id != gen[1]:
await interaction.user.send("Only the user that generated an output can upscale it.")
return
prompt = gen[-1]
img_path = str(gen[0]) + "_" + prompt.replace(" ", "_").replace("/","_") + "_" + str(index) + ".png"
db = sqlite3.connect('db.sqlite')
cursor = db.cursor()
cursor.execute("SELECT id FROM images WHERE gid=? AND idx=?",
(gen[0], index))
image_id = cursor.fetchone()[0]
assert image_id
cursor.close()
db.close()
job = UpscaleJob(input=img_path,
iid=image_id,
checkpoint="yfcc_upscaler_2.ckpt",
eta=1.,
output=img_path.replace(".png", "_4x_upscale.png"),
seed=gen[0],
steps=150)
job.gen = gen
job.prompt = prompt
button.label = "Submitted"
button.style = nextcord.ButtonStyle.green
button.disabled = True
await interaction.message.edit(view=self)
jobs.submit(interaction, job)
@nextcord.ui.button(label="U1", custom_id="U1")
async def U1(self, button, interaction):
await self.upscale(button, interaction, 1)
@nextcord.ui.button(label="U2", custom_id="U2")
async def U2(self, button, interaction):
await self.upscale(button, interaction, 2)
@nextcord.ui.button(label="U3", custom_id="U3")
async def U3(self, button, interaction):
await self.upscale(button, interaction, 3)
@nextcord.ui.button(label="U4", custom_id="U4")
async def U4(self, button, interaction):
await self.upscale(button, interaction, 4)
@nextcord.ui.button(label="U5", custom_id="U5")
async def U5(self, button, interaction):
await self.upscale(button, interaction, 5)
@nextcord.ui.button(label="U6", custom_id="U6")
async def U6(self, button, interaction):
await self.upscale(button, interaction, 6)
@nextcord.ui.button(label="U7", custom_id="U7")
async def U7(self, button, interaction):
await self.upscale(button, interaction, 7)
@nextcord.ui.button(label="U8", custom_id="U8")
async def U8(self, button, interaction):
await self.upscale(button, interaction, 8)
class AbstractButtons(nextcord.ui.View):
def __init__(self):
super().__init__(timeout=None)
self.value = None
async def rate(self, interaction, rating):
# TODO: Let users update their rating
if not users.is_user(interaction.user.id):
await interaction.user.send("You must agree to the TOS before using SimulacraBot. Type .signup")
return
message = interaction.message
generation = generations.get_gen_from_mid(message.id)
ratings.record_rating(interaction.user.id, generation[0], rating)
# Update number of ratings
num_ratings = int(message.embeds[0].fields[0].value)
num_ratings += 1
if self.children[15].style != nextcord.ButtonStyle.green:
self.children[15].disabled = False
embed = message.embeds[0].set_field_at(0, name="Ratings",
value=num_ratings)
await message.edit(view=self, embed=embed)
@nextcord.ui.button(label="Worst (1)", custom_id="R1")
async def R1(self, button, interaction):
await self.rate(interaction, 1)
@nextcord.ui.button(label="2", custom_id="R2")
async def R2(self, button, interaction):
await self.rate(interaction, 2)
@nextcord.ui.button(label="3", custom_id="R3")
async def R3(self, button, interaction):
await self.rate(interaction, 3)
@nextcord.ui.button(label="4", custom_id="R4")
async def R4(self, button, interaction):
await self.rate(interaction, 4)
@nextcord.ui.button(label="5", custom_id="R5")
async def R5(self, button, interaction):
await self.rate(interaction, 5)
@nextcord.ui.button(label="6", custom_id="R6")
async def R6(self, button, interaction):
await self.rate(interaction, 6)
@nextcord.ui.button(label="7", custom_id="R7")
async def R7(self, button, interaction):
await self.rate(interaction, 7)
@nextcord.ui.button(label="8", custom_id="R8")
async def R8(self, button, interaction):
await self.rate(interaction, 8)
@nextcord.ui.button(label="9", custom_id="R9")
async def R9(self, button, interaction):
await self.rate(interaction, 9)
@nextcord.ui.button(label="Best (10)", custom_id="R10")
async def R10(self, button, interaction):
await self.rate(interaction, 10)
@nextcord.ui.button(label="---", disabled=True, row=3)
async def filler1(self, button, interaction):
pass
@nextcord.ui.button(label="---", disabled=True, row=3)
async def filler2(self, button, interaction):
pass
@nextcord.ui.button(label="---", disabled=True, row=3)
async def filler3(self, button, interaction):
pass
@nextcord.ui.button(label="---", disabled=True, row=3)
async def filler4(self, button, interaction):
pass
@nextcord.ui.button(label="---", disabled=True, row=3)
async def filler5(self, button, interaction):
pass
@nextcord.ui.button(label="Grid", custom_id="grid", disabled=True, row=4)
async def grid(self, button, interaction):
gen = generations.get_gen_from_mid(interaction.message.id)
if interaction.user.id != gen[1]:
await interaction.user.send("Only the user that generated an output can get its grid.")
return
prompt = gen[-1]
img_path = str(gen[0]) + "_" + prompt.replace(" ", "_").replace("/","_") + "_grid" + ".png"
upload = nextcord.File(img_path)
view = GridButtons(interaction.message)
await interaction.send(
"'" + prompt + f"' full grid",
file=upload,
view=view
)
button.style = nextcord.ButtonStyle.green
button.disabled = True
await interaction.message.edit(view=self)
@nextcord.ui.button(label="------", custom_id="spacer2", disabled=True,
style=nextcord.ButtonStyle.gray, row=4)
async def spacer2(self, button, interaction):
pass
@nextcord.ui.button(label="------", custom_id="spacer3", disabled=True,
style=nextcord.ButtonStyle.gray, row=4)
async def spacer3(self, button, interaction):
pass
class GenerationButtons(AbstractButtons):
def __init__(self):
super().__init__()
@nextcord.ui.button(label="Flag", custom_id="flag", style=nextcord.ButtonStyle.danger, row=4)
async def flag(self, button, interaction):
if not users.is_user(interaction.user.id):
await interaction.user.send("You must agree to the TOS before using SimulacraBot. Type .signup")
return
message = interaction.message
generation = generations.get_gen_from_mid(message.id)
flags.record_flag(interaction.user.id, generation[0])
# Update number of flags
num_flags = int(message.embeds[0].fields[1].value)
num_flags += 1
embed = message.embeds[0].set_field_at(1, name="Flags",
value=num_flags)
await message.edit(embed=embed)
class RatingButtons(AbstractButtons):
def __init__(self, gid, index: int = 1):
super().__init__()
db = sqlite3.connect('db.sqlite')
cursor = db.cursor()
cursor.execute("select * from generations where id=?", (gid,))
self.generation = cursor.fetchone()
self.index = index
cursor.close()
db.close()
async def rate(self, interaction, rating):
# TODO: Let users update their rating
if not users.is_user(interaction.user.id):
await interaction.user.send("You must agree to the TOS before using SimulacraBot. Type .signup")
return
message = interaction.message
generation = self.generation
ratings.record_rating(interaction.user.id, generation[0], rating, index = self.index)
# Update number of ratings
num_ratings = int(message.embeds[0].fields[0].value)
num_ratings += 1
embed = message.embeds[0].set_field_at(0, name="Ratings",
value=num_ratings)
await message.edit(view=self, embed=embed)
@nextcord.ui.button(label="Flag", custom_id="flag", style=nextcord.ButtonStyle.danger, row=4)
async def flag(self, button, interaction):
if not users.is_user(interaction.user.id):
await interaction.user.send("You must agree to the TOS before using SimulacraBot. Type .signup")
return
message = interaction.message
generation = self.generation
flags.record_flag(interaction.user.id, generation[0], index = self.index)
# Update number of flags
num_flags = int(message.embeds[0].fields[1].value)
num_flags += 1
embed = message.embeds[0].set_field_at(1, name="Flags",
value=num_flags)
await message.edit(embed=embed)
class StreamRatingButtons(AbstractButtons):
def __init__(self):
super().__init__()
gen, ratings, flags = self.get_next_image_to_rate()
gid = gen[0]
db = sqlite3.connect('db.sqlite')
cursor = db.cursor()
cursor.execute("select * from generations where id=?", (gid,))
self.generation = cursor.fetchone()
self.index = gen[3]
cursor.close()
db.close()
def get_next_image_to_rate(self):
db = sqlite3.connect('db.sqlite')
cursor = db.cursor()
# Try finding images with no rating
cursor.execute('''SELECT generations.id, images.id, prompt, images.idx, COUNT(rating) as num_ratings FROM
generations LEFT OUTER JOIN images ON images.gid=generations.id
LEFT OUTER JOIN ratings ON images.id=ratings.iid
GROUP BY images.id
HAVING num_ratings == 0;''')
gen = cursor.fetchone()
cursor.execute("SELECT COUNT(*) from ratings where iid=?", (gen[1],))
ratings = cursor.fetchone()[0]
cursor.execute("SELECT COUNT(*) from flags where iid=?", (gen[1],))
flags = cursor.fetchone()[0]
cursor.close()
db.close()
return gen, ratings, flags
async def rate(self, interaction, rating):
# TODO: Let users update their rating
if not users.is_user(interaction.user.id):
await interaction.user.send("You must agree to the TOS before using SimulacraBot. Type .signup")
return
message = interaction.message
generation = self.generation
ratings.record_rating(interaction.user.id, generation[0], rating, index = self.index)
# Update number of ratings
num_ratings = int(message.embeds[0].fields[0].value)
num_ratings += 1
embed = message.embeds[0].set_field_at(0, name="Ratings",
value=num_ratings)
await message.edit(view=self, embed=embed)
# Send next message in the chain
gen, ratings_count, flags = self.get_next_image_to_rate()
db = sqlite3.connect('db.sqlite')
cursor = db.cursor()
cursor.execute("select * from generations where id=?", (gen[0],))
self.generation = cursor.fetchone()
self.index = gen[3]
cursor.close()
db.close()
embed = nextcord.Embed(title="Feedback", description="")
embed.add_field(name="Ratings", value=ratings_count)
embed.add_field(name="Flags", value=flags)
upload = nextcord.File(str(gen[0]) + "_" + gen[2].replace(" ", "_").replace("/","_") +
"_" + str(gen[3]) + ".png")
await interaction.user.send(
gen[2],
file=upload,
embed=embed,
view=self)
@nextcord.ui.button(label="Flag", custom_id="flag", style=nextcord.ButtonStyle.danger, row=4)
async def flag(self, button, interaction):
if not users.is_user(interaction.user.id):
await interaction.user.send("You must agree to the TOS before using SimulacraBot. Type .signup")
return
message = interaction.message
generation = self.generation
flags.record_flag(interaction.user.id, generation[0], index = self.index)
# Update number of flags
num_flags = int(message.embeds[0].fields[1].value)
num_flags += 1
embed = message.embeds[0].set_field_at(1, name="Flags",
value=num_flags)
await message.edit(embed=embed)
class WarningSelect(nextcord.ui.View):
def __init__(self, generation, recipient):
super().__init__(timeout=None)
self.value = None
self.generation = generation
self.recipient = recipient
@nextcord.ui.select(placeholder="Why is this gen being removed?",
custom_id="warnselect",
options=[
nextcord.SelectOption(label="NSFW",
value="was NSFW"),
nextcord.SelectOption(label="Hateful",
value="was hateful"),
nextcord.SelectOption(label="Personal Information",
value="included personal information"),
nextcord.SelectOption(label="Copyright/Trademarks",
value="violated copyright"),
])
async def warning(self, select, interaction):
await self.recipient.send(f"Your generation '{self.generation[-1]}' was "
f"removed from Simulacra Aesthetic captions because it {select.values[0]}.")
class ModerationButtons(nextcord.ui.View):
def __init__(self, gid):
super().__init__(timeout=None)
self.value = None
db = sqlite3.connect('db.sqlite')
cursor = db.cursor()
cursor.execute("SELECT * FROM generations WHERE id=?", (gid,))
self.generation = cursor.fetchone()
cursor.close()
db.close()
@nextcord.ui.button(label="OK", custom_id="ok")
async def ok(self, button, interaction):
db = sqlite3.connect('db.sqlite')
cursor = db.cursor()
cursor.execute("""DELETE FROM flags WHERE flags.iid IN
(SELECT id FROM images WHERE gid=?)""",
(self.generation[0],))
db.commit()
cursor.close()
db.close()
@nextcord.ui.button(label="Warn", custom_id="warn")
async def warn(self, button, interaction):
"""Send a user a notification that their submission was unacceptable."""
user = await bot.fetch_user(self.generation[1])
view = WarningSelect(self.generation, user)
await interaction.user.send("",
view=view)
@nextcord.ui.button(label="Purge", custom_id="purge")
async def purge(self, button, interaction):
"""Purge a submission from the database."""
db = sqlite3.connect('db.sqlite')
cursor = db.cursor()
cursor.execute("SELECT idx FROM images WHERE gid=?", (self.generation[0],))
batch_indexes = [x[0] for x in cursor.fetchall()]
for index in batch_indexes:
prompt = self.generation[-1]
img_path = str(self.generation[0]) + "_" + prompt.replace(" ", "_").replace("/","_") + "_" + str(index) + ".png"
os.remove(img_path)
try:
os.remove(img_path.replace(".png", "_4x_upscale.png"))
except FileNotFoundError:
continue
prompt = self.generation[-1]
img_path = str(self.generation[0]) + "_" + prompt.replace(" ", "_").replace("/","_") + "_grid" + ".png"
os.remove(img_path)
cursor.execute("""DELETE FROM upscales WHERE upscales.iid IN
(SELECT id FROM images WHERE gid=?)""",
(self.generation[0],))
cursor.execute("""DELETE FROM ratings WHERE ratings.iid IN
(SELECT id FROM images WHERE gid=?)""",
(self.generation[0],))
cursor.execute("""DELETE FROM flags WHERE flags.iid IN
(SELECT id FROM images WHERE gid=?)""",
(self.generation[0],))
cursor.execute("DELETE FROM images WHERE gid=?", (self.generation[0],))
cursor.execute("DELETE FROM generations WHERE id=?",
(self.generation[0],))
db.commit()
cursor.close()
db.close()
await interaction.user.send("Purged!")
@nextcord.ui.button(label="Ban", custom_id="ban")
async def ban(self, button, interaction):
"""Ban a user for submitting a prompt."""
pass
class RatingSurvey(AbstractButtons):
def __init__(self):
super().__init__()
self.survey_images = [
("The West Coastline. #west #ocean", "survey/6042_The_West_Coastline._west_ocean_6.png"),
("hyperrealistic bokeh portrait cinematic matte painting concept art "
"african american New Orleans",
"survey/7413_hyperrealistic_bokeh_portrait_cinematic_matte_painting"
"_concept_art_african_american_New_Orleans_1.png"),
("A painting of a swamp at low tide. There is a small portion of a "
"castle and a patch of blue sky in the far off distance.",
"survey/4904_A_painting_of_a_swamp_at_low_tide._There_is_a_small_portion_of_"
"a_castle_and_a_patch_of_blue_sky_in_the_far_off_distance._3.png"),
("fantasy illustration of a river of blood, flowing through a field of bones.",
"survey/549_fantasy_illustration_of_a_river_of_blood_flowing_through_a_field_of_bones._6.png"),
("full color inkscape digital art of a hackers lair bedroom computer setup battlestation",
"survey/1279_full_color_inkscape_digital_art_of_a_hackers_lair_bedroom_computer_setup_battlestation_5.png"),
("traditional dark Hijab Muslim Woman Islam Girl Portrait Fashion "
"People Model Eyes oil on canvas",
"survey/7435_traditional_dark_Hijab_Muslim_Woman_Islam_Girl_Portrait"
"_Fashion_People_Model_Eyes_oil_on_canvas_3.png"),
("accurate beautiful cute anime symmetric danbooru chibi depiction of a magical girl",
"survey/6641_accurate_beautiful_cute_anime_symmetric_danbooru_chibi_depiction_of_a_magical_girl_2.png"),
("A portrait painting of Adolf Hitler",
"survey/Der_Führer_Gemälde_Portrait_painting_of_Adolf_Hitler_by_Heinrich_Knirr"
"_1937_No_known_copyright_restrictions_(artist_died_in_1944)_Imperial_War_Museum_London.png"),
("detailed accurate krita digital masterpiece commissioned street "
"scene of a cyberpunk Tokyo market stall, artstation matte painting",
"survey/6625_detailed_accurate_krita_digital_masterpiece_commissioned_street_"
"scene_of_a_cyberpunk_Tokyo_market_stall_artstation_matte_painting_1.png"),
("professional HDR flambient golden hour real estate photography of"
"futuristic hyperrealism scifi fantasy industrial corporate agents",
"survey/3690_professional_HDR_flambient_golden_hour_real_estate_photography_of"
"_futuristic_hyperrealism_scifi_fantasy_industrial_corporate_agents_5.png"),
("The colors in this illustration are simply gorgeous! They perfectly "
"compliment the scene and add an extra layer of beauty to the already stunning image!",
"survey/6424_The_colors_in_this_illustration_are_simply_gorgeous_They_perfectly"
"_compliment_the_scene_and_add_an_extra_layer_of_beauty_to_the_already_stunning_image_6.png"),
("hyperrealism chiaroscuro cinematic oil on canvas matte painting of "
"professional golden hour flambient real estate photo scifi traditional "
"japanese onsen shinto temple zen garden",
"survey/3647_hyperrealism_chiaroscuro_cinematic_oil_on_canvas_matte_painting"
"_of_professional_golden_hour_flambient_real_estate_photo_scifi_"
"traditional_japanese_onsen_shinto_temple_zen_garden_4.png"),
("I found this abstract art and don't know what it means",
"survey/3226_I_found_this_abstract_art_and_dont_know_what_it_means_4.png"),
("A painting of a mystical creature in a magical forest",
"survey/585_A_painting_of_a_mystical_creature_in_a_magical_forest_2.png"),
("digital painting of a mansion in the middle of a forest, 4k, Ross "
"Tran, Gurney, Frank Frazetta, Skeeva, Gal Barkan, matayosi, high "
"fantasy concept key art",
"survey/4220_digital_painting_of_a_mansion_in_the_middle_of_a_forest_4k_Ross"
"_Tran_Gurney_Frank_Frazetta_Skeeva_Gal_Barkan_matayosi_high_fantasy_"
"concept_key_art_6.png"),
("professional HDR flambient wide angle bokeh photography of a cute "
"pink glittery magical fairy enchanted forest fantasy butterflies and poodles",
"survey/6609_professional_HDR_flambient_wide_angle_bokeh_photography_of_a_"
"cute_pink_glittery_magical_fairy_enchanted_forest_fantasy_butterflies_and_poodles_1.png"),
("chiaroscuro professional oil on canvas matte painting concept art "
"of a masculine warrior from myths and legends",
"survey/6548_chiaroscuro_professional_oil_on_canvas_matte_painting_"
"concept_art_of_a_masculine_warrior_from_myths_and_legends_2.png"),
("A nightmarish creature made of shadows and teeth.",
"survey/1039_A_nightmarish_creature_made_of_shadows_and_teeth._1.png"),
("A crowded subway car, the fluorescent lights flickering and casting"
" an eerie glow on the people inside.",
"survey/5087_A_crowded_subway_car_the_fluorescent_lights_flickering_and_"
"casting_an_eerie_glow_on_the_people_inside._2.png"),
("krita digital masterpiece of the tower of babel as a series of neural net layers",
"survey/6532_krita_digital_masterpiece_of_the_tower_of_babel_as_a_series_of_neural_net_layers_3.png")
]
def get_next_image_for_user(self, user):
# We need to always get the correct next image for the user even if they
# partially complete the survey and then abandon it between bot reboots.
# This way it's not possible for a user to get stuck in the signup process.
db = sqlite3.connect('db.sqlite')
cursor = db.cursor()
cursor.execute("SELECT COUNT(*) FROM survey WHERE uid=?",
(user.id,))
index = cursor.fetchone()[0]
cursor.close()
db.close()
return index
async def rate(self, interaction, rating):
index = self.get_next_image_for_user(interaction.user)
db = sqlite3.connect('db.sqlite')
cursor = db.cursor()
cursor.execute("INSERT INTO survey VALUES (?,?,?)",
(interaction.user.id, index, rating))
db.commit()
cursor.close()
db.close()
index = self.get_next_image_for_user(interaction.user)
if index < 20:
prompt, path = self.survey_images[index]
upload = nextcord.File(path)
await interaction.user.send(f"{index + 1}. " + prompt,
file=upload,
view=self)
else:
db = sqlite3.connect('db.sqlite')
cursor = db.cursor()
cursor.execute("INSERT INTO users VALUES (?,?,?,?,?)",
(interaction.user.id, 0, 0, 0, "deprecated"))
db.commit()
cursor.close()
db.close()
await interaction.user.send("The survey is finished! You can now use the bot.")
await interaction.user.send("You can do so by typing `.add <prompt>`"
" into the channel you first interacted with the bot in.")
class AgreementSelect(nextcord.ui.View):
def __init__(self):
super().__init__()
self.value = None
@nextcord.ui.select(placeholder="Do you agree to these terms of service?",
custom_id="tosagreement",
options=[
nextcord.SelectOption(label="Yes, and understand all work with SimulacraBot is public domain",
value=True),
nextcord.SelectOption(label="No",
value=False),
])
async def agreement(self, select, interaction):
if select.values[0]:
view = RatingSurvey()
index = view.get_next_image_for_user(interaction.user)
prompt, path = view.survey_images[index]
upload = nextcord.File(path)
await interaction.user.send("Before you can use the bot you need to"
" answer a quick 20 question rating task"
" to characterize your aesthetic preferences"
" and rating habits for dataset users.")
await interaction.user.send("You may rate the images however you like"
", we try to keep it as subjective as possible."
" However there are two guidelines we would "
"appreciate you following:\n\n"
"1. Rate images lower if they contain "
"watermarks, logos, or other marks that "
"intrude on the canvas. If an image has "
"a disfiguring watermark across the center "
"it should be no higher than a 3, if it "
"includes a more subtle mark it should be "
"no higher than a 6. Paintings shown on a "
"wall, gallery, etc which encroaches on the "
"256x256 canvas should rate no higher than a 5.\n\n"
"2. As much as possible rate images on "
"the merits of the image, not the prompt "
"fit. Sometimes the AI produces good looking "
"but poorly fitting images, these should be "
"rated highly because they look good.\n\n")
await interaction.user.send(f"{index + 1}. " + prompt,
file=upload,
view=view)
class Job:
def __init__(self, prompt, cloob_checkpoint, scale, cutn, device, ddim_eta,
method, H, W, n_iter, n_samples, seed, ddim_steps, plms):
self.prompt = prompt
self.cloob_checkpoint = cloob_checkpoint
self.scale = scale
self.cutn = cutn
self.device = device
self.ddim_eta = ddim_eta
self.method = method
self.H = H
self.W = W
self.n_iter = n_iter
self.n_samples = n_samples
self.seed = seed
self.ddim_steps = ddim_steps
self.plms = plms
#@bot.slash_command(description="My first slash command", guild_ids=[TESTING_GUILD_ID])
@bot.command()
async def add(interaction: nextcord.Interaction):
if not users.is_user(interaction.message.author.id):
await interaction.message.author.send("You must agree to the TOS before using SimulacraBot. Type .signup")
return
if interaction.channel.id not in channel_whitelist:
return
for word in banned_words:
if (' ' + word) in interaction.message.content:
await interaction.send("'{}' is not an allowed word by the NSFW filter".format(word))
return
prompt = interaction.message.content.split(".add")[1].strip()
if len(prompt) > 255:
await interaction.send("The length of the prompt wouldn't fit on the "
"filesystem. Please shorten it and try again.")
return
seed = generations.get_next_seed()
job = Job(prompt=prompt,
cloob_checkpoint='cloob_laion_400m_vit_b_16_16_epochs',
scale=5.,
cutn=32,
device='cuda:0',
ddim_eta=0.,
method='ddim',
H=256,
W=256,
n_iter=1,
n_samples=8,
seed=seed,
ddim_steps=50,
plms=True)
# Unobtrusively let user know we are generating
success = jobs.submit(interaction, job)
if success:
await interaction.message.add_reaction('👍')
await interaction.message.add_reaction('⏲️')
else:
await interaction.message.add_reaction('👎')
await interaction.message.add_reaction('2️⃣')
@bot.command()
async def rate(interaction: nextcord.Interaction):
if type(interaction.channel) != nextcord.channel.DMChannel:
return
# If all images rated find images with too few ratings
#if not to_rate:
# cursor.execute('''SELECT id, prompt, COUNT(rating) as num_ratings FROM
# images JOIN ratings ON images.id=ratings.iid
# WHERE images.id NOT IN
# (SELECT iid FROM ratings WHERE uid=?)
# GROUP BY images.id HAVING num_ratings < 3;''',
# (interaction.message.author.id,))
# to_rate = cursor.fetchall()[:5]
view = StreamRatingButtons()
gen, ratings, flags = view.get_next_image_to_rate()
embed = nextcord.Embed(title="Feedback", description="")
embed.add_field(name="Ratings", value=ratings)
embed.add_field(name="Flags", value=flags)
upload = nextcord.File(str(gen[0]) + "_" + gen[2].replace(" ", "_").replace("/","_") +
"_" + str(gen[3]) + ".png")
await interaction.channel.send(
gen[2],
file=upload,
embed=embed,
view=view)
@bot.command()
async def export(interaction: nextcord.Interaction):
if type(interaction.channel) != nextcord.channel.DMChannel:
return
else:
export = {}