Download engine/tests/attach_smoke.py from amogaddy/neuronal: direct link, hf CLI and curl.
- Browser
- Download file 9.82 kB
-
https://huggingface.co/spaces/amogaddy/neuronal/resolve/main/engine/tests/attach_smoke.py
- Command line
-
hf download hf://spaces/amogaddy/neuronal/engine/tests/attach_smoke.py
-
curl -L -o attach_smoke.py https://huggingface.co/spaces/amogaddy/neuronal/resolve/main/engine/tests/attach_smoke.py
9.82 kB
| """Allegati nella chat, dalla pagina vera (Chrome headless): graffetta con JSON, PDF di testo e PDF scansionato | |
| (una pagina che è solo un'immagine: va letta con l'OCR). Avvia da sé il motore e Chrome. | |
| Argomento 1: cartella temporanea di lavoro.""" | |
| import asyncio, base64, json, os, shutil, subprocess, sys, tempfile, time, urllib.request | |
| import websockets | |
| sys.stdout.reconfigure(encoding="utf-8") | |
| ROOT = os.path.dirname(os.path.dirname(os.path.dirname(os.path.abspath(__file__)))) | |
| BASE = sys.argv[1] | |
| PORT = 7866 | |
| CHROME = r"C:\Program Files\Google\Chrome\Application\chrome.exe" | |
| RUNTIME = os.path.join(ROOT, "runtime", "llamacpp-b11200") | |
| FILES = os.path.join(BASE, "files") | |
| failures = [] | |
| def check(cond, what): | |
| print((" ok " if cond else " FAIL ") + what) | |
| if not cond: | |
| failures.append(what) | |
| async def observe(ev_list, stop): | |
| """Un secondo client: vede la chat condivisa (messaggio dell'utente, strumenti, risposta).""" | |
| async with websockets.connect(f"ws://127.0.0.1:{PORT}/ws", max_size=None) as ws: | |
| while not stop.is_set(): | |
| try: | |
| m = await asyncio.wait_for(ws.recv(), timeout=1) | |
| except asyncio.TimeoutError: | |
| continue | |
| if isinstance(m, str): | |
| ev_list.append(json.loads(m)) | |
| async def main(): | |
| os.makedirs(FILES, exist_ok=True) | |
| json.dump({"cliente": "Rossi Mario SRL", "ordine": 4471, "importo_euro": 1280.5, "pagato": False}, open(os.path.join(FILES, "ordine.json"), "w")) | |
| chrome = subprocess.Popen([CHROME, "--headless=new", "--remote-debugging-port=9336", "--use-angle=swiftshader", "--enable-unsafe-swiftshader", | |
| "--window-size=1200,900", f"--user-data-dir={tempfile.mkdtemp()}", "about:blank"], | |
| stdout=subprocess.DEVNULL, stderr=subprocess.DEVNULL) | |
| stop = asyncio.Event() | |
| events = [] | |
| try: | |
| for _ in range(60): | |
| try: | |
| tabs = json.load(urllib.request.urlopen("http://127.0.0.1:9336/json")); break | |
| except OSError: | |
| await asyncio.sleep(0.3) | |
| page = [t for t in tabs if t["type"] == "page"][0] | |
| async with websockets.connect(page["webSocketDebuggerUrl"], max_size=None) as cdp: | |
| n = 0 | |
| async def call(m, **p): | |
| nonlocal n | |
| n += 1 | |
| await cdp.send(json.dumps({"id": n, "method": m, "params": p})) | |
| while True: | |
| r = json.loads(await cdp.recv()) | |
| if r.get("id") == n: | |
| if "error" in r: | |
| raise RuntimeError(r["error"]) | |
| return r.get("result", {}) | |
| async def js(e): | |
| r = await call("Runtime.evaluate", expression=e, returnByValue=True, awaitPromise=True) | |
| return r.get("result", {}).get("value") | |
| # i due PDF si fanno con Chrome stesso: uno con testo vero, uno con la sola immagine di una fattura | |
| png = base64.b64encode(open(os.path.join(ROOT, "state", "..", "engine", "tests", "fattura_test.png"), "rb").read()).decode() \ | |
| if os.path.exists(os.path.join(ROOT, "engine", "tests", "fattura_test.png")) else None | |
| await call("Page.enable") | |
| await call("Page.navigate", url="data:text/html;charset=utf-8,<h1>Contratto di affitto</h1><p>Il canone mensile e' di 750 euro. Scadenza: 31 dicembre 2027. Referente: Giulia Bianchi.</p>") | |
| await asyncio.sleep(1) | |
| pdf = await call("Page.printToPDF") | |
| open(os.path.join(FILES, "contratto.pdf"), "wb").write(base64.b64decode(pdf["data"])) | |
| img = base64.b64encode(open(os.path.join(sys.argv[2]), "rb").read()).decode() | |
| await call("Page.navigate", url="data:text/html;charset=utf-8,<body style='margin:0'><img src='data:image/png;base64," + img + "' width=700>") | |
| await asyncio.sleep(1.5) | |
| pdf = await call("Page.printToPDF") | |
| open(os.path.join(FILES, "scansione.pdf"), "wb").write(base64.b64decode(pdf["data"])) | |
| await call("Page.navigate", url=f"http://127.0.0.1:{PORT}/") | |
| for _ in range(60): | |
| await asyncio.sleep(0.5) | |
| if await js("document.getElementById('st-conn')?.textContent") == "connesso": | |
| break | |
| obs = asyncio.ensure_future(observe(events, stop)) | |
| await js("document.getElementById('tools-on').checked = true; true") | |
| print("1) JSON e PDF di testo con la graffetta") | |
| doc = await call("DOM.getDocument") | |
| node = await call("DOM.querySelector", nodeId=doc["root"]["nodeId"], selector="#attach-file") | |
| await call("DOM.setFileInputFiles", files=[os.path.join(FILES, "ordine.json"), os.path.join(FILES, "contratto.pdf")], nodeId=node["nodeId"]) | |
| for _ in range(60): | |
| await asyncio.sleep(0.5) | |
| st = await js("[...document.querySelectorAll('.attach-item')].map(e => e.className + '|' + e.textContent)") | |
| if st and len(st) == 2 and not any("busy" in s.split("|")[0] for s in st): | |
| break | |
| print(" ", st) | |
| check(len(st) == 2 and not any("err" in s.split("|")[0] for s in st), "i due file sono letti dalla pagina (JSON e PDF)") | |
| events.clear() | |
| await js("document.getElementById('input').value = 'Dimmi: di che cliente è l ordine nel JSON, e quanto è il canone mensile nel contratto PDF?'; document.getElementById('composer').requestSubmit(); true") | |
| answer = None | |
| for _ in range(240): | |
| await asyncio.sleep(1) | |
| d = [e for e in events if e["ev"] == "done"] | |
| if d: | |
| answer = d[0]["answer"] | |
| break | |
| user = next((e for e in events if e["ev"] == "user_msg"), None) | |
| print(" messaggio:", (user or {}).get("text", "")[:200].replace("\n", " | ")) | |
| print(" ->", (answer or "")[:300]) | |
| check(user is not None and "ordine.json" in user["text"] and "contratto.pdf" in user["text"], "la chat mostra gli allegati") | |
| check(answer and "rossi" in answer.lower(), "legge il JSON (cliente Rossi)") | |
| check(answer and "750" in answer, "legge il PDF (canone 750)") | |
| check(os.path.exists(os.path.join(BASE, "lavori", "allegati", "ordine.json")) and | |
| os.path.exists(os.path.join(BASE, "lavori", "allegati", "contratto.pdf.txt")), "i file sono salvati in lavori/allegati") | |
| check(await js("document.querySelectorAll('.attach-item').length") == 0, "gli allegati spariscono dalla barra dopo l'invio") | |
| print("2) PDF scansionato, senza strumenti: le pagine sono immagini e le legge l'OCR da sé") | |
| await js("document.getElementById('tools-on').checked = false; true") | |
| await call("DOM.setFileInputFiles", files=[os.path.join(FILES, "scansione.pdf")], nodeId=node["nodeId"]) | |
| for _ in range(120): | |
| await asyncio.sleep(0.5) | |
| st = await js("[...document.querySelectorAll('.attach-item')].map(e => e.className + '|' + e.textContent)") | |
| if st and len(st) == 1 and "busy" not in st[0].split("|")[0]: | |
| break | |
| print(" ", st) | |
| check(st and "scansionat" in st[0], "la pagina riconosce che il PDF è una scansione") | |
| events.clear() | |
| await js("document.getElementById('input').value = 'Dimmi il totale della fattura nel PDF scansionato.'; document.getElementById('composer').requestSubmit(); true") | |
| answer = None | |
| for _ in range(300): | |
| await asyncio.sleep(1) | |
| d = [e for e in events if e["ev"] == "done"] | |
| if d: | |
| answer = d[0]["answer"] | |
| break | |
| tools = [e for e in events if e["ev"] == "tool" and e["phase"] == "end"] | |
| for t in tools: | |
| print(f" [strumento] {t['name']} {t['args'][:80]} -> {t['result'][:160]!r}") | |
| print(" ->", (answer or "")[:300]) | |
| log = open(os.path.join(BASE, "motore.log"), encoding="utf-8", errors="replace").read() | |
| check("[ocr] allegati/scansione-p1.jpg (testo letto)" in log, "l'OCR ha letto la pagina scansionata prima della risposta") | |
| check(answer and ("1.280,50" in answer or "1280,50" in answer), "dice il totale giusto della scansione") | |
| stop.set() | |
| await obs | |
| finally: | |
| chrome.kill() | |
| for name in ("atlas.json", "atlas.fingerprint", "atlas.bin", "atlas.fondo", "integrity.txt"): | |
| if os.path.exists(os.path.join(ROOT, "state", name)): | |
| os.makedirs(os.path.join(BASE, "state"), exist_ok=True) | |
| shutil.copy(os.path.join(ROOT, "state", name), os.path.join(BASE, "state", name)) | |
| eng = subprocess.Popen([os.path.join(ROOT, "build", "neuronald.exe"), "--port", str(PORT), "--state", os.path.join(BASE, "state"), "--local-only", | |
| "--workspace", os.path.join(BASE, "lavori"), "--ctx", "16384", "--web", os.path.join(ROOT, "web")], | |
| cwd=ROOT, env=dict(os.environ, PATH=RUNTIME + os.pathsep + os.environ["PATH"], NEURONAL_NO_BOOT_CHECK="1"), | |
| stdout=open(os.path.join(BASE, "motore.log"), "w", encoding="utf-8"), stderr=subprocess.STDOUT) | |
| try: | |
| for _ in range(300): | |
| try: | |
| urllib.request.urlopen(f"http://127.0.0.1:{PORT}/health", timeout=2) | |
| break | |
| except OSError: | |
| time.sleep(1) | |
| asyncio.run(main()) | |
| finally: | |
| eng.kill() | |
| print("\nRISULTATO:", "OK" if not failures else f"{len(failures)} errori") | |
| sys.exit(1 if failures else 0) | |