Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[cli] support download model from modelscope #2098

Merged
merged 1 commit into from
Nov 1, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
20 changes: 14 additions & 6 deletions wenet/cli/hub.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@
# limitations under the License.

import os
import requests
import sys
import tarfile
from pathlib import Path
Expand Down Expand Up @@ -72,11 +73,11 @@ class Hub(object):
# TODO(Mddct): make assets class to support other language
Assets = {
# wenetspeech
"chinese":
"https://github.com/wenet-e2e/wenet/releases/download/v2.0.1/chs.tar.gz",
"chinese": "wenetspeech_u2pp_conformer_libtorch.tar.gz",
# gigaspeech
"english":
"https://github.com/wenet-e2e/wenet/releases/download/v2.0.1/en.tar.gz"
"english": "gigaspeech_u2pp_conformer_libtorch.tar.gz",
# paraformer
"paraformer": "paraformer.tar.gz"
}

def __init__(self) -> None:
Expand All @@ -89,20 +90,27 @@ def get_model_by_lang(lang: str) -> str:
sys.exit(1)

# NOTE(Mddct): model_dir structure
# Path.Home()/.went
# Path.Home()/.wenet
# - chs
# - units.txt
# - final.zip
# - en
# - units.txt
# - final.zip
model_url = Hub.Assets[lang]
model = Hub.Assets[lang]
model_dir = os.path.join(Path.home(), ".wenet", lang)
if not os.path.exists(model_dir):
os.makedirs(model_dir)
# TODO(Mddct): model metadata
if set(["final.zip",
"units.txt"]).issubset(set(os.listdir(model_dir))):
return model_dir
# If not exist, download
response = requests.get(
"https://modelscope.cn/api/v1/datasets/wenet/wenet_pretrained_models/oss/tree" # noqa
)
model_info = next(data for data in response.json()["Data"]
if data["Key"] == model)
model_url = model_info['Url']
download(model_url, model_dir, only_child=True)
return model_dir
6 changes: 3 additions & 3 deletions wenet/cli/paraformer_model.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,12 +4,12 @@
import torchaudio
import torchaudio.compliance.kaldi as kaldi

from wenet.cli.hub import Hub
from wenet.paraformer.search import paraformer_greedy_search
from wenet.utils.file_utils import read_symbol_table


class Paraformer:

def __init__(self, model_dir: str) -> None:

model_path = os.path.join(model_dir, 'final.zip')
Expand Down Expand Up @@ -60,7 +60,7 @@ def align(self, audio_file: str, label: str) -> dict:
raise NotImplementedError("Align is currently not supported")


def load_model(language: str = None, model_dir: str = None) -> Paraformer:
def load_model(model_dir: str = None) -> Paraformer:
if model_dir is None:
model_dir = Hub.get_model_by_lang(language)
model_dir = Hub.get_model_by_lang('paraformer')
return Paraformer(model_dir)
2 changes: 1 addition & 1 deletion wenet/cli/transcribe.py
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,7 @@ def main():
args = get_args()

if args.paraformer:
model = load_paraformer(args.language, args.model_dir)
model = load_paraformer(args.model_dir)
else:
model = load_model(args.language, args.model_dir)
if args.align:
Expand Down
Loading