Skip to content

Commit

Permalink
Merge pull request #320 from harmony-one/sd_images_gif
Browse files Browse the repository at this point in the history
SD Images: GIF
  • Loading branch information
theofandrich authored Sep 29, 2023
2 parents 919de81 + 531d74a commit 2ebfa1c
Show file tree
Hide file tree
Showing 9 changed files with 386 additions and 35 deletions.
10 changes: 10 additions & 0 deletions src/modules/qrcode/comfy/ComfyClient.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,11 @@ interface HistoryResponseItem {
'subfolder': string
'type': 'output'
}>
gifs: Array<{
'filename': string
'subfolder': string
'type': 'output'
}>
}>
}

Expand Down Expand Up @@ -147,4 +152,9 @@ export class ComfyClient {
const response = await this.httpClient.get<Buffer>(`/view?filename=${filename}&subfolder=&type=output`, { responseType: 'arraybuffer' })
return response.data
}

getFileUrl (filename: string): string {
// return `${this.host}/view?filename=${filename}&subfolder=&type=output`
return `${this.host}/view?filename=${filename}&subfolder=&type=output&format=image%2Fgif`
}
}
36 changes: 27 additions & 9 deletions src/modules/sd-images/SDImagesBotBase.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ import { type ILora } from './api/loras-config'
import { getParamsFromPrompt } from './api/helpers'
import { type IBalancerOperation, OPERATION_STATUS, completeOperation, createOperation, getOperationById } from './balancer'
import { now } from '../../utils/perf'
import { MEDIA_FORMAT } from './api/configs'

export interface MessageExtras {
caption?: string
Expand All @@ -21,6 +22,7 @@ export interface ISession {
prompt: string
model: IModel
lora?: ILora
format?: MEDIA_FORMAT
all_seeds?: string[]
seed?: number
command: COMMAND
Expand Down Expand Up @@ -71,13 +73,14 @@ export class SDImagesBotBase {
prompt: string
model: IModel
lora?: ILora
format?: MEDIA_FORMAT
command: COMMAND
all_seeds?: string[]
seed?: string
}
): Promise<ISession> => {
// eslint-disable-next-line @typescript-eslint/naming-convention
const { prompt, model, command, all_seeds, lora } = params
const { prompt, model, command, all_seeds, lora, format } = params

const authorObj = await ctx.getAuthor()
const author = `@${authorObj.user.username}`
Expand All @@ -93,7 +96,8 @@ export class SDImagesBotBase {
lora,
command,
all_seeds,
message
message,
format
}

this.sessions.push(newSession)
Expand All @@ -109,7 +113,11 @@ export class SDImagesBotBase {
): Promise<{ queueMessageId: number, balancerOperaton: IBalancerOperation }> => {
const params = getParamsFromPrompt(session.prompt)

const lora = params.loraName ? `${params.loraName}.safetensors` : session.lora?.path
let lora = params.loraName ? `${params.loraName}.safetensors` : session.lora?.path

if (!lora && session.format === MEDIA_FORMAT.GIF) {
lora = 'ym201.safetensors'
}

let balancerOperaton = await createOperation({
model: session.model.path,
Expand Down Expand Up @@ -140,7 +148,7 @@ export class SDImagesBotBase {
session: ISession,
specialMessage?: string
): Promise<void> => {
const { model, prompt, seed, lora } = session
const { model, prompt, seed, lora, format } = session

let balancerOperatonId

Expand All @@ -154,7 +162,8 @@ export class SDImagesBotBase {
prompt,
model,
seed,
lora
lora,
format
}, balancerOperaton.server)

if (balancerOperatonId) {
Expand All @@ -166,10 +175,19 @@ export class SDImagesBotBase {
? session.message
: `${session.message} ${prompt}`
: `/${model.aliases[0]} ${prompt}`
await ctx.replyWithPhoto(new InputFile(imageBuffer), {
caption: specialMessage ?? reqMessage,
message_thread_id: ctx.message?.message_thread_id
})

if (format === MEDIA_FORMAT.GIF) {
await ctx.replyWithAnimation(new InputFile(imageBuffer, 'file.gif'), {
caption: specialMessage ?? reqMessage,
message_thread_id: ctx.message?.message_thread_id
})
} else {
await ctx.replyWithPhoto(new InputFile(imageBuffer), {
caption: specialMessage ?? reqMessage,
message_thread_id: ctx.message?.message_thread_id
})
}

if (ctx.chat?.id && queueMessageId) {
await ctx.api.deleteMessage(ctx.chat?.id, queueMessageId)
}
Expand Down
2 changes: 2 additions & 0 deletions src/modules/sd-images/api/configs/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,4 +4,6 @@ export * from './img2img_controlnet_config'
export * from './img2img_controlnet_v2_config'
export * from './text2img_config'
export * from './text2img_lora_config'
export * from './text2gif_config'
export * from './text2gif_lora_config'
export * from './types'
121 changes: 121 additions & 0 deletions src/modules/sd-images/api/configs/text2gif_config.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,121 @@
import { type Txt2ImgOptions } from './types'

export function buildText2GifPrompt (options: Txt2ImgOptions & { clientId: string }): unknown {
return {
client_id: options.clientId,
prompt: {
2: {
inputs: { vae_name: 'vae-ft-mse-840000-ema-pruned.ckpt' },
class_type: 'VAELoader'
},
3: {
inputs: {
text: options.prompt,
clip: [
'4',
0
]
},
class_type: 'CLIPTextEncode'
},
4: {
inputs: {
stop_at_clip_layer: -2,
clip: [
'32',
1
]
},
class_type: 'CLIPSetLastLayer'
},
6: {
inputs: {
text: options.negativePrompt,
clip: [
'4',
0
]
},
class_type: 'CLIPTextEncode'
},
7: {
inputs: {
seed: options.seed,
steps: options.steps ?? 20,
cfg: 8,
sampler_name: 'euler',
scheduler: 'normal',
denoise: 1,
model: [
'27',
0
],
positive: [
'3',
0
],
negative: [
'6',
0
],
latent_image: [
'9',
0
]
},
class_type: 'KSampler'
},
9: {
inputs: {
width: options.width,
height: options.height,
batch_size: options.batchSize ?? 16
},
class_type: 'EmptyLatentImage'
},
10: {
inputs: {
samples: [
'7',
0
],
vae: [
'2',
0
]
},
class_type: 'VAEDecode'
},
26: {
inputs: {
frame_rate: 8,
loop_count: 0,
filename_prefix: 'ComfyUI',
format: 'image/gif',
pingpong: false,
save_image: true,
images: [
'10',
0
]
},
class_type: 'ADE_AnimateDiffCombine'
},
27: {
inputs: {
model_name: 'mm_sd_v14.ckpt',
beta_schedule: 'sqrt_linear (AnimateDiff)',
model: [
'32',
0
]
},
class_type: 'ADE_AnimateDiffLoaderWithContext'
},
32: {
inputs: { ckpt_name: options.model },
class_type: 'CheckpointLoaderSimple'
}
}
}
}
137 changes: 137 additions & 0 deletions src/modules/sd-images/api/configs/text2gif_lora_config.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,137 @@
import { type Txt2ImgOptions } from './types'

export function buildText2GifLoraPrompt (options: Txt2ImgOptions & { clientId: string }): unknown {
return {
client_id: options.clientId,
prompt: {
2: {
inputs: { vae_name: 'vae-ft-mse-840000-ema-pruned.ckpt' },
class_type: 'VAELoader'
},
3: {
inputs: {
text: options.prompt,
clip: [
'4',
0
]
},
class_type: 'CLIPTextEncode'
},
4: {
inputs: {
stop_at_clip_layer: -2,
clip: [
'35',
1
]
},
class_type: 'CLIPSetLastLayer'
},
6: {
inputs: {
text: options.negativePrompt,
clip: [
'4',
0
]
},
class_type: 'CLIPTextEncode'
},
7: {
inputs: {
seed: options.seed,
steps: options.steps ?? 20,
cfg: 8,
sampler_name: 'euler',
scheduler: 'normal',
denoise: 1,
model: [
'27',
0
],
positive: [
'3',
0
],
negative: [
'6',
0
],
latent_image: [
'9',
0
]
},
class_type: 'KSampler'
},
9: {
inputs: {
width: options.width,
height: options.height,
batch_size: options.batchSize ?? 16
},
class_type: 'EmptyLatentImage'
},
10: {
inputs: {
samples: [
'7',
0
],
vae: [
'2',
0
]
},
class_type: 'VAEDecode'
},
26: {
inputs: {
frame_rate: 8,
loop_count: 0,
filename_prefix: 'ComfyUI',
format: 'image/gif',
pingpong: false,
save_image: true,
images: [
'10',
0
]
},
class_type: 'ADE_AnimateDiffCombine'
},
27: {
inputs: {
model_name: 'mm_sd_v14.ckpt',
beta_schedule: 'sqrt_linear (AnimateDiff)',
model: [
'35',
0
]
},
class_type: 'ADE_AnimateDiffLoaderWithContext'
},
32: {
inputs: { ckpt_name: options.model },
class_type: 'CheckpointLoaderSimple'
},
35: {
inputs: {
lora_name: options.loraPath,
strength_model: 1,
strength_clip: 1,
model: [
'32',
0
],
clip: [
'32',
1
]
},
class_type: 'LoraLoader'
}
}
}
}
Loading

0 comments on commit 2ebfa1c

Please sign in to comment.