Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
|
@@ -47,6 +47,26 @@ print("β
result_formatter.py fixed")
|
|
| 47 |
|
| 48 |
ABANDON = set(config["pipeline"]["result_formatter"]["abandon"])
|
| 49 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 50 |
# ββ STEP 3: OCR via Zhipu MaaS (no local model) βββββββββββββββββββββββββββββ
|
| 51 |
def run_ocr(uploaded_file):
|
| 52 |
if uploaded_file is None:
|
|
@@ -55,7 +75,7 @@ def run_ocr(uploaded_file):
|
|
| 55 |
api_key = "4570c28bdea5493c9efae9dae68edc66.sGbA9DLlcX1GlvqV"
|
| 56 |
if not api_key:
|
| 57 |
return (
|
| 58 |
-
"Error: ZHIPU_API_KEY is not set.
|
| 59 |
"Failed."
|
| 60 |
)
|
| 61 |
|
|
@@ -64,7 +84,6 @@ def run_ocr(uploaded_file):
|
|
| 64 |
|
| 65 |
path = uploaded_file.name if hasattr(uploaded_file, "name") else str(uploaded_file)
|
| 66 |
|
| 67 |
-
# PDF β list of image paths (SDK expects images per page)
|
| 68 |
if path.lower().endswith(".pdf"):
|
| 69 |
import fitz
|
| 70 |
doc = fitz.open(path)
|
|
@@ -79,8 +98,6 @@ def run_ocr(uploaded_file):
|
|
| 79 |
else:
|
| 80 |
result = parse(path)
|
| 81 |
|
| 82 |
-
# parse() returns a list directly (list of pages, each page = list of regions)
|
| 83 |
-
# Region: {"label": "header"|"footer"|"text"|..., "content": "...", ...}
|
| 84 |
pages_data = result if isinstance(result, list) else []
|
| 85 |
|
| 86 |
total_headers = 0
|
|
@@ -88,17 +105,16 @@ def run_ocr(uploaded_file):
|
|
| 88 |
all_markdown = []
|
| 89 |
all_summary = []
|
| 90 |
|
| 91 |
-
for page_num,
|
|
|
|
| 92 |
if not isinstance(page_regions, list):
|
| 93 |
page_regions = [page_regions] if page_regions else []
|
| 94 |
page_md = []
|
| 95 |
page_summary = [f"ββ PAGE {page_num + 1} of {len(pages_data)} ββ"]
|
| 96 |
|
| 97 |
for region in page_regions:
|
| 98 |
-
|
| 99 |
-
|
| 100 |
-
label = region.get("label", "text")
|
| 101 |
-
content = str(region.get("content", ""))
|
| 102 |
|
| 103 |
if label in ABANDON:
|
| 104 |
continue
|
|
|
|
| 47 |
|
| 48 |
ABANDON = set(config["pipeline"]["result_formatter"]["abandon"])
|
| 49 |
|
| 50 |
+
def _get_regions(page_item):
|
| 51 |
+
"""Get list of regions from one page. Page can be list of regions or dict with 'regions' key."""
|
| 52 |
+
if isinstance(page_item, list):
|
| 53 |
+
return page_item
|
| 54 |
+
if isinstance(page_item, dict):
|
| 55 |
+
return page_item.get("regions", page_item.get("content", [page_item]))
|
| 56 |
+
return []
|
| 57 |
+
|
| 58 |
+
def _get_content(region):
|
| 59 |
+
"""Get text from a region. Supports 'content', 'text', 'raw'."""
|
| 60 |
+
if not isinstance(region, dict):
|
| 61 |
+
return str(region)
|
| 62 |
+
return str(region.get("content") or region.get("text") or region.get("raw") or "")
|
| 63 |
+
|
| 64 |
+
def _get_label(region):
|
| 65 |
+
"""Get label from a region."""
|
| 66 |
+
if not isinstance(region, dict):
|
| 67 |
+
return "text"
|
| 68 |
+
return region.get("label") or region.get("type") or "text"
|
| 69 |
+
|
| 70 |
# ββ STEP 3: OCR via Zhipu MaaS (no local model) βββββββββββββββββββββββββββββ
|
| 71 |
def run_ocr(uploaded_file):
|
| 72 |
if uploaded_file is None:
|
|
|
|
| 75 |
api_key = "4570c28bdea5493c9efae9dae68edc66.sGbA9DLlcX1GlvqV"
|
| 76 |
if not api_key:
|
| 77 |
return (
|
| 78 |
+
"Error: ZHIPU_API_KEY is not set.",
|
| 79 |
"Failed."
|
| 80 |
)
|
| 81 |
|
|
|
|
| 84 |
|
| 85 |
path = uploaded_file.name if hasattr(uploaded_file, "name") else str(uploaded_file)
|
| 86 |
|
|
|
|
| 87 |
if path.lower().endswith(".pdf"):
|
| 88 |
import fitz
|
| 89 |
doc = fitz.open(path)
|
|
|
|
| 98 |
else:
|
| 99 |
result = parse(path)
|
| 100 |
|
|
|
|
|
|
|
| 101 |
pages_data = result if isinstance(result, list) else []
|
| 102 |
|
| 103 |
total_headers = 0
|
|
|
|
| 105 |
all_markdown = []
|
| 106 |
all_summary = []
|
| 107 |
|
| 108 |
+
for page_num, page_item in enumerate(pages_data):
|
| 109 |
+
page_regions = _get_regions(page_item)
|
| 110 |
if not isinstance(page_regions, list):
|
| 111 |
page_regions = [page_regions] if page_regions else []
|
| 112 |
page_md = []
|
| 113 |
page_summary = [f"ββ PAGE {page_num + 1} of {len(pages_data)} ββ"]
|
| 114 |
|
| 115 |
for region in page_regions:
|
| 116 |
+
label = _get_label(region)
|
| 117 |
+
content = _get_content(region)
|
|
|
|
|
|
|
| 118 |
|
| 119 |
if label in ABANDON:
|
| 120 |
continue
|