Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
|
@@ -3,6 +3,7 @@ import pandas as pd
|
|
| 3 |
import re
|
| 4 |
import unicodedata
|
| 5 |
import io
|
|
|
|
| 6 |
|
| 7 |
# ---------- Helper Functions ----------
|
| 8 |
|
|
@@ -60,7 +61,7 @@ def flow_address_lines(lines, maxlen=35, maxlines=3):
|
|
| 60 |
return [ln[:maxlen] for ln in out]
|
| 61 |
|
| 62 |
def convert_dry_ice_kg(x):
|
| 63 |
-
"""Convert lbs
|
| 64 |
if pd.isna(x) or str(x).strip() == "":
|
| 65 |
return ""
|
| 66 |
try:
|
|
@@ -79,11 +80,11 @@ def clean_csv(file):
|
|
| 79 |
|
| 80 |
df.columns = df.columns.str.strip()
|
| 81 |
|
| 82 |
-
#
|
| 83 |
if "ZipCode" in df.columns:
|
| 84 |
df["ZipCode"] = df["ZipCode"].map(format_zip)
|
| 85 |
|
| 86 |
-
#
|
| 87 |
addr1, addr2, addr3 = [], [], []
|
| 88 |
for _, row in df.iterrows():
|
| 89 |
a1, a2, a3 = flow_address_lines([
|
|
@@ -96,33 +97,33 @@ def clean_csv(file):
|
|
| 96 |
df["Address2"] = addr2
|
| 97 |
df["Address3"] = addr3
|
| 98 |
|
| 99 |
-
# Clean
|
| 100 |
-
|
| 101 |
-
for col in text_cols:
|
| 102 |
if col in df.columns:
|
| 103 |
df[col] = df[col].map(clean_text)
|
| 104 |
|
| 105 |
-
# Dry Ice
|
| 106 |
if "Dry Ice Weight" in df.columns:
|
| 107 |
df["Dry Ice Weight (kg)"] = df["Dry Ice Weight"].map(convert_dry_ice_kg)
|
| 108 |
|
| 109 |
-
# Save to
|
| 110 |
-
|
| 111 |
-
df.to_csv(
|
| 112 |
-
|
| 113 |
-
|
|
|
|
| 114 |
|
| 115 |
# ---------- Gradio UI ----------
|
| 116 |
|
| 117 |
title = "UPS Shipment CSV Cleaner"
|
| 118 |
description = """
|
| 119 |
-
Upload your **raw shipment CSV file
|
| 120 |
-
|
| 121 |
-
- Remove
|
| 122 |
-
- Pad ZIP codes to 5 digits
|
| 123 |
-
- Split long addresses into
|
| 124 |
-
- Convert Dry Ice Weight
|
| 125 |
-
Then download
|
| 126 |
"""
|
| 127 |
|
| 128 |
demo = gr.Interface(
|
|
|
|
| 3 |
import re
|
| 4 |
import unicodedata
|
| 5 |
import io
|
| 6 |
+
import tempfile
|
| 7 |
|
| 8 |
# ---------- Helper Functions ----------
|
| 9 |
|
|
|
|
| 61 |
return [ln[:maxlen] for ln in out]
|
| 62 |
|
| 63 |
def convert_dry_ice_kg(x):
|
| 64 |
+
"""Convert lbs → kg and round."""
|
| 65 |
if pd.isna(x) or str(x).strip() == "":
|
| 66 |
return ""
|
| 67 |
try:
|
|
|
|
| 80 |
|
| 81 |
df.columns = df.columns.str.strip()
|
| 82 |
|
| 83 |
+
# ZIP correction
|
| 84 |
if "ZipCode" in df.columns:
|
| 85 |
df["ZipCode"] = df["ZipCode"].map(format_zip)
|
| 86 |
|
| 87 |
+
# Split address lines
|
| 88 |
addr1, addr2, addr3 = [], [], []
|
| 89 |
for _, row in df.iterrows():
|
| 90 |
a1, a2, a3 = flow_address_lines([
|
|
|
|
| 97 |
df["Address2"] = addr2
|
| 98 |
df["Address3"] = addr3
|
| 99 |
|
| 100 |
+
# Clean key fields
|
| 101 |
+
for col in ["Company Name", "Contact Name", "City", "State", "Phone Number", "Email"]:
|
|
|
|
| 102 |
if col in df.columns:
|
| 103 |
df[col] = df[col].map(clean_text)
|
| 104 |
|
| 105 |
+
# Convert Dry Ice Weight
|
| 106 |
if "Dry Ice Weight" in df.columns:
|
| 107 |
df["Dry Ice Weight (kg)"] = df["Dry Ice Weight"].map(convert_dry_ice_kg)
|
| 108 |
|
| 109 |
+
# Save to a temp file for download
|
| 110 |
+
temp_file = tempfile.NamedTemporaryFile(delete=False, suffix=".csv")
|
| 111 |
+
df.to_csv(temp_file.name, index=False, encoding="utf-8-sig")
|
| 112 |
+
temp_file.close()
|
| 113 |
+
|
| 114 |
+
return temp_file.name # ✅ Return single file path
|
| 115 |
|
| 116 |
# ---------- Gradio UI ----------
|
| 117 |
|
| 118 |
title = "UPS Shipment CSV Cleaner"
|
| 119 |
description = """
|
| 120 |
+
Upload your **raw shipment CSV file**.
|
| 121 |
+
The tool will:
|
| 122 |
+
- Remove bad characters (e.g. ÿ)
|
| 123 |
+
- Pad ZIP codes to 5 digits
|
| 124 |
+
- Split long addresses into 35-character lines
|
| 125 |
+
- Convert Dry Ice Weight (lbs → kg)
|
| 126 |
+
Then download your cleaned CSV ready for UPS Batch import.
|
| 127 |
"""
|
| 128 |
|
| 129 |
demo = gr.Interface(
|