Spaces:
Sleeping
Sleeping
venkatesh commited on
Commit Β·
6cfe1e0
1
Parent(s): b8cf0cb
changed input to flat file structure
Browse files
tab6.py
CHANGED
|
@@ -32,8 +32,11 @@ def extract_uploaded_zip(uploaded_file):
|
|
| 32 |
def read_zip_structure(temp_dir):
|
| 33 |
"""
|
| 34 |
Analyze the extracted zip structure and identify:
|
| 35 |
-
-
|
| 36 |
-
-
|
|
|
|
|
|
|
|
|
|
| 37 |
"""
|
| 38 |
temp_path = Path(temp_dir)
|
| 39 |
|
|
@@ -46,43 +49,65 @@ def read_zip_structure(temp_dir):
|
|
| 46 |
'processed_files_dir': None
|
| 47 |
}
|
| 48 |
|
| 49 |
-
#
|
| 50 |
-
|
| 51 |
-
|
| 52 |
-
|
| 53 |
-
|
| 54 |
-
|
| 55 |
-
|
| 56 |
-
|
| 57 |
-
|
| 58 |
-
if
|
| 59 |
-
|
| 60 |
-
|
| 61 |
-
|
| 62 |
-
|
| 63 |
-
|
| 64 |
-
|
| 65 |
-
|
| 66 |
-
|
| 67 |
-
|
| 68 |
-
|
| 69 |
-
|
| 70 |
-
|
| 71 |
-
|
| 72 |
-
|
| 73 |
-
|
| 74 |
-
|
| 75 |
-
|
| 76 |
-
|
| 77 |
-
#
|
| 78 |
-
|
| 79 |
-
|
| 80 |
-
|
| 81 |
-
|
| 82 |
-
|
| 83 |
-
|
| 84 |
-
|
| 85 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 86 |
|
| 87 |
return structure
|
| 88 |
|
|
@@ -331,7 +356,7 @@ def generate_updated_pdf_using_helper(renamed_files_by_folder_and_marke: Dict, m
|
|
| 331 |
return False, f"Error generating PDF: {str(e)}\n{traceback.format_exc()}"
|
| 332 |
|
| 333 |
|
| 334 |
-
def generate_updated_excel_using_helper(
|
| 335 |
file_to_factorgroup: Dict, png_to_ignore: Set[str],
|
| 336 |
output_path: Path) -> Tuple[bool, str]:
|
| 337 |
"""
|
|
@@ -344,7 +369,7 @@ def generate_updated_excel_using_helper(processed_files_dir: Path, marken_index:
|
|
| 344 |
data = []
|
| 345 |
|
| 346 |
# Process all files - EXACT same logic as helper.py
|
| 347 |
-
for file in sorted(
|
| 348 |
if not file.is_file():
|
| 349 |
continue
|
| 350 |
|
|
@@ -524,7 +549,7 @@ def updated_file_processor_tab():
|
|
| 524 |
|
| 525 |
if not structure['excel_data']:
|
| 526 |
st.error("β Cannot proceed: Excel file is required to extract folder structure and metadata.")
|
| 527 |
-
st.info("π‘ Make sure your zip contains
|
| 528 |
else:
|
| 529 |
if st.button("π― Analyze Files & Generate Reports", type="primary"):
|
| 530 |
with st.spinner("Processing files and generating reports..."):
|
|
@@ -532,7 +557,7 @@ def updated_file_processor_tab():
|
|
| 532 |
# Step 1: Extract metadata from existing Excel
|
| 533 |
metadata = extract_metadata_from_excel(structure['excel_data'])
|
| 534 |
|
| 535 |
-
st.info(f"π Found {len(metadata['existing_files'])} existing files in {len(metadata['folder_names'])}
|
| 536 |
|
| 537 |
# Step 2: Find PNG duplicates
|
| 538 |
png_to_ignore = find_duplicate_pngs(structure['processed_files'])
|
|
@@ -566,7 +591,7 @@ def updated_file_processor_tab():
|
|
| 566 |
excel_output_path = temp_dir_path / "IcAt_Overview_Final_Updated.xlsx"
|
| 567 |
|
| 568 |
excel_success, excel_msg = generate_updated_excel_using_helper(
|
| 569 |
-
structure['
|
| 570 |
marken_index,
|
| 571 |
file_to_factorgroup,
|
| 572 |
png_to_ignore,
|
|
@@ -727,15 +752,27 @@ def updated_file_processor_tab():
|
|
| 727 |
|
| 728 |
with st.expander("π How to Use This Tool"):
|
| 729 |
st.markdown("""
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 730 |
### Workflow:
|
| 731 |
|
| 732 |
1. **Get zip from Tab 5**: Download "Complete Package" from Tab 5
|
| 733 |
|
| 734 |
2. **Optional: Update files manually**:
|
| 735 |
- Extract the zip locally
|
| 736 |
-
- Add/remove/modify
|
| 737 |
- Keep the filename structure: `01B11brandname01description.ext`
|
| 738 |
-
- Re-zip
|
| 739 |
|
| 740 |
3. **Upload here**: Upload the zip file
|
| 741 |
|
|
|
|
| 32 |
def read_zip_structure(temp_dir):
|
| 33 |
"""
|
| 34 |
Analyze the extracted zip structure and identify:
|
| 35 |
+
- Asset files (images, videos, audio, text files)
|
| 36 |
+
- Excel report (any .xlsx/.xls file - there should be only one)
|
| 37 |
+
- PDF report (any .pdf file - there should be only one)
|
| 38 |
+
|
| 39 |
+
Handles both flat structure and single-subfolder structure (when ZIP creates a wrapper folder)
|
| 40 |
"""
|
| 41 |
temp_path = Path(temp_dir)
|
| 42 |
|
|
|
|
| 49 |
'processed_files_dir': None
|
| 50 |
}
|
| 51 |
|
| 52 |
+
# All allowed asset extensions
|
| 53 |
+
all_asset_extensions = (
|
| 54 |
+
ALLOWED_IMAGE_EXTENSIONS |
|
| 55 |
+
ALLOWED_VIDEO_EXTENSIONS |
|
| 56 |
+
ALLOWED_AUDIO_EXTENSIONS |
|
| 57 |
+
ALLOWED_TEXT_EXTENSIONS
|
| 58 |
+
)
|
| 59 |
+
|
| 60 |
+
# Determine the actual root directory
|
| 61 |
+
# Check if ZIP extracted into a single subfolder (common behavior)
|
| 62 |
+
items = list(temp_path.iterdir())
|
| 63 |
+
if len(items) == 1 and items[0].is_dir():
|
| 64 |
+
# ZIP created a wrapper folder - use that as the root
|
| 65 |
+
actual_root = items[0]
|
| 66 |
+
else:
|
| 67 |
+
# Files are directly in temp_path
|
| 68 |
+
actual_root = temp_path
|
| 69 |
+
|
| 70 |
+
# Find the Excel report (any .xlsx or .xls file)
|
| 71 |
+
excel_report = None
|
| 72 |
+
for file in actual_root.iterdir():
|
| 73 |
+
if file.is_file() and file.suffix.lower() in ['.xlsx', '.xls']:
|
| 74 |
+
excel_report = file
|
| 75 |
+
break
|
| 76 |
+
|
| 77 |
+
if excel_report:
|
| 78 |
+
structure['excel_report'] = excel_report
|
| 79 |
+
structure['excel_path'] = excel_report
|
| 80 |
+
# Read Excel data
|
| 81 |
+
try:
|
| 82 |
+
df_assets = pd.read_excel(excel_report, sheet_name='Assets')
|
| 83 |
+
df_reordered = pd.read_excel(excel_report, sheet_name='Reordered')
|
| 84 |
+
structure['excel_data'] = {'Assets': df_assets, 'Reordered': df_reordered}
|
| 85 |
+
except Exception as e:
|
| 86 |
+
st.warning(f"Could not read Excel file: {e}")
|
| 87 |
+
|
| 88 |
+
# Find the PDF report (any .pdf file)
|
| 89 |
+
pdf_report = None
|
| 90 |
+
for file in actual_root.iterdir():
|
| 91 |
+
if file.is_file() and file.suffix.lower() == '.pdf':
|
| 92 |
+
pdf_report = file
|
| 93 |
+
break
|
| 94 |
+
|
| 95 |
+
if pdf_report:
|
| 96 |
+
structure['pdf_report'] = pdf_report
|
| 97 |
+
|
| 98 |
+
# Collect all asset files (excluding Excel and PDF reports)
|
| 99 |
+
asset_files = []
|
| 100 |
+
for file in actual_root.iterdir():
|
| 101 |
+
if file.is_file():
|
| 102 |
+
# Skip the reports
|
| 103 |
+
if file == excel_report or file == pdf_report:
|
| 104 |
+
continue
|
| 105 |
+
# Include only asset files with valid extensions
|
| 106 |
+
if file.suffix.lower() in all_asset_extensions:
|
| 107 |
+
asset_files.append(file)
|
| 108 |
+
|
| 109 |
+
structure['processed_files'] = asset_files
|
| 110 |
+
structure['processed_files_dir'] = actual_root
|
| 111 |
|
| 112 |
return structure
|
| 113 |
|
|
|
|
| 356 |
return False, f"Error generating PDF: {str(e)}\n{traceback.format_exc()}"
|
| 357 |
|
| 358 |
|
| 359 |
+
def generate_updated_excel_using_helper(processed_files: List[Path], marken_index: Dict,
|
| 360 |
file_to_factorgroup: Dict, png_to_ignore: Set[str],
|
| 361 |
output_path: Path) -> Tuple[bool, str]:
|
| 362 |
"""
|
|
|
|
| 369 |
data = []
|
| 370 |
|
| 371 |
# Process all files - EXACT same logic as helper.py
|
| 372 |
+
for file in sorted(processed_files, key=lambda f: f.name):
|
| 373 |
if not file.is_file():
|
| 374 |
continue
|
| 375 |
|
|
|
|
| 549 |
|
| 550 |
if not structure['excel_data']:
|
| 551 |
st.error("β Cannot proceed: Excel file is required to extract folder structure and metadata.")
|
| 552 |
+
st.info("π‘ Make sure your zip contains one Excel file (.xlsx or .xls) with 'Assets' and 'Reordered' sheets.")
|
| 553 |
else:
|
| 554 |
if st.button("π― Analyze Files & Generate Reports", type="primary"):
|
| 555 |
with st.spinner("Processing files and generating reports..."):
|
|
|
|
| 557 |
# Step 1: Extract metadata from existing Excel
|
| 558 |
metadata = extract_metadata_from_excel(structure['excel_data'])
|
| 559 |
|
| 560 |
+
st.info(f"π Found {len(metadata['existing_files'])} existing files in {len(metadata['folder_names'])} blocks")
|
| 561 |
|
| 562 |
# Step 2: Find PNG duplicates
|
| 563 |
png_to_ignore = find_duplicate_pngs(structure['processed_files'])
|
|
|
|
| 591 |
excel_output_path = temp_dir_path / "IcAt_Overview_Final_Updated.xlsx"
|
| 592 |
|
| 593 |
excel_success, excel_msg = generate_updated_excel_using_helper(
|
| 594 |
+
structure['processed_files'],
|
| 595 |
marken_index,
|
| 596 |
file_to_factorgroup,
|
| 597 |
png_to_ignore,
|
|
|
|
| 752 |
|
| 753 |
with st.expander("π How to Use This Tool"):
|
| 754 |
st.markdown("""
|
| 755 |
+
### Expected ZIP Structure:
|
| 756 |
+
|
| 757 |
+
Your zip file should contain all files in the root (flat structure):
|
| 758 |
+
```
|
| 759 |
+
uploaded.zip
|
| 760 |
+
βββ 01B11brandname01description.png (asset files)
|
| 761 |
+
βββ 02B15anotherbrand02item.mp4
|
| 762 |
+
βββ ...
|
| 763 |
+
βββ *.xlsx (one Excel report - required)
|
| 764 |
+
βββ *.pdf (one PDF report - optional)
|
| 765 |
+
```
|
| 766 |
+
|
| 767 |
### Workflow:
|
| 768 |
|
| 769 |
1. **Get zip from Tab 5**: Download "Complete Package" from Tab 5
|
| 770 |
|
| 771 |
2. **Optional: Update files manually**:
|
| 772 |
- Extract the zip locally
|
| 773 |
+
- Add/remove/modify asset files
|
| 774 |
- Keep the filename structure: `01B11brandname01description.ext`
|
| 775 |
+
- Re-zip all files together (flat, no subfolders)
|
| 776 |
|
| 777 |
3. **Upload here**: Upload the zip file
|
| 778 |
|