jeffrey1963 commited on
Commit
01260a9
·
verified ·
1 Parent(s): 06ac205

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +58 -11
app.py CHANGED
@@ -208,24 +208,52 @@ def audit_against_expected(expected: pd.DataFrame, actual: pd.DataFrame):
208
 
209
 
210
  # ---------- Gradio callbacks ----------
 
 
 
 
 
 
 
 
 
211
  def handle_docx(file):
212
  if file is None:
213
- return "(no file)", None, None, {}, pd.DataFrame()
 
 
214
  df_raw, header = _docx_to_table_and_text(file.name if hasattr(file, "name") else file)
215
  params = _extract_params(header or "")
216
  df_norm = _normalize_depr_columns(df_raw) if df_raw is not None else None
217
- return header or "(no text found)", params, df_norm, params, (df_norm if df_norm is not None else pd.DataFrame())
 
 
 
 
 
 
 
 
218
 
219
  def handle_image(img):
220
  if img is None:
221
- return "(no image)", None, None, None, {}, pd.DataFrame()
222
  from PIL import Image as PILImage
223
  pil = img if isinstance(img, PILImage.Image) else PILImage.fromarray(img)
224
  ocr_text = _image_to_text(pil)
225
  params = _extract_params(ocr_text or "")
226
  df_raw = _table_from_ocr_text(ocr_text or "")
227
  df_norm = _normalize_depr_columns(df_raw) if df_raw is not None else None
228
- return ocr_text or "(empty OCR)", params, df_raw, df_norm, params, (df_norm if df_norm is not None else pd.DataFrame())
 
 
 
 
 
 
 
 
 
229
 
230
 
231
  # ---------- UI ----------
@@ -239,7 +267,18 @@ with gr.Blocks(title="Jerry • HW Intake (Echo)") as demo:
239
  header_txt = gr.Textbox(label="Header/Text (for params)", lines=8)
240
  params_json = gr.JSON(label="Detected parameters")
241
  table_df = gr.Dataframe(label="Detected table (normalized)", interactive=False)
242
- btn1.click(handle_docx, inputs=docx_in, outputs=[header_txt, params_json, table_df])
 
 
 
 
 
 
 
 
 
 
 
243
 
244
  with gr.Tab("Upload Image (.png/.jpg)"):
245
  img_in = gr.Image(type="pil", label="Photo or screenshot of your table")
@@ -248,7 +287,19 @@ with gr.Blocks(title="Jerry • HW Intake (Echo)") as demo:
248
  params_json2 = gr.JSON(label="Detected parameters")
249
  raw_df = gr.Dataframe(label="Raw table guess", interactive=False)
250
  norm_df = gr.Dataframe(label="Detected table (normalized)", interactive=False)
251
- btn2.click(handle_image, inputs=img_in, outputs=[ocr_txt, params_json2, raw_df, norm_df])
 
 
 
 
 
 
 
 
 
 
 
 
252
 
253
  with gr.Tab("Straight-Line • Solve & Check"):
254
  gr.Markdown("Enter params (auto-filled if detected) → build the correct SL schedule → compare to your uploaded table.")
@@ -314,11 +365,7 @@ with gr.Blocks(title="Jerry • HW Intake (Echo)") as demo:
314
  def prefill_inputs(p): return fill_from_state(p)
315
 
316
 
317
- last_params.change(
318
- fn=lambda p: fill_from_state(p),
319
- inputs=last_params,
320
- outputs=[in_cost, in_salv, in_life, in_year]
321
- )
322
 
323
 
324
 
 
208
 
209
 
210
  # ---------- Gradio callbacks ----------
211
+ def _params_tuple(p):
212
+ p = p or {}
213
+ return (
214
+ float(p.get("cost", 0.0)),
215
+ float(p.get("salvage", 0.0)),
216
+ int(p.get("life", 10)),
217
+ int(p.get("start_year", pd.Timestamp.now().year)),
218
+ )
219
+
220
  def handle_docx(file):
221
  if file is None:
222
+ # header, params_json, norm_df, cost, salv, life, year, state_params, state_table
223
+ return "(no file)", {}, pd.DataFrame(), 0.0, 0.0, 10, pd.Timestamp.now().year, {}, pd.DataFrame()
224
+
225
  df_raw, header = _docx_to_table_and_text(file.name if hasattr(file, "name") else file)
226
  params = _extract_params(header or "")
227
  df_norm = _normalize_depr_columns(df_raw) if df_raw is not None else None
228
+ cost, salv, life, year = _params_tuple(params)
229
+ return (
230
+ header or "(no text found)",
231
+ params,
232
+ (df_norm if df_norm is not None else pd.DataFrame()),
233
+ cost, salv, life, year, # ⬅ push directly to the inputs
234
+ params,
235
+ (df_norm if df_norm is not None else pd.DataFrame()),
236
+ )
237
 
238
  def handle_image(img):
239
  if img is None:
240
+ return "(no image)", {}, None, pd.DataFrame(), 0.0, 0.0, 10, pd.Timestamp.now().year, {}, pd.DataFrame()
241
  from PIL import Image as PILImage
242
  pil = img if isinstance(img, PILImage.Image) else PILImage.fromarray(img)
243
  ocr_text = _image_to_text(pil)
244
  params = _extract_params(ocr_text or "")
245
  df_raw = _table_from_ocr_text(ocr_text or "")
246
  df_norm = _normalize_depr_columns(df_raw) if df_raw is not None else None
247
+ cost, salv, life, year = _params_tuple(params)
248
+ return (
249
+ ocr_text or "(empty OCR)",
250
+ params,
251
+ df_raw,
252
+ (df_norm if df_norm is not None else pd.DataFrame()),
253
+ cost, salv, life, year, # ⬅ push directly to the inputs
254
+ params,
255
+ (df_norm if df_norm is not None else pd.DataFrame()),
256
+ )
257
 
258
 
259
  # ---------- UI ----------
 
267
  header_txt = gr.Textbox(label="Header/Text (for params)", lines=8)
268
  params_json = gr.JSON(label="Detected parameters")
269
  table_df = gr.Dataframe(label="Detected table (normalized)", interactive=False)
270
+ btn1.click(
271
+ handle_docx,
272
+ inputs=docx_in,
273
+ outputs=[
274
+ header_txt, # text
275
+ params_json, # json
276
+ table_df, # norm table visible in tab 1
277
+ in_cost, in_salv, in_life, in_year, # ⬅ autofill the four inputs
278
+ last_params, # state
279
+ last_table, # state
280
+ ],
281
+ )
282
 
283
  with gr.Tab("Upload Image (.png/.jpg)"):
284
  img_in = gr.Image(type="pil", label="Photo or screenshot of your table")
 
287
  params_json2 = gr.JSON(label="Detected parameters")
288
  raw_df = gr.Dataframe(label="Raw table guess", interactive=False)
289
  norm_df = gr.Dataframe(label="Detected table (normalized)", interactive=False)
290
+ btn2.click(
291
+ handle_image,
292
+ inputs=img_in,
293
+ outputs=[
294
+ ocr_txt, # raw OCR text
295
+ params_json2, # json
296
+ raw_df, # raw table
297
+ norm_df, # norm table visible in tab 2
298
+ in_cost, in_salv, in_life, in_year, # ⬅ autofill the four inputs
299
+ last_params, # state
300
+ last_table, # state
301
+ ],
302
+ )
303
 
304
  with gr.Tab("Straight-Line • Solve & Check"):
305
  gr.Markdown("Enter params (auto-filled if detected) → build the correct SL schedule → compare to your uploaded table.")
 
365
  def prefill_inputs(p): return fill_from_state(p)
366
 
367
 
368
+
 
 
 
 
369
 
370
 
371