add nai diffusion

Signed-off-by: hashcoko <hashcoko@gmail.com>
This commit is contained in:
hashcoko
2023-11-27 07:13:22 +09:00
parent d9e16cc427
commit b69f31caae
12 changed files with 226 additions and 40 deletions

View File

@@ -2,11 +2,13 @@ import { get } from "svelte/store"
import { DataBase, type character } from "../storage/database"
import { requestChatData } from "./request"
import { alertError } from "../alert"
import { globalFetch } from "../storage/globalApi"
import { globalFetch, readImage } from "../storage/globalApi"
import { CharEmotion } from "../stores"
import type { OpenAIChat } from "."
import { processZip } from "./processzip"
import { convertToBase64 } from "./uinttobase64"
import type { List } from "lodash"
import { generateRandomSeed } from "./generateSeed"
export async function stableDiff(currentChar:character,prompt:string){
const mainPrompt = "assistant is a chat analyzer.\nuser will input a data of situation with key and values before chat, and a chat of a user and character.\nView the status of the chat and change the data.\nif data's key starts with $, it must change it every time.\nif data value is none, it must change it."
let db = get(DataBase)
@@ -175,39 +177,80 @@ export async function stableDiff(currentChar:character,prompt:string){
}
const uri = new URL(db.NAIImgUrl)
uri.pathname = '/ai/generate-image'
const reqlist = {
body: {
"input": prompts.join(','),
"model": db.NAIImgModel,
"parameters": {
"width": db.NAIImgConfig.width,
"height": db.NAIImgConfig.height,
"sampler": db.NAIImgConfig.sampler,
"steps": db.NAIImgConfig.steps,
"scale": db.NAIImgConfig.scale,
"negative_prompt": neg,
"sm": db.NAIImgConfig.sm,
"sm_dyn": db.NAIImgConfig.sm_dyn
const charimg = currentChar.image; // Uint8Array 형태의 이미지 데이터
console.log("charimg:" + charimg);
const img = await readImage(charimg)
console.log("img:" + img);
const base64 = await convertToBase64(img);
const base64img = base64.split('base64,')[1];
console.log("base64img:" + base64img);
let reqlist= {}
if(db.NAII2I){
let randomseed = generateRandomSeed(10);
let seed = parseInt(randomseed, 10);
reqlist = {
body: {
"action": "img2img",
"input": prompts.join(','),
"model": db.NAIImgModel,
"parameters": {
"seed": seed,
"extra_noise_seed": seed,
"add_original_image": false,
"cfg_rescale": 0,
"controlnet_strength": 1,
"dynamic_threshold": false,
"n_samples": 1,
"width": db.NAIImgConfig.width,
"height": db.NAIImgConfig.height,
"sampler": db.NAIImgConfig.sampler,
"steps": db.NAIImgConfig.steps,
"scale": db.NAIImgConfig.scale,
"negative_prompt": neg,
"sm": false,
"sm_dyn": false,
"noise": db.NAIImgConfig.noise,
"noise_schedule": "native",
"strength": db.NAIImgConfig.strength,
"image": base64img,
"ucPreset": 2,
"uncond_scale": 1
}
},
headers:{
"Authorization": "Bearer " + db.NAIApiKey
}
}
}else{
reqlist = {
body: {
"input": prompts.join(','),
"model": db.NAIImgModel,
"parameters": {
"width": db.NAIImgConfig.width,
"height": db.NAIImgConfig.height,
"sampler": db.NAIImgConfig.sampler,
"steps": db.NAIImgConfig.steps,
"scale": db.NAIImgConfig.scale,
"negative_prompt": neg,
"sm": db.NAIImgConfig.sm,
"sm_dyn": db.NAIImgConfig.sm_dyn
}
},
headers:{
"Authorization": "Bearer " + db.NAIApiKey
}
},
headers:{
'Authorization:': 'Bearer ' + db.NAIApiKey,
'Content-Type': 'application/json'
}
}
console.log(uri)
console.log(reqlist)
try {
const da = await globalFetch(uri.toString(), reqlist)
const da = await globalFetch(db.NAIImgUrl, reqlist)
console.log(da)
if(da.ok){
if(da){
let charemotions = get(CharEmotion)
const img = `data:image/png;base64,${da.data.images[0]}`
console.log(img)
const img = await processZip(da.data);
const emos:[string, string,number][] = [[img, img, Date.now()]]
charemotions[currentChar.chaId] = emos
CharEmotion.set(charemotions)