|
| 1 | +# Copyright (c) 2022 Mddct(hamddct@gmail.com) |
| 2 | +# |
| 3 | +# Licensed under the Apache License, Version 2.0 (the "License"); |
| 4 | +# you may not use this file except in compliance with the License. |
| 5 | +# You may obtain a copy of the License at |
| 6 | +# |
| 7 | +# http://www.apache.org/licenses/LICENSE-2.0 |
| 8 | +# |
| 9 | +# Unless required by applicable law or agreed to in writing, software |
| 10 | +# distributed under the License is distributed on an "AS IS" BASIS, |
| 11 | +# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 12 | +# See the License for the specific language governing permissions and |
| 13 | +# limitations under the License. |
| 14 | + |
| 15 | +import os |
| 16 | +import tarfile |
| 17 | +from pathlib import Path |
| 18 | +from urllib.request import urlretrieve |
| 19 | + |
| 20 | +import tqdm |
| 21 | + |
| 22 | + |
| 23 | +def download(url: str, dest: str, only_child=True): |
| 24 | + """ download from url to dest |
| 25 | + """ |
| 26 | + assert os.path.exists(dest) |
| 27 | + |
| 28 | + def progress_hook(t): |
| 29 | + last_b = [0] |
| 30 | + |
| 31 | + def update_to(b=1, bsize=1, tsize=None): |
| 32 | + if tsize not in (None, -1): |
| 33 | + t.total = tsize |
| 34 | + displayed = t.update((b - last_b[0]) * bsize) |
| 35 | + last_b[0] = b |
| 36 | + return displayed |
| 37 | + return update_to |
| 38 | + |
| 39 | + # *.tar.gz |
| 40 | + name = url.split("/")[-1] |
| 41 | + tar_path = os.path.join(dest, name) |
| 42 | + with tqdm.tqdm(unit='B', |
| 43 | + unit_scale=True, |
| 44 | + unit_divisor=1024, |
| 45 | + miniters=1, |
| 46 | + desc=(name)) as t: |
| 47 | + urlretrieve(url, |
| 48 | + filename=tar_path, |
| 49 | + reporthook=progress_hook(t), |
| 50 | + data=None) |
| 51 | + t.total = t.n |
| 52 | + |
| 53 | + with tarfile.open(tar_path) as f: |
| 54 | + if not only_child: |
| 55 | + f.extractall(dest) |
| 56 | + else: |
| 57 | + for tarinfo in f: |
| 58 | + if "/" not in tarinfo.name: |
| 59 | + continue |
| 60 | + name = os.path.basename(tarinfo.name) |
| 61 | + f.extract(tarinfo, os.path.join(dest, name)) |
| 62 | + |
| 63 | + |
| 64 | +class Hub(object): |
| 65 | + """Hub for wenet pretrain runtime model |
| 66 | + """ |
| 67 | + # TODO(Mddct): make assets class to support other language |
| 68 | + Assets = { |
| 69 | + # wenetspeech |
| 70 | + "chs": |
| 71 | + "https://wenet-1256283475.cos.ap-shanghai.myqcloud.com/models/wenetspeech/20220506_u2pp_conformer_libtorch.tar.gz", |
| 72 | + # gigaspeech |
| 73 | + "en": |
| 74 | + "https://wenet-1256283475.cos.ap-shanghai.myqcloud.com/models/gigaspeech/20210728_u2pp_conformer_libtorch.tar.gz" |
| 75 | + } |
| 76 | + |
| 77 | + def __init__(self) -> None: |
| 78 | + pass |
| 79 | + |
| 80 | + @staticmethod |
| 81 | + def get_model_by_lang(lang: str) -> str: |
| 82 | + assert lang in Hub.Assets.keys() |
| 83 | + # NOTE(Mddct): model_dir structure |
| 84 | + # Path.Home()/.went |
| 85 | + # - chs |
| 86 | + # - units.txt |
| 87 | + # - final.zip |
| 88 | + # - en |
| 89 | + # - units.txt |
| 90 | + # - final.zip |
| 91 | + model_url = Hub.Assets[lang] |
| 92 | + model_dir = os.path.join(Path.home(), ".wenet", lang) |
| 93 | + if not os.path.exists(model_dir): |
| 94 | + os.makedirs(model_dir) |
| 95 | + # TODO(Mddct): model metadata |
| 96 | + if set(["final.zip", |
| 97 | + "units.txt"]).issubset(set(os.listdir(model_dir))): |
| 98 | + return model_dir |
| 99 | + download(model_url, model_dir, only_child=True) |
| 100 | + return model_dir |
0 commit comments