[go: up one dir, main page]

Skip to content

Commit

Permalink
feat: 兼容自定义变焦,完善modal操作
Browse files Browse the repository at this point in the history
  • Loading branch information
Calcium-Ion committed Mar 14, 2024
1 parent 614220a commit d704902
Show file tree
Hide file tree
Showing 12 changed files with 147 additions and 110 deletions.
5 changes: 3 additions & 2 deletions common/model-ratio.go
Original file line number Diff line number Diff line change
Expand Up @@ -100,13 +100,14 @@ var DefaultModelPrice = map[string]float64{
"mj_variation": 0.1,
"mj_reroll": 0.1,
"mj_blend": 0.1,
"mj_inpaint": 0.1,
"mj_modal": 0.1,
"mj_zoom": 0.1,
"mj_shorten": 0.1,
"mj_high_variation": 0.1,
"mj_low_variation": 0.1,
"mj_pan": 0.1,
"mj_inpaint_pre": 0,
"mj_inpaint": 0,
"mj_custom_zoom": 0,
"mj_describe": 0.05,
"mj_upscale": 0.05,
"swap_face": 0.05,
Expand Down
6 changes: 4 additions & 2 deletions constant/midjourney.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,8 +13,9 @@ const (
MjActionVariation = "VARIATION"
MjActionReRoll = "REROLL"
MjActionInPaint = "INPAINT"
MjActionInPaintPre = "INPAINT_PRE"
MjActionModal = "MODAL"
MjActionZoom = "ZOOM"
MjActionCustomZoom = "CUSTOM_ZOOM"
MjActionShorten = "SHORTEN"
MjActionHighVariation = "HIGH_VARIATION"
MjActionLowVariation = "LOW_VARIATION"
Expand All @@ -29,9 +30,10 @@ var MidjourneyModel2Action = map[string]string{
"mj_upscale": MjActionUpscale,
"mj_variation": MjActionVariation,
"mj_reroll": MjActionReRoll,
"mj_modal": MjActionModal,
"mj_inpaint": MjActionInPaint,
"mj_inpaint_pre": MjActionInPaintPre,
"mj_zoom": MjActionZoom,
"mj_custom_zoom": MjActionCustomZoom,
"mj_shorten": MjActionShorten,
"mj_high_variation": MjActionHighVariation,
"mj_low_variation": MjActionLowVariation,
Expand Down
2 changes: 2 additions & 0 deletions controller/relay.go
Original file line number Diff line number Diff line change
Expand Up @@ -67,6 +67,8 @@ func RelayMidjourney(c *gin.Context) {
err = relay.RelayMidjourneyNotify(c)
case relayconstant.RelayModeMidjourneyTaskFetch, relayconstant.RelayModeMidjourneyTaskFetchByCondition:
err = relay.RelayMidjourneyTask(c, relayMode)
case relayconstant.RelayModeMidjourneyTaskImageSeed:
err = relay.RelayMidjourneyTaskImageSeed(c)
default:
err = relay.RelayMidjourneySubmit(c, relayMode)
}
Expand Down
5 changes: 5 additions & 0 deletions dto/midjourney.go
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,11 @@ type MidjourneyResponse struct {
Result string `json:"result"`
}

type MidjourneyResponseWithStatusCode struct {
StatusCode int `json:"statusCode"`
Response MidjourneyResponse
}

type MidjourneyDto struct {
MjId string `json:"id"`
Action string `json:"action"`
Expand Down
3 changes: 2 additions & 1 deletion middleware/distributor.go
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,8 @@ func Distribute() func(c *gin.Context) {
relayMode := relayconstant.Path2RelayModeMidjourney(c.Request.URL.Path)
if relayMode == relayconstant.RelayModeMidjourneyTaskFetch ||
relayMode == relayconstant.RelayModeMidjourneyTaskFetchByCondition ||
relayMode == relayconstant.RelayModeMidjourneyNotify {
relayMode == relayconstant.RelayModeMidjourneyNotify ||
relayMode == relayconstant.RelayModeMidjourneyTaskImageSeed {
shouldSelectChannel = false
} else {
midjourneyRequest := dto.MidjourneyRequest{}
Expand Down
3 changes: 3 additions & 0 deletions relay/constant/relay_mode.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ const (
RelayModeMidjourneySimpleChange
RelayModeMidjourneyNotify
RelayModeMidjourneyTaskFetch
RelayModeMidjourneyTaskImageSeed
RelayModeMidjourneyTaskFetchByCondition
RelayModeAudioSpeech
RelayModeAudioTranscription
Expand Down Expand Up @@ -77,6 +78,8 @@ func Path2RelayModeMidjourney(path string) int {
relayMode = RelayModeMidjourneyChange
} else if strings.HasSuffix(path, "/fetch") {
relayMode = RelayModeMidjourneyTaskFetch
} else if strings.HasSuffix(path, "/image-seed") {
relayMode = RelayModeMidjourneyTaskImageSeed
} else if strings.HasSuffix(path, "/list-by-condition") {
relayMode = RelayModeMidjourneyTaskFetchByCondition
}
Expand Down
139 changes: 42 additions & 97 deletions relay/relay-mj.go
Original file line number Diff line number Diff line change
Expand Up @@ -138,6 +138,31 @@ func coverMidjourneyTaskDto(c *gin.Context, originTask *model.Midjourney) (midjo
return
}

func RelayMidjourneyTaskImageSeed(c *gin.Context) *dto.MidjourneyResponse {
//taskId := c.Param("id")
//userId := c.GetInt("id")
//originTask := model.GetByMJId(userId, taskId)
//if originTask == nil {
// return service.MidjourneyErrorWrapper(constant.MjRequestError, "task_no_found")
//}
//channel, err := model.GetChannelById(originTask.ChannelId, false)
//if err != nil {
// return service.MidjourneyErrorWrapper(constant.MjRequestError, "get_channel_info_failed")
//}
//if channel.Status != common.ChannelStatusEnabled {
// return service.MidjourneyErrorWrapper(constant.MjRequestError, "该任务所属渠道已被禁用")
//}
//c.Set("channel_id", originTask.ChannelId)
//requestURL := c.Request.URL.String()
//fullRequestURL := fmt.Sprintf("%s%s", channel.GetBaseURL(), requestURL)
//req, err := http.NewRequest(c.Request.Method, fullRequestURL, c.Request.Body)
//if err != nil {
// return service.MidjourneyErrorWrapper(constant.MjRequestError, "create_request_failed")
//}
log.Println("RelayMidjourneyTaskImageSeed")
return nil
}

func RelayMidjourneyTask(c *gin.Context, relayMode int) *dto.MidjourneyResponse {
userId := c.GetInt("id")
var err error
Expand Down Expand Up @@ -259,11 +284,11 @@ func RelayMidjourneySubmit(c *gin.Context, relayMode int) *dto.MidjourneyRespons
mjId = params.TaskId
midjRequest.Action = params.Action
} else if relayMode == relayconstant.RelayModeMidjourneyModal {
if midjRequest.MaskBase64 == "" {
return service.MidjourneyErrorWrapper(constant.MjRequestError, "mask_base64_is_required")
}
//if midjRequest.MaskBase64 == "" {
// return service.MidjourneyErrorWrapper(constant.MjRequestError, "mask_base64_is_required")
//}
mjId = midjRequest.TaskId
midjRequest.Action = constant.MjActionInPaint
midjRequest.Action = constant.MjActionModal
}

originTask := model.GetByMJId(userId, mjId)
Expand Down Expand Up @@ -293,29 +318,10 @@ func RelayMidjourneySubmit(c *gin.Context, relayMode int) *dto.MidjourneyRespons
//}
}

if midjRequest.Action == constant.MjActionInPaintPre {
if midjRequest.Action == constant.MjActionInPaint || midjRequest.Action == constant.MjActionCustomZoom {
consumeQuota = false
}

// map model name
//modelMapping := c.GetString("model_mapping")
//isModelMapped := false
//if modelMapping != "" {
// modelMap := make(map[string]string)
// err := json.Unmarshal([]byte(modelMapping), &modelMap)
// if err != nil {
// //return errorWrapper(err, "unmarshal_model_mapping_failed", http.StatusInternalServerError)
// return &dto.MidjourneyResponse{
// Code: 4,
// Description: "unmarshal_model_mapping_failed",
// }
// }
// if modelMap[imageModel] != "" {
// imageModel = modelMap[imageModel]
// isModelMapped = true
// }
//}

//baseURL := common.ChannelBaseURLs[channelType]
requestURL := c.Request.URL.String()

Expand All @@ -325,20 +331,6 @@ func RelayMidjourneySubmit(c *gin.Context, relayMode int) *dto.MidjourneyRespons

fullRequestURL := fmt.Sprintf("%s%s", baseURL, requestURL)

var requestBody io.Reader
//if isModelMapped {
// jsonStr, err := json.Marshal(midjRequest)
// if err != nil {
// return &dto.MidjourneyResponse{
// Code: 4,
// Description: "marshal_text_request_failed",
// }
// }
// requestBody = bytes.NewBuffer(jsonStr)
//} else {
//}
requestBody = c.Request.Body

modelName := service.CoverActionToModelName(midjRequest.Action)
modelPrice := common.GetModelPrice(modelName, true)
// 如果没有配置价格,则使用默认价格
Expand Down Expand Up @@ -368,40 +360,11 @@ func RelayMidjourneySubmit(c *gin.Context, relayMode int) *dto.MidjourneyRespons
}
}

req, err := http.NewRequest(c.Request.Method, fullRequestURL, requestBody)
if err != nil {
return &dto.MidjourneyResponse{
Code: 4,
Description: "create_request_failed",
}
}
//req.Header.Set("ApiKey", c.Request.Header.Get("ApiKey"))
timeout := time.Second * 30
ctx, cancel := context.WithTimeout(context.Background(), timeout)
// 使用带有超时的 context 创建新的请求
req = req.WithContext(ctx)
req.Header.Set("Content-Type", c.Request.Header.Get("Content-Type"))
req.Header.Set("Accept", c.Request.Header.Get("Accept"))
req.Header.Set("mj-api-secret", strings.Split(c.Request.Header.Get("Authorization"), " ")[1])
// print request header
//log.Printf("request header: %s", req.Header)
//log.Printf("request body: %s", midjRequest.Prompt)

defer cancel()
resp, err := service.GetHttpClient().Do(req)
if err != nil {
return service.MidjourneyErrorWrapper(constant.MjErrorUnknown, "do_request_failed")
}

err = req.Body.Close()
if err != nil {
return service.MidjourneyErrorWrapper(constant.MjErrorUnknown, "close_request_body_failed")
}
err = c.Request.Body.Close()
midjResponseWithStatus, responseBody, err := service.DoMidjourneyHttpRequest(c, time.Second*30, fullRequestURL, &midjRequest)
if err != nil {
return service.MidjourneyErrorWrapper(constant.MjErrorUnknown, "close_request_body_failed")
return &midjResponseWithStatus.Response
}
var midjResponse dto.MidjourneyResponse
midjResponse := &midjResponseWithStatus.Response

defer func(ctx context.Context) {
if consumeQuota {
Expand All @@ -424,25 +387,6 @@ func RelayMidjourneySubmit(c *gin.Context, relayMode int) *dto.MidjourneyRespons
}
}(c.Request.Context())

responseBody, err := io.ReadAll(resp.Body)

if err != nil {
return service.MidjourneyErrorWrapper(constant.MjErrorUnknown, "read_response_body_failed")
}
err = resp.Body.Close()
if err != nil {
return service.MidjourneyErrorWrapper(constant.MjErrorUnknown, "close_response_body_failed")
}
if resp.StatusCode != 200 {
return service.MidjourneyErrorWrapper(constant.MjErrorUnknown, "unexpected_response_status")
}
err = json.Unmarshal(responseBody, &midjResponse)
log.Printf("responseBody: %s", string(responseBody))
log.Printf("midjResponse: %v", midjResponse)
if err != nil {
return service.MidjourneyErrorWrapper(constant.MjErrorUnknown, "unmarshal_response_body_failed")
}

// 文档:https://github.com/novicezk/midjourney-proxy/blob/main/docs/api.md
//1-提交成功
// 21-任务已存在(处理中或者有结果了) {"code":21,"description":"任务已存在","result":"0741798445574458","properties":{"status":"SUCCESS","imageUrl":"https://xxxx"}}
Expand Down Expand Up @@ -494,7 +438,7 @@ func RelayMidjourneySubmit(c *gin.Context, relayMode int) *dto.MidjourneyRespons
}
}
//修改返回值
if midjRequest.Action != constant.MjActionInPaintPre {
if midjRequest.Action != constant.MjActionInPaint && midjRequest.Action != constant.MjActionCustomZoom {
newBody := strings.Replace(string(responseBody), `"code":21`, `"code":1`, -1)
responseBody = []byte(newBody)
}
Expand All @@ -514,21 +458,22 @@ func RelayMidjourneySubmit(c *gin.Context, relayMode int) *dto.MidjourneyRespons
responseBody = []byte(newBody)
}

resp.Body = io.NopCloser(bytes.NewBuffer(responseBody))
//resp.Body = io.NopCloser(bytes.NewBuffer(responseBody))
bodyReader := io.NopCloser(bytes.NewBuffer(responseBody))

for k, v := range resp.Header {
c.Writer.Header().Set(k, v[0])
}
c.Writer.WriteHeader(resp.StatusCode)
//for k, v := range resp.Header {
// c.Writer.Header().Set(k, v[0])
//}
c.Writer.WriteHeader(midjResponseWithStatus.StatusCode)

_, err = io.Copy(c.Writer, resp.Body)
_, err = io.Copy(c.Writer, bodyReader)
if err != nil {
return &dto.MidjourneyResponse{
Code: 4,
Description: "copy_response_body_failed",
}
}
err = resp.Body.Close()
err = bodyReader.Close()
if err != nil {
return &dto.MidjourneyResponse{
Code: 4,
Expand Down
1 change: 1 addition & 0 deletions router/relay-router.go
Original file line number Diff line number Diff line change
Expand Up @@ -57,6 +57,7 @@ func SetRelayRouter(router *gin.Engine) {
relayMjRouter.POST("/submit/blend", controller.RelayMidjourney)
relayMjRouter.POST("/notify", controller.RelayMidjourney)
relayMjRouter.GET("/task/:id/fetch", controller.RelayMidjourney)
relayMjRouter.GET("/task/:id/image-seed", controller.RelayMidjourney)
relayMjRouter.POST("/task/list-by-condition", controller.RelayMidjourney)
}
//relayMjRouter.Use()
Expand Down
7 changes: 7 additions & 0 deletions service/error.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,13 @@ func MidjourneyErrorWrapper(code int, desc string) *dto.MidjourneyResponse {
}
}

func MidjourneyErrorWithStatusCodeWrapper(code int, desc string, statusCode int) *dto.MidjourneyResponseWithStatusCode {
return &dto.MidjourneyResponseWithStatusCode{
StatusCode: statusCode,
Response: *MidjourneyErrorWrapper(code, desc),
}
}

// OpenAIErrorWrapper wraps an error into an OpenAIErrorWithStatusCode
func OpenAIErrorWrapper(err error, code string, statusCode int) *dto.OpenAIErrorWithStatusCode {
text := err.Error()
Expand Down
Loading

0 comments on commit d704902

Please sign in to comment.