feat: add funcitons manger page

This commit is contained in:
RockYang
2023-12-21 08:58:24 +08:00
parent bf19120c27
commit 7c4dfe96ee
12 changed files with 216 additions and 190 deletions

View File

@@ -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)
}

View File

@@ -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)
}

View File

@@ -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
}

View File

@@ -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/")

View File

@@ -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}",
"<p>A beautiful Chinese girl strolls along the beach<br>\nSteps: 30, Sampler: DPM++ 2M SDE Karras, CFG scale: 7, Seed: 291934632, Size: 1024x1024, Model hash: 2a4411ef93, Model: sdxlUnstableDiffusers_v7ElectricMind, Version: v1.5.2</p>",
"<p class='comments'></p><div class='performance'><p class='time'>Time taken: <wbr><span class='measurement'>11.2 sec.</span></p><p class='vram'><abbr title='Active: peak amount of video memory used during generation (excluding cached data)'>A</abbr>: <span class='measurement'>10.26 GB</span>, <wbr><abbr title='Reserved: total amout of video memory allocated by the Torch library '>R</abbr>: <span class='measurement'>13.34 GB</span>, <wbr><abbr title='System: peak amout of video memory allocated by all running programs, out of total capacity'>Sys</abbr>: <span class='measurement'>13.8/23.6904 GB</span> (58.1%)</p></div>"
[],
"",
"",
""
],
"event_data": null,
"fn_index": 232,
"session_hash": "sstp2i5ytoj"
"fn_index": 446,
"session_hash": "nk5noh1rz1o"
}

View File

@@ -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)
}

View File

@@ -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()

View File

@@ -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

View File

@@ -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, // 高清修复采样次数
}