Skip to content

Commit

Permalink
assign image, img, i. to dalle command + add sdimage sdimages sdimg c…
Browse files Browse the repository at this point in the history
…ommands/prefix
  • Loading branch information
fegloff committed Jan 9, 2024
1 parent f672772 commit 8b70ba0
Show file tree
Hide file tree
Showing 4 changed files with 39 additions and 19 deletions.
5 changes: 1 addition & 4 deletions src/config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@ export default {
model: 'chat-bison',
minimumBalance: 0,
isEnabled: Boolean(parseInt(process.env.LLMS_ENABLED ?? '1')),
prefixes: { bardPrefix: [',', 'b.', 'B.'] },
prefixes: { bardPrefix: ['b.', 'B.'] },
pdfUrl: process.env.PDF_URL ?? '',
processingTime: 300000
},
Expand Down Expand Up @@ -84,9 +84,6 @@ export default {
chatPrefix: process.env.ASK_PREFIX
? process.env.ASK_PREFIX.split(',')
: ['a.', '.'], // , "?", ">",
dallePrefix: process.env.DALLE_PREFIX
? process.env.DALLE_PREFIX.split(',')
: ['d.'],
newPrefix: process.env.NEW_PREFIX
? process.env.NEW_PREFIX.split(',')
: ['n.', '..'],
Expand Down
8 changes: 5 additions & 3 deletions src/modules/open-ai/helpers.ts
Original file line number Diff line number Diff line change
Expand Up @@ -16,15 +16,17 @@ export const SupportedCommands = {
ask32: { name: 'ask32' },
gpt: { name: 'gpt' },
last: { name: 'last' },
dalle: { name: 'DALLE' },
dalleLC: { name: 'dalle' },
dalle: { name: 'image' },
dalleShort: { name: 'img' },
genImgEn: { name: 'genImgEn' },
on: { name: 'on' },
off: { name: 'off' }
}

export const MAX_TRIES = 3

const DALLE_PREFIX_LIST = ['i.', ', ', 'image ', 'd.', 'img ']

export const isMentioned = (
ctx: OnMessageContext | OnCallBackQueryData
): boolean => {
Expand Down Expand Up @@ -52,7 +54,7 @@ export const hasChatPrefix = (prompt: string): string => {
}

export const hasDallePrefix = (prompt: string): string => {
const prefixList = config.openAi.chatGpt.prefixes.dallePrefix
const prefixList = DALLE_PREFIX_LIST
for (let i = 0; i < prefixList.length; i++) {
if (prompt.toLocaleLowerCase().startsWith(prefixList[i])) {
return prefixList[i]
Expand Down
33 changes: 27 additions & 6 deletions src/modules/open-ai/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@ import {
getMessageExtras,
getPromptPrice,
hasChatPrefix,
hasDallePrefix,
hasNewPrefix,
hasPrefix,
hasUrl,
Expand Down Expand Up @@ -101,7 +102,7 @@ export class OpenAIBot implements PayableBot {
}
if (
ctx.hasCommand(SupportedCommands.dalle.name) ||
ctx.hasCommand(SupportedCommands.dalleLC.name)
ctx.hasCommand(SupportedCommands.dalleShort.name)
) {
const imageNumber = ctx.session.openAi.imageGen.numImages
const imageSize = ctx.session.openAi.imageGen.imgSize
Expand Down Expand Up @@ -226,9 +227,8 @@ export class OpenAIBot implements PayableBot {
}

if (
ctx.hasCommand(SupportedCommands.dalle.name) ||
ctx.hasCommand(SupportedCommands.dalleLC.name) ||
(ctx.message?.text?.startsWith('dalle ') && ctx.chat?.type === 'private')
ctx.hasCommand([SupportedCommands.dalle.name, SupportedCommands.dalleShort.name]) ||
(ctx.message?.text?.startsWith('image ') && ctx.chat?.type === 'private')
) {
let prompt = (ctx.match ? ctx.match : ctx.message?.text) as string
if (!prompt || prompt.split(' ').length === 1) {
Expand Down Expand Up @@ -257,13 +257,34 @@ export class OpenAIBot implements PayableBot {
return
}

if (hasNewPrefix(ctx.message?.text ?? '') !== '') {
const text = ctx.message?.text ?? ''

if (hasNewPrefix(text) !== '') {
await this.onEnd(ctx)
await this.onPrefix(ctx)
return
}

if (hasChatPrefix(ctx.message?.text ?? '') !== '') {
if (hasDallePrefix(text) !== '') {
const prefix = hasDallePrefix(text)
let prompt = (ctx.match ? ctx.match : ctx.message?.text) as string
if (!prompt || prompt.split(' ').length === 1) {
prompt = config.openAi.dalle.defaultPrompt
}
ctx.session.openAi.imageGen.imgRequestQueue.push({
command: 'dalle',
prompt: prompt.slice(prefix.length)
})
if (!ctx.session.openAi.imageGen.isProcessingQueue) {
ctx.session.openAi.imageGen.isProcessingQueue = true
await this.onImgRequestHandler(ctx).then(() => {
ctx.session.openAi.imageGen.isProcessingQueue = false
})
}
return
}

if (hasChatPrefix(text) !== '') {
await this.onPrefix(ctx)
return
}
Expand Down
12 changes: 6 additions & 6 deletions src/modules/sd-images/helpers.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,9 +5,9 @@ import { getLoraByParam, type ILora } from './api/loras-config'
import { childrenWords, sexWords } from './words-blacklist'

export enum COMMAND {
TEXT_TO_IMAGE = 'image',
TEXT_TO_IMAGE = 'sdimage',
IMAGE_TO_IMAGE = 'img2img',
TEXT_TO_IMAGES = 'images',
TEXT_TO_IMAGES = 'sdimages',
CONSTRUCTOR = 'constructor',
HELP = 'help',
TRAIN = 'train'
Expand Down Expand Up @@ -38,7 +38,7 @@ const removeSpaceFromBegin = (text: string): string => {
return text.slice(idx)
}

const SPECIAL_IMG_CMD_SYMBOLS = ['i.', 'l.', 'I.', '? ', '! ', ': ', '; ', 'r.', 'R.', 'd.', 'D.', '( ', '$ ', '& ', '< ']
const SPECIAL_IMG_CMD_SYMBOLS = ['l.', '? ', '! ', ': ', '; ', 'r.', 'R.', 'd.', 'D.', '( ', '$ ', '& ', '< ']

export const getPrefix = (prompt: string, prefixList: string[]): string => {
for (let i = 0; i < prefixList.length; i++) {
Expand Down Expand Up @@ -126,13 +126,13 @@ export const parseCtx = (ctx: Context): IOperation | false => {
}

if (
(hasCommand(ctx, 'image') || hasCommand(ctx, 'imagine')) || hasCommand(ctx, 'img')
(hasCommand(ctx, 'sdimage') || hasCommand(ctx, 'sdimagine')) || hasCommand(ctx, 'sdimg')
) {
command = COMMAND.TEXT_TO_IMAGE
}

if (
(hasCommand(ctx, 'image2') || hasCommand(ctx, 'imagine2')) || hasCommand(ctx, 'img2')
(hasCommand(ctx, 'sdimage2') || hasCommand(ctx, 'sdimagine2')) || hasCommand(ctx, 'sdimg2')
) {
command = COMMAND.TEXT_TO_IMAGE
model = model && ({ ...model, serverNumber: 2 })
Expand All @@ -146,7 +146,7 @@ export const parseCtx = (ctx: Context): IOperation | false => {
lora = getLoraByParam('logo', model?.baseModel ?? 'SDXL 1.0')
}

if (hasCommand(ctx, 'images')) {
if (hasCommand(ctx, 'sd-images')) {
command = COMMAND.TEXT_TO_IMAGES
}

Expand Down

0 comments on commit 8b70ba0

Please sign in to comment.