image.vision\ocr\ocr_deployment.mbt

85.7% · 78 / 91 points · 424 lines

Background color reports the state of each line. Underlined characters mark exact instrumented byte offsets; hover them to inspect the visit count.

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