Icannos commited on
Commit ·
da93298
1
Parent(s): a1a7114
first commit
Browse files- lichess_games.py +118 -0
lichess_games.py
ADDED
|
@@ -0,0 +1,118 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
"""This a module for downloading games from database.lihess.org."""
|
| 2 |
+
|
| 3 |
+
import os
|
| 4 |
+
import io
|
| 5 |
+
import sys
|
| 6 |
+
|
| 7 |
+
import chess.pgn
|
| 8 |
+
import traceback
|
| 9 |
+
import zstandard
|
| 10 |
+
|
| 11 |
+
import requests
|
| 12 |
+
import datasets
|
| 13 |
+
|
| 14 |
+
_CITATION = """NOTTHING YET"""
|
| 15 |
+
|
| 16 |
+
_DESCRIPTION = """\
|
| 17 |
+
Lichess.org is a free/libre, open-source chess server powered by volunteers and donations and provides all of its content
|
| 18 |
+
in CC0. This script download all the games from the database and provide them in LLM pretraining friendly format.
|
| 19 |
+
"""
|
| 20 |
+
|
| 21 |
+
_HOMEPAGE = ""
|
| 22 |
+
|
| 23 |
+
_LICENSE = "Creative Commons Zero v1.0 Universal"
|
| 24 |
+
|
| 25 |
+
_URLS = {
|
| 26 |
+
"base_url": "https://database.lichess.org/standard/",
|
| 27 |
+
}
|
| 28 |
+
|
| 29 |
+
_FILE_TEMPLATE = "lichess_db_standard_rated_{}.pgn.zst"
|
| 30 |
+
|
| 31 |
+
_DOWNLOAD_LIST = "https://database.lichess.org/standard/list.txt"
|
| 32 |
+
|
| 33 |
+
|
| 34 |
+
def get_dates_configs():
|
| 35 |
+
# download download_list using requests:
|
| 36 |
+
r = requests.get(_DOWNLOAD_LIST)
|
| 37 |
+
files = r.text.split("\n")
|
| 38 |
+
|
| 39 |
+
# https://database.lichess.org/standard/lichess_db_standard_rated_2023-06.pgn.zst
|
| 40 |
+
# split every line to get the date
|
| 41 |
+
dates_configs = [f.split("_")[-1].split(".")[0] for f in files]
|
| 42 |
+
|
| 43 |
+
return dates_configs
|
| 44 |
+
|
| 45 |
+
|
| 46 |
+
class LichessGames(datasets.GeneratorBasedBuilder):
|
| 47 |
+
"""Lichess games"""
|
| 48 |
+
|
| 49 |
+
VERSION = datasets.Version("1.0.0")
|
| 50 |
+
|
| 51 |
+
configs = get_dates_configs()
|
| 52 |
+
BUILDER_CONFIGS = [
|
| 53 |
+
datasets.BuilderConfig(
|
| 54 |
+
name=d,
|
| 55 |
+
version=datasets.Version("1.0.0"),
|
| 56 |
+
description=f"Games of {d}",
|
| 57 |
+
)
|
| 58 |
+
for d in configs
|
| 59 |
+
]
|
| 60 |
+
|
| 61 |
+
DEFAULT_CONFIG_NAME = configs[0]
|
| 62 |
+
|
| 63 |
+
def _info(self):
|
| 64 |
+
features = datasets.Features(
|
| 65 |
+
{
|
| 66 |
+
"text": datasets.Value("string"),
|
| 67 |
+
}
|
| 68 |
+
)
|
| 69 |
+
|
| 70 |
+
return datasets.DatasetInfo(
|
| 71 |
+
description=_DESCRIPTION,
|
| 72 |
+
features=features,
|
| 73 |
+
homepage=_HOMEPAGE,
|
| 74 |
+
license=_LICENSE,
|
| 75 |
+
citation=_CITATION,
|
| 76 |
+
)
|
| 77 |
+
|
| 78 |
+
def _split_generators(self, dl_manager):
|
| 79 |
+
file_name = _FILE_TEMPLATE.format(self.config.name)
|
| 80 |
+
url = {self.config.name: _URLS["base_url"] + file_name}
|
| 81 |
+
|
| 82 |
+
data_dir = dl_manager.download(url)
|
| 83 |
+
|
| 84 |
+
return [
|
| 85 |
+
datasets.SplitGenerator(
|
| 86 |
+
name=datasets.Split.TRAIN,
|
| 87 |
+
# These kwargs will be passed to _generate_examples
|
| 88 |
+
gen_kwargs={
|
| 89 |
+
"filepath": data_dir[self.config.name],
|
| 90 |
+
"split": "train",
|
| 91 |
+
},
|
| 92 |
+
),
|
| 93 |
+
]
|
| 94 |
+
|
| 95 |
+
# method parameters are unpacked from `gen_kwargs` as given in `_split_generators`
|
| 96 |
+
def _generate_examples(self, filepath, split):
|
| 97 |
+
try:
|
| 98 |
+
with open(filepath, 'rb') as f:
|
| 99 |
+
dctx = zstandard.ZstdDecompressor()
|
| 100 |
+
stream_reader = dctx.stream_reader(f)
|
| 101 |
+
pgn = io.TextIOWrapper(stream_reader, encoding="utf-8")
|
| 102 |
+
|
| 103 |
+
k = 0
|
| 104 |
+
while True:
|
| 105 |
+
try:
|
| 106 |
+
game = chess.pgn.read_game(pgn)
|
| 107 |
+
except Exception as e:
|
| 108 |
+
print(e)
|
| 109 |
+
break
|
| 110 |
+
|
| 111 |
+
if game is None:
|
| 112 |
+
break
|
| 113 |
+
|
| 114 |
+
k += 1
|
| 115 |
+
yield f"{self.config.name}-{k}", {'text': str(game)}
|
| 116 |
+
except Exception as e:
|
| 117 |
+
traceback.print_exc(file=sys.stdout)
|
| 118 |
+
return
|