File size: 2,356 Bytes
011bd7a
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
83
84
85
86
87
88
89
from collections import defaultdict
from time import sleep

from datasets import DatasetDict, load_dataset
from huggingface_hub import HfApi, hf_hub_download

languages = [
    "de",
    "bn",
    "it",
    "pt",
    "nl",
    "cs",
    "ro",
    "bg",
    "sr",
    "fi",
    "fa",
    "hi",
    "da",
    "en",
    "no",
    "sv",
]


def map_corpus_to_query(example, negatives_dict):
    query = example["query"]
    title = example["title"]
    positive = [example["text"]]
    negatives = negatives_dict[title]
    return {"query": query, "positive": positive, "negative": negatives}


ds_dict = DatasetDict()
for lang in languages:
    sleep(5)  # HF hub rate limit
    ds_queries = load_dataset(
        f"rasdani/cohere-wikipedia-2023-11-{lang}-queries", split="train"
    )
    ds_corpus = load_dataset(
        f"rasdani/cohere-wikipedia-2023-11-{lang}-1.5k-articles", split="train"
    )
    ds_corpus = ds_corpus.filter(lambda x: x["score"] != 1)
    sleep(5)

    negatives_dict = defaultdict(list)
    for row in ds_corpus:
        negatives_dict[row["title"]].append(row["text"])

    ds = ds_queries.map(
        lambda x: map_corpus_to_query(x, negatives_dict),
        remove_columns=ds_queries.column_names,
    )

    repo_id = "ellamind/wikipedia-2023-11-reranking-multilingual"
    ds.push_to_hub(repo_id, config_name=lang, split="test")

# Download the README from the repository
sleep(5)
readme_path = hf_hub_download(
    repo_id=repo_id, filename="README.md", repo_type="dataset"
)

with open(readme_path, "r") as f:
    readme_content = f.read()

readme = """
This dataset is derived from Cohere's wikipedia-2023-11 dataset, which is in turn derived from `wikimedia/wikipedia`.
The dataset is licensed under the Creative Commons CC BY-SA 3.0 license.
"""
# Prepend the license key to the YAML header and append the custom README
if "- license: " not in readme_content and readme not in readme_content:
    license = "cc-by-sa-3.0"

    updated_readme = readme_content.replace(
        "---\ndataset_info:", "---\nlicense: {license}\ndataset_info:"
    ).format(license=license)
    updated_readme += readme

    api = HfApi()
    readme_bytes = updated_readme.encode("utf-8")
    api.upload_file(
        path_or_fileobj=readme_bytes,
        path_in_repo="README.md",
        repo_id=repo_id,
        repo_type="dataset",
    )