Spaces:
Running
Running
File size: 2,078 Bytes
db24499 5138fe1 db24499 5138fe1 db24499 | 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 | """Glue between the page and Groundline. Runs inside the browser (Pyodide); Groundline's own code is loaded from GitHub."""
import html
import json
import os
from groundline.checks import check_all
from groundline.cli import draft_sentences
from groundline.report import LABEL, METHOD_HINT, METHOD_TITLE, counts, methodology_body, to_html, to_markdown
from groundline.text import passages_from
TONE = {"matched": "green", "check": "amber", "no match": "red", "contradicted": "purple"}
def run(source_paths_json, names_json, draft_path):
e = html.escape
paths, names = json.loads(source_paths_json), json.loads(names_json)
try:
passages = passages_from(paths)
except (SystemExit, Exception):
return json.dumps({"error": "One of the source files could not be read. Check it opens normally, or paste its text instead."})
if not passages:
return json.dumps({"error": "No text was found in the sources. Scanned PDFs need OCR first."})
results = check_all(draft_sentences(draft_path), passages)
c = counts(results)
out = ['<div class="summary">' + f"{len(results)} sentences checked: " + ", ".join(
f'<span class="t-{TONE[k]}">{v} {LABEL[k].lower()}</span>' for k, v in c.items() if v) + "</div>"]
for i, r in enumerate(results, 1):
out.append(f'<div class="sent tone-{TONE[r.verdict]}"><div class="lab">{i}. {LABEL[r.verdict]}</div><div>{e(r.sentence)}</div>')
if r.flags:
out.append("<ul>" + "".join(f"<li><b>{e(f.kind)}:</b> {e(f.text)}</li>" for f in r.flags) + "</ul>")
if r.matches and r.verdict != "matched":
s, p = r.matches[0]
out.append(f'<div class="src">Closest passage ({e(p.where)}): {e(p.text)}</div>')
out.append("</div>")
out.append(f'<div class="method"><b>{e(METHOD_TITLE)}.</b> <i>{e(METHOD_HINT)}</i><br>{e(methodology_body(names, False))}</div>')
return json.dumps({"html": "\n".join(out), "report_html": to_html(results, names, "draft"),
"report_md": to_markdown(results, names, "draft")})
|