aitrace / web.py
Ghongha007's picture
In-browser demo: runs the tool's own code with Pyodide
d6ca2e7 verified
Raw History Blame Contribute Delete
1.7 kB
"""Glue between the page and aitrace. Runs inside the browser (Pyodide); aitrace's own code is loaded from GitHub."""
import html
import json
import os
from aitrace.core import ALWAYS, INFO, MODERATE, STRONG, WEAK, inspect
ORDER = {STRONG: 0, MODERATE: 1, WEAK: 2, INFO: 3}
TONE = {"DECLARED AI-GENERATED": "red", "DECLARED SYNTHETIC": "red", "AI PRODUCT NAMED": "amber",
"CAMERA ORIGIN CLAIMED, WITH CONTENT CREDENTIALS": "green", "CONTENT CREDENTIALS PRESENT": "green",
"CAMERA METADATA PRESENT": "blue", "EDITING OR ENCODING SOFTWARE NAMED": "blue"}
def check(path, name):
rep = inspect(path)
e = html.escape
dims = f", {rep.width}×{rep.height}" if rep.width and rep.height else ""
out = [f'<div class="verdict tone-{TONE.get(rep.verdict, "grey")}"><div class="v-title">{e(rep.verdict)}</div>',
f'<p>{e(rep.summary)}</p><p class="meta">{e(name)} · {e(rep.format)}{dims} · {rep.size_bytes:,} bytes</p></div>']
if rep.findings:
out.append('<h3>What was found</h3><ul class="findings">')
for f in sorted(rep.findings, key=lambda f: ORDER[f.strength]):
out.append(f'<li><span class="badge s-{f.strength}">{e(f.strength)}</span> <b>{e(f.what)}</b>'
f'<br><span class="where">Where: {e(f.where)}</span>'
+ (f'<br><span class="caveat">Caution: {e(f.caveat)}</span>' if f.caveat else "") + "</li>")
out.append("</ul>")
out.append(f'<p class="always">{e(ALWAYS)}</p>')
meta = json.dumps(rep.metadata, indent=1, ensure_ascii=False, default=str) if rep.metadata else "No metadata was read from this file."
return json.dumps({"html": "\n".join(out), "meta": meta})