🐛 ai初始化 & 错误处理

This commit is contained in:
sugarBecomer
2025-11-11 11:57:50 +08:00
parent 49f4475991
commit 05b9a94d41
7 changed files with 26 additions and 25 deletions
@@ -1,6 +1,7 @@
package classifier
import (
"embed"
_ "embed"
"likedream-goframe/internal/application/recognizer"
"likedream-goframe/internal/framework/logger"
@@ -11,11 +12,8 @@ import (
"go.uber.org/zap"
)
//go:embed static/classes.txt
var classesBytes []byte
//go:embed static/model.onnx
var modelBytes []byte
//go:embed static/*
var fs embed.FS
var imageClassifier *ImageClassifier
func init() {
@@ -39,10 +37,12 @@ func initModel() (modelPath, classesPath string) {
os.Mkdir(staticPath, 0755)
}
modelPath = filepath.Join(staticPath, "model.onnx")
modelBytes, _ := fs.ReadFile("static/model.onnx")
if _, err := os.Stat(modelPath); os.IsNotExist(err) {
os.WriteFile(modelPath, modelBytes, 0644)
}
classesPath = filepath.Join(staticPath, "classes.txt")
classesBytes, _ := fs.ReadFile("static/classes.txt")
if _, err := os.Stat(classesPath); os.IsNotExist(err) {
os.WriteFile(classesPath, classesBytes, 0644)
}
@@ -1,6 +1,7 @@
package ddddocr
import (
"embed"
_ "embed"
"likedream-goframe/internal/application/recognizer"
"os"
@@ -8,8 +9,8 @@ import (
"strings"
)
//go:embed static/common_old.onnx
var commonOldBytes []byte
//go:embed static/*
var fs embed.FS
func initModel() string {
if !recognizer.OnnxIsInit {
@@ -24,8 +25,9 @@ func initModel() string {
os.Mkdir(staticPath, 0755)
}
modelPath := filepath.Join(staticPath, "common_old.onnx")
modelBytes, _ := fs.ReadFile("static/common_old.onnx")
if _, err := os.Stat(modelPath); os.IsNotExist(err) {
os.WriteFile(modelPath, commonOldBytes, 0644)
os.WriteFile(modelPath, modelBytes, 0644)
}
return modelPath
}
+6 -11
View File
@@ -1,6 +1,7 @@
package recognizer
import (
"embed"
_ "embed"
"likedream-goframe/internal/framework/logger"
"os"
@@ -12,14 +13,8 @@ import (
"go.uber.org/zap"
)
//go:embed assets/onnxruntime.dll
var onnxRuntimeFileWindows []byte
//go:embed assets/libonnxruntime.1.23.2.dylib
var onnxRuntimeFileDarwin []byte
//go:embed assets/libonnxruntime.so.1.23.2
var onnxRuntimeFileLinux []byte
//go:embed static/*
var fs embed.FS
// OnnxIsInit 初始化onnx配置
var OnnxIsInit = false
@@ -48,15 +43,15 @@ func getOnnxRuntimeName() (onnxRuntimeFileName string, onnxRuntimeFileBytes []by
switch systemOS {
case "windows":
onnxRuntimeFileName = "onnxruntime.dll"
onnxRuntimeFileBytes = onnxRuntimeFileWindows
onnxRuntimeFileBytes, _ = fs.ReadFile("static/" + onnxRuntimeFileName)
return
case "darwin":
onnxRuntimeFileName = "onnxruntime.dylib"
onnxRuntimeFileBytes = onnxRuntimeFileDarwin
onnxRuntimeFileBytes, _ = fs.ReadFile("static/" + onnxRuntimeFileName)
return
case "linux":
onnxRuntimeFileName = "onnxruntime.so"
onnxRuntimeFileBytes = onnxRuntimeFileLinux
onnxRuntimeFileBytes, _ = fs.ReadFile("static/" + onnxRuntimeFileName)
return
}
logger.Logger.Panic("暂不支持的操作系统:", zap.String("系统名称", systemOS))
+10 -6
View File
@@ -18,17 +18,17 @@ func ExceptionHandler(r *ghttp.Request) {
if r.Response.Status == 404 {
return
}
if r.Response.BufferLength() > 0 {
return
}
if r.Response.Header().Get("Content-Type") == "image/png" {
return
}
var (
err = r.GetError()
res = r.GetHandlerResponse()
code = gerror.Code(err)
)
if err == nil && r.Response.BufferLength() > 0 {
return
}
if r.Response.Header().Get("Content-Type") == "image/png" {
return
}
// 如果没有异常,则直接返回
if err == nil && res != nil {
@@ -49,6 +49,10 @@ func ExceptionHandler(r *ghttp.Request) {
tmpErr := errors.CodeParam
tmpErr.SetMessage((err.(*ent.ConstraintError)).Error())
code = tmpErr.Code()
} else if ent.IsNotFound(err) {
tmpErr := errors.CodeNotFound
tmpErr.SetMessage("数据未找到")
code = tmpErr.Code()
} else if code == gcode.CodeNil {
code = errors.CodeInternal.Code()
} else if code == gcode.CodeValidationFailed {