feat: update url

This commit is contained in:
2025-12-15 23:29:28 +08:00
parent 50922641a9
commit 9ceb5fe92a
3 changed files with 26 additions and 11 deletions

View File

@@ -11,3 +11,8 @@ type GetFormulaTaskResponse struct {
Count int `json:"count"` Count int `json:"count"`
Latex string `json:"latex"` Latex string `json:"latex"`
} }
// FormulaRecognitionResponse 公式识别服务返回的响应
type FormulaRecognitionResponse struct {
Result string `json:"result"`
}

View File

@@ -263,11 +263,11 @@ func (s *RecognitionService) processFormulaTask(ctx context.Context, taskID int6
return err return err
} }
downloadURL, err := oss.GetDownloadURL(ctx, fileURL) // downloadURL, err := oss.GetDownloadURL(ctx, fileURL)
if err != nil { // if err != nil {
log.Error(ctx, "func", "processFormulaTask", "msg", "获取下载URL失败", "error", err) // log.Error(ctx, "func", "processFormulaTask", "msg", "获取下载URL失败", "error", err)
return err // return err
} // }
// 将图片转为base64编码 // 将图片转为base64编码
base64Image := base64.StdEncoding.EncodeToString(imageData) base64Image := base64.StdEncoding.EncodeToString(imageData)
@@ -275,7 +275,6 @@ func (s *RecognitionService) processFormulaTask(ctx context.Context, taskID int6
// 创建JSON请求 // 创建JSON请求
requestData := map[string]string{ requestData := map[string]string{
"image_base64": base64Image, "image_base64": base64Image,
"img_url": downloadURL,
} }
jsonData, err := json.Marshal(requestData) 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)} headers := map[string]string{"Content-Type": "application/json", utils.RequestIDHeaderKey: utils.GetRequestIDFromContext(ctx)}
// 发送请求时会使用带超时的context // 发送请求时会使用带超时的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 err != nil {
if ctx.Err() == context.DeadlineExceeded { if ctx.Err() == context.DeadlineExceeded {
log.Error(ctx, "func", "processFormulaTask", "msg", "请求超时") log.Error(ctx, "func", "processFormulaTask", "msg", "请求超时")
@@ -299,13 +298,21 @@ func (s *RecognitionService) processFormulaTask(ctx context.Context, taskID int6
} }
defer resp.Body.Close() defer resp.Body.Close()
log.Info(ctx, "func", "processFormulaTask", "msg", "请求成功", "resp", resp.Body) log.Info(ctx, "func", "processFormulaTask", "msg", "请求成功")
body := &bytes.Buffer{} body := &bytes.Buffer{}
if _, err = body.ReadFrom(resp.Body); err != nil { if _, err = body.ReadFrom(resp.Body); err != nil {
log.Error(ctx, "func", "processFormulaTask", "msg", "读取响应体失败", "error", err) log.Error(ctx, "func", "processFormulaTask", "msg", "读取响应体失败", "error", err)
return 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} content := &dao.FormulaRecognitionContent{Latex: katex}
b, _ := json.Marshal(content) b, _ := json.Marshal(content)
// Save recognition result // Save recognition result
@@ -500,8 +507,9 @@ func (s *RecognitionService) processOneTask(ctx context.Context) {
ctx = context.WithValue(ctx, utils.RequestIDKey, task.TaskUUID) ctx = context.WithValue(ctx, utils.RequestIDKey, task.TaskUUID)
log.Info(ctx, "func", "processFormulaQueue", "msg", "获取任务成功", "task_id", taskID) 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) log.Error(ctx, "func", "processFormulaQueue", "msg", "处理任务失败", "error", err)
return return
} }

View File

@@ -23,6 +23,8 @@ func rmDollarSurr(text string) string {
func ToKatex(formula string) string { func ToKatex(formula string) string {
res := formula res := formula
res = strings.ReplaceAll(res, "\n", "")
// Remove mbox surrounding // Remove mbox surrounding
res = changeAll(res, `\mbox `, " ", "{", "}", "", "") res = changeAll(res, `\mbox `, " ", "{", "}", "", "")
res = changeAll(res, `\mbox`, " ", "{", "}", "", "") res = changeAll(res, `\mbox`, " ", "{", "}", "", "")