File size: 1,329 Bytes
04c91be
f7a11cd
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
"""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