jialinyyzz commited on
Commit
dba86fa
·
verified ·
1 Parent(s): b3ed7fc

Keep rewrite at two inputs: the usage-log event rides in the facts value

Browse files

Tabs opened before the previous update (and old API calls) send two inputs; the three-input version rejected them (5 errors in an hour).

Files changed (1) hide show
  1. app.py +25 -10
app.py CHANGED
@@ -637,19 +637,34 @@ def _fact_retry_note(n: int):
637
  f"保留的文字被改动了,重新改写(第 {n}/{factlib.FACT_TRIES} 次)……")
638
 
639
 
640
- def rewrite(draft: str, facts_raw: str = "[]", event: str = "submit"):
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
641
  """Never hand back nothing when something was written: parts that finished (and the half of a part the GPU
642
  stopped in, unless it holds a kept passage) stay on screen with a notice saying what is missing.
643
  Per part: an empty sample gets one automatic retry; a sample in the wrong language is resampled up to
644
  hz_text.LANG_RETRIES times; a sample that lost a kept passage is rewritten, up to factlib.FACT_TRIES tries.
645
- event: "submit", or "regenerate" when the page sends the same draft again (set by the js in go.click)."""
646
- event = event if event in ("submit", "regenerate") else "submit"
647
  draft = (draft or "").strip()
648
  if not draft:
649
  yield panel_message("warn", "Paste a draft first.", "先贴一篇草稿。")
650
  return
651
  cjk = hz_text.is_cjk(draft)
652
- prot = factlib.protect(draft, factlib.parse(facts_raw)) # kept passages -> placeholders, on the whole draft
653
  parts = _parts(prot.text)
654
  todo, left = parts[:PARTS], parts[PARTS:]
655
  yield panel_wait()
@@ -980,8 +995,6 @@ with gr.Blocks(title="humanizer: rewrite AI drafts so they read like a person wr
980
  gr.HTML(page.draft_foot(EXAMPLES), padding=False, apply_default_css=False)
981
  # Hidden helpers driven by page.js: the kept passages (a JSON list) and the file upload.
982
  facts_box = gr.Textbox(value="[]", visible="hidden", elem_id="hz-facts", show_label=False, container=False)
983
- # "submit" or "regenerate" (the same draft sent again), set by the js in go.click; for the usage log
984
- event_box = gr.Textbox(value="submit", visible="hidden", elem_id="hz-event", show_label=False, container=False)
985
  doc_upload = gr.UploadButton("Import", file_types=[".docx", ".pdf"], file_count="single", type="filepath",
986
  visible="hidden", elem_id="hz-upload")
987
  with gr.Column(elem_id="rail", scale=0, min_width=92):
@@ -994,11 +1007,13 @@ with gr.Blocks(title="humanizer: rewrite AI drafts so they read like a person wr
994
  gr.HTML(page.examples_section(EXAMPLES) + page.local_section() + page.results_section()
995
  + page.limits_section() + page.footer(), elem_id="hz-body", padding=False, apply_default_css=False)
996
 
997
- go.click(rewrite, inputs=[draft_box, facts_box, event_box], outputs=out_html, show_progress="hidden",
998
- concurrency_limit=2, api_visibility="undocumented",
999
- js="(d, f, e) => { document.documentElement.classList.add('hz-busy');"
 
 
1000
  " const ev = (window.__hzLastDraft === d) ? 'regenerate' : 'submit'; window.__hzLastDraft = d;"
1001
- " return [d, window.HZ ? window.HZ.factsJSON() : '[]', ev]; }") \
1002
  .then(None, js="() => { document.documentElement.classList.remove('hz-busy'); }")
1003
  doc_upload.upload(import_document, inputs=doc_upload, outputs=[draft_box, toast, doc_upload], show_progress="hidden",
1004
  concurrency_limit=4, api_visibility="private") \
 
637
  f"保留的文字被改动了,重新改写(第 {n}/{factlib.FACT_TRIES} 次)……")
638
 
639
 
640
+ def _facts_and_event(facts_raw):
641
+ """facts_raw is the kept passages as a JSON list (older pages, API callers) or, from the current page,
642
+ {"facts": [...], "event": "submit" | "regenerate"}. Two inputs either way, so an open tab from before an update
643
+ and old API calls keep working."""
644
+ event = "submit"
645
+ try:
646
+ obj = json.loads(facts_raw) if isinstance(facts_raw, str) and facts_raw.strip() else facts_raw
647
+ except ValueError:
648
+ obj = facts_raw
649
+ if isinstance(obj, dict):
650
+ event = obj.get("event") if obj.get("event") in ("submit", "regenerate") else "submit"
651
+ obj = obj.get("facts", [])
652
+ return factlib.parse(obj if isinstance(obj, list) else []), event
653
+
654
+
655
+ def rewrite(draft: str, facts_raw: str = "[]"):
656
  """Never hand back nothing when something was written: parts that finished (and the half of a part the GPU
657
  stopped in, unless it holds a kept passage) stay on screen with a notice saying what is missing.
658
  Per part: an empty sample gets one automatic retry; a sample in the wrong language is resampled up to
659
  hz_text.LANG_RETRIES times; a sample that lost a kept passage is rewritten, up to factlib.FACT_TRIES tries.
660
+ facts_raw may also carry the usage-log event: "regenerate" when the page sends the same draft again (go.click js)."""
661
+ facts_list, event = _facts_and_event(facts_raw)
662
  draft = (draft or "").strip()
663
  if not draft:
664
  yield panel_message("warn", "Paste a draft first.", "先贴一篇草稿。")
665
  return
666
  cjk = hz_text.is_cjk(draft)
667
+ prot = factlib.protect(draft, facts_list) # kept passages -> placeholders, on the whole draft
668
  parts = _parts(prot.text)
669
  todo, left = parts[:PARTS], parts[PARTS:]
670
  yield panel_wait()
 
995
  gr.HTML(page.draft_foot(EXAMPLES), padding=False, apply_default_css=False)
996
  # Hidden helpers driven by page.js: the kept passages (a JSON list) and the file upload.
997
  facts_box = gr.Textbox(value="[]", visible="hidden", elem_id="hz-facts", show_label=False, container=False)
 
 
998
  doc_upload = gr.UploadButton("Import", file_types=[".docx", ".pdf"], file_count="single", type="filepath",
999
  visible="hidden", elem_id="hz-upload")
1000
  with gr.Column(elem_id="rail", scale=0, min_width=92):
 
1007
  gr.HTML(page.examples_section(EXAMPLES) + page.local_section() + page.results_section()
1008
  + page.limits_section() + page.footer(), elem_id="hz-body", padding=False, apply_default_css=False)
1009
 
1010
+ # Still two inputs (an open tab from before an update sends two): the js folds the usage-log event, "regenerate"
1011
+ # when the same draft is sent again, into the facts value as {"facts": [...], "event": ...}.
1012
+ go.click(rewrite, inputs=[draft_box, facts_box], outputs=out_html, show_progress="hidden", concurrency_limit=2,
1013
+ api_visibility="undocumented",
1014
+ js="(d, f) => { document.documentElement.classList.add('hz-busy');"
1015
  " const ev = (window.__hzLastDraft === d) ? 'regenerate' : 'submit'; window.__hzLastDraft = d;"
1016
+ " return [d, JSON.stringify({facts: JSON.parse(window.HZ ? window.HZ.factsJSON() : '[]'), event: ev})]; }") \
1017
  .then(None, js="() => { document.documentElement.classList.remove('hz-busy'); }")
1018
  doc_upload.upload(import_document, inputs=doc_upload, outputs=[draft_box, toast, doc_upload], show_progress="hidden",
1019
  concurrency_limit=4, api_visibility="private") \