File size: 2,267 Bytes
515204e
 
c3156f6
 
 
 
 
 
 
 
 
 
515204e
 
 
 
 
 
c3156f6
 
515204e
c3156f6
 
 
 
 
 
515204e
 
 
 
 
 
 
 
 
 
 
 
 
 
 
c3156f6
 
 
 
 
 
 
515204e
c3156f6
515204e
 
c3156f6
515204e
c3156f6
 
 
 
515204e
c3156f6
 
515204e
 
c3156f6
 
 
 
 
515204e
 
 
 
 
c3156f6
515204e
 
c3156f6
 
515204e
 
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
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
"""Log user interactions to a HuggingFace Hub dataset."""

import json
import os
import uuid
from pathlib import Path

from huggingface_hub import HfApi, HfFileSystem

DATASET_REPO = "EarthSpeciesProject/naturelm-audio-space-logs"
SPLIT = "test"
TESTING = os.getenv("TESTING", "0") == "1"

_hf_token = os.getenv("HF_TOKEN", None)
_api = HfApi(token=_hf_token)
_fs = HfFileSystem(token=_hf_token)

_METADATA_PATH = f"datasets/{DATASET_REPO}/{SPLIT}/metadata.jsonl"


def log_interaction(
    audio: str | Path,
    user_text: str,
    model_response: str,
    session_id: str = "",
    model_version: str = "",
) -> None:
    """Log a single user/model exchange (audio + text) to the Hub dataset.

    Parameters
    ----------
    audio : str | Path
        Local path to the audio file uploaded by the user.
    user_text : str
        The user's query text.
    model_response : str
        The model's generated response.
    session_id : str
        Unique identifier for the user session.
    model_version : str
        Version string of the model that produced the response.
    """
    data_id = str(uuid.uuid4())

    if TESTING:
        data_id = "test-" + data_id
        session_id = "test-" + session_id

    suffix = Path(audio).suffix
    audio_repo_path = f"{SPLIT}/audio/{session_id}{suffix}"

    if not _fs.exists(f"datasets/{DATASET_REPO}/{audio_repo_path}"):
        _api.upload_file(
            path_or_fileobj=str(audio),
            path_in_repo=audio_repo_path,
            repo_id=DATASET_REPO,
            repo_type="dataset",
        )

    record = {
        "user_message": user_text,
        "model_response": model_response,
        "file_name": f"audio/{session_id}{suffix}",
        "original_fn": Path(audio).name,
        "id": data_id,
        "session_id": session_id,
        "model_version": model_version,
    }

    line = json.dumps(record) + "\n"

    # HfFileSystem doesn't support append, so read-then-write.
    if _fs.exists(_METADATA_PATH):
        with _fs.open(_METADATA_PATH, "r") as f:
            lines = f.readlines()
        lines.append(line)
        with _fs.open(_METADATA_PATH, "w") as f:
            f.writelines(lines)
    else:
        with _fs.open(_METADATA_PATH, "w") as f:
            f.write(line)