Skip to content

Commit

Permalink
replacement filter implementation and documentation
Browse files Browse the repository at this point in the history
  • Loading branch information
ZeldaFan0225 committed Jun 8, 2023
1 parent bb0aa0a commit 1d6fdd9
Show file tree
Hide file tree
Showing 10 changed files with 24 additions and 43 deletions.
10 changes: 10 additions & 0 deletions changelog.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,15 @@
# Changelog

## V3.0.1

- add loras to generate and advanced_generate
- implement replacement filter
- add userinfo for horde ids

## V3.0.0

- update dependencies and require node 18 or above

## V2.4.1

- fixes to the kudos transfer by reacting
Expand Down
11 changes: 4 additions & 7 deletions config.md
Original file line number Diff line number Diff line change
Expand Up @@ -11,10 +11,6 @@ Here you can see an explanation of what which option does
"advanced": {
"dev": Whether this instance is for development or not (BOOLEAN) *4,
"encrypt_token": Encrypt the users token before saving in the database (BOOLEAN) *9
"pre_check_prompts_for_suspicion": {
"enabled": Check if the prompt the user gave is suspicious and would get your IP banned (BOOLEAN) *10,
"timeout_duration": The duration to put the user into timeout for
}
},
"filter_actions": {
"mode": The mode on how the guilds list should act ('whitelist' or 'blacklist'),
Expand Down Expand Up @@ -68,6 +64,7 @@ Here you can see an explanation of what which option does
"require_login": Set to true if you want the user to log in with their ai horde token (BOOLEAN),
"trusted_workers": Whether to only use trusted workers (BOOLEAN) *1,
"censor_nsfw": Whether to censor NSFW images if they were generated by accident (BOOLEAN) *1,
"replacement_filter": If enabled, suspicious prompts are sanitized through a string replacement filter instead. *1,
"blacklisted_models": A list of blacklisted models which users are not allowed to use (ARRAY OF STRING),
"blacklisted_words": A list of blacklisted words which users are not allowed to use (ARRAY OF STRING),
"update_generation_status_interval_seconds": The interval at which the embed in Discord gets updated (INTEGER),
Expand Down Expand Up @@ -150,12 +147,13 @@ Here you can see an explanation of what which option does
"require_login": Set to true if you want the user to log in with their ai horde token (BOOLEAN),
"trusted_workers": Whether to only use trusted workers (BOOLEAN) *1,
"censor_nsfw": Whether to censor NSFW images if they were generated by accident (BOOLEAN) *1,
"replacement_filter": If enabled, suspicious prompts are sanitized through a string replacement filter instead. *1,
"blacklisted_styles": A list of blacklisted styles which users are not allowed to use (ARRAY OF STRING),
"blacklisted_words": A list of blacklisted words which users are not allowed to use (ARRAY OF STRING),
"update_generation_status_interval_seconds": The interval at which the embed in Discord gets updated (INTEGER),
"convert_a1111_weight_to_horde_weight": Whether to convert a1111 to weighted prompt required by the api (BOOLEAN) *7 *8,
"improve_loading_time": Try to improve the displaying time between generation finished and generation displayed in discord (BOOLEAN) *7
"styles_source": The default source where to fetch the styles from (STRING) *11,
"styles_source": The default source where to fetch the styles from (STRING) *10,
"default": {
"tiling": Whether the result should be tileable if nothing is specified (BOOLEAN) *1,
"share:" The default for sharing the result (BOOLEAN) *1,
Expand Down Expand Up @@ -225,5 +223,4 @@ Here you can see an explanation of what which option does
`*7` EXPERIMENTAL OPTION, ENABLE AT YOUR OWN RISK
`*8` For example (((test))) will be automatically converted to (test:1.3)
`*9` Uses an encryption key only known to you to encrypt the users tokens. This is recommended to increase security. Enabling this option may have an impact on speed.
`*10` This Option requires a `OPERATOR_API_KEY` to be set in the `.env`
`*11` Must follow the scheme of https://github.com/db0/Stable-Horde-Styles/blob/main/styles.json
`*10` Must follow the scheme of https://github.com/db0/Stable-Horde-Styles/blob/main/styles.json
2 changes: 1 addition & 1 deletion package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "zeldafan_discord_bot",
"version": "3.0.0",
"version": "3.0.1",
"description": "",
"main": "dist/index.js",
"scripts": {
Expand Down
12 changes: 1 addition & 11 deletions src/commands/advanced_generate.ts
Original file line number Diff line number Diff line change
Expand Up @@ -333,17 +333,6 @@ export default class extends Command {
}
}

if(ctx.client.config.advanced?.pre_check_prompts_for_suspicion?.enabled) {
if(ctx.client.timeout_users.has(ctx.interaction.user.id)) return ctx.error({error: "Your previous prompt has been marked as suspicious.\nYou have been timed out, try again later."})
const filter_result = await ctx.ai_horde_manager.postFilters({
prompt
}, {token: process.env["OPERATOR_API_KEY"]}).catch(console.error)
if(filter_result && Number(filter_result.suspicion) >= 2) {
ctx.client.timeout_users.set(ctx.interaction.user.id, ctx.interaction.user.id, (ctx.client.config.advanced.pre_check_prompts_for_suspicion.timeout_duration ?? 1000 * 60 * 60))
}
if(ctx.client.timeout_users.has(ctx.interaction.user.id)) return ctx.error({error: "Your prompt has been marked as suspicious.\nTherefore you have been timed out!"})
}

const post_processing = [] as (typeof ModelGenerationInputPostProcessingTypes[keyof typeof ModelGenerationInputPostProcessingTypes])[]

if(gfpgan) post_processing.push(ModelGenerationInputPostProcessingTypes.GFPGAN)
Expand All @@ -366,6 +355,7 @@ export default class extends Command {
karras,
loras: lora_id ? [{name: lora_id}] : undefined
},
replacement_filter: ctx.client.config.advanced_generate.replacement_filter,
nsfw: ctx.client.config.advanced_generate?.user_restrictions?.allow_nsfw,
censor_nsfw: ctx.client.config.advanced_generate?.censor_nsfw,
trusted_workers: ctx.client.config.advanced_generate?.trusted_workers,
Expand Down
12 changes: 1 addition & 11 deletions src/commands/generate.ts
Original file line number Diff line number Diff line change
Expand Up @@ -200,17 +200,6 @@ export default class extends Command {
}
}

if(ctx.client.config.advanced?.pre_check_prompts_for_suspicion?.enabled) {
if(ctx.client.timeout_users.has(ctx.interaction.user.id)) return ctx.error({error: "Your previous prompt has been marked as suspicious.\nYou have been timed out, try again later."})
const filter_result = await ctx.ai_horde_manager.postFilters({
prompt
}, {token: process.env["OPERATOR_API_KEY"]}).catch(console.error)
if(filter_result && Number(filter_result.suspicion) >= 2) {
ctx.client.timeout_users.set(ctx.interaction.user.id, ctx.interaction.user.id, (ctx.client.config.advanced.pre_check_prompts_for_suspicion.timeout_duration ?? 1000 * 60 * 60))
}
if(ctx.client.timeout_users.has(ctx.interaction.user.id)) return ctx.error({error: "Your prompt has been marked as suspicious.\nTherefore you have been timed out!"})
}


const generation_data: ImageGenerationInput = {
prompt,
Expand All @@ -225,6 +214,7 @@ export default class extends Command {
loras: style.loras,
steps: style.steps
},
replacement_filter: ctx.client.config.generate.replacement_filter,
nsfw: ctx.client.config.generate?.user_restrictions?.allow_nsfw,
censor_nsfw: ctx.client.config.generate?.censor_nsfw,
trusted_workers: ctx.client.config.generate?.trusted_workers,
Expand Down
1 change: 0 additions & 1 deletion src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -83,7 +83,6 @@ client.on("ready", async () => {
await client.application?.commands.set([...client.commands.createPostBody(), ...client.contexts.createPostBody()]).catch(console.error)
if((client.config.advanced_generate?.user_restrictions?.amount?.max ?? 4) > 10) throw new Error("More than 10 images are not supported in the bot")
if(client.config.filter_actions?.guilds?.length && (client.config.filter_actions?.mode !== "whitelist" && client.config.filter_actions?.mode !== "blacklist")) throw new Error("The actions filter mode must be set to either whitelist, blacklist.")
if(client.config.advanced?.pre_check_prompts_for_suspicion?.enabled && !process.env["OPERATOR_API_KEY"]) throw new Error("The OPERATOR_API_KEY in the .env is required when pre checking prompts for being suspicious")
if(client.config.party?.enabled && !client.config.generate?.enabled) throw new Error("When party is enabled the /generate command also needs to be enabled")

if(client.config.party?.enabled && connection) {
Expand Down
6 changes: 2 additions & 4 deletions src/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -181,10 +181,6 @@ export interface Config {
advanced?: {
dev?: boolean,
encrypt_token?: boolean
pre_check_prompts_for_suspicion?: {
enabled: boolean,
timeout_duration?: number
}
},
filter_actions?: {
mode?: "whitelist" | "blacklist",
Expand Down Expand Up @@ -235,6 +231,7 @@ export interface Config {
require_login?: boolean,
trusted_workers?: boolean,
censor_nsfw?: boolean,
replacement_filter?: boolean,
workers?: string[],
blacklisted_words?: string[],
blacklisted_models?: string[],
Expand Down Expand Up @@ -323,6 +320,7 @@ export interface Config {
require_login?: boolean,
trusted_workers?: boolean,
censor_nsfw?: boolean,
replacement_filter?: boolean,
blacklisted_words?: string[],
blacklisted_styles?: string[],
update_generation_status_interval_seconds?: number,
Expand Down
8 changes: 3 additions & 5 deletions template.config.json
Original file line number Diff line number Diff line change
Expand Up @@ -9,11 +9,7 @@
],
"advanced": {
"dev": false,
"encrypt_token": true,
"pre_check_prompts_for_suspicion": {
"enabled": false,
"timeout_duration": 3600000
}
"encrypt_token": true
},
"filter_actions": {
"mode": "whitelist",
Expand Down Expand Up @@ -68,6 +64,7 @@
"require_login": true,
"trusted_workers": true,
"censor_nsfw": true,
"replacement_filter": true,
"blacklisted_models": [],
"blacklisted_words": [],
"update_generation_status_interval_seconds": 5,
Expand Down Expand Up @@ -155,6 +152,7 @@
"require_login": true,
"trusted_workers": true,
"censor_nsfw": true,
"replacement_filter": true,
"blacklisted_styles": [],
"blacklisted_words": [],
"update_generation_status_interval_seconds": 5,
Expand Down
3 changes: 1 addition & 2 deletions template.env
Original file line number Diff line number Diff line change
Expand Up @@ -4,5 +4,4 @@ DB_USERNAME= The Database user
DB_PASSWORD= The Database password
DB_IP= The database IP
DB_PORT= The database Port
ENCRYPTION_KEY= The key to encrypt user tokens with
OPERATOR_API_KEY= The API key of the user who operates the bot (optional, read config.md for more information)
ENCRYPTION_KEY= The key to encrypt user tokens with

0 comments on commit 1d6fdd9

Please sign in to comment.