mirror of
https://github.com/snapotter-hq/SnapOtter.git
synced 2026-08-03 07:46:42 +02:00
feat(ai): add emit_progress() calls to all Python AI scripts
This commit is contained in:
@@ -3,6 +3,11 @@ import sys
|
||||
import json
|
||||
|
||||
|
||||
def emit_progress(percent, stage):
|
||||
"""Emit structured progress to stderr for bridge.ts to capture."""
|
||||
print(json.dumps({"progress": percent, "stage": stage}), file=sys.stderr, flush=True)
|
||||
|
||||
|
||||
def main():
|
||||
input_path = sys.argv[1]
|
||||
settings = json.loads(sys.argv[2]) if len(sys.argv) > 2 else {}
|
||||
@@ -11,12 +16,15 @@ def main():
|
||||
language = settings.get("language", "en")
|
||||
|
||||
try:
|
||||
emit_progress(10, "Loading OCR engine")
|
||||
if engine == "paddleocr":
|
||||
try:
|
||||
from paddleocr import PaddleOCR
|
||||
|
||||
ocr = PaddleOCR(use_angle_cls=True, lang=language)
|
||||
emit_progress(30, "Analyzing text regions")
|
||||
result = ocr.ocr(input_path, cls=True)
|
||||
emit_progress(70, "Extracting text")
|
||||
text = "\n".join(
|
||||
[
|
||||
line[1][0]
|
||||
@@ -26,6 +34,7 @@ def main():
|
||||
if line and line[1]
|
||||
]
|
||||
)
|
||||
emit_progress(95, "Formatting results")
|
||||
print(
|
||||
json.dumps({"success": True, "text": text, "engine": "paddleocr"})
|
||||
)
|
||||
@@ -47,15 +56,18 @@ def main():
|
||||
tess_lang = lang_map.get(language, "eng")
|
||||
|
||||
try:
|
||||
emit_progress(30, "Running Tesseract")
|
||||
result = subprocess.run(
|
||||
["tesseract", input_path, "stdout", "-l", tess_lang],
|
||||
capture_output=True,
|
||||
text=True,
|
||||
timeout=120,
|
||||
)
|
||||
emit_progress(70, "Extracting text")
|
||||
text = result.stdout.strip()
|
||||
if result.returncode != 0 and not text:
|
||||
raise RuntimeError(result.stderr.strip() or "Tesseract failed")
|
||||
emit_progress(95, "Formatting results")
|
||||
print(
|
||||
json.dumps(
|
||||
{"success": True, "text": text, "engine": "tesseract"}
|
||||
|
||||
Reference in New Issue
Block a user