From d04987302fc7b94a398d9e38190534fab5d51242 Mon Sep 17 00:00:00 2001 From: forDecember <1921567337@qq.com> Date: Thu, 21 Nov 2024 01:26:40 +0800 Subject: [PATCH] =?UTF-8?q?update:=20cat=20face=E6=8E=A5=E5=8F=A3?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- app/http/controller/web/animal_controller.go | 32 +++++++++++++++++++ .../web_register_validator.go | 4 +++ .../validator/web/animal/catface_guess.go | 29 +++++++++++++++++ app/service/catface/catface.go | 32 +++++++++++++++++++ app/service/nlp/glm/glm.go | 11 +++---- routers/web.go | 2 ++ test/python/embedding_test.go | 2 +- 7 files changed, 104 insertions(+), 8 deletions(-) create mode 100644 app/http/validator/web/animal/catface_guess.go create mode 100644 app/service/catface/catface.go diff --git a/app/http/controller/web/animal_controller.go b/app/http/controller/web/animal_controller.go index 496882c..804834a 100644 --- a/app/http/controller/web/animal_controller.go +++ b/app/http/controller/web/animal_controller.go @@ -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") diff --git a/app/http/validator/common/register_validator/web_register_validator.go b/app/http/validator/common/register_validator/web_register_validator.go index 50e9c1e..323d407 100644 --- a/app/http/validator/common/register_validator/web_register_validator.go +++ b/app/http/validator/common/register_validator/web_register_validator.go @@ -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" diff --git a/app/http/validator/web/animal/catface_guess.go b/app/http/validator/web/animal/catface_guess.go new file mode 100644 index 0000000..d353471 --- /dev/null +++ b/app/http/validator/web/animal/catface_guess.go @@ -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) + } +} diff --git a/app/service/catface/catface.go b/app/service/catface/catface.go new file mode 100644 index 0000000..03c5ad4 --- /dev/null +++ b/app/service/catface/catface.go @@ -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 +} diff --git a/app/service/nlp/glm/glm.go b/app/service/nlp/glm/glm.go index 29ea303..6ff80f8 100644 --- a/app/service/nlp/glm/glm.go +++ b/app/service/nlp/glm/glm.go @@ -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()) diff --git a/routers/web.go b/routers/web.go index 6e04783..8d1d305 100644 --- a/routers/web.go +++ b/routers/web.go @@ -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 检查 diff --git a/test/python/embedding_test.go b/test/python/embedding_test.go index 6303d99..08dbb53 100644 --- a/test/python/embedding_test.go +++ b/test/python/embedding_test.go @@ -8,7 +8,7 @@ import ( ) func TestEmbeddingApi(t *testing.T) { - res, ok := nlp.GetEmbedding("一段测试文本。") + res, ok := nlp.GetEmbedding([]string{"一段测试文本。"}) if !ok { t.Error("获取嵌入向量失败") }