Skip to content

Rust rewrite - #2

Merged
mitsa-ng merged 17 commits into
mainfrom
rust-rewrite
Nov 11, 2026
Merged

mitsa-ng merged 17 commits into
mainfrom
rust-rewrite

Conversation

@mitsa-ng

Copy link
Copy Markdown
Owner

No description provided.

Nati added 17 commits November 10, 2026 21:07
…, Ollama NLP)

- eat-core: language_prompts + nlp_engine port (4-tier JSON parser, validation,
  hallucination filters); default model qwen2.5:7b (benchmark: 6/6 planted
  errors vs phi3's 0-in-6-minutes)
- eat-pdf: pdfium digital word extraction + sidebar annotator; word-count
  parity with Python golden (pdfs/standard-test)
- eat-cli: main.py port (path sanitization, intermediate JSONs)
- golden tests: extraction parity + annotation; E2E identical 6 errors vs Python
- vendor: pdfium mac-arm64 dylib (bblanchon/pdfium-binaries)
- eat-ocr: PP-OCRv6 det (DB postprocess port: components, min-area rect,
  unclip, dilation verified against cv2), rec (CTC + embedded dictionary),
  dynamic batch widths; perspective crop via homography
- orientation: rec-score flip-retry replaces cls model (regressed under
  ORT >=1.31 — zeros tensor diverges vs onnxruntime 1.24)
- parity vs Python RapidOCR on handwritten scan: 37=37 det boxes, 35/37
  line texts match, avg IoU 0.875
- eat-pdf: page render to RGB (pdfium) for the scan path; eat-cli wires
  300 DPI render -> OCR -> px-to-pt boxes, conf >= 0.4 filter
- eat-core: Ollama streaming with loop detection — model loops on noisy
  OCR essays (>10 min per chunk, causes 600 s timeout); cut on 3rd repeat
  of an original value, tier-3 parser recovers prefix. E2E handwritten:
  Rust 23 errors in 159 s (Python previously timed out; fix also ported
  to nlp_engine.py in main tree)
- release: lto off (ort static + CoreML symbols fail dead-strip link)
…p bundle

- eat-app: pipeline extracted from CLI (shared with GUI) + folder→ZIP batch
  (skips own *_annotated outputs); pdfium/OCR engines shared per-process
  (pdfium binds exactly once — second bind crashed the batch loop)
- eat-pdf: proportional word-box splitting for OCR lines → scanned essays now
  get real body tags (handwritten E2E: 0 → 29 annotations; Python: 0);
  bundled Noto CJK TC font for zh/ja/ko sidebar tags (CID-keyed load with
  Helvetica fallback)
- eat-gui (egui): folder picker, model/url/language settings, Ollama status
  check with install link + one-click model pull, live log, results table,
  ZIP reveal — port of gui.py's batch flow
- scripts/make-app.sh: Project-EAT.app (103 MB; binary+pdfium+models+font,
  ad-hoc codesigned). Verified: clean-env run (env -i, cwd=/) completes the
  handwritten E2E with zero bind failures; GUI window launches from bundle
- eat-core logging: subscriber hook so GUI mirrors pipeline logs
- eat-gui first-run: detect missing Ollama → one-click 下載(Ollama-darwin.zip
  229 MB,實測 25s)→ ditto 解壓 → /Applications(fallback ~/Applications)
  → open 啟動 → 輪詢 API 到連線 → 接續模型下載。macOS 唯一手動步驟消除。
- Windows x64 cross-build (mingw): eat.exe + Project-EAT.exe (egui/glow
  links on windows-gnu) + pdfium.dll + onnxruntime.dll (ort load-dynamic
  gated to cfg(windows); macOS keeps static linking); make-win-dist.sh
  → Project-EAT-win.zip 61 MB(解壓 98 MB)。Runtime 未在真機 Windows 驗證
  (無 Windows 環境);DLL 依賴經 objdump 檢查僅系統 DLL + 隨附 DLL。
- live camera 移植:GUI 即時分頁(nokhwa/AVFoundation)— 預覽串流、2.5s
  節流 OCR(共用 PP-OCR 引擎)、conf ≥ 0.35、<3 行跳過、文字未變跳過 LLM
  (live_mode.py 的節流邏輯)。NSCameraUsageDescription 已入 Info.plist。
- macOS bundle 加相機權限後重建;全套測試 24 綠
回應「OCR 準確度太低」:三層升級,全部復用既有 Ollama/ONNX 架構(用戶
提的 baidu/Unlimited-OCR 是伺服器級 VLM 文件解析器——輸出無字框、需
A800 級 GPU、無 GGUF——與零門檻目標衝突,改以本地 VLM 達成同類品質)。

1. PP-OCR medium 模型檔(det 62MB+rec 77MB,字典相同):EAT_PPOCR_PROFILE
   選擇。實測手寫卷差異有限(修 tuitlon→tuition,但新增 fob/gob 假錯)。
2. VLM 行讀取(eat-ocr::VlmReader + run_with_reader):PP-OCR det 給行框,
   qwen2.5vl:7b 逐行轉錄。實測慘勝:part-time job/fob→job、
   finand/finance situatlon→financial situation、m order→in order,
   PP-OCR 造成的假錯誤消失。E2E 34→24 分鐘(單行推理地板 ~60s,
   非 tokens 瓶頸——多圖合併單請求是後續優化項)。
3. 垃圾回應防護:空白裁切會讓 VLM 幻覺吐 'system'(17/38 行),且
   score:1.0 繞過信心過濾——改用 NO TEXT 提示詞+模板詞過濾。
   縮圖順序 bug(先限寬 640→行高 13px 不可讀)改為先行高後寬(≤64px)。

介面:GUI「掃描讀取引擎」下拉(預設 PP-OCR;VLM 標示每頁約 30 分鐘)
+視覺模型欄+未拉模型警告;CLI --reader vlm --vlm-model。
發行包維持 small 模型(103MB),medium 留 vendor 供進階設定。
量測瓶頸:不是生成(1s/行)而是每請求的視覺編碼 prefill(15.7s)×38
次請求。兩個失敗的中間方案都有教訓:僅限寬縮圖會把行高壓到 13px(不可
讀→亂讀);要求模型輸出編號行的對齊成功率不足(7-14/38)。

定案架構(run_with_reader v2):
1. PP-OCR 全量本地讀取(8s,行框+基準文字+信心)
2. 全部行裁切堆疊成單張圖(行高 56px、寬 ≤1600、白底間隔)→ 一次
   VLM 請求校正(108s 實測,38 行 → 12 行回傳)
3. 以 levenshtein 相似度把 VLM 行貪婪對齊回 PP-OCR 行(≥0.35),
   對上的取代原讀、對不上的保留 PP-OCR——不依賴模型格式服從性,
   VLM 失敗時整頁仍以 PP-OCR 完成(優雅降級)

E2E 手寫卷:全程 2:40(VLM 校正 10/38 行);錯誤清單從 OCR 假錯誤
(tuitlon/finand)變成真實學生錯字(woris、prblems、childhus、
soclety、prtected、expennenced)——速度與品質同時到位。
- score example(eat-app):以 Standard 1/2 試卷規準(Topic Sentence 6 /
  Supporting & Unity 8 / Grammar & Academic Diction 10 / Concluding 6,
  滿分 30)為每份作文打分+中文短評,輸出 CSV。實測 28 份共約 5 分鐘。
- 錯誤清單顯示移除 p1 頁碼前綴(使用者要求)。
- 備註:Standard_1/2 本身是考卷規準照片,非學生作文,其分數僅供參考。
- report 模組:sentence_errors(每個錯誤對應到包含它的句子——「哪一句有
  問題、拼錯字在哪」)+ grade(Standard 1/2 規準 30 分制+中文短評)
- run_batch:每份作文完成後評分+句級明細,輸出 批改總表.csv(分數分項
  +短評)與 錯誤明細.csv(句子/類型/錯字/建議),CSV 隨標註 PDF 一起
  進 ZIP;GUI 批次預設啟用
- 表單雜訊過濾:頁碼條(Page N of N)、規準文字、點數標示(/20、
  Concluding Sentence /10)不再進報表;*.annotated.pdf 與 *_annotated
  一律跳過(先前 .annotated 誘餌會被再批改一次產出垃圾分數)
- 錯誤清單顯示移除頁碼前綴;CLI --score(預設開)
- 驗證:verify2 兩份 → 誘餌跳過 ✓、表單假錯誤消失 ✓、句級明細正確
 ('First, when students are using AI to study…' → it can provides →
  it can provide)
- run_batch:廢除合併式 錯誤明細.csv,改輸出 <作文>_錯誤明細.csv
  (欄位:總分/問題句子/類型/錯字或錯處/建議);批改總表.csv 保留
- ZIP 打包掃 input_dir 內所有 *_錯誤明細.csv 一併放入
- 既有 28 份成績以離線腳本從 final28 OCR/錯誤 JSON 重產個別明細,
  併入 批改結果_全部28份.zip(57 檔案:28 PDF+總表+28 明細)
- RubricCfg(criteria 文字+dims: Vec<(名稱,滿分)>),預設 Standard 1/2
  的 30 分制(主題句6/支持句8/文法10/結論6)
- grade_with(規準,…):模型依動態維度評分(d0..dN JSON 鍵),總分=
  各維度 min(得分,滿分) 之和;CSV 表頭動態生成(主題句10,...總分40)
- GUI:批改標準群組=可折疊的評分說明文字框+四欄(名稱+滿分)佔比
  編輯+滿分即時加總+重設預設;設定存 ~/eat-rubric.json(下次開啟自動
  載入),按「開始批改」時寫入
- CLI/eat-app:PipelineOpts.rubric;batch 範例讀 ~/eat-rubric.json
- 驗證:佔比改 10/10/10/10 → 同份作文 23/40 分,表頭與總分正確切換;
  GUI 截圖 OCR 確認「批改標準/評分佔比/滿分/四維名稱」皆渲染
- Dim {name, desc, max}:desc=老師自己寫的該項標準,直接注入評分
  prompt(d0 主題句 (0-6 pts): <desc>);留空則只用名稱
- 預設 desc 填入 Standard 1/2 試卷規準原文(Topic+Controlling Idea、
  5-6 Supporting Sentences with strict Unity、Grammar & Academic Diction、
  transition word conclusion),整體要求=至少 8 句/100-150 字原文
- GUI:評分佔比改為每維度一個可折疊列——標題「主題句 — 6 分」,展開
  編輯名稱/滿分/此項標準說明;~/eat-rubric.json 自動記憶整份規準
- rubric_dump 範例驗證預設規準四維原文正確;GUI 截圖 OCR 確認折疊列
  與滿分 30 渲染
- GUI:『+ 新增維度』(預設名稱「新維度」/5 分,可改名改說明);
  每維度展開內含『✕ 刪除此維度』
- run_batch:dims 為空時跳過評分(避免 0 維輸出無意義分數)
- 5 維度 e2e 驗證:加「字數 5 分」→ CSV 表頭
  主題句6,支持句8,文法10,結論6,字數5,總分35,該生字數 0/5(未達
  8 句要求),總分 16/35 — 動態維度全鏈路(prompt/CSV/總分)正確
- 頂欄新增模式切換:批次批改 / 瀏覽批改
- 瀏覽模式:結果資料夾選擇(自動配對 *.annotated.pdf|*_annotated.pdf ↔
  *_errors.json,實測 28/28 配對);左=pdfium 渲染標註後原稿
  (多頁垂直串接、可捲動);右=錯誤條列(依序編號、拼字紅/文法橘/
  語意藍色標);←/→ 切換上一篇/下一篇(文字框聚焦時不攔截)、
  頂列 ◀/▶ 按鈕同功能;↻ 重新載入
- 展示掛鉤:~/eat-browse-folder.txt 存在時自動進瀏覽模式(開發/演示用)
- 驗證:28 份 /tmp/browse-demo 自動載入、右側「錯誤(7)」條目 1-6 渲染、
  左側 PDF 深色像素 42%(頁面已渲染)
- BrowseState.rotation(0-3 檔四分之一圈);↑ 順時針、↓ 逆時針,
  texture_key 帶 rotation 觸發重渲;rotate_rgba_cw 像素級旋轉
- 導覽列提示「↑轉90° ↓轉-90°」;旋轉非零時顯示「↺ 90/180/270°」
  歸零鈕;旋轉跨文件保留(同批相機照片方向一致)
- 實測(像素寬高比):0°=0.97 直向 → ↑=1.75 橫向 → ↑↑=0.97 直向
- build.yml:
  test job(macos-14 arm64+windows-latest x64 MSVC)→ cargo test --release
  package-macos(make-app.sh → .app → ad-hoc codesign → upload artifact)
  package-windows(cargo build msvc → 組裝 exe+pdfium.dll+onnxruntime.dll+
  models+font → Compress-Archive → upload artifact)
  release(tag v* 時 → download artifacts → GitHub Release)
- vendor 補 Windows pdfium.dll(bblanchon/pdfium-binaries win-x64)
- Windows onnxruntime.dll 由 CI 下載(ort load-dynamic 模式)
- 推 rust-rewrite branch 或 tag v* 即觸發打包與發布
- NlpEngine::check_endpoint:依 URL 自動分流——含 /api/generate 或
  :11434 → Ollama /api/tags;其他 → OpenAI-compatible /v1/models
  (Bearer)。分辨 401(key 無效/過期)、403(權限不足)、404(端點
  不存在)、連線失敗,回傳(可達, 模型列表, 說明)
- NlpEngine::with_key + build_request:Ollama 與 OpenAI chat 格式雙支援,
  Authorization Bearer 自動附加;VlmReader 同步支援 key
- GUI:『API 端點』欄(帶「檢查」鈕即時驗證)+『API Key』密碼欄
  (Ollama 免填提示);狀態列依端點類型顯示『本地 Ollama 已連線』
  或『API 已連線』
- CLI:--api-url / --api-key(--ollama-url 保留為隱藏別名)
- 實測:本地 Ollama ok=true(qwen2.5vl/qwen2.5/gemma3 三模型);
  OpenAI 無效 key → ok=false『API key 無效或過期』(401 路徑);
  測試 24 綠
- update() 用 .take() 取出 OllamaStatus 後未放回:第一幀消費
  Reachable,之後每幀都是 None → 永遠顯示「正在檢查」(實測卡死)
- 改為 .clone()(OllamaStatus 補 derive(Clone))
- 驗證:重啟後 OCR 確認「本地 Ollama 已連線」正常顯示
@mitsa-ng
mitsa-ng merged commit 6fed1fc into main Nov 11, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant