Spaces:
Running
Running
Download web.py from Ghongha007/groundline: direct link, hf CLI and curl.
- Browser
- Download file 2.08 kB
-
https://huggingface.co/spaces/Ghongha007/groundline/resolve/main/web.py
- Command line
-
hf download hf://spaces/Ghongha007/groundline/web.py
-
curl -L -o web.py https://huggingface.co/spaces/Ghongha007/groundline/resolve/main/web.py
2.08 kB
| """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")}) | |