diff --git a/internal/model/formula/response.go b/internal/model/formula/response.go index 9b76dc8..923943d 100644 --- a/internal/model/formula/response.go +++ b/internal/model/formula/response.go @@ -11,3 +11,8 @@ type GetFormulaTaskResponse struct { Count int `json:"count"` Latex string `json:"latex"` } + +// FormulaRecognitionResponse 公式识别服务返回的响应 +type FormulaRecognitionResponse struct { + Result string `json:"result"` +} diff --git a/internal/service/recognition_service.go b/internal/service/recognition_service.go index 8283f19..6af1c17 100644 --- a/internal/service/recognition_service.go +++ b/internal/service/recognition_service.go @@ -263,11 +263,11 @@ func (s *RecognitionService) processFormulaTask(ctx context.Context, taskID int6 return err } - downloadURL, err := oss.GetDownloadURL(ctx, fileURL) - if err != nil { - log.Error(ctx, "func", "processFormulaTask", "msg", "获取下载URL失败", "error", err) - return err - } + // downloadURL, err := oss.GetDownloadURL(ctx, fileURL) + // if err != nil { + // log.Error(ctx, "func", "processFormulaTask", "msg", "获取下载URL失败", "error", err) + // return err + // } // 将图片转为base64编码 base64Image := base64.StdEncoding.EncodeToString(imageData) @@ -275,7 +275,6 @@ func (s *RecognitionService) processFormulaTask(ctx context.Context, taskID int6 // 创建JSON请求 requestData := map[string]string{ "image_base64": base64Image, - "img_url": downloadURL, } jsonData, err := json.Marshal(requestData) @@ -288,7 +287,7 @@ func (s *RecognitionService) processFormulaTask(ctx context.Context, taskID int6 headers := map[string]string{"Content-Type": "application/json", utils.RequestIDHeaderKey: utils.GetRequestIDFromContext(ctx)} // 发送请求时会使用带超时的context - resp, err := s.httpClient.RequestWithRetry(ctx, http.MethodPost, "", bytes.NewReader(jsonData), headers) + resp, err := s.httpClient.RequestWithRetry(ctx, http.MethodPost, "http://cloud.texpixel.com:1080/formula/predict", bytes.NewReader(jsonData), headers) if err != nil { if ctx.Err() == context.DeadlineExceeded { log.Error(ctx, "func", "processFormulaTask", "msg", "请求超时") @@ -299,13 +298,21 @@ func (s *RecognitionService) processFormulaTask(ctx context.Context, taskID int6 } defer resp.Body.Close() - log.Info(ctx, "func", "processFormulaTask", "msg", "请求成功", "resp", resp.Body) + log.Info(ctx, "func", "processFormulaTask", "msg", "请求成功") body := &bytes.Buffer{} if _, err = body.ReadFrom(resp.Body); err != nil { log.Error(ctx, "func", "processFormulaTask", "msg", "读取响应体失败", "error", err) return err } - katex := utils.ToKatex(body.String()) + log.Info(ctx, "func", "processFormulaTask", "msg", "响应内容", "body", body.String()) + + // 解析 JSON 响应 + var formulaResp formula.FormulaRecognitionResponse + if err := json.Unmarshal(body.Bytes(), &formulaResp); err != nil { + log.Error(ctx, "func", "processFormulaTask", "msg", "解析响应JSON失败", "error", err) + return err + } + katex := utils.ToKatex(formulaResp.Result) content := &dao.FormulaRecognitionContent{Latex: katex} b, _ := json.Marshal(content) // Save recognition result @@ -500,8 +507,9 @@ func (s *RecognitionService) processOneTask(ctx context.Context) { ctx = context.WithValue(ctx, utils.RequestIDKey, task.TaskUUID) log.Info(ctx, "func", "processFormulaQueue", "msg", "获取任务成功", "task_id", taskID) - // 处理具体任务 - if err := s.processVLFormulaTask(ctx, taskID, task.FileURL, utils.ModelVLQwen3VL32BInstruct); err != nil { + // 处理任务 + err = s.processFormulaTask(ctx, taskID, task.FileURL) + if err != nil { log.Error(ctx, "func", "processFormulaQueue", "msg", "处理任务失败", "error", err) return } diff --git a/pkg/utils/katex.go b/pkg/utils/katex.go index ba6e76d..c05e6ba 100644 --- a/pkg/utils/katex.go +++ b/pkg/utils/katex.go @@ -23,6 +23,8 @@ func rmDollarSurr(text string) string { func ToKatex(formula string) string { res := formula + res = strings.ReplaceAll(res, "\n", "") + // Remove mbox surrounding res = changeAll(res, `\mbox `, " ", "{", "}", "", "") res = changeAll(res, `\mbox`, " ", "{", "}", "", "")