rongo1 commited on
Commit
94cacbb
Β·
1 Parent(s): d9decab
Files changed (1) hide show
  1. app.py +84 -33
app.py CHANGED
@@ -11,7 +11,6 @@ import base64
11
  import logging
12
  import sys
13
  import shutil
14
- import zipfile
15
 
16
  # Configure logging
17
  # Simplified logging for cloud deployment
@@ -282,18 +281,6 @@ def process_business_cards(images, model_name="gemini-2.5-flash", save_images=Tr
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())
@@ -463,13 +450,25 @@ def process_business_cards(images, model_name="gemini-2.5-flash", save_images=Tr
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
@@ -497,13 +496,25 @@ def process_business_cards(images, model_name="gemini-2.5-flash", save_images=Tr
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,13 +536,11 @@ def process_business_cards(images, model_name="gemini-2.5-flash", save_images=Tr
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")
@@ -578,6 +587,47 @@ def process_business_cards(images, model_name="gemini-2.5-flash", save_images=Tr
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
 
@@ -600,9 +650,10 @@ with gr.Blocks(title="Business Card Data Extractor") as demo:
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
 
 
11
  import logging
12
  import sys
13
  import shutil
 
14
 
15
  # Configure logging
16
  # Simplified logging for cloud deployment
 
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())
 
450
 
451
  logger.info(f"Current run Excel file saved successfully: {current_filename}")
452
 
453
+ # Create a manifest file for the current run directory
454
+ manifest_filename = output_dir / f"current_run_{timestamp}_manifest.txt"
455
  try:
456
+ with open(manifest_filename, "w") as f:
457
+ f.write(f"Current Run Directory: {output_dir}\n")
458
+ f.write(f"Images Directory: {images_dir}\n")
459
+ f.write(f"Current Run File: {current_filename}\n")
460
+ f.write(f"Cumulative File: {cumulative_filename}\n")
461
+ f.write(f"Total Cards in Database: {len(cumulative_df)}\n")
462
+ f.write(f"Total Images Saved: {len(saved_image_paths) if saved_image_paths else 0}\n")
463
+ f.write(f"Total API Calls Made: {len(image_batches)}\n")
464
+ f.write(f"Model Used: {model_name}\n")
465
+ f.write(f"Save Images: {save_images}\n")
466
+ f.write(f"Processing Date: {datetime.now().strftime('%Y-%m-%d %H:%M:%S')}\n")
467
+ f.write(f"Session ID: {timestamp}\n")
468
+ f.write(f"Session Summary: {summary}\n") # Use the summary variable from the previous run
469
+ logger.info(f"Manifest file created: {manifest_filename}")
470
  except Exception as e:
471
+ logger.error(f"Failed to create manifest file: {e}")
472
  except Exception as e:
473
  logger.error(f"Failed to write current run Excel file: {e}")
474
  raise
 
496
 
497
  logger.info(f"Cumulative Excel file saved successfully: {cumulative_filename}")
498
 
499
+ # Create a manifest file for the cumulative directory
500
+ manifest_filename = output_dir / f"all_business_cards_total_{timestamp}_manifest.txt"
501
  try:
502
+ with open(manifest_filename, "w") as f:
503
+ f.write(f"All Business Cards Directory: {output_dir}\n")
504
+ f.write(f"Images Directory: {images_dir}\n")
505
+ f.write(f"Current Run File: {current_filename}\n")
506
+ f.write(f"Cumulative File: {cumulative_filename}\n")
507
+ f.write(f"Total Cards in Database: {len(cumulative_df)}\n")
508
+ f.write(f"Total Images Saved: {len(saved_image_paths) if saved_image_paths else 0}\n")
509
+ f.write(f"Total API Calls Made: {len(image_batches)}\n")
510
+ f.write(f"Model Used: {model_name}\n")
511
+ f.write(f"Save Images: {save_images}\n")
512
+ f.write(f"Processing Date: {datetime.now().strftime('%Y-%m-%d %H:%M:%S')}\n")
513
+ f.write(f"Session ID: {timestamp}\n")
514
+ f.write(f"Session Summary: {summary}\n") # Use the summary variable from the previous run
515
+ logger.info(f"Manifest file created: {manifest_filename}")
516
  except Exception as e:
517
+ logger.error(f"Failed to create manifest file: {e}")
518
  except Exception as e:
519
  logger.error(f"Failed to write cumulative Excel file: {e}")
520
  raise
 
536
  summary += f"πŸ“ Total cumulative file: {cumulative_filename.name}\n"
537
  summary += f"πŸ“Š Total cards in database: {len(cumulative_df)}\n\n"
538
 
539
+ # Add note about file locations
540
+ summary += "πŸ“Œ File Locations:\n"
541
+ summary += f" - Excel files: business_card_exports/ folder\n"
542
+ summary += f" - Images: business_cards/ folder\n"
543
+ summary += f" - Check FOLDER_CONTENTS.txt for complete file listing\n\n"
 
 
544
 
545
  if errors:
546
  logger.warning(f"Encountered {len(errors)} errors during processing")
 
587
  except Exception as e:
588
  logger.error(f"Error listing root directory: {e}")
589
 
590
+ # Create a directory listing file in root showing folder contents
591
+ try:
592
+ with open("FOLDER_CONTENTS.txt", "w") as f:
593
+ f.write("=== BUSINESS CARD ANALYZER - FOLDER CONTENTS ===\n")
594
+ f.write(f"Generated: {datetime.now().strftime('%Y-%m-%d %H:%M:%S')}\n\n")
595
+
596
+ # List business_card_exports folder
597
+ f.write("πŸ“ business_card_exports/\n")
598
+ f.write("-" * 50 + "\n")
599
+ if output_dir.exists():
600
+ files = sorted(output_dir.iterdir())
601
+ for file in files:
602
+ if file.is_file():
603
+ size = file.stat().st_size
604
+ f.write(f" πŸ“„ {file.name} ({size:,} bytes)\n")
605
+ else:
606
+ f.write(" [Folder does not exist]\n")
607
+ f.write(f"\nTotal files: {len(list(output_dir.glob('*')))}\n\n")
608
+
609
+ # List business_cards folder
610
+ f.write("πŸ“ business_cards/\n")
611
+ f.write("-" * 50 + "\n")
612
+ if images_dir.exists():
613
+ files = sorted(images_dir.iterdir())
614
+ for file in files:
615
+ if file.is_file():
616
+ size = file.stat().st_size
617
+ f.write(f" πŸ–ΌοΈ {file.name} ({size:,} bytes)\n")
618
+ else:
619
+ f.write(" [Folder does not exist]\n")
620
+ f.write(f"\nTotal files: {len(list(images_dir.glob('*')))}\n\n")
621
+
622
+ # Add latest processing summary
623
+ f.write("πŸ“Š Latest Processing Summary\n")
624
+ f.write("-" * 50 + "\n")
625
+ f.write(summary)
626
+
627
+ logger.info("Created FOLDER_CONTENTS.txt in root directory")
628
+ except Exception as e:
629
+ logger.error(f"Failed to create folder contents file: {e}")
630
+
631
  # Return string paths for Gradio File components
632
  return str(current_filename), str(cumulative_filename), summary, preview_df
633
 
 
650
  - πŸ“ **Tracking**: Image file paths included in Excel database
651
 
652
  **πŸ“Œ Finding Your Files in Hugging Face Spaces:**
653
+ - All files are saved in the `business_card_exports` and `business_cards` folders
654
+ - Check `FOLDER_CONTENTS.txt` in the Files tab to see a complete list of all files
655
+ - Excel files are in `business_card_exports/`
656
+ - Images are in `business_cards/` (if save option enabled)
657
  """
658
  )
659