Spaces:
Running
Running
Marc Allen Lopez commited on
Commit ·
edb3860
1
Parent(s): e4fceaf
Implement PP-OCRv5 using official model names
Browse files- Use text_detection_model_name='PP-OCRv5_server_det'
- Use text_recognition_model_name='PP-OCRv5_server_rec'
- Add USE_PP_OCRV5 environment flag (defaults to true)
- Enable textline orientation classification for better accuracy
- Add version tracking in health endpoint and app title
- Robust fallback to default models if PP-OCRv5 fails to load
Based on official Hugging Face PP-OCRv5 documentation:
https://huggingface.co/PaddlePaddle/PP-OCRv5_server_det
app.py
CHANGED
|
@@ -14,22 +14,43 @@ OCR_LANG = os.getenv("OCR_LANG", "en")
|
|
| 14 |
PPOCR_HOME = os.getenv("PPOCR_HOME", "/tmp/.paddleocr")
|
| 15 |
os.makedirs(PPOCR_HOME, exist_ok=True)
|
| 16 |
os.environ.setdefault("PPOCR_HOME", PPOCR_HOME)
|
| 17 |
-
|
|
|
|
|
|
|
|
|
|
| 18 |
|
| 19 |
|
| 20 |
def load_ocr():
|
|
|
|
| 21 |
try:
|
| 22 |
-
|
| 23 |
-
|
| 24 |
-
|
| 25 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 26 |
ocr = PaddleOCR(use_angle_cls=True, lang=OCR_LANG, show_log=False)
|
|
|
|
| 27 |
return ocr
|
| 28 |
|
| 29 |
|
| 30 |
ocr = load_ocr()
|
| 31 |
|
| 32 |
-
app = FastAPI(title="TextSense OCR (PaddleOCR)")
|
| 33 |
|
| 34 |
|
| 35 |
def read_image_from_upload(upload: UploadFile) -> Image.Image:
|
|
@@ -96,4 +117,4 @@ async def extract(
|
|
| 96 |
|
| 97 |
@app.get("/healthz")
|
| 98 |
async def healthz():
|
| 99 |
-
return {"ok": True, "lang": OCR_LANG}
|
|
|
|
| 14 |
PPOCR_HOME = os.getenv("PPOCR_HOME", "/tmp/.paddleocr")
|
| 15 |
os.makedirs(PPOCR_HOME, exist_ok=True)
|
| 16 |
os.environ.setdefault("PPOCR_HOME", PPOCR_HOME)
|
| 17 |
+
|
| 18 |
+
# PP-OCRv5 model configuration
|
| 19 |
+
USE_PP_OCRV5 = os.getenv("USE_PP_OCRV5", "true").lower() == "true"
|
| 20 |
+
ACTIVE_OCR_VERSION = "unknown" # Will be set during OCR initialization
|
| 21 |
|
| 22 |
|
| 23 |
def load_ocr():
|
| 24 |
+
global ACTIVE_OCR_VERSION
|
| 25 |
try:
|
| 26 |
+
if USE_PP_OCRV5:
|
| 27 |
+
# Use PP-OCRv5 models as specified in the official documentation
|
| 28 |
+
ocr = PaddleOCR(
|
| 29 |
+
use_angle_cls=True,
|
| 30 |
+
lang=OCR_LANG,
|
| 31 |
+
text_detection_model_name="PP-OCRv5_server_det",
|
| 32 |
+
text_recognition_model_name="PP-OCRv5_server_rec",
|
| 33 |
+
use_doc_orientation_classify=False,
|
| 34 |
+
use_doc_unwarping=False,
|
| 35 |
+
use_textline_orientation=True,
|
| 36 |
+
show_log=False
|
| 37 |
+
)
|
| 38 |
+
ACTIVE_OCR_VERSION = "PP-OCRv5"
|
| 39 |
+
else:
|
| 40 |
+
# Fallback to default models
|
| 41 |
+
ocr = PaddleOCR(use_angle_cls=True, lang=OCR_LANG, show_log=False)
|
| 42 |
+
ACTIVE_OCR_VERSION = "default"
|
| 43 |
+
except Exception as e:
|
| 44 |
+
# Final fallback for any initialization errors
|
| 45 |
+
print(f"PP-OCRv5 initialization failed: {e}. Falling back to default models.")
|
| 46 |
ocr = PaddleOCR(use_angle_cls=True, lang=OCR_LANG, show_log=False)
|
| 47 |
+
ACTIVE_OCR_VERSION = "default-fallback"
|
| 48 |
return ocr
|
| 49 |
|
| 50 |
|
| 51 |
ocr = load_ocr()
|
| 52 |
|
| 53 |
+
app = FastAPI(title=f"TextSense OCR (PaddleOCR {ACTIVE_OCR_VERSION})")
|
| 54 |
|
| 55 |
|
| 56 |
def read_image_from_upload(upload: UploadFile) -> Image.Image:
|
|
|
|
| 117 |
|
| 118 |
@app.get("/healthz")
|
| 119 |
async def healthz():
|
| 120 |
+
return {"ok": True, "lang": OCR_LANG, "ocr_version": ACTIVE_OCR_VERSION}
|