pcm-modelarts/internal/logic/modelartsservice/imagereasoningurllogic.go

79 lines
2.2 KiB
Go

package modelartsservicelogic
import (
"bytes"
"context"
"fmt"
"github.com/zeromicro/go-zero/core/logx"
"gitlink.org.cn/JointCloud/pcm-modelarts/internal/svc"
"gitlink.org.cn/JointCloud/pcm-modelarts/internal/util"
"gitlink.org.cn/JointCloud/pcm-modelarts/modelarts"
"k8s.io/apimachinery/pkg/util/json"
"strconv"
)
type ImageReasoningUrlLogic struct {
ctx context.Context
svcCtx *svc.ServiceContext
logx.Logger
}
func NewImageReasoningUrlLogic(ctx context.Context, svcCtx *svc.ServiceContext) *ImageReasoningUrlLogic {
return &ImageReasoningUrlLogic{
ctx: ctx,
svcCtx: svcCtx,
Logger: logx.WithContext(ctx),
}
}
type ServicesResponse struct {
Services []Service `json:"services"`
TotalCount int `json:"total_count"`
ErrorMsg string `json:"error_msg"`
}
type Service struct {
ServiceID string `json:"service_id"`
IsFree bool `json:"is_free"`
}
func (l *ImageReasoningUrlLogic) ImageReasoningUrl(in *modelarts.ImageReasoningUrlReq) (*modelarts.ImageReasoningUrlResp, error) {
// todo: add your logic here and delete this line
var resp modelarts.ImageReasoningUrlResp
judgeLimit := strconv.Itoa(int(1))
offset := strconv.Itoa(int(0))
var limit string
if judgeLimit != "0" {
limit = strconv.Itoa(int(1))
} else {
limit = "10"
}
platform, err := util.GetModelArtsConfWithPlatform("modelarts-CloudBrain2")
if err != nil {
return nil, err
}
ServiceName := in.ModelName + in.Type + in.Card
body, err := util.SendRequest("GET", platform.Endpoint+"v1/"+platform.ProjectId+"/services?offset="+offset+"&"+"limit="+limit+"&"+"serviceName"+ServiceName,
bytes.NewBuffer([]byte("foo=bar")), "modelarts-CloudBrain2")
if err != nil {
return nil, err
}
// 解析JSON到结构体
var response ServicesResponse
errs := json.Unmarshal(*body, &response)
if errs != nil {
fmt.Println("Error parsing JSON:", err)
}
if len(response.Services) > 0 {
fmt.Println("Service ID:", response.Services[0].ServiceID)
ServiceID := response.Services[0].ServiceID
url := platform.Endpoint + "v1/infers/" + ServiceID + "/image"
resp.Url = url
resp.Code = "200"
resp.Message = "success"
} else {
fmt.Println("No services found in the response.")
}
return &resp, nil
}