Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
|
@@ -31,7 +31,6 @@ def convert_ci(by_line_item_file, combined_file, password, max_rows):
|
|
| 31 |
description = re.sub(r"\d+", "", description)
|
| 32 |
return re.sub(r"[^\w\s]", "", description)
|
| 33 |
|
| 34 |
-
# by_line_item_df["Part"] = by_line_item_df.iloc[:, 0].astype(str)
|
| 35 |
by_line_item_df["Part"] = ""
|
| 36 |
by_line_item_df["Tariff_Number"] = by_line_item_df.iloc[:, 1].apply(lambda x: str(x).replace('.0', '') if isinstance(x, float) and str(x).endswith('.0') else str(x))
|
| 37 |
by_line_item_df["Commercial_Description"] = by_line_item_df.apply(clean_desc, axis=1)
|
|
@@ -138,37 +137,35 @@ def convert_ci(by_line_item_file, combined_file, password, max_rows):
|
|
| 138 |
'CVD_Case_Number', 'AD_Non_Reimbursement_Statement',
|
| 139 |
'AD-CVD_Certification_Designation'
|
| 140 |
]
|
| 141 |
-
|
| 142 |
-
# Update SHEIN Incorrect MID:
|
| 143 |
-
MID_mapping_df = pd.read_csv("MID_replace.csv")
|
| 144 |
-
mid_map = dict(zip(MID_mapping_df["SHEIN_MID_Code"], MID_mapping_df["Replacement"]))
|
| 145 |
-
merged_df["MID_Code"] = merged_df["MID_Code"].map(mid_map).fillna(merged_df["MID_Code"])
|
| 146 |
-
|
| 147 |
for col in column_order:
|
| 148 |
if col not in merged_df.columns:
|
| 149 |
merged_df[col] = ""
|
| 150 |
merged_df = merged_df[column_order]
|
| 151 |
|
| 152 |
-
# Save merged output partitioned
|
| 153 |
outputs = []
|
| 154 |
-
|
| 155 |
-
|
| 156 |
-
|
| 157 |
-
suffix = chr(65 + i) if i < 26 else f"part{i + 1}" # A, B, C... then part27+
|
| 158 |
filename = f"{mawb} T01 Manifest - {suffix}.xlsx"
|
| 159 |
-
with pd.ExcelWriter(filename, engine='
|
| 160 |
chunk.to_excel(writer, sheet_name="Converted", index=False)
|
|
|
|
| 161 |
worksheet = writer.sheets['Converted']
|
| 162 |
-
|
| 163 |
-
|
| 164 |
-
|
| 165 |
-
|
| 166 |
-
for cell in column_cells:
|
| 167 |
-
cell.number_format = "@"
|
| 168 |
outputs.append(filename)
|
| 169 |
|
| 170 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 171 |
|
|
|
|
| 172 |
# Gradio Interface
|
| 173 |
iface = gr.Interface(
|
| 174 |
fn=convert_ci,
|
|
@@ -176,9 +173,9 @@ iface = gr.Interface(
|
|
| 176 |
gr.File(label="Upload 'By Line Item' File (.xlsx)", type="filepath"),
|
| 177 |
gr.File(label="Upload 'Combined' File (.xlsx)", type="filepath"),
|
| 178 |
gr.Textbox(label="Password for 'By Line Item' File", type="password"),
|
| 179 |
-
gr.Number(label="Max Rows per Output File", value=
|
| 180 |
],
|
| 181 |
-
outputs=gr.File(label="Download
|
| 182 |
title="SHEIN CI to Magaya Format Conversion Tool",
|
| 183 |
description="Upload both 'By Line Item' (with password) and 'Combined' Excel files to merge and generate the Manifest in Magaya T01 Format."
|
| 184 |
)
|
|
|
|
| 31 |
description = re.sub(r"\d+", "", description)
|
| 32 |
return re.sub(r"[^\w\s]", "", description)
|
| 33 |
|
|
|
|
| 34 |
by_line_item_df["Part"] = ""
|
| 35 |
by_line_item_df["Tariff_Number"] = by_line_item_df.iloc[:, 1].apply(lambda x: str(x).replace('.0', '') if isinstance(x, float) and str(x).endswith('.0') else str(x))
|
| 36 |
by_line_item_df["Commercial_Description"] = by_line_item_df.apply(clean_desc, axis=1)
|
|
|
|
| 137 |
'CVD_Case_Number', 'AD_Non_Reimbursement_Statement',
|
| 138 |
'AD-CVD_Certification_Designation'
|
| 139 |
]
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 140 |
for col in column_order:
|
| 141 |
if col not in merged_df.columns:
|
| 142 |
merged_df[col] = ""
|
| 143 |
merged_df = merged_df[column_order]
|
| 144 |
|
| 145 |
+
# Save merged output partitioned by user-defined max_rows
|
| 146 |
outputs = []
|
| 147 |
+
for i, start in enumerate(range(0, len(merged_df), int(max_rows))):
|
| 148 |
+
chunk = merged_df.iloc[start : start + int(max_rows)]
|
| 149 |
+
suffix = chr(65 + i) if i < 26 else f"part{i + 1}"
|
|
|
|
| 150 |
filename = f"{mawb} T01 Manifest - {suffix}.xlsx"
|
| 151 |
+
with pd.ExcelWriter(filename, engine='xlsxwriter') as writer:
|
| 152 |
chunk.to_excel(writer, sheet_name="Converted", index=False)
|
| 153 |
+
workbook = writer.book
|
| 154 |
worksheet = writer.sheets['Converted']
|
| 155 |
+
# Format all cells as text
|
| 156 |
+
text_format = workbook.add_format({'num_format': '@'})
|
| 157 |
+
worksheet.set_column(0, len(chunk.columns) - 1, None, text_format)
|
| 158 |
+
|
|
|
|
|
|
|
| 159 |
outputs.append(filename)
|
| 160 |
|
| 161 |
+
import zipfile
|
| 162 |
+
zip_name = f"{mawb} T01 Manifest.zip"
|
| 163 |
+
with zipfile.ZipFile(zip_name, 'w') as zipf:
|
| 164 |
+
for file in outputs:
|
| 165 |
+
zipf.write(file, arcname=file)
|
| 166 |
+
return zip_name
|
| 167 |
|
| 168 |
+
# Gradio Interface
|
| 169 |
# Gradio Interface
|
| 170 |
iface = gr.Interface(
|
| 171 |
fn=convert_ci,
|
|
|
|
| 173 |
gr.File(label="Upload 'By Line Item' File (.xlsx)", type="filepath"),
|
| 174 |
gr.File(label="Upload 'Combined' File (.xlsx)", type="filepath"),
|
| 175 |
gr.Textbox(label="Password for 'By Line Item' File", type="password"),
|
| 176 |
+
gr.Number(label="Max Rows per Output File", value=700, precision=0)
|
| 177 |
],
|
| 178 |
+
outputs=gr.File(label="Download Zipped Excel Output", file_types=[".zip"]),
|
| 179 |
title="SHEIN CI to Magaya Format Conversion Tool",
|
| 180 |
description="Upload both 'By Line Item' (with password) and 'Combined' Excel files to merge and generate the Manifest in Magaya T01 Format."
|
| 181 |
)
|