👍[Update] Model : CTranslate2のweightデータを起動時に取得するように変更
This commit is contained in:
39
model.py
39
model.py
@@ -1,3 +1,4 @@
|
||||
import tempfile
|
||||
from zipfile import ZipFile
|
||||
from subprocess import Popen
|
||||
from os import makedirs as os_makedirs
|
||||
@@ -9,9 +10,10 @@ from logging import getLogger, FileHandler, Formatter, INFO
|
||||
from time import sleep
|
||||
from queue import Queue
|
||||
from threading import Thread, Event
|
||||
from requests import get as requests_get
|
||||
from requests import get as requests_get, head as requests_head
|
||||
import webbrowser
|
||||
|
||||
from tqdm import tqdm
|
||||
from flashtext import KeywordProcessor
|
||||
from models.translation.translation_translator import Translator
|
||||
from models.transcription.transcription_utils import getInputDevices, getDefaultOutputDevice
|
||||
@@ -70,7 +72,8 @@ class Model:
|
||||
self.speaker_audio_recorder = None
|
||||
self.speaker_energy_recorder = None
|
||||
self.speaker_energy_plot_progressbar = None
|
||||
self.translator = Translator(config.PATH_LOCAL)
|
||||
self.downloadCTranslate2Weight()
|
||||
self.translator = Translator(config.PATH_LOCAL, config.CTRANSLATE2_WIGHTS[config.WEIGHT_TYPE])
|
||||
self.keyword_processor = KeywordProcessor()
|
||||
|
||||
def resetTranslator(self):
|
||||
@@ -106,6 +109,38 @@ class Model:
|
||||
self.logger.disabled = True
|
||||
self.logger = None
|
||||
|
||||
@staticmethod
|
||||
def downloadCTranslate2Weight():
|
||||
weight_type = config.WEIGHT_TYPE
|
||||
url = config.CTRANSLATE2_WIGHTS[weight_type]["url"]
|
||||
filename = 'weight.zip'
|
||||
directory_name = 'weight'
|
||||
current_directory = config.PATH_LOCAL
|
||||
weight_directory_name = config.CTRANSLATE2_WIGHTS[weight_type]["directory_name"]
|
||||
files = ["model.bin", "sentencepiece.model", "shared_vocabulary.txt"]
|
||||
|
||||
# check already downloaded
|
||||
if all(os_path.exists(os_path.join(current_directory, directory_name, weight_directory_name, file)) for file in files):
|
||||
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:
|
||||
file_size = int(requests_head(url).headers["content-length"])
|
||||
res = requests_get(url, stream=True)
|
||||
pbar = tqdm(total=file_size, unit="B", unit_scale=True)
|
||||
with open(os_path.join(tmp_path, filename), 'wb') as file:
|
||||
for chunk in res.iter_content(chunk_size=1024):
|
||||
file.write(chunk)
|
||||
pbar.update(len(chunk))
|
||||
pbar.close()
|
||||
|
||||
with ZipFile(os_path.join(tmp_path, filename)) as zf:
|
||||
zf.extractall(os_path.join(current_directory, directory_name))
|
||||
except Exception as e:
|
||||
print("error:downloadCTranslate2Weight()", e)
|
||||
|
||||
@staticmethod
|
||||
def getListLanguageAndCountry():
|
||||
langs = []
|
||||
|
||||
Reference in New Issue
Block a user