update: cat face接口

This commit is contained in:
forDecember 2024-11-21 01:26:40 +08:00
parent 1ca75f6567
commit d04987302f
No known key found for this signature in database
GPG Key ID: 3AE052A6E085438C
7 changed files with 104 additions and 8 deletions

View File

@ -9,6 +9,7 @@ import (
"catface/app/model_es"
"catface/app/model_redis"
"catface/app/service/animals/curd"
"catface/app/service/catface"
"catface/app/service/upload_file"
"catface/app/utils/query_handler"
"catface/app/utils/response"
@ -22,6 +23,37 @@ import (
type Animals struct { // INFO 起到一个标记的作用,这样 web.xxx 的时候不同模块就不会命名冲突了。
}
func (a *Animals) Guess(context *gin.Context) {
// 1. Get Params
filePath := context.GetString(consts.ValidatorPrefix + "file_path")
// 2. Get Result
catRes := catface.GetCatfaceResult(filePath)
// 3. Response
type subT struct {
Id int64 `json:"id"`
Name string `json:"name"`
Status uint8 `json:"status"`
Department uint8 `json:"department"`
}
type t struct {
List []subT `json:"list"`
}
var resList t
for _, v := range catRes.Cats {
resList.List = append(resList.List, subT{
Id: v.Id,
Name: model.CreateAnimalFactory("").ShowByID(v.Id).Name,
Status: model.CreateAnimalFactory("").ShowByID(v.Id).Status,
Department: model.CreateAnimalFactory("").ShowByID(v.Id).Department,
})
}
response.Success(context, consts.CurdStatusOkMsg, resList)
}
func (a *Animals) List(context *gin.Context) {
// 1. Get Params
attrs := context.GetString(consts.ValidatorPrefix + "attrs")

View File

@ -64,6 +64,10 @@ func WebRegisterValidator() {
key = consts.ValidatorPrefix + "AnimalName"
containers.Set(key, animal.Name{})
// +cat face
key = consts.ValidatorPrefix + "AnimalCatfaceGuess"
containers.Set(key, animal.CatfaceGuess{})
key = consts.ValidatorPrefix + "AnimalLikeCreate"
containers.Set(key, animal_like.Create{})
key = consts.ValidatorPrefix + "AnimalLikeDelete"

View File

@ -0,0 +1,29 @@
package animal
import (
"catface/app/global/consts"
"catface/app/http/controller/web"
"catface/app/http/validator/core/data_transfer"
"catface/app/utils/response"
"github.com/gin-gonic/gin"
)
type CatfaceGuess struct {
}
func (c CatfaceGuess) CheckParams(context *gin.Context) {
if err := context.ShouldBind(&c); err != nil {
// 将表单参数验证器出现的错误直接交给错误翻译器统一处理即可
response.ValidatorError(context, err)
return
}
// 该函数主要是将本结构体的字段(成员)按照 consts.ValidatorPrefix+ json标签对应的 键 => 值 形式绑定在上下文,便于下一步(控制器)可以直接通过 context.Get(键) 获取相关值
extraAddBindDataContext := data_transfer.DataAddContext(c, consts.ValidatorPrefix, context)
if extraAddBindDataContext == nil {
response.ErrorSystem(context, "CatfaceGuess表单验证器json化失败", "")
} else {
// 验证完成,调用控制器,并将验证器成员(字段)递给控制器,保持上下文数据一致性
(&web.Animals{}).Guess(extraAddBindDataContext)
}
}

View File

@ -0,0 +1,32 @@
package catface
import (
"catface/app/global/variable"
"catface/app/utils/micro_service"
"context"
"github.com/carlmjohnson/requests"
)
type FaceRes struct {
FaceBreed int `json:"face_breed"`
Cats []struct {
Id int64 `json:"id"`
Prob float64 `json:"prob"`
} `json:"cats"`
}
func GetCatfaceResult(filePath string) FaceRes {
body := map[string]interface{}{
"file_path": filePath,
}
var res FaceRes
err := requests.URL(micro_service.FetchPythonServiceUrl("cnn/detect_cat")).
BodyJSON(&body).
ToJSON(&res).
Fetch(context.Background())
if err != nil {
variable.ZapLog.Error("获取cat face结果集失败: " + err.Error())
}
return res
}

View File

@ -1,15 +1,12 @@
package glm
import (
"catface/app/global/variable"
"context"
"errors"
"fmt"
"strings"
"time"
"github.com/yankeguo/zhipu"
"go.uber.org/zap"
)
// ChatWithGLM 封装了与GLM模型进行对话的逻辑
@ -40,10 +37,10 @@ func ChatStream(message string, ch chan<- string, client *zhipu.ChatCompletionSe
})
// Test
messages := client.GetMessages()
for id, message := range messages {
variable.ZapLog.Info(fmt.Sprintf("message-%d", id+1), zap.String("message", message.(zhipu.ChatCompletionMessage).Role), zap.String("content", message.(zhipu.ChatCompletionMessage).Content))
}
//messages := client.GetMessages()
//for id, message := range messages {
// variable.ZapLog.Info(fmt.Sprintf("message-%d", id+1), zap.String("message", message.(zhipu.ChatCompletionMessage).Role), zap.String("content", message.(zhipu.ChatCompletionMessage).Content))
//}
// 执行服务调用
res, err := service.Do(context.Background())

View File

@ -128,6 +128,8 @@ func InitWebRouter() *gin.Engine {
animal.POST("like", validatorFactory.Create(consts.ValidatorPrefix+"AnimalLikeCreate"))
animal.DELETE("like", validatorFactory.Create(consts.ValidatorPrefix+"AnimalLikeDelete"))
animal.POST("catface", validatorFactory.Create(consts.ValidatorPrefix+"AnimalCatfaceGuess"))
}
// backend.Use(authorization.CheckTokenAuth()) // INFO token 检查

View File

@ -8,7 +8,7 @@ import (
)
func TestEmbeddingApi(t *testing.T) {
res, ok := nlp.GetEmbedding("一段测试文本。")
res, ok := nlp.GetEmbedding([]string{"一段测试文本。"})
if !ok {
t.Error("获取嵌入向量失败")
}