awacke1 commited on
Commit
d00a49e
·
verified ·
1 Parent(s): 66bb6dd

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +24 -4
app.py CHANGED
@@ -481,14 +481,34 @@ def process_image_hf(image: Image.Image, prompt: str, use_api: bool) -> str:
481
 
482
  async def process_hf_ocr(image: Image.Image, output_file: str, use_api: bool) -> str:
483
  """ Performs OCR using the process_image_hf function framework. """
484
- ocr_prompt = "Perform OCR on this image. Extract all text content." # More specific prompt
 
485
  result = process_image_hf(image, ocr_prompt, use_api=use_api) # Pass use_api flag
 
 
486
  if result and not result.startswith("Error") and not result.startswith("["):
487
  try:
488
- async with aiofiles.open(output_file, "w", encoding='utf-8') as f: await f.write(result)
 
489
  logger.info(f"HF OCR result saved to {output_file}")
490
- except IOError as e: logger.error(f"Failed to save HF OCR output to {output_file}: {e}"); result += f"\n[Error saving file: {e}]"
491
- elif os.path.exists(output_file): try: os.remove(output_file) except OSError: pass
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
492
  return result
493
 
494
  # --- Character Functions (Keep from previous) -----------
 
481
 
482
  async def process_hf_ocr(image: Image.Image, output_file: str, use_api: bool) -> str:
483
  """ Performs OCR using the process_image_hf function framework. """
484
+ # Simple prompt for OCR task
485
+ ocr_prompt = "Extract text content from this image."
486
  result = process_image_hf(image, ocr_prompt, use_api=use_api) # Pass use_api flag
487
+
488
+ # Save the result if it looks like text (basic check)
489
  if result and not result.startswith("Error") and not result.startswith("["):
490
  try:
491
+ async with aiofiles.open(output_file, "w", encoding='utf-8') as f:
492
+ await f.write(result)
493
  logger.info(f"HF OCR result saved to {output_file}")
494
+ except IOError as e:
495
+ logger.error(f"Failed to save HF OCR output to {output_file}: {e}")
496
+ result += f"\n[Error saving file: {e}]" # Append error to result if save fails
497
+
498
+ # --- CORRECTED BLOCK ---
499
+ elif os.path.exists(output_file):
500
+ # Remove file if processing failed or was just a placeholder message
501
+ try:
502
+ os.remove(output_file)
503
+ except OSError:
504
+ # Log error or just ignore if removal fails
505
+ logger.warning(f"Could not remove potentially empty/failed OCR file: {output_file}")
506
+ pass # Ignore removal error
507
+ except Exception as e_rem: # Catch any other error during removal
508
+ logger.warning(f"Error removing OCR file {output_file}: {e_rem}")
509
+ pass
510
+ # --- END CORRECTION ---
511
+
512
  return result
513
 
514
  # --- Character Functions (Keep from previous) -----------