leadingbridge commited on
Commit
ee9c648
·
verified ·
1 Parent(s): e433620

Update app.py

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