Skip to content

Commit decedb8

Browse files
authored
Fixes and Optimizations
Optimize the code and resolve the issue in Method 2 where the latest model cannot be found due to the slow website updates.
1 parent 44baf78 commit decedb8

File tree

1 file changed

+22
-34
lines changed

1 file changed

+22
-34
lines changed

kata-weights.py

Lines changed: 22 additions & 34 deletions
Original file line numberDiff line numberDiff line change
@@ -77,26 +77,6 @@ def get_page(url, get_content=True):
7777
except:
7878
raise
7979

80-
def get_page_number(pattern):
81-
# 获取第一个匹配的模型所在页数
82-
url = 'https://katagotraining.org/api/networks-for-elo/?format=json' #更新较慢
83-
response, content_ = get_page(url)
84-
infos = json.loads(content_)
85-
numModels = len(infos)
86-
total_numPages = (numModels - 1) // 20 + 1
87-
model_num = 0
88-
page_number = ""
89-
for info in infos:
90-
model_num = model_num + 1
91-
model_name = info['name']
92-
if re.search(pattern, model_name, re.IGNORECASE):
93-
page_number = (model_num - 1) // 20 + 1
94-
break
95-
if not page_number:
96-
print(f'ERROR: No weights matching "{pattern}" were found.')
97-
sys.exit(1)
98-
return page_number, total_numPages
99-
10080
model_url = None
10181
regexp_mode = False
10282

@@ -144,9 +124,7 @@ def get_page_number(pattern):
144124
if re.search('-new', WEIGHT_FILE, re.IGNORECASE):
145125
use_new = True
146126
if SAMPLE == None and not use_new:
147-
if BLOCK == '60':
148-
model_url = "https://media.katagotraining.org/uploaded/networks/models/kata1/kata1-b60c320-s9356080896-d3824355768.bin.gz"
149-
elif BLOCK == '30':
127+
if BLOCK == '30':
150128
model_url = "https://github.com/lightvector/KataGo/releases/download/v1.4.5/g170-b30c320x2-s4824661760-d1229536699.bin.gz"
151129
elif BLOCK == '20':
152130
model_url = "https://media.katagotraining.org/uploaded/networks/models/kata1/kata1-b20c256x2-s5303129600-d1228401921.bin.gz"
@@ -175,10 +153,8 @@ def get_page_number(pattern):
175153
max_lower_elo = -1
176154
i = 0
177155
# 遍历表格行
178-
for row in table.xpath('.//tr'):
156+
for row in table.xpath('.//tr')[1:]:
179157
columns = row.xpath('.//td')
180-
if not len(columns):
181-
continue
182158
model_name = columns[0].text.strip()
183159
if re.search(pattern, model_name, re.IGNORECASE) == None:
184160
continue
@@ -196,19 +172,31 @@ def get_page_number(pattern):
196172
continue
197173
max_lower_elo = lower_elo
198174
model_url = columns[3].xpath('.//a/@href')[0]
199-
# 方法2,'lxml'模块不可用时,通过api方式获取模型链接
175+
# 方法2,'lxml'模块不可用时,从api获取模型链接
200176
else:
201177
base_url = "https://katagotraining.org/api/networks/?format=json&page={}" #每页20个模型
202-
page_number, total_numPages = get_page_number(pattern)
203-
numPagesSearch = 5 #第一个匹配到的模型所在的页面和之后的4页
178+
url = 'https://katagotraining.org/api/networks-for-elo/?format=json' #更新较慢
179+
response, content_ = get_page(url)
180+
infos = json.loads(content_)
181+
numModels = len(infos)
182+
total_numPages = (numModels - 1) // 20 + 1
183+
model_num = 0
184+
page_number = 1
185+
for info in infos:
186+
model_num = model_num + 1
187+
model_name = info['name']
188+
if re.search(pattern, model_name, re.IGNORECASE):
189+
page_number = (model_num - 1) // 20 + 1
190+
break
191+
numPagesToSearch = 5 #第一个匹配到的模型所在的页面和之后的4页
204192
if use_new or regexp_mode or SAMPLE:
205-
numPagesSearch = 2
206-
if total_numPages - page_number + 1 < numPagesSearch: #避免超出最大页数
207-
numPagesSearch = total_numPages - page_number + 1
193+
numPagesToSearch = 2
194+
if total_numPages - page_number + 1 < numPagesToSearch: #避免超出最大页数
195+
numPagesToSearch = total_numPages - page_number + 1
208196
urls = []
209-
for n in range(page_number, page_number + numPagesSearch):
197+
for n in range(page_number, page_number + numPagesToSearch):
210198
urls.append(base_url.format(n))
211-
with ThreadPoolExecutor(max_workers=5) as executor:
199+
with ThreadPoolExecutor(max_workers=numPagesToSearch) as executor:
212200
futures = [executor.submit(get_page, url) for url in urls] #多线程获取网页
213201
max_lower_elo = -1
214202
for future in futures:

0 commit comments

Comments
 (0)