setup.py 8.6 KB
Newer Older
H
Hui Zhang 已提交
1 2 3 4 5 6 7 8 9 10 11 12 13
# Copyright (c) 2020 PaddlePaddle Authors. All Rights Reserved.
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
#     http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
H
Hui Zhang 已提交
14 15
import contextlib
import inspect
H
Hui Zhang 已提交
16 17
import io
import os
H
Hui Zhang 已提交
18
import subprocess as sp
H
Hui Zhang 已提交
19
import sys
H
Hui Zhang 已提交
20
from pathlib import Path
H
Hui Zhang 已提交
21

H
Hui Zhang 已提交
22
from setuptools import Command
H
Hui Zhang 已提交
23 24
from setuptools import find_packages
from setuptools import setup
H
Hui Zhang 已提交
25 26
from setuptools.command.develop import develop
from setuptools.command.install import install
K
KP 已提交
27
from setuptools.command.test import test
H
Hui Zhang 已提交
28

H
Hui Zhang 已提交
29
HERE = Path(os.path.abspath(os.path.dirname(__file__)))
H
Hui Zhang 已提交
30

H
Hui Zhang 已提交
31
VERSION = '0.0.0'
H
Hui Zhang 已提交
32
COMMITID = 'none'
H
huangyuxin 已提交
33

H
Hui Zhang 已提交
34
base = [
小湉湉's avatar
小湉湉 已提交
35 36 37 38 39 40 41 42 43 44 45 46
    "editdistance",
    "g2p_en",
    "g2pM",
    "h5py",
    "inflect",
    "jieba",
    "jsonlines",
    "kaldiio",
    "librosa==0.8.1",
    "loguru",
    "matplotlib",
    "nara_wpe",
47
    "onnxruntime==1.10.0",
B
BarryKCL 已提交
48
    "opencc",
小湉湉's avatar
小湉湉 已提交
49
    "pandas",
Z
zxcd 已提交
50
    "paddlenlp>=2.4.3",
小湉湉's avatar
小湉湉 已提交
51
    "paddlespeech_feat",
小湉湉's avatar
小湉湉 已提交
52
    "Pillow>=9.0.0",
小湉湉's avatar
小湉湉 已提交
53
    "praatio==5.0.0",
小湉湉's avatar
小湉湉 已提交
54
    "protobuf>=3.1.0, <=3.20.0",
55
    "pypinyin<=0.44.0",
小湉湉's avatar
小湉湉 已提交
56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73
    "pypinyin-dict",
    "python-dateutil",
    "pyworld==0.2.12",
    "resampy==0.2.2",
    "sacrebleu",
    "scipy",
    "sentencepiece~=0.1.96",
    "soundfile~=0.10",
    "textgrid",
    "timer",
    "tqdm",
    "typeguard",
    "visualdl",
    "webrtcvad",
    "yacs~=0.1.8",
    "prettytable",
    "zhon",
    "colorlog",
74
    "pathos==0.2.8",
75
    "braceexpand",
H
Hui Zhang 已提交
76 77
    "pyyaml",
    "pybind11",
H
Hui Zhang 已提交
78
    "paddleslim==2.3.4",
H
Hui Zhang 已提交
79 80
]

81
server = ["fastapi", "uvicorn", "pattern_singleton", "websockets"]
H
huangyuxin 已提交
82

H
huangyuxin 已提交
83
requirements = {
H
Hui Zhang 已提交
84 85
    "install":
    base + server,
H
huangyuxin 已提交
86 87 88 89
    "develop": [
        "ConfigArgParse",
        "coverage",
        "gpustat",
H
huangyuxin 已提交
90
        "paddlespeech_ctcdecoders",
H
huangyuxin 已提交
91
        "phkit",
92
        "pypi-kenlm",
H
huangyuxin 已提交
93
        "snakeviz",
94 95
        "sox",
        "soxbindings",
H
huangyuxin 已提交
96 97 98 99 100 101
        "unidecode",
        "yq",
        "pre-commit",
    ]
}

102

H
Hui Zhang 已提交
103 104 105 106 107 108 109 110 111 112 113 114
def check_call(cmd: str, shell=False, executable=None):
    try:
        sp.check_call(
            cmd.split(),
            shell=shell,
            executable="/bin/bash" if shell else executable)
    except sp.CalledProcessError as e:
        print(
            f"{__file__}:{inspect.currentframe().f_lineno}: CMD: {cmd}, Error:",
            e.output,
            file=sys.stderr)
        raise e
115

H
format  
Hui Zhang 已提交
116

H
Hui Zhang 已提交
117 118 119 120
def check_output(cmd: str, shell=False):
    try:
        out_bytes = sp.check_output(cmd.split())
    except sp.CalledProcessError as e:
H
format  
Hui Zhang 已提交
121 122
        out_bytes = e.output  # Output generated before error
        code = e.returncode  # Return code
H
Hui Zhang 已提交
123 124 125 126 127
        print(
            f"{__file__}:{inspect.currentframe().f_lineno}: CMD: {cmd}, Error:",
            out_bytes,
            file=sys.stderr)
    return out_bytes.strip().decode('utf8')
H
huangyuxin 已提交
128

H
huangyuxin 已提交
129

H
Hui Zhang 已提交
130 131 132 133
@contextlib.contextmanager
def pushd(new_dir):
    old_dir = os.getcwd()
    os.chdir(new_dir)
134
    print(new_dir)
H
Hui Zhang 已提交
135 136
    yield
    os.chdir(old_dir)
137
    print(old_dir)
H
Hui Zhang 已提交
138 139


H
Hui Zhang 已提交
140
def read(*names, **kwargs):
H
Hui Zhang 已提交
141 142 143
    with io.open(
            os.path.join(os.path.dirname(__file__), *names),
            encoding=kwargs.get("encoding", "utf8")) as fp:
H
Hui Zhang 已提交
144 145 146
        return fp.read()


147 148 149 150
def _remove(files: str):
    for f in files:
        f.unlink()

H
format  
Hui Zhang 已提交
151

H
Hui Zhang 已提交
152 153
################################# Install ##################################

154 155

def _post_install(install_lib_dir):
H
Hui Zhang 已提交
156 157
    # tools/make
    tool_dir = HERE / "tools"
158
    _remove(tool_dir.glob("*.done"))
H
Hui Zhang 已提交
159
    with pushd(tool_dir):
H
Hui Zhang 已提交
160
        check_call("make")
H
Hui Zhang 已提交
161
    print("tools install.")
H
Hui Zhang 已提交
162

H
Hui Zhang 已提交
163
    # ctcdecoder
164
    ctcdecoder_dir = HERE / 'third_party/ctc_decoders'
H
Hui Zhang 已提交
165 166 167 168
    with pushd(ctcdecoder_dir):
        check_call("bash -e setup.sh")
    print("ctcdecoder install.")

H
Hui Zhang 已提交
169

H
Hui Zhang 已提交
170 171 172
class DevelopCommand(develop):
    def run(self):
        develop.run(self)
173
        # must after develop.run, or pkg install by shell will not see
H
Hui Zhang 已提交
174 175 176 177 178 179 180 181
        self.execute(_post_install, (self.install_lib, ), msg="Post Install...")


class InstallCommand(install):
    def run(self):
        install.run(self)


K
KP 已提交
182 183 184 185 186 187 188 189 190 191 192 193 194
class TestCommand(test):
    def finalize_options(self):
        test.finalize_options(self)
        self.test_args = []
        self.test_suite = True

    def run_tests(self):
        # Run nose ensuring that argv simulates running nosetests directly
        import nose
        nose.run_exit(argv=['nosetests', '-w', 'tests'])


# cmd: python setup.py upload
H
Hui Zhang 已提交
195 196 197 198 199 200 201 202 203 204 205 206 207
class UploadCommand(Command):
    description = "Build and publish the package."
    user_options = []

    def initialize_options(self):
        pass

    def finalize_options(self):
        pass

    def run(self):
        try:
            print("Removing previous dist/ ...")
H
Hui Zhang 已提交
208
            shutil.rmtree(str(HERE / "dist"))
H
Hui Zhang 已提交
209 210 211 212 213 214 215 216 217
        except OSError:
            pass
        print("Building source distribution...")
        sp.check_call([sys.executable, "setup.py", "sdist"])
        print("Uploading package to PyPi...")
        sp.check_call(["twine", "upload", "dist/*"])
        sys.exit()


H
Hui Zhang 已提交
218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254
################################# Version ##################################
def write_version_py(filename='paddlespeech/__init__.py'):
    import paddlespeech
    if hasattr(paddlespeech,
               "__version__") and paddlespeech.__version__ == VERSION:
        return
    with open(filename, "a") as f:
        out_str = f"\n__version__ = '{VERSION}'\n"
        print(out_str)
        f.write(f"\n__version__ = '{VERSION}'\n")

    COMMITID = check_output("git rev-parse HEAD")
    with open(filename, 'a') as f:
        out_str = f"\n__commit__ = '{COMMITID}'\n"
        print(out_str)
        f.write(f"\n__commit__ = '{COMMITID}'\n")

    print(f"{inspect.currentframe().f_code.co_name} done")


def remove_version_py(filename='paddlespeech/__init__.py'):
    with open(filename, "r") as f:
        lines = f.readlines()
    with open(filename, "w") as f:
        for line in lines:
            if "__version__" in line or "__commit__" in line:
                continue
            f.write(line)
    print(f"{inspect.currentframe().f_code.co_name} done")


@contextlib.contextmanager
def version_info():
    write_version_py()
    yield
    remove_version_py()

H
huangyuxin 已提交
255

H
Hui Zhang 已提交
256
################################# Steup ##################################
H
Hui Zhang 已提交
257 258
setup_info = dict(
    # Metadata
H
Hui Zhang 已提交
259
    name='paddlespeech',
H
huangyuxin 已提交
260
    version=VERSION,
261 262 263 264
    author='PaddlePaddle Speech and Language Team',
    author_email='paddlesl@baidu.com',
    url='https://github.com/PaddlePaddle/PaddleSpeech',
    license='Apache 2.0',
H
Hui Zhang 已提交
265
    description='Speech tools and models based on Paddlepaddle',
H
Hui Zhang 已提交
266
    long_description=read("README.md"),
H
Hui Zhang 已提交
267
    long_description_content_type="text/markdown",
H
Hui Zhang 已提交
268 269 270 271
    keywords=[
        "speech",
        "asr",
        "tts",
272 273
        "speaker verfication",
        "speech classfication",
H
Hui Zhang 已提交
274 275 276
        "text frontend",
        "MFA",
        "paddlepaddle",
277 278 279
        "beam search",
        "ctcdecoder",
        "deepspeech2",
H
Hui Zhang 已提交
280 281 282 283 284 285 286
        "transformer",
        "conformer",
        "fastspeech",
        "vocoder",
        "pwgan",
        "gan",
    ],
H
huangyuxin 已提交
287
    python_requires='>=3.7',
H
huangyuxin 已提交
288
    install_requires=requirements["install"],
H
Hui Zhang 已提交
289
    extras_require={
H
huangyuxin 已提交
290 291
        'develop':
        requirements["develop"],
H
Hui Zhang 已提交
292 293 294 295
        'doc': [
            "sphinx", "sphinx-rtd-theme", "numpydoc", "myst_parser",
            "recommonmark>=0.5.0", "sphinx-markdown-tables", "sphinx-autobuild"
        ],
K
KP 已提交
296
        'test': ['nose', 'torchaudio==0.10.2'],
H
Hui Zhang 已提交
297
    },
H
Hui Zhang 已提交
298 299 300 301
    cmdclass={
        'develop': DevelopCommand,
        'install': InstallCommand,
        'upload': UploadCommand,
K
KP 已提交
302
        'test': TestCommand,
H
Hui Zhang 已提交
303
    },
H
Hui Zhang 已提交
304 305

    # Package info
J
Jackwaterveg 已提交
306
    packages=find_packages(include=('paddlespeech*')),
H
Hui Zhang 已提交
307 308
    zip_safe=True,
    classifiers=[
309
        'Development Status :: 5 - Production/Stable',
H
Hui Zhang 已提交
310 311 312 313
        'Intended Audience :: Developers',
        'Intended Audience :: Science/Research',
        'Topic :: Scientific/Engineering :: Artificial Intelligence',
        'License :: OSI Approved :: Apache Software License',
H
Hui Zhang 已提交
314 315
        'Programming Language :: Python',
        'Programming Language :: Python :: 3',
H
Hui Zhang 已提交
316
        'Programming Language :: Python :: 3.7',
H
Hui Zhang 已提交
317
        'Programming Language :: Python :: 3.8',
318
        'Programming Language :: Python :: 3.9',
K
KP 已提交
319 320
    ],
    entry_points={
L
lym0302 已提交
321 322
        'console_scripts': [
            'paddlespeech=paddlespeech.cli.entry:_execute',
L
lym0302 已提交
323 324
            'paddlespeech_server=paddlespeech.server.entry:server_execute',
            'paddlespeech_client=paddlespeech.server.entry:client_execute'
L
lym0302 已提交
325
        ]
K
KP 已提交
326
    })
H
Hui Zhang 已提交
327

H
Hui Zhang 已提交
328
with version_info():
329
    setup(**setup_info, include_package_data=True)