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

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +57 -56
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
- # Output headers
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"].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,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()