fix:glm max tokens
This commit is contained in:
@@ -62,20 +62,20 @@ async def process_image_ocr(
|
||||
try:
|
||||
log.info("Starting image OCR processing")
|
||||
|
||||
# Preprocess image
|
||||
# Preprocess image (load only, no padding yet)
|
||||
preprocess_start = time.time()
|
||||
image = image_processor.preprocess(
|
||||
image_url=request.image_url,
|
||||
image_base64=request.image_base64,
|
||||
)
|
||||
|
||||
# Apply padding if enabled (before layout detection)
|
||||
# Apply padding only for layout detection
|
||||
processed_image = image
|
||||
if image_processor and settings.is_padding:
|
||||
processed_image = image_processor.add_padding(image)
|
||||
|
||||
preprocess_time = time.time() - preprocess_start
|
||||
log.debug(f"Image preprocessing completed in {preprocess_time:.3f}s")
|
||||
log.debug(f"Image loading completed in {preprocess_time:.3f}s")
|
||||
|
||||
# Layout detection (using padded image if padding is enabled)
|
||||
layout_start = time.time()
|
||||
@@ -83,14 +83,14 @@ async def process_image_ocr(
|
||||
layout_time = time.time() - layout_start
|
||||
log.info(f"Layout detection completed in {layout_time:.3f}s")
|
||||
|
||||
# OCR recognition
|
||||
# OCR recognition (use original image without padding)
|
||||
ocr_start = time.time()
|
||||
if layout_info.MixedRecognition:
|
||||
recognition_method = "MixedRecognition (MinerU)"
|
||||
log.info(f"Using {recognition_method}")
|
||||
|
||||
# Convert numpy array to image bytes (image already padded)
|
||||
success, encoded_image = cv2.imencode(".png", processed_image)
|
||||
# Convert original image (without padding) to bytes
|
||||
success, encoded_image = cv2.imencode(".png", image)
|
||||
if not success:
|
||||
raise RuntimeError("Failed to encode image")
|
||||
|
||||
@@ -100,7 +100,29 @@ async def process_image_ocr(
|
||||
else:
|
||||
recognition_method = "FormulaOnly (GLMOCR)"
|
||||
log.info(f"Using {recognition_method}")
|
||||
ocr_result = glmocr_service.recognize(processed_image)
|
||||
|
||||
# Try GLM-OCR first, fallback to MinerU if token limit exceeded
|
||||
try:
|
||||
ocr_result = glmocr_service.recognize(image)
|
||||
except Exception as e:
|
||||
error_msg = str(e)
|
||||
# Check if error is due to token limit (max_model_len exceeded)
|
||||
if "max_model_len" in error_msg or "decoder prompt" in error_msg or "BadRequestError" in error_msg:
|
||||
log.warning(f"GLM-OCR failed due to token limit: {error_msg}")
|
||||
log.info("Falling back to MinerU for recognition")
|
||||
recognition_method = "FormulaOnly (MinerU fallback)"
|
||||
|
||||
# Convert original image to bytes for MinerU
|
||||
success, encoded_image = cv2.imencode(".png", image)
|
||||
if not success:
|
||||
raise RuntimeError("Failed to encode image")
|
||||
|
||||
image_bytes = BytesIO(encoded_image.tobytes())
|
||||
image_bytes.seek(0)
|
||||
ocr_result = mineru_service.recognize(image_bytes)
|
||||
else:
|
||||
# Re-raise other errors
|
||||
raise
|
||||
ocr_time = time.time() - ocr_start
|
||||
|
||||
total_time = time.time() - preprocess_start
|
||||
|
||||
@@ -532,8 +532,10 @@ class GLMOCRService(OCRServiceBase):
|
||||
|
||||
Returns:
|
||||
Dict with 'latex', 'markdown', 'mathml', 'mml' keys.
|
||||
|
||||
Raises:
|
||||
RuntimeError: If recognition fails (preserves original exception for fallback handling).
|
||||
"""
|
||||
try:
|
||||
# Add padding to image
|
||||
padded_image = self.image_processor.add_padding(image)
|
||||
|
||||
@@ -549,6 +551,7 @@ class GLMOCRService(OCRServiceBase):
|
||||
prompt = "Formula Recognition:"
|
||||
messages = [{"role": "user", "content": [{"type": "image_url", "image_url": {"url": image_url}}, {"type": "text", "text": prompt}]}]
|
||||
|
||||
# Don't catch exceptions here - let them propagate for fallback handling
|
||||
response = self.openai_client.chat.completions.create(
|
||||
model="glm-ocr",
|
||||
messages=messages,
|
||||
@@ -574,8 +577,6 @@ class GLMOCRService(OCRServiceBase):
|
||||
"mml": convert_result.mml,
|
||||
"markdown": markdown_content,
|
||||
}
|
||||
except Exception as e:
|
||||
raise RuntimeError(f"GLM formula recognition failed: {e}") from e
|
||||
|
||||
def recognize(self, image: np.ndarray) -> dict:
|
||||
"""Recognize content using GLM-4V.
|
||||
|
||||
Reference in New Issue
Block a user