leadingbridge commited on
Commit
78f2a85
·
verified ·
1 Parent(s): 257c894

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +32 -27
app.py CHANGED
@@ -3,7 +3,8 @@ 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()
@@ -16,7 +17,7 @@ def process_file(file):
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",
@@ -46,12 +47,12 @@ def process_file(file):
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
57
  if "Shipping City" in df.columns:
@@ -61,21 +62,23 @@ def process_file(file):
61
  if "Shipping Province" in df.columns:
62
  output_df["Province/State"] = df["Shipping Province"]
63
 
64
- # 6. ZipCode (pad 4-digit US zips with a leading zero + apostrophe for text)
65
- if "Shipping ZIP" in df.columns:
66
- zip_raw = df["Shipping ZIP"].astype(str).str.strip()
67
- mask_us = output_df["CountryCode"] == "US"
68
- mask_4 = zip_raw.str.len() == 4
69
- # pad to 5 digits
70
- zip_padded = zip_raw.where(~(mask_us & mask_4), "0" + zip_raw)
71
- # prefix apostrophe so Excel treats it as text and preserves leading zero
72
- zip_final = zip_padded.where(~(mask_us & mask_4), "'" + zip_padded)
73
- output_df["ZipCode"] = zip_final
74
-
75
- # 7. CountryCode
76
  if "Shipping Country Code" in df.columns:
77
  output_df["CountryCode"] = df["Shipping Country Code"]
78
 
 
 
 
 
 
 
 
 
 
 
 
 
 
79
  # 8. phone
80
  if "Shipping Address Phone" in df.columns:
81
  output_df["phone"] = df["Shipping Address Phone"]
@@ -89,18 +92,19 @@ def process_file(file):
89
  output_df["DeclaredQuantity1"] = df.groupby("Order Number")["Quantity"].transform("sum")
90
 
91
  # 11. Fixed defaults and conditional RoutingCode
92
- mask = output_df["CustomerOrderNo."].notna() & (output_df["CustomerOrderNo."] != "")
93
- mask_norway = mask & (output_df["CountryCode"] == "NO")
94
- mask_other = mask & (output_df["CountryCode"] != "NO")
95
 
96
- output_df.loc[mask_other, "RoutingCode"] = "HKTHZXR"
97
- output_df.loc[mask_norway, "RoutingCode"] = "HK-ASS-PF"
 
 
 
98
 
99
  output_df.loc[mask, "UnitPrice1"] = 2
100
  output_df.loc[mask, "CurrencyCode"] = "USD"
101
  output_df.loc[mask, "ItemDescription1"] = "Eye Cosmetic Accessories"
102
- output_df.loc[mask, "UnitWeight1"] = 0.02
103
  output_df.loc[mask, "ForeignItemDescription1"] = "Eye Cosmetic Accessories"
 
104
 
105
  EU_COUNTRIES = {"AT","BE","BG","CY","CZ","DE","DK","EE","ES","FI",
106
  "FR","HR","HU","IE","IT","LT","LU","LV","MT","NL",
@@ -121,6 +125,7 @@ def process_file(file):
121
 
122
  return output_df, output_file_name
123
 
 
124
  # Gradio interface
125
  with gr.Blocks(title="Shipping - YunExpress") as demo:
126
  gr.Markdown("# Shipping - YunExpress")
@@ -146,10 +151,10 @@ with gr.Blocks(title="Shipping - YunExpress") as demo:
146
  <div style="text-align: center; font-size: 16px; margin-top: 20px;">
147
  <h3>Administration Tools</h3>
148
  <a href="https://huggingface.co/spaces/leadingbridge/email-template">Email Template</a> |
149
- <a href="https://huggingface.co/spaces/leadingbridge/product-feed">Google Merchant</a> |
150
- <a href="https://huggingface.co/spaces/leadingbridge/tss-order">Order Processing</a>
151
  </div>
152
  """
153
  )
154
 
155
- demo.launch()
 
3
  from datetime import datetime
4
  import pytz
5
 
6
+ # Updated Gradio app for YunExpress with new header mappings, hyperlinks,
7
+ # ZIP padding bug fix, and RoutingCode change (US/NO -> HK-ASS-PF; others -> HKTHZXR)
8
 
9
  def process_file(file):
10
  file_name = file.name.lower()
 
17
  except Exception as e:
18
  return f"Error reading file: {e}", None
19
 
20
+ # New output headers as requested (kept as in original)
21
  output_headers = [
22
  "CustomerOrderNo.", "RoutingCode", "Trackingnumber", "AdditionalServices",
23
  "ShipmentProtectionPlusService", "SignatureService", "VatNumber", "EoriNumber",
 
47
  first = df["Shipping First Name"].fillna("").astype(str).str.strip()
48
  last = df["Shipping Last Name"].fillna("").astype(str).str.strip()
49
  comp = df["Shipping Company"].fillna("").astype(str).str.strip()
50
+ output_df["Name"] = (first + " " + last + " " + comp).str.replace(r"\s+", " ", regex=True).str.strip()
51
 
52
  # 3. Street: Address 1 + Address 2 → Street
53
+ addr1 = df.get("Shipping Address 1", pd.Series([""] * len(df), index=df.index)).fillna("").astype(str).str.strip()
54
+ addr2 = df.get("Shipping Address 2", pd.Series([""] * len(df), index=df.index)).fillna("").astype(str).str.strip()
55
+ output_df["Street"] = (addr1 + " " + addr2).str.replace(r"\s+", " ", regex=True).str.strip()
56
 
57
  # 4. City
58
  if "Shipping City" in df.columns:
 
62
  if "Shipping Province" in df.columns:
63
  output_df["Province/State"] = df["Shipping Province"]
64
 
65
+ # 7. CountryCode (moved BEFORE ZIP handling to fix the bug)
 
 
 
 
 
 
 
 
 
 
 
66
  if "Shipping Country Code" in df.columns:
67
  output_df["CountryCode"] = df["Shipping Country Code"]
68
 
69
+ # 6. ZipCode (pad 4-digit US zips to 5 with leading zero; prefix apostrophe so Excel keeps it)
70
+ if "Shipping ZIP" in df.columns:
71
+ zip_raw = (
72
+ df["Shipping ZIP"]
73
+ .astype(str)
74
+ .str.strip()
75
+ .str.replace(r"\.0$", "", regex=True) # clean "1234.0"
76
+ )
77
+ mask_us = output_df["CountryCode"].eq("US")
78
+ mask_4 = zip_raw.str.fullmatch(r"\d{4}")
79
+ zip_padded = zip_raw.where(~(mask_us & mask_4), "0" + zip_raw)
80
+ output_df["ZipCode"] = zip_padded.where(~(mask_us & mask_4), "'" + zip_padded)
81
+
82
  # 8. phone
83
  if "Shipping Address Phone" in df.columns:
84
  output_df["phone"] = df["Shipping Address Phone"]
 
92
  output_df["DeclaredQuantity1"] = df.groupby("Order Number")["Quantity"].transform("sum")
93
 
94
  # 11. Fixed defaults and conditional RoutingCode
95
+ mask = output_df["CustomerOrderNo."].astype(str).str.len() > 0
 
 
96
 
97
+ # RoutingCode: HK-ASS-PF if CountryCode ∈ {NO, US}; otherwise HKTHZXR
98
+ mask_us_or_no = mask & output_df["CountryCode"].isin(["US", "NO"])
99
+ mask_other = mask & ~output_df["CountryCode"].isin(["US", "NO"])
100
+ output_df.loc[mask_us_or_no, "RoutingCode"] = "HK-ASS-PF"
101
+ output_df.loc[mask_other, "RoutingCode"] = "HKTHZXR"
102
 
103
  output_df.loc[mask, "UnitPrice1"] = 2
104
  output_df.loc[mask, "CurrencyCode"] = "USD"
105
  output_df.loc[mask, "ItemDescription1"] = "Eye Cosmetic Accessories"
 
106
  output_df.loc[mask, "ForeignItemDescription1"] = "Eye Cosmetic Accessories"
107
+ output_df.loc[mask, "UnitWeight1"] = 0.02
108
 
109
  EU_COUNTRIES = {"AT","BE","BG","CY","CZ","DE","DK","EE","ES","FI",
110
  "FR","HR","HU","IE","IT","LT","LU","LV","MT","NL",
 
125
 
126
  return output_df, output_file_name
127
 
128
+
129
  # Gradio interface
130
  with gr.Blocks(title="Shipping - YunExpress") as demo:
131
  gr.Markdown("# Shipping - YunExpress")
 
151
  <div style="text-align: center; font-size: 16px; margin-top: 20px;">
152
  <h3>Administration Tools</h3>
153
  <a href="https://huggingface.co/spaces/leadingbridge/email-template">Email Template</a> |
154
+ <a href="https://huggingface.co/spaces/leadingbridge/product-feeding">Google Merchant</a> |
155
+ <a href="https://huggingface.co/spaces/leadingbridge/tss">Order Processing</a>
156
  </div>
157
  """
158
  )
159
 
160
+ demo.launch()