yonilev commited on
Commit
2d5ba42
Β·
verified Β·
1 Parent(s): c1ca482

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +37 -23
app.py CHANGED
@@ -231,7 +231,7 @@ def _today_str():
231
 
232
 
233
  def decide_fiscal(note, issuer_status, parse, doc_radio,
234
- clarify_vat=None, clarify_tax_id=None):
235
  """
236
  Returns (issuer_dict, final_parse, questions_dict).
237
  questions_dict empty β†’ ready to generate.
@@ -247,6 +247,8 @@ def decide_fiscal(note, issuer_status, parse, doc_radio,
247
  "is_company": False}
248
 
249
  # ── 2. Mandatory content ──────────────────────────────────────────────
 
 
250
  if not parse.get("client_name", "").strip():
251
  questions["client_name"] = "שם Χ”ΧœΧ§Χ•Χ— / Χ”ΧžΧ©ΧœΧ?"
252
  if not parse.get("items"):
@@ -503,44 +505,45 @@ def render_recommendations(recs):
503
  # ─────────────────────────────────────────────────────────────────────────────
504
  # 6. Main pipeline
505
  # ─────────────────────────────────────────────────────────────────────────────
506
- def generate(note, issuer_radio, doc_choice, clarify_vat, clarify_tax_id):
507
  """
508
  Stateless single handler.
509
- Returns: (status_md, parse_html, doc_html, recs_html, vat_group_visible, tax_group_visible)
510
  """
511
  note = (note or "").strip()
512
  if not note:
513
- return "✏️ Χ›ΧͺΧ•Χ‘ Χ€ΧͺΧ§ Χ”Χ›Χ Χ‘Χ” Χ•ΧœΧ—Χ₯ Χ”Χ€Χ§ מבמך.", "", "", "", False, False
514
 
515
  issuer_status = issuer_radio if issuer_radio in ("Χ€Χ˜Χ•Χ¨", "ΧžΧ•Χ¨Χ©Χ”") else None
516
 
517
- # Issuer check BEFORE model (saves ~90s on CPU)
518
  if not issuer_status:
519
- return "πŸ‘€ Χ‘Χ—Χ¨ ΧͺΧ—Χ™ΧœΧ”: **Χ’Χ•Χ‘Χ§ Χ€Χ˜Χ•Χ¨** או **Χ’Χ•Χ‘Χ§ ΧžΧ•Χ¨Χ©Χ”** ΧœΧžΧ’ΧœΧ”.", "", "", "", False, False
520
 
521
  _lazy_init()
522
  parse = model_parse(note)
523
 
524
  if parse is None or _missing_fields(parse) == ["parse_failed"]:
525
  return ("πŸ€” לא Χ”Χ¦ΧœΧ—ΧͺΧ™ ΧœΧ—ΧœΧ₯ Χ€Χ¨Χ˜Χ™Χ. Χ Χ‘Χ— ΧžΧ—Χ“Χ© β€” ΧœΧ“Χ•Χ’ΧžΧ”: 'Χ§Χ™Χ‘ΧœΧͺΧ™ 500β‚ͺ ΧžΧžΧ©Χ” גל Χ™Χ™Χ’Χ•Χ₯'.",
526
- "", "", "", False, False)
527
 
528
- cvat = clarify_vat if clarify_vat not in (None, "לא Χ¦Χ•Χ™ΧŸ") else None
529
- ctax = clarify_tax_id.strip() if clarify_tax_id and clarify_tax_id.strip() else None
 
530
 
531
  issuer, final, questions = decide_fiscal(
532
- note, issuer_status, parse, doc_choice, cvat, ctax)
533
 
534
  if questions:
535
- show_vat_q = "vat_basis" in questions
 
536
  show_taxid_q = "client_tax_id" in questions
537
  return (_build_clarification_message(questions),
538
- "", "", "", show_vat_q, show_taxid_q)
539
 
540
  try:
541
  completed = core.complete(issuer, final, _STATE["rng"])
542
  except Exception as e:
543
- return f"⚠️ שגיאה Χ‘Χ’Χ™Χ‘Χ•Χ“: {e}", "", "", "", False, False
544
 
545
  show_vat = (final["doc_type"] != "receipt")
546
 
@@ -548,7 +551,7 @@ def generate(note, issuer_radio, doc_choice, clarify_vat, clarify_tax_id):
548
  render_parse_panel(final, completed, show_vat),
549
  render_document(completed, show_vat),
550
  render_recommendations(recommend(note)),
551
- False, False)
552
 
553
 
554
  # ─────────────────────────────────────────────────────────────────────────────
@@ -608,6 +611,13 @@ with gr.Blocks(title="Text2Receipt", theme=gr.themes.Soft(
608
  status_output = gr.Markdown("")
609
 
610
  # Clarification fields β€” appear only when needed
 
 
 
 
 
 
 
611
  with gr.Group(visible=False) as clarify_vat_group:
612
  gr.HTML('<div dir="rtl" style="font-size:12px;font-weight:600;color:#7c3aed;'
613
  'margin-bottom:6px;">πŸ’° Χ‘Χ‘Χ™Χ‘ מג״מ Χ Χ“Χ¨Χ©</div>')
@@ -635,29 +645,33 @@ with gr.Blocks(title="Text2Receipt", theme=gr.themes.Soft(
635
  recs_output = gr.HTML()
636
 
637
  # Handlers
638
- def _on_generate(note, issuer_r, doc_c, cvat, ctax):
639
- status, phtml, dhtml, rhtml, show_vat_q, show_taxid_q = generate(
640
- note, issuer_r, doc_c, cvat, ctax)
641
  return (status, phtml, dhtml, rhtml,
 
642
  gr.update(visible=show_vat_q),
643
  gr.update(visible=show_taxid_q))
644
 
645
  submit_btn.click(
646
  fn=_on_generate,
647
- inputs=[note_input, issuer_radio, doc_choice, clarify_vat, clarify_tax_id],
 
648
  outputs=[status_output, parse_output, doc_output, recs_output,
649
- clarify_vat_group, clarify_tax_group],
650
  )
651
 
652
  def _on_clear():
653
- return ("", None, "Χ–Χ™Χ”Χ•Χ™ ΧΧ•Χ˜Χ•ΧžΧ˜Χ™", "לא Χ¦Χ•Χ™ΧŸ", "", "", "", "", "",
654
- gr.update(visible=False), gr.update(visible=False))
 
655
 
656
  clear_btn.click(
657
  fn=_on_clear,
658
- outputs=[note_input, issuer_radio, doc_choice, clarify_vat, clarify_tax_id,
 
659
  status_output, parse_output, doc_output, recs_output,
660
- clarify_vat_group, clarify_tax_group],
661
  )
662
 
663
  # ─────────────────────────────────────────────────────────────────────────────
 
231
 
232
 
233
  def decide_fiscal(note, issuer_status, parse, doc_radio,
234
+ clarify_vat=None, clarify_tax_id=None, clarify_name=None):
235
  """
236
  Returns (issuer_dict, final_parse, questions_dict).
237
  questions_dict empty β†’ ready to generate.
 
247
  "is_company": False}
248
 
249
  # ── 2. Mandatory content ──────────────────────────────────────────────
250
+ if clarify_name and clarify_name.strip():
251
+ parse["client_name"] = clarify_name.strip()
252
  if not parse.get("client_name", "").strip():
253
  questions["client_name"] = "שם Χ”ΧœΧ§Χ•Χ— / Χ”ΧžΧ©ΧœΧ?"
254
  if not parse.get("items"):
 
505
  # ─────────────────────────────────────────────────────────────────────────────
506
  # 6. Main pipeline
507
  # ─────────────────────────────────────────────────────────────────────────────
508
+ def generate(note, issuer_radio, doc_choice, clarify_vat, clarify_tax_id, clarify_name):
509
  """
510
  Stateless single handler.
511
+ Returns: (status_md, parse_html, doc_html, recs_html, name_vis, vat_vis, tax_vis)
512
  """
513
  note = (note or "").strip()
514
  if not note:
515
+ return "✏️ Χ›ΧͺΧ•Χ‘ Χ€ΧͺΧ§ Χ”Χ›Χ Χ‘Χ” Χ•ΧœΧ—Χ₯ Χ”Χ€Χ§ מבמך.", "", "", "", False, False, False
516
 
517
  issuer_status = issuer_radio if issuer_radio in ("Χ€Χ˜Χ•Χ¨", "ΧžΧ•Χ¨Χ©Χ”") else None
518
 
 
519
  if not issuer_status:
520
+ return "πŸ‘€ Χ‘Χ—Χ¨ ΧͺΧ—Χ™ΧœΧ”: **Χ’Χ•Χ‘Χ§ Χ€Χ˜Χ•Χ¨** או **Χ’Χ•Χ‘Χ§ ΧžΧ•Χ¨Χ©Χ”** ΧœΧžΧ’ΧœΧ”.", "", "", "", False, False, False
521
 
522
  _lazy_init()
523
  parse = model_parse(note)
524
 
525
  if parse is None or _missing_fields(parse) == ["parse_failed"]:
526
  return ("πŸ€” לא Χ”Χ¦ΧœΧ—ΧͺΧ™ ΧœΧ—ΧœΧ₯ Χ€Χ¨Χ˜Χ™Χ. Χ Χ‘Χ— ΧžΧ—Χ“Χ© β€” ΧœΧ“Χ•Χ’ΧžΧ”: 'Χ§Χ™Χ‘ΧœΧͺΧ™ 500β‚ͺ ΧžΧžΧ©Χ” גל Χ™Χ™Χ’Χ•Χ₯'.",
527
+ "", "", "", False, False, False)
528
 
529
+ cvat = clarify_vat if clarify_vat not in (None, "לא Χ¦Χ•Χ™ΧŸ") else None
530
+ ctax = clarify_tax_id.strip() if clarify_tax_id and clarify_tax_id.strip() else None
531
+ cname = clarify_name.strip() if clarify_name and clarify_name.strip() else None
532
 
533
  issuer, final, questions = decide_fiscal(
534
+ note, issuer_status, parse, doc_choice, cvat, ctax, cname)
535
 
536
  if questions:
537
+ show_name_q = "client_name" in questions
538
+ show_vat_q = "vat_basis" in questions
539
  show_taxid_q = "client_tax_id" in questions
540
  return (_build_clarification_message(questions),
541
+ "", "", "", show_name_q, show_vat_q, show_taxid_q)
542
 
543
  try:
544
  completed = core.complete(issuer, final, _STATE["rng"])
545
  except Exception as e:
546
+ return f"⚠️ שגיאה Χ‘Χ’Χ™Χ‘Χ•Χ“: {e}", "", "", "", False, False, False
547
 
548
  show_vat = (final["doc_type"] != "receipt")
549
 
 
551
  render_parse_panel(final, completed, show_vat),
552
  render_document(completed, show_vat),
553
  render_recommendations(recommend(note)),
554
+ False, False, False)
555
 
556
 
557
  # ─────────────────────────────────────────────────────────────────────────────
 
611
  status_output = gr.Markdown("")
612
 
613
  # Clarification fields β€” appear only when needed
614
+ with gr.Group(visible=False) as clarify_name_group:
615
+ gr.HTML('<div dir="rtl" style="font-size:12px;font-weight:600;color:#7c3aed;'
616
+ 'margin-bottom:6px;">πŸ‘€ שם ΧœΧ§Χ•Χ— Χ Χ“Χ¨Χ©</div>')
617
+ clarify_name = gr.Textbox(
618
+ label="שם Χ”ΧœΧ§Χ•Χ— / Χ”ΧžΧ©ΧœΧ", placeholder="למשל: Χ Χ•Χ’Χ” Χ›Χ”ΧŸ", value="",
619
+ )
620
+
621
  with gr.Group(visible=False) as clarify_vat_group:
622
  gr.HTML('<div dir="rtl" style="font-size:12px;font-weight:600;color:#7c3aed;'
623
  'margin-bottom:6px;">πŸ’° Χ‘Χ‘Χ™Χ‘ מג״מ Χ Χ“Χ¨Χ©</div>')
 
645
  recs_output = gr.HTML()
646
 
647
  # Handlers
648
+ def _on_generate(note, issuer_r, doc_c, cvat, ctax, cname):
649
+ status, phtml, dhtml, rhtml, show_name_q, show_vat_q, show_taxid_q = generate(
650
+ note, issuer_r, doc_c, cvat, ctax, cname)
651
  return (status, phtml, dhtml, rhtml,
652
+ gr.update(visible=show_name_q),
653
  gr.update(visible=show_vat_q),
654
  gr.update(visible=show_taxid_q))
655
 
656
  submit_btn.click(
657
  fn=_on_generate,
658
+ inputs=[note_input, issuer_radio, doc_choice,
659
+ clarify_vat, clarify_tax_id, clarify_name],
660
  outputs=[status_output, parse_output, doc_output, recs_output,
661
+ clarify_name_group, clarify_vat_group, clarify_tax_group],
662
  )
663
 
664
  def _on_clear():
665
+ return ("", None, "Χ–Χ™Χ”Χ•Χ™ ΧΧ•Χ˜Χ•ΧžΧ˜Χ™", "לא Χ¦Χ•Χ™ΧŸ", "", "",
666
+ "", "", "", "",
667
+ gr.update(visible=False), gr.update(visible=False), gr.update(visible=False))
668
 
669
  clear_btn.click(
670
  fn=_on_clear,
671
+ outputs=[note_input, issuer_radio, doc_choice,
672
+ clarify_vat, clarify_tax_id, clarify_name,
673
  status_output, parse_output, doc_output, recs_output,
674
+ clarify_name_group, clarify_vat_group, clarify_tax_group],
675
  )
676
 
677
  # ─────────────────────────────────────────────────────────────────────────────