Hfzune commited on
Commit
67dffc4
·
verified ·
1 Parent(s): a46dc0c

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +4 -11
app.py CHANGED
@@ -8,10 +8,9 @@ import logging
8
  import asyncio
9
  import uuid
10
  import httpx
11
- import pymupdf # Replaced 'fitz' to fix the deprecation warning
12
  from PIL import Image
13
  from pypdf import PdfReader, PdfWriter
14
- # FIX: Rename the Gradio handle_file to avoid collision with the Telegram handler
15
  from gradio_client import Client, handle_file as gradio_handle_file
16
  from telegram import Update
17
  from telegram.ext import (
@@ -54,7 +53,6 @@ except Exception as e:
54
 
55
  # --- 4. PDF Handling & Image Conversion ---
56
  def split_pdf_by_size(input_path: str, max_size_mb: float = MAX_PDF_SIZE_MB) -> list[str]:
57
- """Splits a PDF into chunks smaller than max_size_mb."""
58
  chunks = []
59
  temp_dir = tempfile.mkdtemp(prefix="ocr_chunks_")
60
  reader = PdfReader(input_path)
@@ -93,12 +91,10 @@ def split_pdf_by_size(input_path: str, max_size_mb: float = MAX_PDF_SIZE_MB) ->
93
  return chunks
94
 
95
  def convert_pdf_to_images(pdf_path: str, output_dir: str) -> list[str]:
96
- """Converts each page of a PDF to a high-res PNG image using PyMuPDF."""
97
  doc = pymupdf.open(pdf_path)
98
  image_paths = []
99
  for page_num in range(len(doc)):
100
  page = doc.load_page(page_num)
101
- # Render at 2x zoom for better OCR accuracy
102
  pix = page.get_pixmap(matrix=pymupdf.Matrix(2, 2))
103
  img_path = os.path.join(output_dir, f"page_{page_num + 1}.png")
104
  pix.save(img_path)
@@ -106,7 +102,7 @@ def convert_pdf_to_images(pdf_path: str, output_dir: str) -> list[str]:
106
  doc.close()
107
  return image_paths
108
 
109
- # --- 5. OCR Call with Robust Image Normalization ---
110
  def call_space_ocr(image_path: str) -> str:
111
  """
112
  Converts the image to a standard RGB JPEG, wraps it in gradio_handle_file,
@@ -120,14 +116,13 @@ def call_space_ocr(image_path: str) -> str:
120
  rgb_img = img.convert("RGB")
121
  rgb_img.save(clean_image_path, "JPEG")
122
 
123
- # 2. Call the API with gradio_handle_file and verbose=True
124
  result = gradio_client.predict(
125
- image=gradio_handle_file(clean_image_path), # FIX: Use the renamed import
126
  model_size="Gundam (Recommended)",
127
  task_type="📄 Convert to Markdown",
128
  ref_text=" ",
129
  api_name="/process_ocr_task",
130
- verbose=True, # Prints exact server errors if it fails
131
  )
132
 
133
  if isinstance(result, tuple) and len(result) > 0:
@@ -138,7 +133,6 @@ def call_space_ocr(image_path: str) -> str:
138
  logger.exception(f"OCR call failed for {image_path}")
139
  raise exc
140
  finally:
141
- # 3. Cleanup
142
  if os.path.exists(clean_image_path):
143
  try:
144
  os.remove(clean_image_path)
@@ -147,7 +141,6 @@ def call_space_ocr(image_path: str) -> str:
147
 
148
  # --- 6. Record Parsing ---
149
  def parse_records_from_text(raw_text: str) -> list[dict]:
150
- # Look for <|ref|>Name<|/ref|><|det|>[[x1,y1,x2,y2]]<|/det|>
151
  pattern = re.compile(
152
  r"<\|ref\|>(.*?)<\|/ref\|>\s*<\|det\|>\[\[(\d+),\s*(\d+),\s*(\d+),\s*(\d+)\]\]<\|/det\|>",
153
  re.DOTALL
 
8
  import asyncio
9
  import uuid
10
  import httpx
11
+ import pymupdf
12
  from PIL import Image
13
  from pypdf import PdfReader, PdfWriter
 
14
  from gradio_client import Client, handle_file as gradio_handle_file
15
  from telegram import Update
16
  from telegram.ext import (
 
53
 
54
  # --- 4. PDF Handling & Image Conversion ---
55
  def split_pdf_by_size(input_path: str, max_size_mb: float = MAX_PDF_SIZE_MB) -> list[str]:
 
56
  chunks = []
57
  temp_dir = tempfile.mkdtemp(prefix="ocr_chunks_")
58
  reader = PdfReader(input_path)
 
91
  return chunks
92
 
93
  def convert_pdf_to_images(pdf_path: str, output_dir: str) -> list[str]:
 
94
  doc = pymupdf.open(pdf_path)
95
  image_paths = []
96
  for page_num in range(len(doc)):
97
  page = doc.load_page(page_num)
 
98
  pix = page.get_pixmap(matrix=pymupdf.Matrix(2, 2))
99
  img_path = os.path.join(output_dir, f"page_{page_num + 1}.png")
100
  pix.save(img_path)
 
102
  doc.close()
103
  return image_paths
104
 
105
+ # --- 5. OCR Call ---
106
  def call_space_ocr(image_path: str) -> str:
107
  """
108
  Converts the image to a standard RGB JPEG, wraps it in gradio_handle_file,
 
116
  rgb_img = img.convert("RGB")
117
  rgb_img.save(clean_image_path, "JPEG")
118
 
119
+ # 2. Call the API (verbose flag removed for compatibility)
120
  result = gradio_client.predict(
121
+ image=gradio_handle_file(clean_image_path),
122
  model_size="Gundam (Recommended)",
123
  task_type="📄 Convert to Markdown",
124
  ref_text=" ",
125
  api_name="/process_ocr_task",
 
126
  )
127
 
128
  if isinstance(result, tuple) and len(result) > 0:
 
133
  logger.exception(f"OCR call failed for {image_path}")
134
  raise exc
135
  finally:
 
136
  if os.path.exists(clean_image_path):
137
  try:
138
  os.remove(clean_image_path)
 
141
 
142
  # --- 6. Record Parsing ---
143
  def parse_records_from_text(raw_text: str) -> list[dict]:
 
144
  pattern = re.compile(
145
  r"<\|ref\|>(.*?)<\|/ref\|>\s*<\|det\|>\[\[(\d+),\s*(\d+),\s*(\d+),\s*(\d+)\]\]<\|/det\|>",
146
  re.DOTALL