👍️[Update] Model : AI モデルのダウンロード方法を修正

- AIモデルのダウンロード済み確認辞書を追加
	- selectable_ctranslate2_weight_type_dict
	- selectable_whisper_weight_type_dict
- AIモデルダウンロード処理完了のエンドポイントを追加
	- /run/download_ctranslate2_weight
	- /run/downloaded_whisper_weight
This commit is contained in:
misyaguziya
2024-10-29 22:58:18 +09:00
parent 134ef373af
commit 7fd3fad3ea
6 changed files with 228 additions and 116 deletions

View File

@@ -63,17 +63,16 @@ def checkWhisperWeight(root, weight_type):
pass
return result
def downloadWhisperWeight(root, weight_type, callbackFunc):
def downloadWhisperWeight(root, weight_type, callback=None, end_callback=None):
path = os_path.join(root, "weights", "whisper", weight_type)
os_makedirs(path, exist_ok=True)
if checkWhisperWeight(root, weight_type) is True:
callbackFunc(1)
return
for filename in _FILENAMES:
file_path = os_path.join(path, filename)
url = huggingface_hub.hf_hub_url(_MODELS[weight_type], filename)
downloadFile(url, file_path, func=callbackFunc)
if checkWhisperWeight(root, weight_type) is False:
for filename in _FILENAMES:
file_path = os_path.join(path, filename)
url = huggingface_hub.hf_hub_url(_MODELS[weight_type], filename)
downloadFile(url, file_path, func=callback)
if isinstance(end_callback, Callable):
end_callback()
def getWhisperModel(root, weight_type, device="cpu", device_index=0):
path = os_path.join(root, "weights", "whisper", weight_type)
@@ -93,10 +92,14 @@ if __name__ == "__main__":
print(value)
pass
downloadWhisperWeight("./", "tiny", callback)
downloadWhisperWeight("./", "base", callback)
downloadWhisperWeight("./", "small", callback)
downloadWhisperWeight("./", "medium", callback)
downloadWhisperWeight("./", "large-v1", callback)
downloadWhisperWeight("./", "large-v2", callback)
downloadWhisperWeight("./", "large-v3", callback)
def end_callback():
print("end")
pass
downloadWhisperWeight("./", "tiny", callback, end_callback)
downloadWhisperWeight("./", "base", callback, end_callback)
downloadWhisperWeight("./", "small", callback, end_callback)
downloadWhisperWeight("./", "medium", callback, end_callback)
downloadWhisperWeight("./", "large-v1", callback, end_callback)
downloadWhisperWeight("./", "large-v2", callback, end_callback)
downloadWhisperWeight("./", "large-v3", callback, end_callback)

View File

@@ -60,30 +60,30 @@ def checkCTranslate2Weight(path, weight_type="Small"):
already_downloaded = True
return already_downloaded
def downloadCTranslate2Weight(root, weight_type="Small", callbackFunc=None):
def downloadCTranslate2Weight(root, weight_type="Small", callback=None, end_callback=None):
url = ctranslate2_weights[weight_type]["url"]
filename = "weight.zip"
path = os_path.join(root, "weights", "ctranslate2")
os_makedirs(path, exist_ok=True)
if checkCTranslate2Weight(path, weight_type):
callbackFunc(1)
return
if checkCTranslate2Weight(path, weight_type) is False:
try:
with tempfile.TemporaryDirectory() as tmp_path:
res = requests_get(url, stream=True)
file_size = int(res.headers.get('content-length', 0))
total_chunk = 0
with open(os_path.join(tmp_path, filename), 'wb') as file:
for chunk in res.iter_content(chunk_size=1024*2000):
file.write(chunk)
if isinstance(callback, Callable):
total_chunk += len(chunk)
callback(total_chunk/file_size)
printLog(f"Downloading CTranslate Model: {total_chunk/file_size:.0%}")
try:
with tempfile.TemporaryDirectory() as tmp_path:
res = requests_get(url, stream=True)
file_size = int(res.headers.get('content-length', 0))
total_chunk = 0
with open(os_path.join(tmp_path, filename), 'wb') as file:
for chunk in res.iter_content(chunk_size=1024*2000):
file.write(chunk)
if isinstance(callbackFunc, Callable):
total_chunk += len(chunk)
callbackFunc(total_chunk/file_size)
printLog(f"Downloading CTranslate Model: {total_chunk/file_size:.0%}")
with ZipFile(os_path.join(tmp_path, filename)) as zf:
zf.extractall(path)
except Exception as e:
printLog("warning:downloadCTranslate2Weight()", e)
with ZipFile(os_path.join(tmp_path, filename)) as zf:
zf.extractall(path)
except Exception as e:
printLog("warning:downloadCTranslate2Weight()", e)
if isinstance(end_callback, Callable):
end_callback()