[WIP/TEST] Model : モデルの保存位置の変更
- speakerの文字起こし処理のバグを修正
This commit is contained in:
@@ -2,7 +2,7 @@ import os
|
||||
from deepl import Translator as deepl_Translator
|
||||
from translators import translate_text as other_web_Translator
|
||||
from .translation_languages import translation_lang
|
||||
from .utils import ctranslate2_weights
|
||||
from .translation_utils import ctranslate2_weights
|
||||
|
||||
import ctranslate2
|
||||
import transformers
|
||||
@@ -27,8 +27,8 @@ class Translator():
|
||||
def changeCTranslate2Model(self, path, model_type):
|
||||
directory_name = ctranslate2_weights[model_type]["directory_name"]
|
||||
tokenizer = ctranslate2_weights[model_type]["tokenizer"]
|
||||
weight_path = os.path.join(path, "weight", directory_name)
|
||||
tokenizer_path = os.path.join(path, "weight", directory_name, "tokenizer")
|
||||
weight_path = os.path.join(path, "weights", "ctranslate2", directory_name)
|
||||
tokenizer_path = os.path.join(path, "weights", "ctranslate2", directory_name, "tokenizer")
|
||||
self.ctranslate2_translator = ctranslate2.Translator(
|
||||
weight_path,
|
||||
device="cpu",
|
||||
@@ -41,7 +41,7 @@ class Translator():
|
||||
self.ctranslate2_tokenizer = transformers.AutoTokenizer.from_pretrained(tokenizer, cache_dir=tokenizer_path)
|
||||
except Exception as e:
|
||||
print("Error: changeCTranslate2Model()", e)
|
||||
tokenizer_path = os.path.join("./weight", directory_name, "tokenizer")
|
||||
tokenizer_path = os.path.join("./weights", "ctranslate2", directory_name, "tokenizer")
|
||||
self.ctranslate2_tokenizer = transformers.AutoTokenizer.from_pretrained(tokenizer, cache_dir=tokenizer_path)
|
||||
|
||||
@staticmethod
|
||||
|
||||
@@ -39,36 +39,36 @@ def calculate_file_hash(file_path, block_size=65536):
|
||||
return hash_object.hexdigest()
|
||||
|
||||
def checkCTranslate2Weight(path, weight_type="Small"):
|
||||
directory_name = 'weight'
|
||||
current_directory = path
|
||||
weight_directory_name = ctranslate2_weights[weight_type]["directory_name"]
|
||||
hash_data = ctranslate2_weights[weight_type]["hash"]
|
||||
files = ["model.bin", "sentencepiece.model", "shared_vocabulary.txt"]
|
||||
files = [
|
||||
"model.bin",
|
||||
"sentencepiece.model",
|
||||
"shared_vocabulary.txt"
|
||||
]
|
||||
|
||||
# check already downloaded
|
||||
already_downloaded = False
|
||||
if all(os_path.exists(os_path.join(current_directory, directory_name, weight_directory_name, file)) for file in files):
|
||||
if all(os_path.exists(os_path.join(path, weight_directory_name, file)) for file in files):
|
||||
# check hash
|
||||
for file in files:
|
||||
original_hash = hash_data[file]
|
||||
current_hash = calculate_file_hash(os_path.join(current_directory, directory_name, weight_directory_name, file))
|
||||
current_hash = calculate_file_hash(os_path.join(path, weight_directory_name, file))
|
||||
if original_hash != current_hash:
|
||||
break
|
||||
already_downloaded = True
|
||||
return already_downloaded
|
||||
|
||||
def downloadCTranslate2Weight(path, weight_type="Small", func=None):
|
||||
def downloadCTranslate2Weight(root, weight_type="Small", func=None):
|
||||
url = ctranslate2_weights[weight_type]["url"]
|
||||
filename = 'weight.zip'
|
||||
directory_name = 'weight'
|
||||
current_directory = path
|
||||
filename = "weight.zip"
|
||||
path = os_path.join(root, "weights", "ctranslate2")
|
||||
os_makedirs(path, exist_ok=True)
|
||||
|
||||
if checkCTranslate2Weight(path, weight_type):
|
||||
return
|
||||
|
||||
try:
|
||||
os_makedirs(os_path.join(current_directory, directory_name), exist_ok=True)
|
||||
print(os_path.join(current_directory, directory_name))
|
||||
with tempfile.TemporaryDirectory() as tmp_path:
|
||||
res = requests_get(url, stream=True)
|
||||
file_size = int(res.headers.get('content-length', 0))
|
||||
@@ -81,6 +81,6 @@ def downloadCTranslate2Weight(path, weight_type="Small", func=None):
|
||||
func(total_chunk/file_size)
|
||||
|
||||
with ZipFile(os_path.join(tmp_path, filename)) as zf:
|
||||
zf.extractall(os_path.join(current_directory, directory_name))
|
||||
zf.extractall(path)
|
||||
except Exception as e:
|
||||
print("error:downloadCTranslate2Weight()", e)
|
||||
Reference in New Issue
Block a user