Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
@@ -1,70 +1,78 @@
|
|
1 |
import gradio as gr
|
2 |
import pandas as pd
|
3 |
from datetime import datetime
|
|
|
|
|
4 |
|
5 |
-
def process_file(
|
6 |
-
# 1.
|
7 |
-
|
8 |
-
if not
|
9 |
-
return "
|
10 |
|
11 |
-
|
12 |
-
|
13 |
-
|
|
|
14 |
|
15 |
-
#
|
16 |
output_headers = [
|
17 |
"Usage", "District", "Address", "Longitude", "Latitude",
|
18 |
-
"Floor", "Unit", "Area", "PriceInMillion",
|
19 |
-
"
|
20 |
-
"WeekNumber", "DeliveryDate", "MemoNo."
|
21 |
]
|
22 |
-
output_df = pd.DataFrame("", index=range(len(df)), columns=output_headers)
|
23 |
|
24 |
-
#
|
25 |
-
output_df
|
26 |
-
output_df["Floor"] = df.iloc[:, 1] # col 2 → Floor
|
27 |
-
output_df["Unit"] = df.iloc[:, 2] # col 3 → Unit
|
28 |
-
output_df["Area"] = df.iloc[:, 3] # col 4 → Area
|
29 |
-
output_df["PriceInMillion"] = pd.to_numeric(
|
30 |
-
df.iloc[:, 4]
|
31 |
-
.replace(r"[^0-9\.]", "", regex=True),
|
32 |
-
errors="coerce"
|
33 |
-
) # col 5 → PriceInMillion
|
34 |
-
output_df["PricePerSquareFeet"] = pd.to_numeric(
|
35 |
-
df.iloc[:, 5]
|
36 |
-
.replace(r"[^0-9\.]", "", regex=True),
|
37 |
-
errors="coerce"
|
38 |
-
) # col 6 → PricePerSquareFeet
|
39 |
-
output_df["InstrumentDate"] = pd.to_datetime(
|
40 |
-
df.iloc[:, 6],
|
41 |
-
errors="coerce"
|
42 |
-
) # col 7 → InstrumentDate
|
43 |
|
44 |
-
#
|
45 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
46 |
output_df["WeekNumber"] = output_df["InstrumentDate"].dt.isocalendar().week
|
47 |
|
48 |
-
# 6.
|
49 |
-
#
|
50 |
-
|
|
|
|
|
|
|
|
|
51 |
|
52 |
-
#
|
53 |
-
|
54 |
-
out_name = f"data-clean-{suffix}.xlsx"
|
55 |
|
56 |
-
#
|
57 |
-
output_df
|
58 |
-
return output_df, out_name
|
59 |
|
60 |
-
with gr.Blocks(title="
|
61 |
-
gr.Markdown("##
|
62 |
with gr.Row():
|
63 |
-
|
64 |
-
|
65 |
with gr.Row():
|
66 |
-
df_out
|
67 |
-
|
68 |
-
|
|
|
|
|
|
|
|
|
69 |
|
70 |
-
|
|
|
|
1 |
import gradio as gr
|
2 |
import pandas as pd
|
3 |
from datetime import datetime
|
4 |
+
import pytz
|
5 |
+
import os
|
6 |
|
7 |
+
def process_file(uploaded_file):
|
8 |
+
# 1. Read Excel file (first sheet automatically)
|
9 |
+
fname = uploaded_file.name.lower()
|
10 |
+
if not fname.endswith(('.xls', '.xlsx', '.xlsm')):
|
11 |
+
return "❌ Unsupported format. Please upload .xls/.xlsx/.xlsm", None
|
12 |
|
13 |
+
try:
|
14 |
+
df = pd.read_excel(uploaded_file.name)
|
15 |
+
except Exception as e:
|
16 |
+
return f"❌ Error reading file: {e}", None
|
17 |
|
18 |
+
# 2. Define the output headers in order
|
19 |
output_headers = [
|
20 |
"Usage", "District", "Address", "Longitude", "Latitude",
|
21 |
+
"Floor", "Unit", "Area", "PriceInMillion", "PricePerSquareFeet",
|
22 |
+
"InstrumentDate", "Year", "WeekNumber", "DeliveryDate", "MemoNo."
|
|
|
23 |
]
|
|
|
24 |
|
25 |
+
# 3. Prepare an empty DataFrame
|
26 |
+
output_df = pd.DataFrame(index=df.index, columns=output_headers)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
27 |
|
28 |
+
# 4. Map the first 7 columns from the input to the relevant fields:
|
29 |
+
# Col 1 → Address
|
30 |
+
# Col 2 → Floor
|
31 |
+
# Col 3 → Unit
|
32 |
+
# Col 4 → Area
|
33 |
+
# Col 5 → PriceInMillion
|
34 |
+
# Col 6 → PricePerSquareFeet
|
35 |
+
# Col 7 → InstrumentDate
|
36 |
+
output_df["Address"] = df.iloc[:, 0]
|
37 |
+
output_df["Floor"] = df.iloc[:, 1]
|
38 |
+
output_df["Unit"] = df.iloc[:, 2]
|
39 |
+
output_df["Area"] = df.iloc[:, 3]
|
40 |
+
output_df["PriceInMillion"] = df.iloc[:, 4]
|
41 |
+
output_df["PricePerSquareFeet"] = df.iloc[:, 5]
|
42 |
+
output_df["InstrumentDate"] = pd.to_datetime(df.iloc[:, 6], errors="coerce")
|
43 |
+
|
44 |
+
# 5. Derive Year and ISO Week Number from InstrumentDate
|
45 |
+
# Any invalid dates become NaT and yield NaN year/week
|
46 |
+
output_df["Year"] = output_df["InstrumentDate"].dt.year
|
47 |
output_df["WeekNumber"] = output_df["InstrumentDate"].dt.isocalendar().week
|
48 |
|
49 |
+
# 6. Leave Usage, District, Longitude, Latitude, DeliveryDate, MemoNo. empty
|
50 |
+
# (or populate them here if you have logic to do so)
|
51 |
+
|
52 |
+
# 7. Generate filename based on Hong Kong date
|
53 |
+
hk_tz = pytz.timezone("Asia/Hong_Kong")
|
54 |
+
today_hk = datetime.now(hk_tz).strftime("%y%m%d")
|
55 |
+
out_fname = f"data-clean-{today_hk}.xlsx"
|
56 |
|
57 |
+
# 8. Save to Excel (in the current working directory)
|
58 |
+
output_df.to_excel(out_fname, index=False)
|
|
|
59 |
|
60 |
+
# Return the DataFrame for preview and the path to download
|
61 |
+
return output_df, out_fname
|
|
|
62 |
|
63 |
+
with gr.Blocks(title="Data Cleaner") as demo:
|
64 |
+
gr.Markdown("## 🗂️ Excel → Cleaned Data Mapping")
|
65 |
with gr.Row():
|
66 |
+
file_input = gr.File(label="Upload .xls/.xlsx/.xlsm")
|
67 |
+
run_btn = gr.Button("Process")
|
68 |
with gr.Row():
|
69 |
+
df_out = gr.DataFrame(label="Mapped Data Preview")
|
70 |
+
download_btn = gr.File(label="Download Cleaned File")
|
71 |
+
run_btn.click(
|
72 |
+
fn=process_file,
|
73 |
+
inputs=[file_input],
|
74 |
+
outputs=[df_out, download_btn]
|
75 |
+
)
|
76 |
|
77 |
+
if __name__ == "__main__":
|
78 |
+
demo.launch()
|