2023-09-26 18:16:51 +08:00
|
|
|
|
package handler
|
|
|
|
|
|
2024-05-06 14:41:27 +08:00
|
|
|
|
// * +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
|
|
|
|
|
// * Copyright 2023 The Geek-AI Authors. All rights reserved.
|
|
|
|
|
// * Use of this source code is governed by a Apache-2.0 license
|
|
|
|
|
// * that can be found in the LICENSE file.
|
|
|
|
|
// * @Author yangjian102621@163.com
|
|
|
|
|
// * +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
|
|
|
|
|
|
2023-09-28 18:09:45 +08:00
|
|
|
|
import (
|
2024-05-06 14:41:27 +08:00
|
|
|
|
"geekai/core"
|
|
|
|
|
"geekai/core/types"
|
|
|
|
|
"geekai/service"
|
|
|
|
|
"geekai/service/oss"
|
|
|
|
|
"geekai/service/sd"
|
|
|
|
|
"geekai/store"
|
|
|
|
|
"geekai/store/model"
|
|
|
|
|
"geekai/store/vo"
|
|
|
|
|
"geekai/utils"
|
|
|
|
|
"geekai/utils/resp"
|
2023-09-28 18:09:45 +08:00
|
|
|
|
"fmt"
|
2024-01-24 09:33:04 +08:00
|
|
|
|
"net/http"
|
2023-12-15 22:52:57 +08:00
|
|
|
|
"time"
|
|
|
|
|
|
2024-03-03 10:40:32 +08:00
|
|
|
|
"github.com/gorilla/websocket"
|
|
|
|
|
|
2023-09-28 18:09:45 +08:00
|
|
|
|
"github.com/gin-gonic/gin"
|
|
|
|
|
"github.com/go-redis/redis/v8"
|
|
|
|
|
"gorm.io/gorm"
|
|
|
|
|
)
|
|
|
|
|
|
|
|
|
|
type SdJobHandler struct {
|
|
|
|
|
BaseHandler
|
2024-03-29 15:41:58 +08:00
|
|
|
|
redis *redis.Client
|
|
|
|
|
pool *sd.ServicePool
|
|
|
|
|
uploader *oss.UploaderManager
|
|
|
|
|
snowflake *service.Snowflake
|
2024-04-02 17:24:38 +08:00
|
|
|
|
leveldb *store.LevelDB
|
2023-09-28 18:09:45 +08:00
|
|
|
|
}
|
|
|
|
|
|
2024-04-02 17:24:38 +08:00
|
|
|
|
func NewSdJobHandler(app *core.AppServer, db *gorm.DB, pool *sd.ServicePool, manager *oss.UploaderManager, snowflake *service.Snowflake, levelDB *store.LevelDB) *SdJobHandler {
|
2024-03-19 18:25:01 +08:00
|
|
|
|
return &SdJobHandler{
|
2024-03-29 15:41:58 +08:00
|
|
|
|
pool: pool,
|
|
|
|
|
uploader: manager,
|
|
|
|
|
snowflake: snowflake,
|
2024-04-02 17:24:38 +08:00
|
|
|
|
leveldb: levelDB,
|
2024-03-19 18:25:01 +08:00
|
|
|
|
BaseHandler: BaseHandler{
|
|
|
|
|
App: app,
|
|
|
|
|
DB: db,
|
|
|
|
|
},
|
2023-09-28 18:09:45 +08:00
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2024-01-24 09:33:04 +08:00
|
|
|
|
// Client WebSocket 客户端,用于通知任务状态变更
|
|
|
|
|
func (h *SdJobHandler) Client(c *gin.Context) {
|
|
|
|
|
ws, err := (&websocket.Upgrader{CheckOrigin: func(r *http.Request) bool { return true }}).Upgrade(c.Writer, c.Request, nil)
|
|
|
|
|
if err != nil {
|
|
|
|
|
logger.Error(err)
|
|
|
|
|
c.Abort()
|
|
|
|
|
return
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
userId := h.GetInt(c, "user_id", 0)
|
|
|
|
|
if userId == 0 {
|
|
|
|
|
logger.Info("Invalid user ID")
|
|
|
|
|
c.Abort()
|
|
|
|
|
return
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
client := types.NewWsClient(ws)
|
|
|
|
|
h.pool.Clients.Put(uint(userId), client)
|
|
|
|
|
logger.Infof("New websocket connected, IP: %s", c.RemoteIP())
|
|
|
|
|
}
|
|
|
|
|
|
2024-04-21 20:23:47 +08:00
|
|
|
|
func (h *SdJobHandler) preCheck(c *gin.Context) bool {
|
2024-03-19 18:25:01 +08:00
|
|
|
|
user, err := h.GetLoginUser(c)
|
2023-09-28 18:09:45 +08:00
|
|
|
|
if err != nil {
|
|
|
|
|
resp.NotAuth(c)
|
|
|
|
|
return false
|
|
|
|
|
}
|
|
|
|
|
|
2023-12-14 16:48:54 +08:00
|
|
|
|
if !h.pool.HasAvailableService() {
|
|
|
|
|
resp.ERROR(c, "Stable-Diffusion 池子中没有没有可用的服务!")
|
|
|
|
|
return false
|
|
|
|
|
}
|
|
|
|
|
|
2024-03-12 15:40:44 +08:00
|
|
|
|
if user.Power < h.App.SysConfig.SdPower {
|
|
|
|
|
resp.ERROR(c, "当前用户剩余算力不足以完成本次绘画!")
|
2023-09-28 18:09:45 +08:00
|
|
|
|
return false
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return true
|
|
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// Image 创建一个绘画任务
|
|
|
|
|
func (h *SdJobHandler) Image(c *gin.Context) {
|
2024-04-21 20:23:47 +08:00
|
|
|
|
if !h.preCheck(c) {
|
2023-09-28 18:09:45 +08:00
|
|
|
|
return
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
var data struct {
|
|
|
|
|
SessionId string `json:"session_id"`
|
|
|
|
|
types.SdTaskParams
|
|
|
|
|
}
|
|
|
|
|
if err := c.ShouldBindJSON(&data); err != nil || data.Prompt == "" {
|
|
|
|
|
resp.ERROR(c, types.InvalidArgs)
|
|
|
|
|
return
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if data.Width <= 0 {
|
|
|
|
|
data.Width = 512
|
|
|
|
|
}
|
|
|
|
|
if data.Height <= 0 {
|
|
|
|
|
data.Height = 512
|
|
|
|
|
}
|
|
|
|
|
if data.CfgScale <= 0 {
|
|
|
|
|
data.CfgScale = 7
|
|
|
|
|
}
|
|
|
|
|
if data.Seed == 0 {
|
|
|
|
|
data.Seed = -1
|
|
|
|
|
}
|
|
|
|
|
if data.Steps <= 0 {
|
|
|
|
|
data.Steps = 20
|
|
|
|
|
}
|
|
|
|
|
if data.Sampler == "" {
|
|
|
|
|
data.Sampler = "Euler a"
|
|
|
|
|
}
|
|
|
|
|
idValue, _ := c.Get(types.LoginUserID)
|
|
|
|
|
userId := utils.IntValue(utils.InterfaceToString(idValue), 0)
|
2024-03-29 15:41:58 +08:00
|
|
|
|
taskId, err := h.snowflake.Next(true)
|
|
|
|
|
if err != nil {
|
|
|
|
|
resp.ERROR(c, "error with generate task id: "+err.Error())
|
|
|
|
|
return
|
|
|
|
|
}
|
2023-09-28 18:09:45 +08:00
|
|
|
|
params := types.SdTaskParams{
|
2024-04-03 18:13:48 +08:00
|
|
|
|
TaskId: taskId,
|
|
|
|
|
Prompt: data.Prompt,
|
|
|
|
|
NegPrompt: data.NegPrompt,
|
|
|
|
|
Steps: data.Steps,
|
|
|
|
|
Sampler: data.Sampler,
|
|
|
|
|
FaceFix: data.FaceFix,
|
|
|
|
|
CfgScale: data.CfgScale,
|
|
|
|
|
Seed: data.Seed,
|
|
|
|
|
Height: data.Height,
|
|
|
|
|
Width: data.Width,
|
|
|
|
|
HdFix: data.HdFix,
|
|
|
|
|
HdRedrawRate: data.HdRedrawRate,
|
|
|
|
|
HdScale: data.HdScale,
|
|
|
|
|
HdScaleAlg: data.HdScaleAlg,
|
|
|
|
|
HdSteps: data.HdSteps,
|
2023-09-28 18:09:45 +08:00
|
|
|
|
}
|
2024-03-27 13:45:52 +08:00
|
|
|
|
|
2023-09-28 18:09:45 +08:00
|
|
|
|
job := model.SdJob{
|
|
|
|
|
UserId: userId,
|
|
|
|
|
Type: types.TaskImage.String(),
|
|
|
|
|
TaskId: params.TaskId,
|
|
|
|
|
Params: utils.JsonEncode(params),
|
|
|
|
|
Prompt: data.Prompt,
|
|
|
|
|
Progress: 0,
|
2024-03-12 15:40:44 +08:00
|
|
|
|
Power: h.App.SysConfig.SdPower,
|
2023-09-28 18:09:45 +08:00
|
|
|
|
CreatedAt: time.Now(),
|
|
|
|
|
}
|
2024-03-19 18:25:01 +08:00
|
|
|
|
res := h.DB.Create(&job)
|
2023-09-28 18:09:45 +08:00
|
|
|
|
if res.Error != nil {
|
|
|
|
|
resp.ERROR(c, "error with save job: "+res.Error.Error())
|
|
|
|
|
return
|
|
|
|
|
}
|
|
|
|
|
|
2023-12-14 16:48:54 +08:00
|
|
|
|
h.pool.PushTask(types.SdTask{
|
2023-09-28 18:09:45 +08:00
|
|
|
|
Id: int(job.Id),
|
|
|
|
|
SessionId: data.SessionId,
|
|
|
|
|
Type: types.TaskImage,
|
|
|
|
|
Params: params,
|
|
|
|
|
UserId: userId,
|
|
|
|
|
})
|
2023-12-14 16:48:54 +08:00
|
|
|
|
|
2024-02-26 15:45:54 +08:00
|
|
|
|
client := h.pool.Clients.Get(uint(job.UserId))
|
|
|
|
|
if client != nil {
|
|
|
|
|
_ = client.Send([]byte("Task Updated"))
|
|
|
|
|
}
|
|
|
|
|
|
2024-03-12 15:40:44 +08:00
|
|
|
|
// update user's power
|
2024-03-19 18:25:01 +08:00
|
|
|
|
tx := h.DB.Model(&model.User{}).Where("id = ?", job.UserId).UpdateColumn("power", gorm.Expr("power - ?", job.Power))
|
2024-03-12 15:40:44 +08:00
|
|
|
|
// 记录算力变化日志
|
|
|
|
|
if tx.Error == nil && tx.RowsAffected > 0 {
|
2024-03-19 18:25:01 +08:00
|
|
|
|
user, _ := h.GetLoginUser(c)
|
|
|
|
|
h.DB.Create(&model.PowerLog{
|
2024-03-12 15:40:44 +08:00
|
|
|
|
UserId: user.Id,
|
|
|
|
|
Username: user.Username,
|
|
|
|
|
Type: types.PowerConsume,
|
|
|
|
|
Amount: job.Power,
|
|
|
|
|
Balance: user.Power - job.Power,
|
|
|
|
|
Mark: types.PowerSub,
|
|
|
|
|
Model: "stable-diffusion",
|
|
|
|
|
Remark: fmt.Sprintf("绘图操作,任务ID:%s", job.TaskId),
|
|
|
|
|
CreatedAt: time.Now(),
|
|
|
|
|
})
|
|
|
|
|
}
|
2023-12-21 08:58:24 +08:00
|
|
|
|
|
2023-09-28 18:09:45 +08:00
|
|
|
|
resp.SUCCESS(c)
|
|
|
|
|
}
|
|
|
|
|
|
2024-03-03 10:40:32 +08:00
|
|
|
|
// ImgWall 照片墙
|
|
|
|
|
func (h *SdJobHandler) ImgWall(c *gin.Context) {
|
|
|
|
|
page := h.GetInt(c, "page", 0)
|
|
|
|
|
pageSize := h.GetInt(c, "page_size", 0)
|
|
|
|
|
err, jobs := h.getData(true, 0, page, pageSize, true)
|
|
|
|
|
if err != nil {
|
|
|
|
|
resp.ERROR(c, err.Error())
|
|
|
|
|
return
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
resp.SUCCESS(c, jobs)
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// JobList 获取 SD 任务列表
|
2023-09-28 18:09:45 +08:00
|
|
|
|
func (h *SdJobHandler) JobList(c *gin.Context) {
|
2024-03-03 10:40:32 +08:00
|
|
|
|
status := h.GetBool(c, "status")
|
|
|
|
|
userId := h.GetLoginUserId(c)
|
2023-10-13 15:16:40 +08:00
|
|
|
|
page := h.GetInt(c, "page", 0)
|
|
|
|
|
pageSize := h.GetInt(c, "page_size", 0)
|
2024-01-19 06:52:23 +08:00
|
|
|
|
publish := h.GetBool(c, "publish")
|
2023-10-13 15:16:40 +08:00
|
|
|
|
|
2024-03-03 10:40:32 +08:00
|
|
|
|
err, jobs := h.getData(status, userId, page, pageSize, publish)
|
|
|
|
|
if err != nil {
|
|
|
|
|
resp.ERROR(c, err.Error())
|
|
|
|
|
return
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
resp.SUCCESS(c, jobs)
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// JobList 获取 MJ 任务列表
|
|
|
|
|
func (h *SdJobHandler) getData(finish bool, userId uint, page int, pageSize int, publish bool) (error, []vo.SdJob) {
|
|
|
|
|
|
2024-03-19 18:25:01 +08:00
|
|
|
|
session := h.DB.Session(&gorm.Session{})
|
2024-03-03 10:40:32 +08:00
|
|
|
|
if finish {
|
2023-10-13 15:16:40 +08:00
|
|
|
|
session = session.Where("progress = ?", 100).Order("id DESC")
|
2023-09-28 18:09:45 +08:00
|
|
|
|
} else {
|
2023-10-13 15:16:40 +08:00
|
|
|
|
session = session.Where("progress < ?", 100).Order("id ASC")
|
|
|
|
|
}
|
|
|
|
|
if userId > 0 {
|
|
|
|
|
session = session.Where("user_id = ?", userId)
|
2023-09-28 18:09:45 +08:00
|
|
|
|
}
|
2024-01-19 06:52:23 +08:00
|
|
|
|
if publish {
|
|
|
|
|
session = session.Where("publish", publish)
|
|
|
|
|
}
|
2023-10-13 15:16:40 +08:00
|
|
|
|
if page > 0 && pageSize > 0 {
|
|
|
|
|
offset := (page - 1) * pageSize
|
|
|
|
|
session = session.Offset(offset).Limit(pageSize)
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
var items []model.SdJob
|
|
|
|
|
res := session.Find(&items)
|
2023-09-28 18:09:45 +08:00
|
|
|
|
if res.Error != nil {
|
2024-03-03 10:40:32 +08:00
|
|
|
|
return res.Error, nil
|
2023-09-28 18:09:45 +08:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
var jobs = make([]vo.SdJob, 0)
|
|
|
|
|
for _, item := range items {
|
|
|
|
|
var job vo.SdJob
|
|
|
|
|
err := utils.CopyObject(item, &job)
|
|
|
|
|
if err != nil {
|
|
|
|
|
continue
|
|
|
|
|
}
|
2023-12-14 16:48:54 +08:00
|
|
|
|
|
2023-09-28 18:09:45 +08:00
|
|
|
|
if item.Progress < 100 {
|
2024-04-02 17:24:38 +08:00
|
|
|
|
// 从 leveldb 中获取图片预览数据
|
2024-04-23 18:46:32 +08:00
|
|
|
|
var imageData string
|
|
|
|
|
err = h.leveldb.Get(item.TaskId, &imageData)
|
2023-12-14 16:48:54 +08:00
|
|
|
|
if err == nil {
|
2024-04-23 18:46:32 +08:00
|
|
|
|
job.ImgURL = "data:image/png;base64," + imageData
|
2023-12-14 16:48:54 +08:00
|
|
|
|
}
|
2023-09-28 18:09:45 +08:00
|
|
|
|
}
|
|
|
|
|
jobs = append(jobs, job)
|
|
|
|
|
}
|
2024-03-03 10:40:32 +08:00
|
|
|
|
|
|
|
|
|
return nil, jobs
|
2023-09-28 18:09:45 +08:00
|
|
|
|
}
|
2023-12-18 17:44:52 +08:00
|
|
|
|
|
|
|
|
|
// Remove remove task image
|
|
|
|
|
func (h *SdJobHandler) Remove(c *gin.Context) {
|
|
|
|
|
var data struct {
|
|
|
|
|
Id uint `json:"id"`
|
2024-02-26 15:45:54 +08:00
|
|
|
|
UserId uint `json:"user_id"`
|
2023-12-18 17:44:52 +08:00
|
|
|
|
ImgURL string `json:"img_url"`
|
|
|
|
|
}
|
|
|
|
|
if err := c.ShouldBindJSON(&data); err != nil {
|
|
|
|
|
resp.ERROR(c, types.InvalidArgs)
|
|
|
|
|
return
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// remove job recode
|
2024-03-19 18:25:01 +08:00
|
|
|
|
res := h.DB.Delete(&model.SdJob{Id: data.Id})
|
2023-12-18 17:44:52 +08:00
|
|
|
|
if res.Error != nil {
|
|
|
|
|
resp.ERROR(c, res.Error.Error())
|
|
|
|
|
return
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// remove image
|
|
|
|
|
err := h.uploader.GetUploadHandler().Delete(data.ImgURL)
|
|
|
|
|
if err != nil {
|
|
|
|
|
logger.Error("remove image failed: ", err)
|
|
|
|
|
}
|
|
|
|
|
|
2024-02-26 15:45:54 +08:00
|
|
|
|
client := h.pool.Clients.Get(data.UserId)
|
|
|
|
|
if client != nil {
|
2024-04-21 20:23:47 +08:00
|
|
|
|
_ = client.Send([]byte(sd.Finished))
|
2024-02-26 15:45:54 +08:00
|
|
|
|
}
|
|
|
|
|
|
2023-12-18 17:44:52 +08:00
|
|
|
|
resp.SUCCESS(c)
|
|
|
|
|
}
|
2024-01-19 06:52:23 +08:00
|
|
|
|
|
|
|
|
|
// Publish 发布/取消发布图片到画廊显示
|
|
|
|
|
func (h *SdJobHandler) Publish(c *gin.Context) {
|
|
|
|
|
var data struct {
|
|
|
|
|
Id uint `json:"id"`
|
|
|
|
|
Action bool `json:"action"` // 发布动作,true => 发布,false => 取消分享
|
|
|
|
|
}
|
|
|
|
|
if err := c.ShouldBindJSON(&data); err != nil {
|
|
|
|
|
resp.ERROR(c, types.InvalidArgs)
|
|
|
|
|
return
|
|
|
|
|
}
|
|
|
|
|
|
2024-03-19 18:25:01 +08:00
|
|
|
|
res := h.DB.Model(&model.SdJob{Id: data.Id}).UpdateColumn("publish", true)
|
2024-01-19 06:52:23 +08:00
|
|
|
|
if res.Error != nil {
|
|
|
|
|
resp.ERROR(c, "更新数据库失败")
|
|
|
|
|
return
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
resp.SUCCESS(c)
|
|
|
|
|
}
|