"""Lightweight runtime inference-trace logger (optional open inference log). Appends one JSON line per inference to IRIS_TRACE_FILE. Opt-in via IRIS_TRACE=1. Privacy: no raw images or audio are stored — only metadata and the produced text, truncated. The JSONL is later converted to a Hugging Face Dataset (see scripts/publish_trace.py) so others can learn how the app behaves. """ import json import os import time import uuid ENABLED = os.environ.get("IRIS_TRACE") == "1" _FILE = os.environ.get("IRIS_TRACE_FILE", "traces/iris_traces.jsonl") def log(endpoint: str, lang: str, question: str, answer: str, latency_s: float, model: str) -> None: if not ENABLED: return try: os.makedirs(os.path.dirname(_FILE) or ".", exist_ok=True) row = { "trace_id": uuid.uuid4().hex, "timestamp": time.strftime("%Y-%m-%dT%H:%M:%SZ", time.gmtime()), "endpoint": endpoint, "lang": lang, "question": (question or "")[:200], "answer": (answer or "")[:300], "latency_s": round(latency_s, 2), "model": model, } with open(_FILE, "a", encoding="utf-8") as f: f.write(json.dumps(row, ensure_ascii=False) + "\n") except Exception: pass # tracing must never break the app