image.vision\ocr\ocr_deployment.mbt
85.7% · 78 / 91 points · 424 linesBackground color reports the state of each line. Underlined characters mark exact instrumented byte offsets; hover them to inspect the visit count.
- 1
///| - 2
/// # OCR 部署 ocr/ocr_deployment - 3
/// - 4
/// 图像处理库子模块 - 5
/// 图像处理库子模块 Image processing library submodule - 6
// v13.6.0 OCR 部署工具(纯 MoonBit 配置生成/CLI/打包) - 7
// Dockerfile 模板 / API Server 配置 / CLI 参数解析 / 模型打包 - 8
- 9
///| - 10
/// 简易字符串转整数 - 11
fn parse_int_str(s : String) -> Int32 { - 12●
let mut result = 0 - 13
let mut sign = 1 - 14●
let start = if s.length() > 0 && s[0] == '-' { - 15○
sign = -1 - 16
1 - 17
} else { - 18●
0 - 19
} - 20●
for i in start..<s.length() { - 21●
let c = s[i] - 22
if c >= '0' && c <= '9' { - 23●
result = result * 10 + (c.to_int() - '0'.to_int()) - 24
} - 25
} - 26
sign * result - 27
} - 28
- 29
///| - 30
/// 部署环境类型 - 31
/// `#non_exhaustive` 表示未来可能添加新构造器 - 32
#derive(Eq, @debug.Debug) - 33
#non_exhaustive - 34
pub(all) enum DeploymentEnvironment { - 35
Development - 36
Staging - 37
Production - 38
} - 39
- 40
///| - 41
pub extend DeploymentEnvironment with @debug.Debug::{to_repr} - 42
- 43
///| - 44
pub extend DeploymentEnvironment with Eq::{not_equal, equal} - 45
- 46
///| - 47
/// CLI 配置 - 48
#derive(Eq, @debug.Debug) - 49
pub(all) struct CliConfig { - 50
input_path : String - 51
output_path : String - 52
output_format : String - 53
language : String - 54
enable_handwriting : Bool - 55
enable_multilingual : Bool - 56
enable_document_analysis : Bool - 57
verbose : Bool - 58
batch_size : Int32 - 59
} - 60
- 61
///| - 62
pub extend CliConfig with Eq::{not_equal, equal} - 63
- 64
///| - 65
pub extend CliConfig with @debug.Debug::{to_repr} - 66
- 67
///| - 68
/// API Server 配置 - 69
#derive(Eq, @debug.Debug) - 70
pub(all) struct ApiServerConfig { - 71
host : String - 72
port : Int32 - 73
max_concurrent_requests : Int32 - 74
request_timeout_ms : Int32 - 75
max_image_size_mb : Int32 - 76
enable_cors : Bool - 77
auth_token : String - 78
rate_limit_per_minute : Int32 - 79
} - 80
- 81
///| - 82
pub extend ApiServerConfig with Eq::{not_equal, equal} - 83
- 84
///| - 85
pub extend ApiServerConfig with @debug.Debug::{to_repr} - 86
- 87
///| - 88
/// Docker 配置 - 89
#derive(Eq, @debug.Debug) - 90
pub(all) struct DockerConfig { - 91
base_image : String - 92
app_port : Int32 - 93
memory_limit_mb : Int32 - 94
cpu_limit : Double - 95
enable_gpu : Bool - 96
environment : DeploymentEnvironment - 97
volumes : Array[(String, String)] - 98
} - 99
- 100
///| - 101
pub extend DockerConfig with Eq::{not_equal, equal} - 102
- 103
///| - 104
pub extend DockerConfig with @debug.Debug::{to_repr} - 105
- 106
///| - 107
/// 模型打包配置 - 108
#derive(Eq, @debug.Debug) - 109
pub(all) struct ModelPackageConfig { - 110
model_name : String - 111
version : String - 112
model_path : String - 113
labels_path : String - 114
input_width : Int32 - 115
input_height : Int32 - 116
num_classes : Int32 - 117
compression : String - 118
} - 119
- 120
///| - 121
pub extend ModelPackageConfig with Eq::{not_equal, equal} - 122
- 123
///| - 124
pub extend ModelPackageConfig with @debug.Debug::{to_repr} - 125
- 126
///| - 127
/// 创建默认 CLI 配置 - 128
pub fn create_default_cli_config() -> CliConfig { - 129●
CliConfig::{ - 130
input_path: "", - 131
output_path: "output.txt", - 132
output_format: "text", - 133
language: "auto", - 134
enable_handwriting: false, - 135
enable_multilingual: true, - 136
enable_document_analysis: false, - 137
verbose: false, - 138
batch_size: 1, - 139
} - 140
} - 141
- 142
///| - 143
/// 创建默认 API Server 配置 - 144
pub fn create_default_api_server_config() -> ApiServerConfig { - 145●
ApiServerConfig::{ - 146
host: "0.0.0.0", - 147
port: 8080, - 148
max_concurrent_requests: 10, - 149
request_timeout_ms: 30000, - 150
max_image_size_mb: 16, - 151
enable_cors: true, - 152
auth_token: "", - 153
rate_limit_per_minute: 60, - 154
} - 155
} - 156
- 157
///| - 158
/// 创建默认 Docker 配置 - 159
pub fn create_default_docker_config() -> DockerConfig { - 160●
DockerConfig::{ - 161
base_image: "moonbit:latest", - 162
app_port: 8080, - 163
memory_limit_mb: 512, - 164
cpu_limit: 1.0, - 165
enable_gpu: false, - 166
environment: DeploymentEnvironment::Production, - 167
volumes: [], - 168
} - 169
} - 170
- 171
///| - 172
/// 创建默认模型打包配置 - 173
pub fn create_default_model_package_config() -> ModelPackageConfig { - 174●
ModelPackageConfig::{ - 175
model_name: "ocr_model", - 176
version: "1.0.0", - 177
model_path: "./models/ocr_model.bin", - 178
labels_path: "./models/labels.txt", - 179
input_width: 32, - 180
input_height: 32, - 181
num_classes: 10, - 182
compression: "none", - 183
} - 184
} - 185
- 186
///| - 187
/// 生成 Dockerfile 内容 - 188
pub fn generate_dockerfile(config : DockerConfig) -> String { - 189●
let mut dockerfile = "FROM " + config.base_image + "\n\n" - 190
dockerfile = dockerfile + "WORKDIR /app\n\n" - 191
dockerfile = dockerfile + "COPY . .\n\n" - 192●
dockerfile = dockerfile + "EXPOSE " + config.app_port.to_string() + "\n\n" - 193
dockerfile = dockerfile + - 194
"CMD [\"moon\", \"run\", \"--target\", \"native\"]\n" - 195
if config.memory_limit_mb > 0 { - 196●
dockerfile = dockerfile + - 197
"\n# Memory limit: " + - 198●
config.memory_limit_mb.to_string() + - 199
"MB\n" - 200
} - 201
if config.cpu_limit > 0.0 { - 202●
dockerfile = dockerfile + - 203
"# CPU limit: " + - 204●
config.cpu_limit.to_string() + - 205
"\n" - 206
} - 207
if config.enable_gpu { - 208○
dockerfile = dockerfile + "# GPU enabled\n" - 209
} - 210
dockerfile - 211
} - 212
- 213
///| - 214
/// 生成 docker-compose.yml 内容 - 215
pub fn generate_docker_compose(config : DockerConfig) -> String { - 216●
let mut compose = "version: '3.8'\n\n" - 217
compose = compose + "services:\n" - 218
compose = compose + " ocr-server:\n" - 219
compose = compose + " build: .\n" - 220
compose = compose + " ports:\n" - 221
compose = compose + - 222
" - \"" + - 223●
config.app_port.to_string() + - 224
":" + - 225●
config.app_port.to_string() + - 226
"\"\n" - 227
compose = compose + " deploy:\n" - 228
compose = compose + " resources:\n" - 229
compose = compose + " limits:\n" - 230
compose = compose + - 231
" memory: " + - 232●
config.memory_limit_mb.to_string() + - 233
"M\n" - 234●
compose = compose + " cpus: '" + config.cpu_limit.to_string() + "'\n" - 235●
if config.volumes.length() > 0 { - 236○
compose = compose + " volumes:\n" - 237○
for i in 0..<config.volumes.length() { - 238○
let (host, container) = config.volumes[i] - 239
compose = compose + " - \"" + host + ":" + container + "\"\n" - 240
} - 241
} - 242
compose - 243
} - 244
- 245
///| - 246
/// 生成 API Server 启动配置 JSON - 247
pub fn generate_api_config_json(config : ApiServerConfig) -> String { - 248●
let mut json = "{\n" - 249
json = json + " \"host\": \"" + config.host + "\",\n" - 250●
json = json + " \"port\": " + config.port.to_string() + ",\n" - 251
json = json + - 252
" \"max_concurrent_requests\": " + - 253●
config.max_concurrent_requests.to_string() + - 254
",\n" - 255
json = json + - 256
" \"request_timeout_ms\": " + - 257●
config.request_timeout_ms.to_string() + - 258
",\n" - 259
json = json + - 260
" \"max_image_size_mb\": " + - 261●
config.max_image_size_mb.to_string() + - 262
",\n" - 263
json = json + - 264
" \"enable_cors\": " + - 265◐
(if config.enable_cors { "true" } else { "false" }) + - 266
",\n" - 267
json = json + - 268
" \"rate_limit_per_minute\": " + - 269●
config.rate_limit_per_minute.to_string() + - 270
"\n" - 271
json = json + "}\n" - 272
json - 273
} - 274
- 275
///| - 276
/// 生成 CLI 帮助文本 - 277
pub fn generate_cli_help() -> String { - 278●
"stb-image OCR CLI 工具\n\n" + - 279
"用法: ocr_cli [选项] <输入路径>\n\n" + - 280
"选项:\n" + - 281
" -o, --output <路径> 输出文件路径 (默认: output.txt)\n" + - 282
" -f, --format <格式> 输出格式: text/json/markdown (默认: text)\n" + - 283
" -l, --language <语言> 语言: auto/zh/en/ja/ko (默认: auto)\n" + - 284
" --handwriting 启用手写体识别\n" + - 285
" --multilingual 启用多语言混合识别 (默认启用)\n" + - 286
" --document 启用文档版面分析\n" + - 287
" -v, --verbose 详细输出\n" + - 288
" -b, --batch <大小> 批处理大小 (默认: 1)\n" + - 289
" -h, --help 显示帮助\n" - 290
} - 291
- 292
///| - 293
/// 解析 CLI 参数(简化版) - 294
pub fn parse_cli_args(args : Array[String]) -> CliConfig raise @types.LoadError { - 295●
if args.length() == 0 { - 296●
raise @types.LoadError::DecodeFailed( - 297
"parse_cli_args: 至少需要输入路径参数", - 298
) - 299
} - 300●
let default = create_default_cli_config() - 301
let mut config = CliConfig::{ ..default, input_path: args[0], } - 302
let mut i = 1 - 303●
while i < args.length() { - 304●
let arg = args[i] - 305
if arg == "-o" || arg == "--output" { - 306●
if i + 1 < args.length() { - 307●
config = CliConfig::{ ..config, output_path: args[i + 1], } - 308
i += 2 - 309
} else { - 310○
raise @types.LoadError::DecodeFailed("parse_cli_args: -o 需要参数") - 311
} - 312●
} else if arg == "-f" || arg == "--format" { - 313●
if i + 1 < args.length() { - 314●
config = CliConfig::{ ..config, output_format: args[i + 1], } - 315
i += 2 - 316
} else { - 317○
raise @types.LoadError::DecodeFailed("parse_cli_args: -f 需要参数") - 318
} - 319●
} else if arg == "-l" || arg == "--language" { - 320●
if i + 1 < args.length() { - 321●
config = CliConfig::{ ..config, language: args[i + 1], } - 322
i += 2 - 323
} else { - 324○
raise @types.LoadError::DecodeFailed("parse_cli_args: -l 需要参数") - 325
} - 326●
} else if arg == "--handwriting" { - 327●
config = CliConfig::{ ..config, enable_handwriting: true, } - 328
i += 1 - 329●
} else if arg == "--multilingual" { - 330○
config = CliConfig::{ ..config, enable_multilingual: true, } - 331
i += 1 - 332●
} else if arg == "--document" { - 333●
config = CliConfig::{ ..config, enable_document_analysis: true, } - 334
i += 1 - 335●
} else if arg == "-v" || arg == "--verbose" { - 336●
config = CliConfig::{ ..config, verbose: true, } - 337
i += 1 - 338●
} else if arg == "-b" || arg == "--batch" { - 339●
if i + 1 < args.length() { - 340●
config = CliConfig::{ - 341
..config, - 342●
batch_size: parse_int_str(args[i + 1]), - 343
} - 344
i += 2 - 345
} else { - 346○
raise @types.LoadError::DecodeFailed("parse_cli_args: -b 需要参数") - 347
} - 348
} else { - 349○
i += 1 - 350
} - 351
} - 352
config - 353
} - 354
- 355
///| - 356
/// 生成模型打包清单 - 357
pub fn generate_model_manifest(config : ModelPackageConfig) -> String { - 358●
let mut manifest = "{\n" - 359
manifest = manifest + " \"name\": \"" + config.model_name + "\",\n" - 360
manifest = manifest + " \"version\": \"" + config.version + "\",\n" - 361
manifest = manifest + " \"model_path\": \"" + config.model_path + "\",\n" - 362
manifest = manifest + " \"labels_path\": \"" + config.labels_path + "\",\n" - 363
manifest = manifest + - 364
" \"input_width\": " + - 365●
config.input_width.to_string() + - 366
",\n" - 367
manifest = manifest + - 368
" \"input_height\": " + - 369●
config.input_height.to_string() + - 370
",\n" - 371
manifest = manifest + - 372
" \"num_classes\": " + - 373●
config.num_classes.to_string() + - 374
",\n" - 375
manifest = manifest + " \"compression\": \"" + config.compression + "\"\n" - 376
manifest = manifest + "}\n" - 377
manifest - 378
} - 379
- 380
///| - 381
/// 生成环境配置 - 382
pub fn generate_env_config(env : DeploymentEnvironment) -> String { - 383●
match env { - 384
DeploymentEnvironment::Development => - 385●
"NODE_ENV=development\nLOG_LEVEL=debug\n" - 386○
DeploymentEnvironment::Staging => "NODE_ENV=staging\nLOG_LEVEL=info\n" - 387●
DeploymentEnvironment::Production => "NODE_ENV=production\nLOG_LEVEL=warn\n" - 388
} - 389
} - 390
- 391
///| - 392
/// 生成健康检查端点配置 - 393
pub fn generate_health_check_config(config : ApiServerConfig) -> String { - 394●
"health_check:\n" + - 395
" endpoint: /health\n" + - 396
" interval: 30s\n" + - 397
" timeout: 5s\n" + - 398
" retries: 3\n" + - 399
" port: " + - 400●
config.port.to_string() + - 401
"\n" - 402
} - 403
- 404
///| - 405
/// 生成完整部署包 - 406
pub fn generate_deployment_package( - 407
docker_config : DockerConfig, - 408
api_config : ApiServerConfig, - 409
model_config : ModelPackageConfig, - 410
) -> String { - 411●
"=== 部署包 ===\n\n" + - 412
"--- Dockerfile ---\n" + - 413●
generate_dockerfile(docker_config) + - 414
"\n--- docker-compose.yml ---\n" + - 415●
generate_docker_compose(docker_config) + - 416
"\n--- API 配置 ---\n" + - 417●
generate_api_config_json(api_config) + - 418
"\n--- 模型清单 ---\n" + - 419●
generate_model_manifest(model_config) + - 420
"\n--- 环境配置 ---\n" + - 421●
generate_env_config(docker_config.environment) + - 422
"\n--- 健康检查 ---\n" + - 423●
generate_health_check_config(api_config) - 424
}