LaiTool/src/api/sdApi.js

88 lines
2.2 KiB
JavaScript
Raw Normal View History

2024-08-03 12:46:12 +08:00
import { basicApi } from './apiBasic'
import { define } from '../define/define'
2024-05-24 13:46:19 +08:00
import { promises as fspromises } from 'fs'
export class SdApi {
2024-08-03 12:46:12 +08:00
constructor() {
this.baseUrl = global.config?.webui_api_url
this.sd_setting = null
}
2024-05-24 13:46:19 +08:00
2024-08-03 12:46:12 +08:00
/**
* 获取当前SD的服务器中所有的lora信息
* @returns
*/
async getAllLoras(baseURL = null) {
let url = this.baseUrl + 'sdapi/v1/loras'
if (baseURL != null) {
url = baseURL + 'sdapi/v1/loras'
2024-05-24 13:46:19 +08:00
}
2024-08-03 12:46:12 +08:00
return await basicApi.get(url)
}
2024-05-24 13:46:19 +08:00
2024-08-03 12:46:12 +08:00
/**
* 获取当前的所有的checkpoint模型
* @param {*} baseURL
*/
async getAllSDModel(baseURL = null) {
let url = this.baseUrl + 'sdapi/v1/sd-models'
if (baseURL != null) {
url = baseURL + 'sdapi/v1/sd-models'
2024-05-24 13:46:19 +08:00
}
2024-08-03 12:46:12 +08:00
return await basicApi.get(url)
}
2024-05-24 13:46:19 +08:00
2024-08-03 12:46:12 +08:00
/**
* 获取当前连接的所有的samplers采样器
* @param {*} baseURL
* @returns
*/
async getAllSamplers(baseURL = null) {
try {
let url = this.baseUrl + 'sdapi/v1/samplers'
if (baseURL != null) {
url = baseURL + 'sdapi/v1/samplers'
}
return await basicApi.get(url)
} catch (error) {
throw error
2024-05-24 13:46:19 +08:00
}
2024-08-03 12:46:12 +08:00
}
2024-05-24 13:46:19 +08:00
2024-08-03 12:46:12 +08:00
async txt2img(data, baseURL = null) {
try {
if (this.sd_setting == null) {
this.sd_setting = JSON.parse(await fspromises.readFile(define.sd_setting, 'utf-8'))
this.baseUrl = this.sd_setting.setting.webui_api_url
}
2024-05-24 13:46:19 +08:00
2024-08-03 12:46:12 +08:00
// 加上通用前缀
data.prompt = this.sd_setting.webui.prompt + data.prompt
2024-05-24 13:46:19 +08:00
2024-08-03 12:46:12 +08:00
data.negative_prompt = this.sd_setting.webui.negative_prompt
data.sampler_name = this.sd_setting.webui.sampler_name
data.cfg_scale = this.sd_setting.webui.cfg_scale
data.n_iter = 1
data.steps = this.sd_setting.webui.steps
data.save_images = false
data.batch_size = data.batch_size ? data.batch_size : 1
2024-06-01 15:08:22 +08:00
2024-08-03 12:46:12 +08:00
if (data.width == null) {
data.width = 512
}
if (data.height == null) {
data.height = 512
}
2024-05-24 13:46:19 +08:00
2024-08-03 12:46:12 +08:00
let url = this.baseUrl + 'sdapi/v1/txt2img'
if (baseURL != null) {
url = baseURL + 'sdapi/v1/txt2img'
}
let res = await basicApi.post(url, data)
return res
} catch (error) {
throw error
2024-05-24 13:46:19 +08:00
}
2024-08-03 12:46:12 +08:00
}
2024-05-24 13:46:19 +08:00
}