Spaces:
Running
Running
Update app.py
Browse files
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,
|
| 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
|
| 529 |
-
ctax
|
|
|
|
| 530 |
|
| 531 |
issuer, final, questions = decide_fiscal(
|
| 532 |
-
note, issuer_status, parse, doc_choice, cvat, ctax)
|
| 533 |
|
| 534 |
if questions:
|
| 535 |
-
|
|
|
|
| 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,
|
|
|
|
| 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 |
-
|
|
|
|
| 655 |
|
| 656 |
clear_btn.click(
|
| 657 |
fn=_on_clear,
|
| 658 |
-
outputs=[note_input, issuer_radio, doc_choice,
|
|
|
|
| 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 |
# βββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββ
|