File size: 1,776 Bytes
cc8b578
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
import json
import logging
import os

import argilla as rg
from huggingface_hub import HfApi

logger = logging.getLogger(__name__)
logger.setLevel(logging.INFO)

if __name__ == "__main__":
    logger.info("*** Initializing Argilla session ***")
    rg.init(
        api_url=os.getenv("ARGILLA_API_URL"),
        api_key=os.getenv("ARGILLA_API_KEY"),
        extra_headers={"Authorization": f"Bearer {os.getenv('HF_TOKEN')}"},
    )

    logger.info("*** Fetching dataset from Argilla ***")
    dataset = rg.FeedbackDataset.from_argilla(
        os.getenv("SOURCE_DATASET"),
        workspace=os.getenv("SOURCE_WORKSPACE"),
    )
    logger.info("*** Filtering records by `response_status` ***")
    dataset = dataset.filter_by(response_status=["submitted"])  # type: ignore

    logger.info("*** Calculating users and annotation count ***")
    output = {}
    for record in dataset.records:
        for response in record.responses:
            if response.user_id not in output:
                output[response.user_id] = 0
            output[response.user_id] += 1

    for key in list(output.keys()):
        output[rg.User.from_id(key).username] = output.pop(key)

    logger.info("*** Users and annotation count successfully calculated! ***")

    logger.info("*** Dumping Python dict into `stats.json` ***")
    with open("stats.json", "w") as file:
        json.dump(output, file, indent=4)

    logger.info("*** Uploading `stats.json` to Hugging Face Hub ***")
    api = HfApi(token=os.getenv("HF_TOKEN"))
    api.upload_file(
        path_or_fileobj="stats.json",
        path_in_repo="stats.json",
        repo_id="DIBT/prompt-collective-dashboard",
        repo_type="space",
    )
    logger.info("*** `stats.json` successfully uploaded to Hugging Face Hub! ***")