From 7c4dfe96eeaac1d6b9aeaae70d4f953712abfb61 Mon Sep 17 00:00:00 2001 From: RockYang Date: Thu, 21 Dec 2023 08:58:24 +0800 Subject: [PATCH] feat: add funcitons manger page --- api/handler/admin/function_handler.go | 52 +++++++ api/handler/mj_handler.go | 6 + api/handler/sd_handler.go | 7 +- api/main.go | 8 + api/res/sd/text2img.json | 81 ++++------ api/service/mj/service.go | 5 +- api/service/sd/pool.go | 2 +- api/service/sd/service.go | 2 + api/service/sd/types.go | 20 +-- database/update-v3.2.3.sql | 4 +- web/src/views/ImageSd.vue | 4 +- web/src/views/admin/Functions.vue | 215 +++++++++++--------------- 12 files changed, 216 insertions(+), 190 deletions(-) create mode 100644 api/handler/admin/function_handler.go diff --git a/api/handler/admin/function_handler.go b/api/handler/admin/function_handler.go new file mode 100644 index 00000000..8eff4cbd --- /dev/null +++ b/api/handler/admin/function_handler.go @@ -0,0 +1,52 @@ +package admin + +import ( + "chatplus/core" + "chatplus/core/types" + "chatplus/handler" + "chatplus/store/model" + "chatplus/store/vo" + "chatplus/utils/resp" + "github.com/gin-gonic/gin" + "gorm.io/gorm" +) + +type FunctionHandler struct { + handler.BaseHandler + db *gorm.DB +} + +func NewFunctionHandler(app *core.AppServer, db *gorm.DB) *FunctionHandler { + h := FunctionHandler{db: db} + h.App = app + return &h +} + +func (h *FunctionHandler) Save(c *gin.Context) { + var data vo.Function + if err := c.ShouldBindJSON(&data); err != nil { + resp.ERROR(c, types.InvalidArgs) + return + } + + logger.Info(data) + resp.SUCCESS(c) +} + +func (h *FunctionHandler) List(c *gin.Context) { + + resp.SUCCESS(c) +} + +func (h *FunctionHandler) Remove(c *gin.Context) { + id := h.GetInt(c, "id", 0) + + if id > 0 { + res := h.db.Delete(&model.Function{Id: uint(id)}) + if res.Error != nil { + resp.ERROR(c, "更新数据库失败!") + return + } + } + resp.SUCCESS(c) +} diff --git a/api/handler/mj_handler.go b/api/handler/mj_handler.go index db4cb5e5..7b39aeed 100644 --- a/api/handler/mj_handler.go +++ b/api/handler/mj_handler.go @@ -146,6 +146,9 @@ func (h *MidJourneyHandler) Image(c *gin.Context) { Prompt: fmt.Sprintf("%s %s", taskId, prompt), UserId: userId, }) + + // update user's img calls + h.db.Model(&model.User{}).Where("id = ?", job.UserId).UpdateColumn("img_calls", gorm.Expr("img_calls - ?", 1)) resp.SUCCESS(c) } @@ -246,6 +249,9 @@ func (h *MidJourneyHandler) Variation(c *gin.Context) { MessageId: data.MessageId, MessageHash: data.MessageHash, }) + + // update user's img calls + h.db.Model(&model.User{}).Where("id = ?", job.UserId).UpdateColumn("img_calls", gorm.Expr("img_calls - ?", 1)) resp.SUCCESS(c) } diff --git a/api/handler/sd_handler.go b/api/handler/sd_handler.go index 553dba2b..b5515f9f 100644 --- a/api/handler/sd_handler.go +++ b/api/handler/sd_handler.go @@ -133,6 +133,9 @@ func (h *SdJobHandler) Image(c *gin.Context) { UserId: userId, }) + // update user's img calls + h.db.Model(&model.User{}).Where("id = ?", job.UserId).UpdateColumn("img_calls", gorm.Expr("img_calls - ?", 1)) + resp.SUCCESS(c) } @@ -177,8 +180,8 @@ func (h *SdJobHandler) JobList(c *gin.Context) { } if item.Progress < 100 { - // 10 分钟还没完成的任务直接删除 - if time.Now().Sub(item.CreatedAt) > time.Minute*10 { + // 5 分钟还没完成的任务直接删除 + if time.Now().Sub(item.CreatedAt) > time.Minute*5 { h.db.Delete(&item) continue } diff --git a/api/main.go b/api/main.go index 69ed1e83..c6e5b6bf 100644 --- a/api/main.go +++ b/api/main.go @@ -341,6 +341,14 @@ func main() { group.POST("translate", h.Translate) }), + fx.Provide(admin.NewFunctionHandler), + fx.Invoke(func(s *core.AppServer, h *admin.FunctionHandler) { + group := s.Engine.Group("/api/admin/function/") + group.POST("save", h.Save) + group.GET("list", h.List) + group.GET("remove", h.Remove) + }), + fx.Provide(handler.NewTestHandler), fx.Invoke(func(s *core.AppServer, h *handler.TestHandler) { group := s.Engine.Group("/test/") diff --git a/api/res/sd/text2img.json b/api/res/sd/text2img.json index 375c65c3..c15cc35e 100644 --- a/api/res/sd/text2img.json +++ b/api/res/sd/text2img.json @@ -1,24 +1,16 @@ { "data": [ - "task(vpu09bi42w01k5f)", - "A beautiful Chinese girl strolls along the beach", + "task(cxvkpawy8onnfti)", + "a cute girl", "", [], - 30, - "DPM++ 2M SDE Karras", - false, - false, + 20, + "DPM++ 2M Karras", 1, 1, 7, - -1, - -1, - 0, - 0, - 0, - false, - 1024, - 1024, + 512, + 512, false, 0.7, 2, @@ -26,11 +18,24 @@ 0, 0, 0, + "Use same checkpoint", "Use same sampler", "", "", [], "None", + false, + "", + 0.8, + -1, + false, + -1, + 0, + 0, + 0, + null, + null, + null, null, false, false, @@ -54,46 +59,22 @@ false, false, 0, - "Not set", - true, - true, - "", - "", - "", - "", - "", - 1.3, - "Not set", - "Not set", - 1.3, - "Not set", - 1.3, - "Not set", - 1.3, - 1.3, - "Not set", - 1.3, - "Not set", - 1.3, - "Not set", - 1.3, - "Not set", - 1.3, - "Not set", - 1.3, - "Not set", + null, + null, false, - "None", + null, + null, + false, + null, null, false, 50, - [ - ], - "{\"prompt\": \"A beautiful Chinese girl strolls along the beach\", \"all_prompts\": [\"A beautiful Chinese girl strolls along the beach\"], \"negative_prompt\": \"\", \"all_negative_prompts\": [\"\"], \"seed\": 291934632, \"all_seeds\": [291934632], \"subseed\": 2486830045, \"all_subseeds\": [2486830045], \"subseed_strength\": 0, \"width\": 1024, \"height\": 1024, \"sampler_name\": \"DPM++ 2M SDE Karras\", \"cfg_scale\": 7, \"steps\": 30, \"batch_size\": 1, \"restore_faces\": false, \"face_restoration_model\": null, \"sd_model_hash\": \"2a4411ef93\", \"seed_resize_from_w\": 0, \"seed_resize_from_h\": 0, \"denoising_strength\": null, \"extra_generation_params\": {}, \"index_of_first_image\": 0, \"infotexts\": [\"A beautiful Chinese girl strolls along the beach\\nSteps: 30, Sampler: DPM++ 2M SDE Karras, CFG scale: 7, Seed: 291934632, Size: 1024x1024, Model hash: 2a4411ef93, Model: sdxlUnstableDiffusers_v7ElectricMind, Version: v1.5.2\"], \"styles\": [], \"job_timestamp\": \"20231215222321\", \"clip_skip\": 1, \"is_using_inpainting_conditioning\": false}", - "

A beautiful Chinese girl strolls along the beach
\nSteps: 30, Sampler: DPM++ 2M SDE Karras, CFG scale: 7, Seed: 291934632, Size: 1024x1024, Model hash: 2a4411ef93, Model: sdxlUnstableDiffusers_v7ElectricMind, Version: v1.5.2

", - "

Time taken: 11.2 sec.

A: 10.26 GB, R: 13.34 GB, Sys: 13.8/23.6904 GB (58.1%)

" + [], + "", + "", + "" ], "event_data": null, - "fn_index": 232, - "session_hash": "sstp2i5ytoj" + "fn_index": 446, + "session_hash": "nk5noh1rz1o" } \ No newline at end of file diff --git a/api/service/mj/service.go b/api/service/mj/service.go index b49127e1..49d94edc 100644 --- a/api/service/mj/service.go +++ b/api/service/mj/service.go @@ -54,7 +54,6 @@ func (s *Service) Run() { err := s.taskQueue.LPop(&task) if err != nil { logger.Errorf("taking task with error: %v", err) - s.db.Model(&model.MidJourneyJob{Id: uint(task.Id)}).UpdateColumn("progress", -1) continue } @@ -83,6 +82,8 @@ func (s *Service) Run() { logger.Error("绘画任务执行失败:", err) // update the task progress s.db.Model(&model.MidJourneyJob{Id: uint(task.Id)}).UpdateColumn("progress", -1) + // restore img_call quota + s.db.Model(&model.User{}).Where("id = ?", task.UserId).UpdateColumn("img_calls", gorm.Expr("img_calls + ?", 1)) continue } @@ -157,8 +158,6 @@ func (s *Service) Notify(data CBReq) { } if data.Status == Finished { - // update user's img calls - s.db.Model(&model.User{}).Where("id = ?", job.UserId).UpdateColumn("img_calls", gorm.Expr("img_calls - ?", 1)) // release lock task atomic.AddInt32(&s.handledTaskNum, -1) } diff --git a/api/service/sd/pool.go b/api/service/sd/pool.go index 1bd4a065..b31aedf9 100644 --- a/api/service/sd/pool.go +++ b/api/service/sd/pool.go @@ -26,7 +26,7 @@ func NewServicePool(db *gorm.DB, redisCli *redis.Client, manager *oss.UploaderMa // create sd service name := fmt.Sprintf("StableDifffusion Service-%d", k) - service := NewService(name, 4, 600, config, queue, db, manager) + service := NewService(name, 1, 300, config, queue, db, manager) // run sd service go func() { service.Run() diff --git a/api/service/sd/service.go b/api/service/sd/service.go index 31a1e0cc..cef9fe60 100644 --- a/api/service/sd/service.go +++ b/api/service/sd/service.go @@ -69,6 +69,8 @@ func (s *Service) Run() { logger.Error("绘画任务执行失败:", err) // update the task progress s.db.Model(&model.SdJob{Id: uint(task.Id)}).UpdateColumn("progress", -1) + // restore img_call quota + s.db.Model(&model.User{}).Where("id = ?", task.UserId).UpdateColumn("img_calls", gorm.Expr("img_calls + ?", 1)) // release task num atomic.AddInt32(&s.handledTaskNum, -1) continue diff --git a/api/service/sd/types.go b/api/service/sd/types.go index 08e50127..b3a949c6 100644 --- a/api/service/sd/types.go +++ b/api/service/sd/types.go @@ -32,14 +32,14 @@ var ParamKeys = map[string]int{ "negative_prompt": 2, "steps": 4, "sampler": 5, - "face_fix": 6, // 面部修复 - "cfg_scale": 10, - "seed": 11, - "height": 17, - "width": 18, - "hd_fix": 19, - "hd_redraw_rate": 20, //高清修复重绘幅度 - "hd_scale": 21, // 高清修复放大倍数 - "hd_scale_alg": 22, // 高清修复放大算法 - "hd_sample_num": 23, // 高清修复采样次数 + "face_fix": 7, // 面部修复 + "cfg_scale": 8, + "seed": 27, + "height": 10, + "width": 9, + "hd_fix": 11, + "hd_redraw_rate": 12, //高清修复重绘幅度 + "hd_scale": 13, // 高清修复放大倍数 + "hd_scale_alg": 14, // 高清修复放大算法 + "hd_sample_num": 15, // 高清修复采样次数 } diff --git a/database/update-v3.2.3.sql b/database/update-v3.2.3.sql index fe77482f..e64c6ccc 100644 --- a/database/update-v3.2.3.sql +++ b/database/update-v3.2.3.sql @@ -16,4 +16,6 @@ ALTER TABLE `chatgpt_functions` MODIFY `id` int NOT NULL AUTO_INCREMENT; ALTER TABLE `chatgpt_functions` ADD UNIQUE(`name`); -ALTER TABLE `chatgpt_functions` ADD `enabled` TINYINT(1) NOT NULL DEFAULT '0' COMMENT '是否启用' AFTER `action`; \ No newline at end of file +ALTER TABLE `chatgpt_functions` ADD `enabled` TINYINT(1) NOT NULL DEFAULT '0' COMMENT '是否启用' AFTER `action`; + +ALTER TABLE `chatgpt_functions` ADD `lebal` VARCHAR(30) NULL COMMENT '函数标签' AFTER `name`; \ No newline at end of file diff --git a/web/src/views/ImageSd.vue b/web/src/views/ImageSd.vue index 6d19eee8..092fb132 100644 --- a/web/src/views/ImageSd.vue +++ b/web/src/views/ImageSd.vue @@ -533,10 +533,10 @@ const params = ref({ cfg_scale: 7, face_fix: false, hd_fix: false, - hd_redraw_rate: 0.7, + hd_redraw_rate: 0.5, hd_scale: 2, hd_scale_alg: scaleAlg[0], - hd_steps: 10, + hd_steps: 15, prompt: "", negative_prompt: "nsfw, paintings,low quality,easynegative,ng_deepnegative ,lowres,bad anatomy,bad hands,bad feet", }) diff --git a/web/src/views/admin/Functions.vue b/web/src/views/admin/Functions.vue index 5e6cad3a..fa28cb2a 100644 --- a/web/src/views/admin/Functions.vue +++ b/web/src/views/admin/Functions.vue @@ -21,7 +21,7 @@