samurice commited on
Commit
420c0bc
·
verified ·
1 Parent(s): 0d59d66

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +194 -166
app.py CHANGED
@@ -1,29 +1,21 @@
 
1
  import pandas as pd
2
  import numpy as np
3
  from datetime import datetime, date, time
4
  from pathlib import Path
 
5
 
6
- # ===================== USER CONFIG =====================
7
- INPUT_XLSX = "data.xlsx" # your input file
8
- OUTPUT_XLSX = "ot_summary.xlsx" # output path
9
-
10
- # If your headers differ, you can set them here (case-insensitive). Otherwise auto-detect.
11
- EMPLOYEE_COL = None # e.g., "employee_id"
12
- DATE_COL = None # e.g., "date"
13
- TIME_COL = None # e.g., "time"
14
- EVENT_COL = None # e.g., "status" (values like P10/P20), or leave None to auto-detect
15
-
16
- # Policy:
17
- # - Baseline cutoff: 17:00 before 2025-07-07; 17:30 from 2025-07-07
18
- # - Case 4 (virtual check-in): 17:30 before 2025-07-07; 18:00 from 2025-07-07
19
- POLICY_CHANGE = date(2025, 7, 7)
20
- BASELINE_BEFORE = time(17, 0)
21
- BASELINE_AFTER = time(17, 30)
22
- VIRTUAL_BEFORE = time(17, 30)
23
- VIRTUAL_AFTER = time(18, 0)
24
- # =======================================================
25
-
26
- # ----------------- helpers -----------------
27
  def parse_yyyymmdd(s):
28
  s = str(s).strip()
29
  for fmt in ("%Y%m%d", "%Y-%m-%d", "%d/%m/%Y", "%m/%d/%Y"):
@@ -34,12 +26,10 @@ def parse_yyyymmdd(s):
34
  return pd.NaT
35
 
36
  def parse_hms(s):
37
- """
38
- Accept 'HH:MM[:SS]', 'HHMM', 'HHMMSS', or 5-digit like '82547' -> 08:25:47.
39
- """
40
  s = str(s).strip()
41
  if s.isdigit():
42
- s = s.zfill(6)[:6] # left-pad to 6 digits and cap at 6
43
  hh, mm, ss = s[:2], s[2:4], s[4:6]
44
  try:
45
  return time(int(hh), int(mm), int(ss))
@@ -57,170 +47,208 @@ def combine_dt(d, t):
57
  return pd.NaT
58
  return datetime.combine(d, t)
59
 
60
- def is_from_policy(d):
61
  return (not pd.isna(d)) and (d >= POLICY_CHANGE)
62
 
63
- def baseline_cutoff_time(d):
64
- return BASELINE_AFTER if is_from_policy(d) else BASELINE_BEFORE
65
 
66
- def virtual_checkin_time(d):
67
- return VIRTUAL_AFTER if is_from_policy(d) else VIRTUAL_BEFORE
68
-
69
- IN_TOKENS = {"P10", "IN", "INN", "CHECKIN", "CHECK-IN"}
70
- OUT_TOKENS = {"P20", "OUT", "CHECKOUT", "CHECK-OUT"}
71
 
72
  def detect_event_token(v):
73
  u = str(v).upper()
74
  is_in = any(tok == u or tok in u for tok in IN_TOKENS)
75
  is_out = any(tok == u or tok in u for tok in OUT_TOKENS)
76
- if is_in and not is_out:
77
- return "in"
78
- if is_out and not is_in:
79
- return "out"
80
  return np.nan
81
 
82
- def format_unique_times(ts_series):
83
- uniq = sorted({t.strftime("%H:%M:%S") for t in ts_series.dropna()})
84
- return ", ".join(uniq)
85
-
86
- # ----------------- load & normalize -----------------
87
- src = pd.read_excel(INPUT_XLSX, dtype=str)
88
- src.columns = [str(c).strip().lower() for c in src.columns]
89
 
90
- def find_col(candidates, fallback=None):
91
- for col in src.columns:
92
  for pat in candidates:
93
  if pat in col:
94
  return col
95
  return fallback
96
 
97
- emp_col = (EMPLOYEE_COL.lower() if EMPLOYEE_COL else None) or find_col(["emp", "employee", "staff", "id", "person"], src.columns[0] if len(src.columns)>0 else None)
98
- date_col = (DATE_COL.lower() if DATE_COL else None) or find_col(["date", "yyyymmdd", "ngay"], src.columns[1] if len(src.columns)>1 else None)
99
- time_col = (TIME_COL.lower() if TIME_COL else None) or find_col(["time", "gio", "hh", "clock"], src.columns[2] if len(src.columns)>2 else None)
 
100
 
101
- # try to guess a P10/P20 column if not specified
102
- if EVENT_COL and EVENT_COL.lower() in src.columns:
103
- evt_col = EVENT_COL.lower()
104
- else:
 
 
 
 
 
 
 
 
105
  evt_col = None
106
- for c in src.columns:
107
- vals = src[c].astype(str).str.upper()
108
- if vals.isin(["P10","P20"]).any() or vals.str.contains(r"\bP10\b|\bP20\b", regex=True).any():
109
  evt_col = c
110
  break
111
 
112
- if emp_col not in src.columns or date_col not in src.columns or time_col not in src.columns:
113
- raise ValueError(f"Could not find expected columns. Found: {list(src.columns)}")
114
-
115
- work = src.copy()
116
- work["__emp"] = work[emp_col].astype(str).str.strip()
117
- work["__date"] = work[date_col].apply(parse_yyyymmdd)
118
- work["__time"] = work[time_col].apply(parse_hms)
119
- work["__ts"] = [combine_dt(d, t) for d, t in zip(work["__date"], work["__time"])]
120
-
121
- # detect in/out (prefer dedicated event column; otherwise scan)
122
- if evt_col:
123
- work["__event"] = work[evt_col].apply(detect_event_token)
124
- else:
125
- def scan_row(row):
126
- for c in row.index:
127
- ev = detect_event_token(row[c])
128
- if pd.notna(ev):
129
- return ev
130
- return np.nan
131
- work["__event"] = work.apply(scan_row, axis=1)
132
-
133
- # build baseline & virtual cutoffs
134
- work["__baseline_t"] = work["__date"].apply(baseline_cutoff_time)
135
- work["__baseline_dt"] = [combine_dt(d, t) for d, t in zip(work["__date"], work["__baseline_t"])]
136
- work["__virtual_t"] = work["__date"].apply(virtual_checkin_time)
137
- work["__virtual_dt"] = [combine_dt(d, t) for d, t in zip(work["__date"], work["__virtual_t"])]
138
-
139
- # ----------------- compute OT per employee/date -----------------
140
- rows = []
141
- for (emp, d), full_day in work.groupby(["__emp", "__date"], sort=True):
142
- if pd.isna(d):
143
- continue
144
-
145
- # Sort all events of the day
146
- full_day = full_day.sort_values("__ts")
147
-
148
- baseline_dt = full_day["__baseline_dt"].iloc[0]
149
- virtual_dt = full_day["__virtual_dt"].iloc[0]
150
-
151
- # Determine if the person was already inside at the cutoff:
152
- pre = full_day[full_day["__ts"] < baseline_dt]
153
- if not pre.empty:
154
- last_pre_event = pre.iloc[-1]["__event"]
155
- inside_at_cutoff = (last_pre_event == "in")
156
  else:
157
- inside_at_cutoff = False
158
-
159
- # Consider only events at/after the baseline cutoff for outputs
160
- after = full_day[full_day["__ts"] >= baseline_dt].copy()
161
-
162
- # Unique in/out time strings (AFTER cutoff) for the report
163
- ins_after = after.loc[after["__event"].eq("in"), "__ts"]
164
- outs_after = after.loc[after["__event"].eq("out"), "__ts"]
165
- in_times_str = format_unique_times(ins_after)
166
- out_times_str = format_unique_times(outs_after)
167
-
168
- # Compute OT per policy
169
- code = ""
170
- ot_hours = np.nan
171
-
172
- if len(ins_after) > 0 and len(outs_after) > 0:
173
- # Case 1: both in and out after cutoff → earliest IN vs latest OUT
174
- earliest_in = ins_after.min()
175
- latest_out = outs_after.max()
176
- if pd.notna(earliest_in) and pd.notna(latest_out) and latest_out >= earliest_in:
177
- ot_hours = (latest_out - earliest_in).total_seconds() / 3600.0
178
- else:
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
179
  code = "alert"
180
 
181
- elif len(ins_after) == 0 and len(outs_after) > 0:
182
- # Case 2 or Case 4:
183
- # If they were already inside at the cutoff → Case 4 (use virtual check-in)
184
- # Else → Case 2 (use baseline)
185
- start_dt = virtual_dt if inside_at_cutoff else baseline_dt
186
- latest_out = outs_after.max()
187
- if pd.notna(latest_out) and latest_out >= start_dt:
188
- ot_hours = (latest_out - start_dt).total_seconds() / 3600.0
 
 
 
 
 
 
 
 
 
 
 
 
189
  else:
190
- ot_hours = 0.0 # guarded by baseline filter; keep 0 if anomalous
191
-
192
- elif len(ins_after) > 0 and len(outs_after) == 0:
193
- # Case 3: has IN but no OUT after cutoff
194
- code = "alert"
195
-
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
196
  else:
197
- # No events after cutoff → nothing to report
198
- continue
199
-
200
- ot_hours = round(float(ot_hours), 2) if pd.notna(ot_hours) else np.nan
201
-
202
- rows.append({
203
- "employee_id": emp,
204
- "date": d.strftime("%Y%m%d"),
205
- "ot_hours": ot_hours,
206
- "code": code,
207
- "in_times": in_times_str,
208
- "out_times": out_times_str
209
- })
210
-
211
- res = pd.DataFrame(rows, columns=["employee_id","date","ot_hours","code","in_times","out_times"])
212
- if not res.empty:
213
- res = res.sort_values(by=["employee_id","date"]).reset_index(drop=True)
214
-
215
- # ----------------- write Excel (Colab-friendly) -----------------
216
- try:
217
- import xlsxwriter # noqa: F401
218
- engine = "xlsxwriter"
219
- except Exception:
220
- engine = "openpyxl" # available by default in Colab
221
-
222
- with pd.ExcelWriter(OUTPUT_XLSX, engine=engine) as w:
223
- res.to_excel(w, index=False, sheet_name="OT")
224
-
225
- print(f"Done. Wrote: {Path(OUTPUT_XLSX).resolve()}")
226
- display(res.head(50))
 
 
 
 
 
1
+ import gradio as gr
2
  import pandas as pd
3
  import numpy as np
4
  from datetime import datetime, date, time
5
  from pathlib import Path
6
+ import os
7
 
8
+ # ========== Policy (your latest spec) ==========
9
+ POLICY_CHANGE = date(2025, 7, 7) # "from 20250707" inclusive
10
+ BASELINE_BEFORE = time(17, 0) # before policy date
11
+ BASELINE_AFTER = time(17, 30) # from policy date
12
+ VIRTUAL_BEFORE = time(17, 30) # Case 4 (before policy date)
13
+ VIRTUAL_AFTER = time(18, 0) # Case 4 (from policy date)
14
+
15
+ IN_TOKENS = {"P10","IN","INN","CHECKIN","CHECK-IN"}
16
+ OUT_TOKENS = {"P20","OUT","CHECKOUT","CHECK-OUT"}
17
+
18
+ # ========== Helpers ==========
 
 
 
 
 
 
 
 
 
 
19
  def parse_yyyymmdd(s):
20
  s = str(s).strip()
21
  for fmt in ("%Y%m%d", "%Y-%m-%d", "%d/%m/%Y", "%m/%d/%Y"):
 
26
  return pd.NaT
27
 
28
  def parse_hms(s):
29
+ """Accept HH:MM[:SS], HHMM, HHMMSS, or compact like '82547' -> 08:25:47."""
 
 
30
  s = str(s).strip()
31
  if s.isdigit():
32
+ s = s.zfill(6)[:6]
33
  hh, mm, ss = s[:2], s[2:4], s[4:6]
34
  try:
35
  return time(int(hh), int(mm), int(ss))
 
47
  return pd.NaT
48
  return datetime.combine(d, t)
49
 
50
+ def from_policy(d):
51
  return (not pd.isna(d)) and (d >= POLICY_CHANGE)
52
 
53
+ def baseline_cutoff(d):
54
+ return BASELINE_AFTER if from_policy(d) else BASELINE_BEFORE
55
 
56
+ def virtual_checkin(d):
57
+ return VIRTUAL_AFTER if from_policy(d) else VIRTUAL_BEFORE
 
 
 
58
 
59
  def detect_event_token(v):
60
  u = str(v).upper()
61
  is_in = any(tok == u or tok in u for tok in IN_TOKENS)
62
  is_out = any(tok == u or tok in u for tok in OUT_TOKENS)
63
+ if is_in and not is_out: return "in"
64
+ if is_out and not is_in: return "out"
 
 
65
  return np.nan
66
 
67
+ def uniq_times_str(ts):
68
+ return ", ".join(sorted({t.strftime("%H:%M:%S") for t in ts.dropna()}))
 
 
 
 
 
69
 
70
+ def find_col(df, candidates, fallback=None):
71
+ for col in df.columns:
72
  for pat in candidates:
73
  if pat in col:
74
  return col
75
  return fallback
76
 
77
+ # ========== Core computation ==========
78
+ def compute_ot(df_raw: pd.DataFrame) -> pd.DataFrame:
79
+ df = df_raw.copy()
80
+ df.columns = [str(c).strip().lower() for c in df.columns]
81
 
82
+ # infer the 3 main columns (employee, date, time)
83
+ emp_col = find_col(df, ["emp","employee","staff","id","person"], df.columns[0] if len(df.columns)>0 else None)
84
+ date_col = find_col(df, ["date","yyyymmdd","ngay"], df.columns[1] if len(df.columns)>1 else None)
85
+ time_col = find_col(df, ["time","gio","hh","clock"], df.columns[2] if len(df.columns)>2 else None)
86
+
87
+ if any(c is None for c in [emp_col, date_col, time_col]):
88
+ raise ValueError(
89
+ f"Could not infer columns from {list(df.columns)}. "
90
+ "Expected 3 columns: employee, yyyymmdd, hh:mm:ss (or hhmmss)."
91
+ )
92
+
93
+ # optional dedicated P10/P20 column
94
  evt_col = None
95
+ for c in df.columns:
96
+ vals = df[c].astype(str).str.upper()
97
+ if vals.isin(["P10","P20"]).any() or vals.str.contains(r"\bP10\b|\bP20\b", regex=True, na=False).any():
98
  evt_col = c
99
  break
100
 
101
+ # parse fields
102
+ df["__emp"] = df[emp_col].astype(str).str.strip()
103
+ df["__date"] = df[date_col].apply(parse_yyyymmdd)
104
+ df["__time"] = df[time_col].apply(parse_hms)
105
+ df["__ts"] = [combine_dt(d, t) for d, t in zip(df["__date"], df["__time"])]
106
+
107
+ if evt_col:
108
+ df["__event"] = df[evt_col].apply(detect_event_token)
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
109
  else:
110
+ # scan row if no dedicated event column exists
111
+ def scan_row(row):
112
+ for c in row.index:
113
+ ev = detect_event_token(row[c])
114
+ if pd.notna(ev):
115
+ return ev
116
+ return np.nan
117
+ df["__event"] = df.apply(scan_row, axis=1)
118
+
119
+ df = df.dropna(subset=["__ts"]).sort_values(["__emp","__date","__ts"])
120
+
121
+ df["__baseline_dt"] = [combine_dt(d, baseline_cutoff(d)) for d in df["__date"]]
122
+ df["__virtual_dt"] = [combine_dt(d, virtual_checkin(d)) for d in df["__date"]]
123
+
124
+ rows = []
125
+ for (emp, d), day in df.groupby(["__emp","__date"], sort=True):
126
+ if pd.isna(d):
127
+ continue
128
+ day = day.sort_values("__ts")
129
+ baseline_dt = day["__baseline_dt"].iloc[0]
130
+ virtual_dt = day["__virtual_dt"].iloc[0]
131
+
132
+ pre = day[day["__ts"] < baseline_dt]
133
+ after = day[day["__ts"] >= baseline_dt]
134
+ if after.empty:
135
+ continue
136
+
137
+ # "Already inside" at cutoff → last pre == IN or first after == OUT
138
+ last_pre_event = pre.iloc[-1]["__event"] if not pre.empty else np.nan
139
+ first_after_ev = after.iloc[0]["__event"]
140
+ inside_at_cutoff = (last_pre_event == "in") or (first_after_ev == "out")
141
+
142
+ ins_after = after.loc[after["__event"].eq("in"), "__ts"]
143
+ outs_after = after.loc[after["__event"].eq("out"), "__ts"]
144
+
145
+ in_times_list = uniq_times_str(ins_after) # unique actual events after cutoff
146
+ out_times_list = uniq_times_str(outs_after)
147
+
148
+ code = ""
149
+ ot_hours = np.nan
150
+ virtual_used_str = "" # populated only for Case 4
151
+
152
+ if len(outs_after) == 0 and len(ins_after) > 0:
153
+ # Case 3: IN but no OUT after cutoff
154
  code = "alert"
155
 
156
+ elif len(outs_after) > 0 and len(ins_after) == 0:
157
+ # Case 2 or Case 4
158
+ start_dt = virtual_dt if inside_at_cutoff else baseline_dt
159
+ if inside_at_cutoff:
160
+ virtual_used_str = start_dt.strftime("%H:%M:%S")
161
+ end_dt = outs_after.max()
162
+ ot_hours = max(0.0, (end_dt - start_dt).total_seconds()/3600.0)
163
+
164
+ elif len(outs_after) > 0 and len(ins_after) > 0:
165
+ # Case 1 normally; Case 4 if already inside at cutoff
166
+ if inside_at_cutoff:
167
+ start_dt = virtual_dt
168
+ virtual_used_str = start_dt.strftime("%H:%M:%S")
169
+ else:
170
+ start_dt = ins_after.min()
171
+ end_dt = outs_after.max()
172
+ if end_dt >= start_dt:
173
+ ot_hours = (end_dt - start_dt).total_seconds()/3600.0
174
+ else:
175
+ code = "alert"
176
  else:
177
+ continue
178
+
179
+ if pd.notna(ot_hours):
180
+ ot_hours = round(float(ot_hours), 2)
181
+
182
+ rows.append({
183
+ "employee_id": emp,
184
+ "date": d.strftime("%Y%m%d"),
185
+ "ot_hours": ot_hours,
186
+ "code": code,
187
+ "in_times": in_times_list,
188
+ "out_times": out_times_list,
189
+ "virtual_in_time": virtual_used_str
190
+ })
191
+
192
+ res = pd.DataFrame(rows, columns=[
193
+ "employee_id","date","ot_hours","code","in_times","out_times","virtual_in_time"
194
+ ])
195
+ if not res.empty:
196
+ res = res.sort_values(["employee_id","date"]).reset_index(drop=True)
197
+ return res
198
+
199
+ # ========== File loading (Spaces-safe) ==========
200
+ def load_dataframe(upload):
201
+ if upload is None:
202
+ raise gr.Error("Please upload a file.")
203
+ # In Spaces, upload is a FileData with .name (temp file path). Accept str/Path too.
204
+ path = None
205
+ if isinstance(upload, (str, Path)):
206
+ path = str(upload)
207
+ else:
208
+ path = getattr(upload, "name", None) or getattr(upload, "path", None)
209
+ if not path or not os.path.exists(path):
210
+ raise gr.Error("Could not resolve uploaded file path.")
211
+
212
+ lower = path.lower()
213
+ if lower.endswith((".xlsx",".xls")):
214
+ df = pd.read_excel(path, dtype=str)
215
+ elif lower.endswith(".csv"):
216
+ try:
217
+ df = pd.read_csv(path, dtype=str)
218
+ except Exception:
219
+ df = pd.read_csv(path, dtype=str, sep=";")
220
  else:
221
+ raise gr.Error("Unsupported file type. Upload .xlsx/.xls/.csv.")
222
+ return df
223
+
224
+ def process_file(upload):
225
+ df_in = load_dataframe(upload)
226
+ res = compute_ot(df_in)
227
+
228
+ out_path = Path("ot_summary.xlsx") # saved in Space working dir
229
+ try:
230
+ import xlsxwriter; engine = "xlsxwriter"
231
+ except Exception:
232
+ engine = "openpyxl"
233
+ with pd.ExcelWriter(out_path, engine=engine) as w:
234
+ res.to_excel(w, index=False, sheet_name="OT")
235
+
236
+ return res, str(out_path)
237
+
238
+ # ========== UI ==========
239
+ with gr.Blocks(title="OT Calculator (P10/P20)", analytics_enabled=False) as demo:
240
+ gr.Markdown(
241
+ "### OT Calculator (P10/P20)\n"
242
+ "Upload your 3-column file (employee, yyyymmdd, hh:mm:ss). \n"
243
+ "Policy: baseline 17:00 before 2025-07-07; 17:30 from that date. \n"
244
+ "Case 4 uses virtual 17:30 / 18:00 and shows it in `virtual_in_time`."
245
+ )
246
+ file_in = gr.File(label="Upload Excel/CSV", file_types=[".xlsx",".xls",".csv"])
247
+ run_btn = gr.Button("Compute OT", variant="primary")
248
+ df_out = gr.Dataframe(label="Result preview", interactive=False, wrap=True, row_count=(12, "dynamic"))
249
+ dl = gr.File(label="Download OT Excel (.xlsx)")
250
+ run_btn.click(process_file, inputs=file_in, outputs=[df_out, dl])
251
+
252
+ # Spaces runs this automatically; local dev fallback:
253
+ if __name__ == "__main__":
254
+ demo.launch()