rongo1
commited on
Commit
Β·
d9decab
1
Parent(s):
2b81079
fix
Browse files
app.py
CHANGED
@@ -11,6 +11,7 @@ import base64
|
|
11 |
import logging
|
12 |
import sys
|
13 |
import shutil
|
|
|
14 |
|
15 |
# Configure logging
|
16 |
# Simplified logging for cloud deployment
|
@@ -281,6 +282,18 @@ def process_business_cards(images, model_name="gemini-2.5-flash", save_images=Tr
|
|
281 |
|
282 |
logger.info(f"Successfully saved {len(saved_image_paths)} images")
|
283 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
284 |
# List directory contents for debugging
|
285 |
try:
|
286 |
images_list = list(images_dir.iterdir())
|
@@ -449,6 +462,14 @@ def process_business_cards(images, model_name="gemini-2.5-flash", save_images=Tr
|
|
449 |
logger.debug(f"Adjusted column widths: {adjusted_columns}")
|
450 |
|
451 |
logger.info(f"Current run Excel file saved successfully: {current_filename}")
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
452 |
except Exception as e:
|
453 |
logger.error(f"Failed to write current run Excel file: {e}")
|
454 |
raise
|
@@ -475,6 +496,14 @@ def process_business_cards(images, model_name="gemini-2.5-flash", save_images=Tr
|
|
475 |
logger.debug(f"Adjusted column widths: {adjusted_columns}")
|
476 |
|
477 |
logger.info(f"Cumulative Excel file saved successfully: {cumulative_filename}")
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
478 |
except Exception as e:
|
479 |
logger.error(f"Failed to write cumulative Excel file: {e}")
|
480 |
raise
|
@@ -496,6 +525,14 @@ def process_business_cards(images, model_name="gemini-2.5-flash", save_images=Tr
|
|
496 |
summary += f"π Total cumulative file: {cumulative_filename.name}\n"
|
497 |
summary += f"π Total cards in database: {len(cumulative_df)}\n\n"
|
498 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
499 |
if errors:
|
500 |
logger.warning(f"Encountered {len(errors)} errors during processing")
|
501 |
summary += "Errors encountered:\n" + "\n".join(errors)
|
@@ -532,6 +569,15 @@ def process_business_cards(images, model_name="gemini-2.5-flash", save_images=Tr
|
|
532 |
except Exception as e:
|
533 |
logger.error(f"Error listing export directory: {e}")
|
534 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
535 |
# Return string paths for Gradio File components
|
536 |
return str(current_filename), str(cumulative_filename), summary, preview_df
|
537 |
|
@@ -552,6 +598,11 @@ with gr.Blocks(title="Business Card Data Extractor") as demo:
|
|
552 |
**Image Storage:**
|
553 |
- πΎ **Optional**: Save uploaded images to business_cards folder
|
554 |
- π **Tracking**: Image file paths included in Excel database
|
|
|
|
|
|
|
|
|
|
|
555 |
"""
|
556 |
)
|
557 |
|
|
|
11 |
import logging
|
12 |
import sys
|
13 |
import shutil
|
14 |
+
import zipfile
|
15 |
|
16 |
# Configure logging
|
17 |
# Simplified logging for cloud deployment
|
|
|
282 |
|
283 |
logger.info(f"Successfully saved {len(saved_image_paths)} images")
|
284 |
|
285 |
+
# Create a ZIP file of saved images
|
286 |
+
if len(saved_image_paths) > 0:
|
287 |
+
zip_filename = f"business_cards_{timestamp}.zip"
|
288 |
+
zip_path = Path(zip_filename)
|
289 |
+
try:
|
290 |
+
with zipfile.ZipFile(zip_path, 'w') as zipf:
|
291 |
+
for img_path in saved_image_paths:
|
292 |
+
zipf.write(img_path, os.path.basename(img_path))
|
293 |
+
logger.info(f"Created ZIP file with {len(saved_image_paths)} images: {zip_filename}")
|
294 |
+
except Exception as e:
|
295 |
+
logger.error(f"Failed to create ZIP file: {e}")
|
296 |
+
|
297 |
# List directory contents for debugging
|
298 |
try:
|
299 |
images_list = list(images_dir.iterdir())
|
|
|
462 |
logger.debug(f"Adjusted column widths: {adjusted_columns}")
|
463 |
|
464 |
logger.info(f"Current run Excel file saved successfully: {current_filename}")
|
465 |
+
|
466 |
+
# Also save a copy in root directory for Hugging Face Spaces visibility
|
467 |
+
root_current_file = Path(f"current_run_{timestamp}.xlsx")
|
468 |
+
try:
|
469 |
+
shutil.copy2(current_filename, root_current_file)
|
470 |
+
logger.info(f"Copied current run file to root: {root_current_file}")
|
471 |
+
except Exception as e:
|
472 |
+
logger.error(f"Failed to copy current file to root: {e}")
|
473 |
except Exception as e:
|
474 |
logger.error(f"Failed to write current run Excel file: {e}")
|
475 |
raise
|
|
|
496 |
logger.debug(f"Adjusted column widths: {adjusted_columns}")
|
497 |
|
498 |
logger.info(f"Cumulative Excel file saved successfully: {cumulative_filename}")
|
499 |
+
|
500 |
+
# Also save a copy in root directory for Hugging Face Spaces visibility
|
501 |
+
root_cumulative_file = Path("all_business_cards_total.xlsx")
|
502 |
+
try:
|
503 |
+
shutil.copy2(cumulative_filename, root_cumulative_file)
|
504 |
+
logger.info(f"Copied cumulative file to root: {root_cumulative_file}")
|
505 |
+
except Exception as e:
|
506 |
+
logger.error(f"Failed to copy cumulative file to root: {e}")
|
507 |
except Exception as e:
|
508 |
logger.error(f"Failed to write cumulative Excel file: {e}")
|
509 |
raise
|
|
|
525 |
summary += f"π Total cumulative file: {cumulative_filename.name}\n"
|
526 |
summary += f"π Total cards in database: {len(cumulative_df)}\n\n"
|
527 |
|
528 |
+
# Add note about root directory files for HF Spaces
|
529 |
+
summary += "π Note: Files are also saved in the root directory for easy access in Hugging Face Spaces Files tab.\n"
|
530 |
+
summary += " - current_run_[timestamp].xlsx\n"
|
531 |
+
summary += " - all_business_cards_total.xlsx\n"
|
532 |
+
if save_images and 'saved_image_paths' in locals() and len(saved_image_paths) > 0:
|
533 |
+
summary += f" - business_cards_{timestamp}.zip (contains all images)\n"
|
534 |
+
summary += "\n"
|
535 |
+
|
536 |
if errors:
|
537 |
logger.warning(f"Encountered {len(errors)} errors during processing")
|
538 |
summary += "Errors encountered:\n" + "\n".join(errors)
|
|
|
569 |
except Exception as e:
|
570 |
logger.error(f"Error listing export directory: {e}")
|
571 |
|
572 |
+
# List root directory Excel/ZIP files for debugging
|
573 |
+
try:
|
574 |
+
root_files = [f for f in Path(".").iterdir() if f.suffix in ['.xlsx', '.zip'] and f.is_file()]
|
575 |
+
logger.info(f"Root directory contains {len(root_files)} Excel/ZIP files")
|
576 |
+
for root_file in root_files[-5:]: # Show last 5 files
|
577 |
+
logger.info(f" - {root_file.name} ({root_file.stat().st_size} bytes)")
|
578 |
+
except Exception as e:
|
579 |
+
logger.error(f"Error listing root directory: {e}")
|
580 |
+
|
581 |
# Return string paths for Gradio File components
|
582 |
return str(current_filename), str(cumulative_filename), summary, preview_df
|
583 |
|
|
|
598 |
**Image Storage:**
|
599 |
- πΎ **Optional**: Save uploaded images to business_cards folder
|
600 |
- π **Tracking**: Image file paths included in Excel database
|
601 |
+
|
602 |
+
**π Finding Your Files in Hugging Face Spaces:**
|
603 |
+
- Click the "Files" tab at the top of this page
|
604 |
+
- Look for: `current_run_*.xlsx`, `all_business_cards_total.xlsx`, and `business_cards_*.zip`
|
605 |
+
- These files are copied to the root directory for easy access
|
606 |
"""
|
607 |
)
|
608 |
|