Update app.py
Browse files
app.py
CHANGED
@@ -16,7 +16,7 @@ def process_file(file):
|
|
16 |
except Exception as e:
|
17 |
return f"Error reading file: {e}", None
|
18 |
|
19 |
-
#
|
20 |
output_headers = [
|
21 |
"CustomerOrderNo.", "RoutingCode", "Trackingnumber", "AdditionalServices",
|
22 |
"ShipmentProtectionPlusService", "SignatureService", "VatNumber", "EoriNumber",
|
@@ -34,22 +34,23 @@ def process_file(file):
|
|
34 |
"PaymentTransactionNumber"
|
35 |
]
|
36 |
|
|
|
37 |
output_df = pd.DataFrame("", index=range(len(df)), columns=output_headers)
|
38 |
|
39 |
# 1. Order Number → CustomerOrderNo. (prefix "LB")
|
40 |
if "Order Number" in df.columns:
|
41 |
output_df["CustomerOrderNo."] = "LB" + df["Order Number"].astype(str)
|
42 |
|
43 |
-
# 2. Name
|
44 |
-
if {"Shipping First Name","Shipping Last Name","Shipping Company"}.issubset(df.columns):
|
45 |
first = df["Shipping First Name"].fillna("").astype(str).str.strip()
|
46 |
last = df["Shipping Last Name"].fillna("").astype(str).str.strip()
|
47 |
comp = df["Shipping Company"].fillna("").astype(str).str.strip()
|
48 |
output_df["Name"] = (first + " " + last + " " + comp).str.strip()
|
49 |
|
50 |
-
# 3. Street
|
51 |
-
addr1 = df.get("Shipping Address 1", pd.Series([""]*len(df))).fillna("").astype(str).str.strip()
|
52 |
-
addr2 = df.get("Shipping Address 2", pd.Series([""]*len(df))).fillna("").astype(str).str.strip()
|
53 |
output_df["Street"] = (addr1 + " " + addr2).str.strip()
|
54 |
|
55 |
# 4. City
|
@@ -62,74 +63,74 @@ def process_file(file):
|
|
62 |
|
63 |
# 6. ZipCode
|
64 |
if "Shipping ZIP" in df.columns:
|
65 |
-
output_df["ZipCode"] = df["Shipping ZIP"]
|
66 |
|
67 |
# 7. CountryCode
|
68 |
if "Shipping Country Code" in df.columns:
|
69 |
output_df["CountryCode"] = df["Shipping Country Code"]
|
70 |
|
71 |
-
# 8.
|
72 |
-
zip_str = output_df["ZipCode"].fillna("")
|
73 |
-
mask_us = (output_df.get("CountryCode","") == "US") & zip_str.str.match(r'^\d{4}$')
|
74 |
-
output_df.loc[mask_us, "ZipCode"] = "'" + zip_str.str.zfill(5)
|
75 |
-
|
76 |
-
# 9. phone
|
77 |
if "Shipping Address Phone" in df.columns:
|
78 |
output_df["phone"] = df["Shipping Address Phone"]
|
79 |
|
80 |
-
#
|
81 |
if "Total Weight" in df.columns:
|
82 |
output_df["PackageWeight"] = df["Total Weight"] / 1000
|
83 |
|
84 |
-
#
|
85 |
if "Order Number" in df.columns and "Quantity" in df.columns:
|
86 |
output_df["DeclaredQuantity1"] = df.groupby("Order Number")["Quantity"].transform("sum")
|
87 |
|
88 |
-
#
|
89 |
-
mask = output_df["CustomerOrderNo."].
|
90 |
-
|
91 |
-
|
92 |
-
output_df
|
93 |
-
|
94 |
-
|
95 |
-
|
96 |
-
|
97 |
-
|
98 |
-
|
99 |
-
|
100 |
-
|
101 |
-
|
102 |
-
|
103 |
-
|
104 |
-
|
105 |
-
|
106 |
-
|
107 |
-
|
108 |
-
mask_eu =
|
109 |
-
|
110 |
-
|
111 |
-
# Remove duplicates
|
112 |
output_df = output_df.drop_duplicates(subset=["CustomerOrderNo."], keep="first")
|
113 |
|
114 |
-
#
|
115 |
-
|
116 |
-
|
117 |
-
|
118 |
-
|
119 |
-
|
|
|
|
|
|
|
120 |
|
121 |
-
#
|
122 |
-
|
123 |
-
with demo:
|
124 |
gr.Markdown("# Shipping - YunExpress")
|
125 |
-
|
126 |
-
|
127 |
-
|
128 |
-
|
129 |
-
|
|
|
|
|
|
|
|
|
130 |
gr.HTML(
|
131 |
"""
|
132 |
-
<div style="text-align:center;font-size:16px;margin-top:20px;">
|
133 |
<h3>Shipping Tools</h3>
|
134 |
<a href="https://huggingface.co/spaces/leadingbridge/shipping-dhl-e-commerce">DHL</a> |
|
135 |
<a href="https://huggingface.co/spaces/leadingbridge/shipping-ec-ship">EC-Ship</a> |
|
@@ -137,7 +138,7 @@ with demo:
|
|
137 |
<a href="https://huggingface.co/spaces/leadingbridge/shipping-UPS">UPS</a><br>
|
138 |
<a href="https://huggingface.co/spaces/leadingbridge/shipping-yunexpress">Yunexpress</a>
|
139 |
</div>
|
140 |
-
<div style="text-align:center;font-size:16px;margin-top:20px;">
|
141 |
<h3>Administration Tools</h3>
|
142 |
<a href="https://huggingface.co/spaces/leadingbridge/email-template">Email Template</a> |
|
143 |
<a href="https://huggingface.co/spaces/leadingbridge/product-feed">Google Merchant</a> |
|
@@ -146,4 +147,4 @@ with demo:
|
|
146 |
"""
|
147 |
)
|
148 |
|
149 |
-
demo.launch()
|
|
|
16 |
except Exception as e:
|
17 |
return f"Error reading file: {e}", None
|
18 |
|
19 |
+
# New output headers as requested
|
20 |
output_headers = [
|
21 |
"CustomerOrderNo.", "RoutingCode", "Trackingnumber", "AdditionalServices",
|
22 |
"ShipmentProtectionPlusService", "SignatureService", "VatNumber", "EoriNumber",
|
|
|
34 |
"PaymentTransactionNumber"
|
35 |
]
|
36 |
|
37 |
+
# Initialize empty output DataFrame
|
38 |
output_df = pd.DataFrame("", index=range(len(df)), columns=output_headers)
|
39 |
|
40 |
# 1. Order Number → CustomerOrderNo. (prefix "LB")
|
41 |
if "Order Number" in df.columns:
|
42 |
output_df["CustomerOrderNo."] = "LB" + df["Order Number"].astype(str)
|
43 |
|
44 |
+
# 2. Name mapping: First + Last + Company → Name
|
45 |
+
if {"Shipping First Name", "Shipping Last Name", "Shipping Company"}.issubset(df.columns):
|
46 |
first = df["Shipping First Name"].fillna("").astype(str).str.strip()
|
47 |
last = df["Shipping Last Name"].fillna("").astype(str).str.strip()
|
48 |
comp = df["Shipping Company"].fillna("").astype(str).str.strip()
|
49 |
output_df["Name"] = (first + " " + last + " " + comp).str.strip()
|
50 |
|
51 |
+
# 3. Street: Address 1 + Address 2 → Street
|
52 |
+
addr1 = df.get("Shipping Address 1", pd.Series([""] * len(df))).fillna("").astype(str).str.strip()
|
53 |
+
addr2 = df.get("Shipping Address 2", pd.Series([""] * len(df))).fillna("").astype(str).str.strip()
|
54 |
output_df["Street"] = (addr1 + " " + addr2).str.strip()
|
55 |
|
56 |
# 4. City
|
|
|
63 |
|
64 |
# 6. ZipCode
|
65 |
if "Shipping ZIP" in df.columns:
|
66 |
+
output_df["ZipCode"] = df["Shipping ZIP"]
|
67 |
|
68 |
# 7. CountryCode
|
69 |
if "Shipping Country Code" in df.columns:
|
70 |
output_df["CountryCode"] = df["Shipping Country Code"]
|
71 |
|
72 |
+
# 8. phone
|
|
|
|
|
|
|
|
|
|
|
73 |
if "Shipping Address Phone" in df.columns:
|
74 |
output_df["phone"] = df["Shipping Address Phone"]
|
75 |
|
76 |
+
# 9. PackageWeight: Total Weight (g) → kg
|
77 |
if "Total Weight" in df.columns:
|
78 |
output_df["PackageWeight"] = df["Total Weight"] / 1000
|
79 |
|
80 |
+
# 10. DeclaredQuantity1: sum of Quantity per Order Number
|
81 |
if "Order Number" in df.columns and "Quantity" in df.columns:
|
82 |
output_df["DeclaredQuantity1"] = df.groupby("Order Number")["Quantity"].transform("sum")
|
83 |
|
84 |
+
# 11. Fixed defaults and conditional RoutingCode
|
85 |
+
mask = output_df["CustomerOrderNo."].notna() & (output_df["CustomerOrderNo."] != "")
|
86 |
+
# Norway code
|
87 |
+
mask_norway = mask & (output_df["CountryCode"] == "NO")
|
88 |
+
mask_other = mask & (output_df["CountryCode"] != "NO")
|
89 |
+
# RoutingCode assignments
|
90 |
+
output_df.loc[mask_other, "RoutingCode"] = "HKTHZXR"
|
91 |
+
output_df.loc[mask_norway, "RoutingCode"] = "HK-ASS-PF"
|
92 |
+
# Other fixed values
|
93 |
+
output_df.loc[mask, "UnitPrice1"] = 2
|
94 |
+
output_df.loc[mask, "CurrencyCode"] = "USD"
|
95 |
+
output_df.loc[mask, "ItemDescription1"] = "Eye Cosmetic Accessories"
|
96 |
+
output_df.loc[mask, "UnitWeight1"] = 0.02
|
97 |
+
# ForeignItemDescription1
|
98 |
+
output_df.loc[mask, "ForeignItemDescription1"] = "Eye Cosmetic Accessories"
|
99 |
+
# EU AdditionalServices
|
100 |
+
EU_COUNTRIES = {"AT","BE","BG","CY","CZ","DE","DK","EE","ES","FI",
|
101 |
+
"FR","HR","HU","IE","IT","LT","LU","LV","MT","NL",
|
102 |
+
"PL","PT","RO","SE","SI","SK","GR"}
|
103 |
+
mask_eu = mask & output_df["CountryCode"].isin(EU_COUNTRIES)
|
104 |
+
output_df.loc[mask_eu, "AdditionalServices"] = "v1"
|
105 |
+
|
106 |
+
# 12. Remove duplicate rows based on CustomerOrderNo.
|
|
|
107 |
output_df = output_df.drop_duplicates(subset=["CustomerOrderNo."], keep="first")
|
108 |
|
109 |
+
# 13. Generate output filename using HK date
|
110 |
+
hk_tz = pytz.timezone("Asia/Hong_Kong")
|
111 |
+
today_hk = datetime.now(hk_tz).strftime("%y%m%d")
|
112 |
+
output_file_name = f"yunexpress {today_hk}.xlsx"
|
113 |
+
|
114 |
+
# Save to Excel
|
115 |
+
output_df.to_excel(output_file_name, index=False)
|
116 |
+
|
117 |
+
return output_df, output_file_name
|
118 |
|
119 |
+
# Gradio interface
|
120 |
+
with gr.Blocks(title="Shipping - YunExpress") as demo:
|
|
|
121 |
gr.Markdown("# Shipping - YunExpress")
|
122 |
+
with gr.Row():
|
123 |
+
file_input = gr.File(label="Upload Excel File with a YunExpress sheet")
|
124 |
+
process_button = gr.Button("Process Data")
|
125 |
+
with gr.Row():
|
126 |
+
output_data = gr.DataFrame()
|
127 |
+
output_file_component = gr.File(label="Download Processed File")
|
128 |
+
process_button.click(fn=process_file, inputs=[file_input], outputs=[output_data, output_file_component])
|
129 |
+
|
130 |
+
# Restored original hyperlinks + Yunexpress link
|
131 |
gr.HTML(
|
132 |
"""
|
133 |
+
<div style="text-align: center; font-size: 16px; margin-top: 20px;">
|
134 |
<h3>Shipping Tools</h3>
|
135 |
<a href="https://huggingface.co/spaces/leadingbridge/shipping-dhl-e-commerce">DHL</a> |
|
136 |
<a href="https://huggingface.co/spaces/leadingbridge/shipping-ec-ship">EC-Ship</a> |
|
|
|
138 |
<a href="https://huggingface.co/spaces/leadingbridge/shipping-UPS">UPS</a><br>
|
139 |
<a href="https://huggingface.co/spaces/leadingbridge/shipping-yunexpress">Yunexpress</a>
|
140 |
</div>
|
141 |
+
<div style="text-align: center; font-size: 16px; margin-top: 20px;">
|
142 |
<h3>Administration Tools</h3>
|
143 |
<a href="https://huggingface.co/spaces/leadingbridge/email-template">Email Template</a> |
|
144 |
<a href="https://huggingface.co/spaces/leadingbridge/product-feed">Google Merchant</a> |
|
|
|
147 |
"""
|
148 |
)
|
149 |
|
150 |
+
demo.launch()
|