samurice commited on
Commit
b994467
·
verified ·
1 Parent(s): afe0fe1

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +37 -6
app.py CHANGED
@@ -4,8 +4,9 @@ import numpy as np
4
  from datetime import datetime, date, time
5
  from pathlib import Path
6
  import os
 
7
 
8
- # ===== Policy =====
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
@@ -206,9 +207,30 @@ def process_file(path: str):
206
 
207
  return res, str(out_path)
208
  except Exception as e:
209
- # show a readable error in the preview; keep file output empty
210
- err_df = pd.DataFrame({"error": [str(e)]})
211
- return err_df, None
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
212
 
213
  # ===== UI =====
214
  with gr.Blocks(title="OT Calculator (P10/P20)", analytics_enabled=False) as demo:
@@ -218,14 +240,23 @@ with gr.Blocks(title="OT Calculator (P10/P20)", analytics_enabled=False) as demo
218
  "Policy: baseline 17:00 before 2025-07-07; 17:30 from that date. \n"
219
  "Case 4 uses virtual 17:30 / 18:00 and shows it in `virtual_in_time`."
220
  )
221
- # type='filepath' makes the function receive a simple string path
222
  file_in = gr.File(label="Upload Excel/CSV", file_types=[".xlsx",".xls",".csv"], type="filepath")
223
  run_btn = gr.Button("Compute OT", variant="primary")
224
  df_out = gr.Dataframe(label="Result preview", interactive=False, wrap=True, row_count=(12, "dynamic"))
225
  dl = gr.File(label="Download OT Excel (.xlsx)")
226
 
227
- run_btn.click(process_file, inputs=file_in, outputs=[df_out, dl])
 
 
 
 
 
 
 
 
228
 
 
 
229
  # Spaces will run this automatically; local dev fallback:
230
  if __name__ == "__main__":
231
  demo.launch()
 
4
  from datetime import datetime, date, time
5
  from pathlib import Path
6
  import os
7
+ import traceback
8
 
9
+ # ===== Policy (your latest spec) =====
10
  POLICY_CHANGE = date(2025, 7, 7) # "from 20250707" inclusive
11
  BASELINE_BEFORE = time(17, 0) # before policy date
12
  BASELINE_AFTER = time(17, 30) # from policy date
 
207
 
208
  return res, str(out_path)
209
  except Exception as e:
210
+ # Show a readable error table and keep file output empty
211
+ err = f"{type(e).__name__}: {e}"
212
+ # Uncomment for deeper debugging in Space logs:
213
+ # print(traceback.format_exc())
214
+ return pd.DataFrame({"error": [err]}), None
215
+
216
+ # Create a tiny example file so users can test without uploading
217
+ example_path = Path("example.csv")
218
+ if not example_path.exists():
219
+ example_path.write_text(
220
+ "emp,date,time,status\n"
221
+ "3827264,20250626,82547,P10\n"
222
+ "3827264,20250626,82548,P10\n"
223
+ "3827264,20250626,123635,P20\n"
224
+ "3827264,20250626,123641,P20\n"
225
+ "3827264,20250626,123643,P20\n"
226
+ "3827264,20250626,131645,P10\n"
227
+ "3827264,20250626,190534,P20\n"
228
+ "3827264,20250626,192431,P10\n"
229
+ "3827264,20250626,192432,P10\n"
230
+ "3827264,20250626,225026,P20\n"
231
+ "3827264,20250626,225028,P20\n",
232
+ encoding="utf-8"
233
+ )
234
 
235
  # ===== UI =====
236
  with gr.Blocks(title="OT Calculator (P10/P20)", analytics_enabled=False) as demo:
 
240
  "Policy: baseline 17:00 before 2025-07-07; 17:30 from that date. \n"
241
  "Case 4 uses virtual 17:30 / 18:00 and shows it in `virtual_in_time`."
242
  )
 
243
  file_in = gr.File(label="Upload Excel/CSV", file_types=[".xlsx",".xls",".csv"], type="filepath")
244
  run_btn = gr.Button("Compute OT", variant="primary")
245
  df_out = gr.Dataframe(label="Result preview", interactive=False, wrap=True, row_count=(12, "dynamic"))
246
  dl = gr.File(label="Download OT Excel (.xlsx)")
247
 
248
+ # Give users a quick example they can click
249
+ gr.Examples(
250
+ examples=[str(example_path)],
251
+ inputs=file_in,
252
+ label="Try with an example CSV"
253
+ )
254
+
255
+ # IMPORTANT: name the API so Spaces shows “Use via API” and avoid ‘No API found’
256
+ run_btn.click(process_file, inputs=file_in, outputs=[df_out, dl], api_name="compute")
257
 
258
+ # Enable queue for concurrency
259
+ demo.queue()
260
  # Spaces will run this automatically; local dev fallback:
261
  if __name__ == "__main__":
262
  demo.launch()