创建图像存储目录

This commit is contained in:
2023-06-21 17:17:53 +08:00
parent 1d0da612b8
commit a3f0c3a637

View File

@@ -49,6 +49,13 @@ type Model struct {
func init() {
configs.ORMDB().AutoMigrate(&Model{})
// 检查 images 目录是否存在, 不存在则创建
if _, err := os.Stat("data/images"); err != nil {
if err := os.MkdirAll("data/images", 0777); err != nil {
log.Println(err)
}
}
// 处理推理任务
//go func() {
// for {
@@ -63,120 +70,120 @@ func (model *Model) Inference(image_list []Image, callback func()) {
log.Println(image_list)
// 模型未部署到推理機
if model.ServerID != "" {
if model.ServerID == "" {
//log.Println("模型未部署到推理機, 开始部署模型")
log.Println("模型已部署到推理機, 开始推理模型")
var server Server
if err := configs.ORMDB().Where("models LIKE ?", "%"+model.Name+"%").Take(&server).Error; err != nil {
log.Println(err)
// 如果没有则寻找空闲服务器
// 如果没有空闲则创建新服务器
// 取一台空闲的推理机上传并切换到此模型
// 新建一台推理机上传并切换到此模型
}
server.IP = "106.15.192.42"
server.Port = 7860
//if err := configs.ORMDB().Where("models LIKE ?", "%"+model.Name+"%").Take(&server).Error; err != nil {
// log.Println(err)
// // 如果没有则寻找空闲服务器
// // 如果没有空闲则创建新服务器
// // 取一台空闲的推理机上传并切换到此模型
// // 新建一台推理机上传并切换到此模型
//}
// 执行生成任务
if model.Image == "" {
var data = struct {
EnableHr bool `json:"enable_hr"`
DenoisingStrength int `json:"denoising_strength"`
FirstphaseWidth int `json:"firstphase_width"`
FirstphaseHeight int `json:"firstphase_height"`
HrScale int `json:"hr_scale"`
HrUpscaler string `json:"hr_upscaler"`
HrSecondPassSteps int `json:"hr_second_pass_steps"`
HrResizeX int `json:"hr_resize_x"`
HrResizeY int `json:"hr_resize_y"`
HrSamplerName string `json:"hr_sampler_name"`
HrPrompt string `json:"hr_prompt"`
HrNegativePrompt string `json:"hr_negative_prompt"`
Prompt string `json:"prompt"`
Styles []string `json:"styles"`
Seed int `json:"seed"`
Subseed int `json:"subseed"`
SubseedStrength int `json:"subseed_strength"`
SeedResizeFromH int `json:"seed_resize_from_h"`
SeedResizeFromW int `json:"seed_resize_from_w"`
SamplerName string `json:"sampler_name"`
BatchSize int `json:"batch_size"`
NIter int `json:"n_iter"`
Steps int `json:"steps"`
CfgScale int `json:"cfg_scale"`
Width int `json:"width"`
Height int `json:"height"`
RestoreFaces bool `json:"restore_faces"`
Tiling bool `json:"tiling"`
DoNotSaveSamples bool `json:"do_not_save_samples"`
DoNotSaveGrid bool `json:"do_not_save_grid"`
NegativePrompt string `json:"negative_prompt"`
Eta int `json:"eta"`
SMinUncond int `json:"s_min_uncond"`
SChurn int `json:"s_churn"`
STmax int `json:"s_tmax"`
STmin int `json:"s_tmin"`
SNoise int `json:"s_noise"`
OverrideSettings map[string]string `json:"override_settings"`
OverrideSettingsRestoreAfterwards bool `json:"override_settings_restore_afterwards"`
ScriptArgs []interface{} `json:"script_args"`
SamplerIndex string `json:"sampler_index"`
ScriptName string `json:"script_name"`
SendImages bool `json:"send_images"`
SaveImages bool `json:"save_images"`
AlwaysonScripts map[string]string `json:"alwayson_scripts"`
//EnableHr bool `json:"enable_hr"`
//DenoisingStrength int `json:"denoising_strength"`
//FirstphaseWidth int `json:"firstphase_width"`
//FirstphaseHeight int `json:"firstphase_height"`
//HrScale int `json:"hr_scale"`
//HrUpscaler string `json:"hr_upscaler"`
//HrSecondPassSteps int `json:"hr_second_pass_steps"`
//HrResizeX int `json:"hr_resize_x"`
//HrResizeY int `json:"hr_resize_y"`
//HrSamplerName string `json:"hr_sampler_name"`
//HrPrompt string `json:"hr_prompt"`
//HrNegativePrompt string `json:"hr_negative_prompt"`
Prompt string `json:"prompt"`
//Styles []string `json:"styles"`
//Seed int `json:"seed"`
//Subseed int `json:"subseed"`
//SubseedStrength int `json:"subseed_strength"`
//SeedResizeFromH int `json:"seed_resize_from_h"`
//SeedResizeFromW int `json:"seed_resize_from_w"`
//SamplerName string `json:"sampler_name"`
//BatchSize int `json:"batch_size"`
//NIter int `json:"n_iter"`
//Steps int `json:"steps"`
//CfgScale int `json:"cfg_scale"`
//Width int `json:"width"`
//Height int `json:"height"`
//RestoreFaces bool `json:"restore_faces"`
//Tiling bool `json:"tiling"`
//DoNotSaveSamples bool `json:"do_not_save_samples"`
//DoNotSaveGrid bool `json:"do_not_save_grid"`
//NegativePrompt string `json:"negative_prompt"`
//Eta int `json:"eta"`
//SMinUncond int `json:"s_min_uncond"`
//SChurn int `json:"s_churn"`
//STmax int `json:"s_tmax"`
//STmin int `json:"s_tmin"`
//SNoise int `json:"s_noise"`
//OverrideSettings map[string]string `json:"override_settings"`
//OverrideSettingsRestoreAfterwards bool `json:"override_settings_restore_afterwards"`
//ScriptArgs []interface{} `json:"script_args"`
//SamplerIndex string `json:"sampler_index"`
//ScriptName string `json:"script_name"`
//SendImages bool `json:"send_images"`
//SaveImages bool `json:"save_images"`
//AlwaysonScripts map[string]string `json:"alwayson_scripts"`
}{
EnableHr: false,
DenoisingStrength: 0,
FirstphaseWidth: 0,
FirstphaseHeight: 0,
HrScale: 2,
HrUpscaler: "nearest",
HrSecondPassSteps: 0,
HrResizeX: 0,
HrResizeY: 0,
HrSamplerName: "",
HrPrompt: "",
HrNegativePrompt: "",
Prompt: "miao~",
Styles: []string{},
Seed: -1,
Subseed: -1,
SubseedStrength: 0,
SeedResizeFromH: -1,
SeedResizeFromW: -1,
SamplerName: "beamsearch",
BatchSize: 1,
NIter: 1,
Steps: 50,
CfgScale: 7,
Width: 512,
Height: 512,
RestoreFaces: false,
Tiling: false,
DoNotSaveSamples: false,
DoNotSaveGrid: false,
NegativePrompt: "",
Eta: 0,
SMinUncond: 0,
SChurn: 0,
STmax: 0,
STmin: 0,
SNoise: 1,
OverrideSettings: map[string]string{},
OverrideSettingsRestoreAfterwards: false,
ScriptArgs: []interface{}{},
SamplerIndex: "Euler",
ScriptName: "generate",
SendImages: true,
SaveImages: false,
AlwaysonScripts: map[string]string{},
//EnableHr: false,
//DenoisingStrength: 0,
//FirstphaseWidth: 0,
//FirstphaseHeight: 0,
//HrScale: 2,
//HrUpscaler: "nearest",
//HrSecondPassSteps: 0,
//HrResizeX: 0,
//HrResizeY: 0,
//HrSamplerName: "",
//HrPrompt: "",
//HrNegativePrompt: "",
Prompt: "miao~",
//Styles: []string{},
//Seed: -1,
//Subseed: -1,
//SubseedStrength: 0,
//SeedResizeFromH: -1,
//SeedResizeFromW: -1,
//SamplerName: "beamsearch",
//BatchSize: 1,
//NIter: 1,
//Steps: 50,
//CfgScale: 7,
//Width: 512,
//Height: 512,
//RestoreFaces: false,
//Tiling: false,
//DoNotSaveSamples: false,
//DoNotSaveGrid: false,
//NegativePrompt: "",
//Eta: 0,
//SMinUncond: 0,
//SChurn: 0,
//STmax: 0,
//STmin: 0,
//SNoise: 1,
//OverrideSettings: map[string]string{},
//OverrideSettingsRestoreAfterwards: false,
//ScriptArgs: []interface{}{},
//SamplerIndex: "Euler",
//ScriptName: "generate",
//SendImages: true,
//SaveImages: false,
//AlwaysonScripts: map[string]string{},
}
// 接收到的图片列表
var rest = struct {
Images []string `json:"images"`
}{
Images: []string{},
}
}{}
var url = fmt.Sprintf("http://%s:%d/sdapi/v1/txt2img", server.IP, server.Port)
if err := goreq.Post(url).SetJsonBody(data).Do().BindJSON(&rest); err != nil {
log.Println("API 查询失败:", err)
@@ -185,12 +192,14 @@ func (model *Model) Inference(image_list []Image, callback func()) {
for _, img := range rest.Images {
log.Println("保存图片:", img)
// 将base64编码的图片保存到本地webp
if err := SaveBase64Image(img, "data/images/"+img+".webp"); err != nil {
var filename = fmt.Sprintf("%x", md5.Sum([]byte(img)))
if err := SaveBase64Image(img, "data/images/"+filename+".webp"); err != nil {
log.Println(err)
}
}
}
callback()
return
}
log.Println("模型未部署到推理機, 取消推理模型")
}