def download_models(self, *_largs): def download_complete(req, tmp_path, path, model): try: os.rename(tmp_path, path) self.katrain.log(f"Download of {model} model complete -> {path}", OUTPUT_INFO) except Exception as e: self.katrain.log(f"Download of {model} model complete, but could not move file: {e}", OUTPUT_ERROR) self.check_models() for c in self.download_progress_box.children: if isinstance(c, ProgressLoader) and c.request: c.request.cancel() self.download_progress_box.clear_widgets() downloading = False dist_models = {k: v for k, v in self.katrain.config("dist_models", {}).items() if k in self.MODEL_ENDPOINTS} for name, url in self.MODEL_ENDPOINTS.items(): try: http = urllib3.PoolManager() response = http.request("GET", url) dist_models[name] = json.loads(response.data.decode("utf-8"))["model_file"] except Exception as e: self.katrain.log(f"Failed to retrieve info for model: {e}", OUTPUT_INFO) self.katrain._config["dist_models"] = dist_models self.katrain.save_config(key="dist_models") for name, url in {**self.MODELS, **dist_models}.items(): filename = os.path.split(url)[1] if not any(os.path.split(f)[1] == filename for f in self.model_files.values): savepath = os.path.expanduser(os.path.join(DATA_FOLDER, filename)) savepath_tmp = savepath + ".part" self.katrain.log(f"Downloading {name} model from {url} to {savepath_tmp}", OUTPUT_INFO) progress = ProgressLoader( download_url=url, path_to_file=savepath_tmp, downloading_text=f"Downloading {name} model: " + "{}", label_downloading_text=f"Starting download for {name} model", download_complete=lambda req, tmp=savepath_tmp, path=savepath, model=name: download_complete( req, tmp, path, model ), download_redirected=lambda req, mname=name: self.katrain.log( f"Download {mname} redirected {req.resp_headers}", OUTPUT_DEBUG ), download_error=lambda req, error, mname=name: self.katrain.log( f"Download of {mname} failed or cancelled ({error})", OUTPUT_ERROR ), ) progress.start(self.download_progress_box) downloading = True if not downloading: self.download_progress_box.add_widget( Label(text=i18n._("All models downloaded"), font_name=i18n.font_name, text_size=(None, dp(50))) )
def download_models(self, *_largs): def download_complete(req, tmp_path, path, model): try: os.rename(tmp_path, path) self.katrain.log( f"Download of {model} model complete -> {path}", OUTPUT_INFO) except Exception as e: self.katrain.log( f"Download of {model} model complete, but could not move file: {e}", OUTPUT_ERROR) self.check_models() for c in self.download_progress_box.children: if isinstance(c, ProgressLoader) and c.request: c.request.cancel() self.download_progress_box.clear_widgets() downloading = False for name, url in self.MODELS.items(): filename = os.path.split(url)[1] if not any( os.path.split(f)[1] == filename for f in self.model_files.values): savepath = os.path.expanduser( os.path.join("~/.katrain", filename)) savepath_tmp = savepath + ".part" self.katrain.log( f"Downloading {name} model from {url} to {savepath_tmp}", OUTPUT_INFO) progress = ProgressLoader( download_url=url, path_to_file=savepath_tmp, downloading_text=f"Downloading {name} model: " + "{}", label_downloading_text= f"Starting download for {name} model", download_complete=lambda req, tmp=savepath_tmp, path= savepath, model=name: download_complete( req, tmp, path, model), download_redirected=lambda req, mname=name: self.katrain. log(f"Download {mname} redirected {req.resp_headers}", OUTPUT_DEBUG), download_error=lambda req, error, mname=name: self.katrain. log(f"Download of {mname} failed or cancelled ({error})", OUTPUT_ERROR), ) progress.start(self.download_progress_box) downloading = True if not downloading: self.download_progress_box.add_widget( Label(text=i18n._("All models downloaded"), text_size=(None, dp(50)))) print("x")
def download_katas(self, *_largs): def unzipped_name(zipfile): if platform == "win": return zipfile.replace(".zip", ".exe") else: return zipfile.replace(".zip", "") def download_complete(req, tmp_path, path, binary): try: if tmp_path.endswith(".zip"): with ZipFile(tmp_path, "r") as zipObj: exes = [ f for f in zipObj.namelist() if f.startswith("katago") ] if len(exes) != 1: raise FileNotFoundError( f"Zip file {tmp_path} does not contain exactly 1 file starting with 'katago' (contents: {zipObj.namelist()})" ) with open(path, "wb") as fout: fout.write(zipObj.read(exes[0])) os.chmod( path, os.stat(path).st_mode | stat.S_IXUSR | stat.S_IXGRP) for f in zipObj.namelist(): if f.lower().endswith("dll"): with open( os.path.join( os.path.split(path)[0], f), "wb") as fout: fout.write(zipObj.read(f)) os.remove(tmp_path) else: os.rename(tmp_path, path) self.katrain.log( f"Download of katago binary {binary} model complete -> {path}", OUTPUT_INFO) except Exception as e: self.katrain.log( f"Download of katago binary {binary} complete, but could not move file: {e}", OUTPUT_ERROR) self.check_katas() for c in self.katago_download_progress_box.children: if isinstance(c, ProgressLoader) and c.request: c.request.cancel() self.katago_download_progress_box.clear_widgets() downloading = False for name, url in self.KATAGOS.get(platform, {}).items(): filename = os.path.split(url)[1] exe_name = unzipped_name(filename) if not any( os.path.split(f)[1] == exe_name for f in self.katago_files.values): savepath_tmp = os.path.expanduser( os.path.join(DATA_FOLDER, filename)) exe_path_name = os.path.expanduser( os.path.join(DATA_FOLDER, exe_name)) self.katrain.log( f"Downloading binary {name} from {url} to {savepath_tmp}", OUTPUT_INFO) progress = ProgressLoader( download_url=url, path_to_file=savepath_tmp, downloading_text=f"Downloading {name}: " + "{}", label_downloading_text=f"Starting download for {name}", download_complete=lambda req, tmp=savepath_tmp, path= exe_path_name, model=name: download_complete( req, tmp, path, model), download_redirected=lambda req, mname=name: self.katrain. log(f"Download {mname} redirected {req.resp_headers}", OUTPUT_DEBUG), download_error=lambda req, error, mname=name: self.katrain. log(f"Download of {mname} failed or cancelled ({error})", OUTPUT_ERROR), ) progress.start(self.katago_download_progress_box) downloading = True if not downloading: if not self.KATAGOS.get(platform): self.katago_download_progress_box.add_widget( Label( text=f"No binaries available for platform {platform}", text_size=(None, dp(50)))) else: self.katago_download_progress_box.add_widget( Label(text=i18n._("All binaries downloaded"), font_name=i18n.font_name, text_size=(None, dp(50))))