Skip to content

Commit

Permalink
Implement spoiler functionality
Browse files Browse the repository at this point in the history
* channel-wide spoiler option available via /settings
* per /draw option
* force spoiler on role, option available via /settings
  • Loading branch information
FoxxMD committed Feb 28, 2024
1 parent 8ee436d commit 6f4f832
Show file tree
Hide file tree
Showing 6 changed files with 90 additions and 10 deletions.
3 changes: 3 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -57,6 +57,9 @@ To generate a prompt from a couple of words, use the /generate command and inclu
- Get Image Info - view information of an image generated by Stable Diffusion.
- Quick Upscale - upscale an image without needing to set options.
- Batch Download - download all images of a batch set without needing to specify batch_id and image_id
- mark image as spoiler
- Per image (on `/draw`)
- Set channel-wide default or force based on role with `/settings`
- [configuration file](https://github.com/Kilvoctu/aiyabot/wiki/Configuration) - can change some of AIYA's operational aspects.


Expand Down
3 changes: 2 additions & 1 deletion core/queuehandler.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
class DrawObject:
def __init__(self, cog, ctx, simple_prompt, prompt, negative_prompt, data_model, steps, width, height,
guidance_scale, sampler, seed, strength, init_image, batch, styles, facefix, highres_fix,
clip_skip, extra_net, epoch_time, view):
clip_skip, extra_net, spoiler, epoch_time, view):
self.cog = cog
self.ctx = ctx
self.simple_prompt = simple_prompt
Expand All @@ -27,6 +27,7 @@ def __init__(self, cog, ctx, simple_prompt, prompt, negative_prompt, data_model,
self.highres_fix = highres_fix
self.clip_skip = clip_skip
self.extra_net = extra_net
self.spoiler = spoiler
self.epoch_time = epoch_time
self.view = view

Expand Down
13 changes: 13 additions & 0 deletions core/settings.py
Original file line number Diff line number Diff line change
Expand Up @@ -82,6 +82,9 @@
batch = "1,1"
max_batch = "1,1"
upscaler_1 = "ESRGAN_4x"
spoiler = false
# role ID (not name)
spoiler_role = ""
"""


Expand Down Expand Up @@ -120,6 +123,8 @@ class GlobalVar:
prompt_ignore_list = []
display_ignored_words = "False"
negative_prompt_prefix = []
spoiler = False
spoiler_role = None


global_var = GlobalVar()
Expand Down Expand Up @@ -276,6 +281,8 @@ def generate_template(template_pop, config):
template_pop['batch'] = config['batch']
template_pop['max_batch'] = config['max_batch']
template_pop['upscaler_1'] = config['upscaler_1']
template_pop['spoiler'] = config['spoiler']
template_pop['spoiler_role'] = config['spoiler_role']
return template_pop


Expand All @@ -300,6 +307,12 @@ def read(channel_id):
with open(path + channel_id + '.json', 'w') as configfile2:
json.dump(settings, configfile2, indent=1)

if settings['spoiler_role'] is not None:
if str(settings['spoiler_role']).strip() == '':
settings['spoiler_role'] = None
else:
settings['spoiler_role'] = str(settings['spoiler_role'])

return settings


Expand Down
41 changes: 39 additions & 2 deletions core/settingscog.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import discord
from discord import option
from discord.ext import commands
from typing import Optional
from typing import Optional, List, Union

from core import settings

Expand Down Expand Up @@ -199,6 +199,24 @@ def size_autocomplete(self: discord.AutocompleteContext):
description='Use to update global lists (models, styles, embeddings, etc.)',
required=False,
)
@option(
'spoiler',
bool,
description='Mark images as spoilers (when not specified in /draw)',
required=False,
)
@option(
'spoiler_role',
discord.Role,
description='Force images from users in this role as spoilers',
required=False,
)
@option(
'remove_spoiler_role',
bool,
description='Remove assigned spoiler role',
required=False,
)
async def settings_handler(self, ctx,
current_settings: Optional[bool] = True,
n_prompt: Optional[str] = None,
Expand All @@ -218,7 +236,11 @@ async def settings_handler(self, ctx,
batch: Optional[str] = None,
max_batch: Optional[str] = None,
upscaler_1: Optional[str] = None,
refresh: Optional[bool] = False):
refresh: Optional[bool] = False,
spoiler: Optional[bool] = None,
spoiler_role: Optional[discord.Role] = None,
remove_spoiler_role: Optional[bool] = None
):
# get the channel id and check if a settings file exists
channel = '% s' % ctx.channel.id
settings.check(channel)
Expand All @@ -236,6 +258,8 @@ async def settings_handler(self, ctx,
for key, value in cur_set.items():
if key == 'negative_prompt':
pass
elif key == 'spoiler_role' and value is not None:
current += f'\n{key} - <@&{value}>'
else:
if value == '':
value = ' '
Expand Down Expand Up @@ -403,6 +427,19 @@ async def settings_handler(self, ctx,
new += f'\nbatch (count,size): ``{batch[0]},{batch[1]}``'
set_new = True

if spoiler is not None:
settings.update(channel, 'spoiler', spoiler)
new += f'\nDefault Spoiler: ``{spoiler}``'
set_new = True
if remove_spoiler_role is not None and remove_spoiler_role:
settings.update(channel, 'spoiler_role', None)
new += '\nRemoved Spoiler Role'
set_new = True
elif spoiler_role is not None:
settings.update(channel, 'spoiler_role', str(spoiler_role.id))
new += f'\n Spoiler Role: <@&{spoiler_role.id}>'
set_new = True

if set_new:
embed.add_field(name=f'New defaults', value=new, inline=False)
if new_n_prompt:
Expand Down
35 changes: 30 additions & 5 deletions core/stablecog.py
Original file line number Diff line number Diff line change
Expand Up @@ -152,6 +152,12 @@ async def on_ready(self):
description='The number of images to generate. Batch format: count,size',
required=False,
)
@option(
'spoiler',
bool,
description='Mark generated image as spoiler?',
required=False,
)
async def dream_handler(self, ctx: discord.ApplicationContext, *,
prompt: str, negative_prompt: str = None,
data_model: Optional[str] = None,
Expand All @@ -168,7 +174,8 @@ async def dream_handler(self, ctx: discord.ApplicationContext, *,
strength: Optional[str] = None,
init_image: Optional[discord.Attachment] = None,
init_url: Optional[str],
batch: Optional[str] = None):
batch: Optional[str] = None,
spoiler: Optional[bool] = None):

# update defaults with any new defaults from settingscog
channel = '% s' % ctx.channel.id
Expand Down Expand Up @@ -197,6 +204,16 @@ async def dream_handler(self, ctx: discord.ApplicationContext, *,
strength = settings.read(channel)['strength']
if batch is None:
batch = settings.read(channel)['batch']
if spoiler is None:
spoiler = settings.read(channel)['spoiler']

derived_spoiler = spoiler
spoiler_role = settings.read(channel)['spoiler_role']
if not derived_spoiler and spoiler_role is not None:
for role in ctx.author.roles:
if str(role.id) == spoiler_role:
derived_spoiler = True
break

# if a model is not selected, do nothing
model_name = 'Default'
Expand Down Expand Up @@ -234,9 +251,9 @@ async def dream_handler(self, ctx: discord.ApplicationContext, *,
prompt = settings.extra_net_defaults(prompt, channel)

if data_model != '':
print(f'Request -- {ctx.author.name}#{ctx.author.discriminator} -- Prompt: {prompt}')
print(f'Request -- {ctx.author.name}#{ctx.author.discriminator} -- Prompt: {prompt} -- Spoiler: {derived_spoiler}')
else:
print(f'Request -- {ctx.author.name}#{ctx.author.discriminator} -- Prompt: {prompt} -- Using model: {data_model}')
print(f'Request -- {ctx.author.name}#{ctx.author.discriminator} -- Prompt: {prompt} -- Using model: {data_model} -- Spoiler: {derived_spoiler}')

if seed == -1:
seed = random.randint(0, 0xFFFFFFFF)
Expand Down Expand Up @@ -322,13 +339,17 @@ async def dream_handler(self, ctx: discord.ApplicationContext, *,
reply_adds += f'\nFace restoration: ``{facefix}``'
if clip_skip != settings.read(channel)['clip_skip']:
reply_adds += f'\nCLIP skip: ``{clip_skip}``'


if derived_spoiler or derived_spoiler != settings.read(channel)['spoiler']:
bool_emoji = ':white_check_mark:' if derived_spoiler else ':negative_squared_cross_mark:'
reply_adds += f'\nSpoiler: {bool_emoji}'

epoch_time = int(time.time())

# set up tuple of parameters to pass into the Discord view
input_tuple = (
ctx, simple_prompt, prompt, negative_prompt, data_model, steps, width, height, guidance_scale, sampler, seed, strength,
init_image, batch, styles, facefix, highres_fix, clip_skip, extra_net, epoch_time)
init_image, batch, styles, facefix, highres_fix, clip_skip, extra_net, derived_spoiler, epoch_time)

view = viewhandler.DrawView(input_tuple)
# setup the queue
Expand Down Expand Up @@ -554,6 +575,8 @@ def dream(self, event_loop: queuehandler.GlobalQueue.event_loop, queue_object: q
id_start = current_grid * grid_count + 1
id_end = id_start + last_grid_count - 1
filename=f'{queue_object.seed}-{current_grid}.png'
if queue_object.spoiler:
filename=f'SPOILER_{queue_object.seed}-{count}.png'
file = add_metadata_to_image(grid,images[current_grid * 25][2], filename)
if current_grid == 0:
content = f'<@{queue_object.ctx.author.id}>, {message}\n Batch ID: {epoch_time}-{queue_object.seed}\n Image IDs: {id_start}-{id_end}'
Expand All @@ -570,6 +593,8 @@ def dream(self, event_loop: queuehandler.GlobalQueue.event_loop, queue_object: q
else:
content = f'<@{queue_object.ctx.author.id}>, {message}'
filename=f'{queue_object.seed}-{count}.png'
if queue_object.spoiler:
filename=f'SPOILER_{queue_object.seed}-{count}.png'
file = add_metadata_to_image(image,str_parameters, filename)
queuehandler.process_post(
self, queuehandler.PostObject(
Expand Down
5 changes: 3 additions & 2 deletions core/viewhandler.py
Original file line number Diff line number Diff line change
Expand Up @@ -32,11 +32,12 @@
[16] = highres_fix
[17] = clip_skip
[18] = extra_net
[19] = epoch_time
[19] = spoiler
[20] = epoch_time
'''
tuple_names = ['ctx', 'simple_prompt', 'prompt', 'negative_prompt', 'data_model', 'steps', 'width', 'height',
'guidance_scale', 'sampler', 'seed', 'strength', 'init_image', 'batch', 'styles', 'facefix',
'highres_fix', 'clip_skip', 'extra_net', 'epoch_time']
'highres_fix', 'clip_skip', 'extra_net', 'spoiler', 'epoch_time']


# the modal that is used for the 🖋 button
Expand Down

1 comment on commit 6f4f832

@JoshBaldwin101
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'm finding that the upscale dropdown no longer works in this commit.

Steps to reproduce:

  • Generate an image with a batch size of 2,1 or greater (total of >1 images).
  • Try to upscale either image.
  • Observe that an error is given.

issue

Please sign in to comment.