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|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import os
|
| 2 |
+
import subprocess
|
| 3 |
+
import requests
|
| 4 |
+
import zipfile
|
| 5 |
+
import gzip
|
| 6 |
+
import shutil
|
| 7 |
+
import json
|
| 8 |
+
|
| 9 |
+
# Step 1: Download and extract libzip5 and libssl1.1
|
| 10 |
+
print("Downloading libzip5 and libssl1.1...")
|
| 11 |
+
libzip_url = "http://archive.ubuntu.com/ubuntu/pool/universe/libz/libzip/libzip5_1.5.1-0ubuntu1_amd64.deb"
|
| 12 |
+
libssl_url = "http://archive.ubuntu.com/ubuntu/pool/main/o/openssl/libssl1.1_1.1.1f-1ubuntu2_amd64.deb"
|
| 13 |
+
libzip_deb = "libzip5_1.5.1-0ubuntu1_amd64.deb"
|
| 14 |
+
libssl_deb = "libssl1.1_1.1.1f-1ubuntu2_amd64.deb"
|
| 15 |
+
lib_dir = "libs"
|
| 16 |
+
|
| 17 |
+
os.makedirs(lib_dir, exist_ok=True)
|
| 18 |
+
|
| 19 |
+
for url, deb in [(libzip_url, libzip_deb), (libssl_url, libssl_deb)]:
|
| 20 |
+
try:
|
| 21 |
+
print(f"Downloading {deb}...")
|
| 22 |
+
response = requests.get(url)
|
| 23 |
+
with open(deb, "wb") as f:
|
| 24 |
+
f.write(response.content)
|
| 25 |
+
print(f"Extracting {deb}...")
|
| 26 |
+
subprocess.run(["dpkg-deb", "-x", deb, lib_dir])
|
| 27 |
+
if os.path.exists(deb):
|
| 28 |
+
os.remove(deb)
|
| 29 |
+
print(f"Deleted {deb}.")
|
| 30 |
+
except Exception as e:
|
| 31 |
+
print(f"Error processing {deb}: {e}")
|
| 32 |
+
|
| 33 |
+
libzip_lib_path = os.path.join(lib_dir, "usr", "lib", "x86_64-linux-gnu")
|
| 34 |
+
libssl_lib_path = os.path.join(lib_dir, "lib", "x86_64-linux-gnu")
|
| 35 |
+
os.environ["LD_LIBRARY_PATH"] = f"{libzip_lib_path}:{libssl_lib_path}"
|
| 36 |
+
|
| 37 |
+
# Step 2: Download and unzip KataGo (Eigen version)
|
| 38 |
+
print("Downloading KataGo (Eigen version)...")
|
| 39 |
+
katago_url = "https://github.com/lightvector/KataGo/releases/download/v1.15.3/katago-v1.15.3-eigen-linux-x64.zip"
|
| 40 |
+
katago_zip = "katago-v1.15.3-eigen-linux-x64.zip"
|
| 41 |
+
katago_dir = "katago"
|
| 42 |
+
|
| 43 |
+
try:
|
| 44 |
+
print(f"Downloading {katago_zip}...")
|
| 45 |
+
response = requests.get(katago_url)
|
| 46 |
+
with open(katago_zip, "wb") as f:
|
| 47 |
+
f.write(response.content)
|
| 48 |
+
print(f"Extracting {katago_zip}...")
|
| 49 |
+
with zipfile.ZipFile(katago_zip, "r") as zip_ref:
|
| 50 |
+
zip_ref.extractall(katago_dir)
|
| 51 |
+
if os.path.exists(katago_zip):
|
| 52 |
+
os.remove(katago_zip)
|
| 53 |
+
print(f"Deleted {katago_zip}.")
|
| 54 |
+
os.chmod(os.path.join(katago_dir, "katago"), 0o755)
|
| 55 |
+
print("KataGo setup complete.")
|
| 56 |
+
except Exception as e:
|
| 57 |
+
print(f"Error setting up KataGo: {e}")
|
| 58 |
+
|
| 59 |
+
# Step 3: Download the KataGo model
|
| 60 |
+
print("Downloading KataGo model...")
|
| 61 |
+
model_url = "https://media.katagotraining.org/uploaded/networks/models/kata1/kata1-b18c384nbt-s9937771520-d4300882049.bin.gz"
|
| 62 |
+
model_gz = "kata1-b18c384nbt-s9937771520-d4300882049.bin.gz"
|
| 63 |
+
model_bin = "kata1-b18c384nbt-s9937771520-d4300882049.bin"
|
| 64 |
+
|
| 65 |
+
try:
|
| 66 |
+
print(f"Downloading {model_gz}...")
|
| 67 |
+
response = requests.get(model_url)
|
| 68 |
+
with open(model_gz, "wb") as f:
|
| 69 |
+
f.write(response.content)
|
| 70 |
+
print(f"Extracting {model_gz}...")
|
| 71 |
+
with gzip.open(model_gz, "rb") as gz_file:
|
| 72 |
+
with open(os.path.join(katago_dir, model_bin), "wb") as bin_file:
|
| 73 |
+
shutil.copyfileobj(gz_file, bin_file)
|
| 74 |
+
if os.path.exists(model_gz):
|
| 75 |
+
os.remove(model_gz)
|
| 76 |
+
print(f"Deleted {model_gz}.")
|
| 77 |
+
print("KataGo model setup complete.")
|
| 78 |
+
except Exception as e:
|
| 79 |
+
print(f"Error setting up KataGo model: {e}")
|
| 80 |
+
|
| 81 |
+
# Step 4: Download gtp2ogs
|
| 82 |
+
print("Downloading gtp2ogs...")
|
| 83 |
+
gtp2ogs_url = "https://github.com/online-go/gtp2ogs/releases/download/9.0/gtp2ogs-9.0.0-linux"
|
| 84 |
+
gtp2ogs_binary = "gtp2ogs"
|
| 85 |
+
|
| 86 |
+
try:
|
| 87 |
+
print(f"Downloading {gtp2ogs_binary}...")
|
| 88 |
+
response = requests.get(gtp2ogs_url)
|
| 89 |
+
with open(gtp2ogs_binary, "wb") as f:
|
| 90 |
+
f.write(response.content)
|
| 91 |
+
os.chmod(gtp2ogs_binary, 0o755)
|
| 92 |
+
print("gtp2ogs setup complete.")
|
| 93 |
+
except Exception as e:
|
| 94 |
+
print(f"Error setting up gtp2ogs: {e}")
|
| 95 |
+
|
| 96 |
+
# Step 5: Modify default_gtp.cfg
|
| 97 |
+
print("Updating default_gtp.cfg...")
|
| 98 |
+
default_gtp_cfg_path = os.path.join(katago_dir, "default_gtp.cfg")
|
| 99 |
+
|
| 100 |
+
try:
|
| 101 |
+
with open(default_gtp_cfg_path, "r") as f:
|
| 102 |
+
lines = f.readlines()
|
| 103 |
+
|
| 104 |
+
# Apply the original changes
|
| 105 |
+
lines[54] = "logSearchInfo = true\n"
|
| 106 |
+
lines[63] = "ogsChatToStderr = True\n"
|
| 107 |
+
lines[300] = "# maxVisits = 500\n"
|
| 108 |
+
lines[302] = "maxTime = 1.0\n"
|
| 109 |
+
lines[305] = "ponderingEnabled = true\n"
|
| 110 |
+
|
| 111 |
+
# Apply the new rules configuration (lines 113 to 149)
|
| 112 |
+
lines[113:150] = [
|
| 113 |
+
"# rules = tromp-taylor\n",
|
| 114 |
+
"\n",
|
| 115 |
+
"# By default, the \"rules\" parameter is used, but if you comment it out and\n",
|
| 116 |
+
"# uncomment one option in each of the sections below, you can specify an\n",
|
| 117 |
+
"# arbitrary combination of individual rules.\n",
|
| 118 |
+
"\n",
|
| 119 |
+
"# koRule = SIMPLE # Simple ko rules (triple ko = no result)\n",
|
| 120 |
+
"koRule = POSITIONAL # Positional superko\n",
|
| 121 |
+
"# koRule = SITUATIONAL # Situational superko\n",
|
| 122 |
+
"\n",
|
| 123 |
+
"scoringRule = AREA # Area scoring\n",
|
| 124 |
+
"# scoringRule = TERRITORY # Territory scoring (special computer-friendly territory rules)\n",
|
| 125 |
+
"\n",
|
| 126 |
+
"taxRule = NONE # All surrounded empty points are scored\n",
|
| 127 |
+
"# taxRule = SEKI # Eyes in seki do NOT count as points\n",
|
| 128 |
+
"# taxRule = ALL # All groups are taxed up to 2 points for the two eyes needed to live\n",
|
| 129 |
+
"\n",
|
| 130 |
+
"# Is multiple-stone suicide legal? (Single-stone suicide is always illegal).\n",
|
| 131 |
+
"# multiStoneSuicideLegal = false\n",
|
| 132 |
+
"multiStoneSuicideLegal = true # Allow multi-stone suicide\n",
|
| 133 |
+
"\n",
|
| 134 |
+
"# \"Button go\" - the first pass when area scoring awards 0.5 points and does\n",
|
| 135 |
+
"# not count for ending the game.\n",
|
| 136 |
+
"# Allows area scoring rulesets that have far simpler rules to achieve the same\n",
|
| 137 |
+
"# final scoring precision and reward for precise play as territory scoring.\n",
|
| 138 |
+
"# hasButton = false\n",
|
| 139 |
+
"# hasButton = true\n",
|
| 140 |
+
"\n",
|
| 141 |
+
"# Is this a human ruleset where it's okay to pass before having physically\n",
|
| 142 |
+
"# captured and removed all dead stones?\n",
|
| 143 |
+
"# friendlyPassOk = false\n",
|
| 144 |
+
"friendlyPassOk = true # Allow friendly pass\n",
|
| 145 |
+
"\n",
|
| 146 |
+
"# How handicap stones in handicap games are compensated\n",
|
| 147 |
+
"# whiteHandicapBonus = 0 # White gets no compensation for black's handicap stones (Tromp-taylor, NZ, JP)\n",
|
| 148 |
+
"# whiteHandicapBonus = N-1 # White gets N-1 points for black's N handicap stones (AGA)\n",
|
| 149 |
+
"# whiteHandicapBonus = N # White gets N points for black's N handicap stones (Chinese)\n",
|
| 150 |
+
]
|
| 151 |
+
|
| 152 |
+
# Write the updated content back to the file
|
| 153 |
+
with open(default_gtp_cfg_path, "w") as f:
|
| 154 |
+
f.writelines(lines)
|
| 155 |
+
print("default_gtp.cfg has been updated successfully!")
|
| 156 |
+
except Exception as e:
|
| 157 |
+
print(f"Error updating default_gtp.cfg: {e}")
|
| 158 |
+
|
| 159 |
+
# Step 6: Generate kata_speed.json5
|
| 160 |
+
print("Generating kata_speed.json5...")
|
| 161 |
+
kata_speed_config = {
|
| 162 |
+
"blacklist": [""],
|
| 163 |
+
"whitelist": [""],
|
| 164 |
+
"allow_ranked": True,
|
| 165 |
+
"decline_new_challenges": True,
|
| 166 |
+
"max_games_per_player": 1,
|
| 167 |
+
"hidden": False,
|
| 168 |
+
"allowed_board_sizes": [19],
|
| 169 |
+
"engine": "KataGo v1.15.3",
|
| 170 |
+
"allow_unranked": True,
|
| 171 |
+
"farewellscore": True,
|
| 172 |
+
"bot": {
|
| 173 |
+
"send_pv_data": True,
|
| 174 |
+
"send_chats": True
|
| 175 |
+
},
|
| 176 |
+
# Disable correspondence games
|
| 177 |
+
"allowed_correspondence_settings": None
|
| 178 |
+
}
|
| 179 |
+
|
| 180 |
+
try:
|
| 181 |
+
with open("kata_speed.json5", "w", encoding="utf-8") as f:
|
| 182 |
+
json.dump(kata_speed_config, f, indent=4, ensure_ascii=False)
|
| 183 |
+
print("kata_speed.json5 has been generated successfully!")
|
| 184 |
+
except Exception as e:
|
| 185 |
+
print(f"Error generating kata_speed.json5: {e}")
|
| 186 |
+
|
| 187 |
+
# Step 7: Run gtp2ogs with KataGo
|
| 188 |
+
print("Running gtp2ogs with KataGo...")
|
| 189 |
+
api_key ="3a6eabc09f8669c66cb975b9de77e49aa70d169a"
|
| 190 |
+
# "87e161967084e7db5e1099ba8b59c46365d1887b",
|
| 191 |
+
# "c2c5c771b3b12f78ee58e9dc2954035a6d383cee"
|
| 192 |
+
command = [
|
| 193 |
+
"./gtp2ogs",
|
| 194 |
+
"--apikey", api_key,
|
| 195 |
+
"--config", "kata_speed.json5",
|
| 196 |
+
"--",
|
| 197 |
+
os.path.join(katago_dir, "katago"),
|
| 198 |
+
"gtp",
|
| 199 |
+
"-config", os.path.join(katago_dir, "default_gtp.cfg"),
|
| 200 |
+
"-model", os.path.join(katago_dir, model_bin)
|
| 201 |
+
]
|
| 202 |
+
|
| 203 |
+
try:
|
| 204 |
+
subprocess.run(command)
|
| 205 |
+
except Exception as e:
|
| 206 |
+
print(f"Error running gtp2ogs: {e}")
|