From 3ccf2c7ab3f70a42382189a4152577f6e1a82493 Mon Sep 17 00:00:00 2001 From: Michael Hansen Date: Wed, 6 Apr 2022 18:45:27 -0400 Subject: [PATCH] Fix PyInstaller crash with swagger ui page --- Dockerfile.binary | 2 +- mimic3-http/mimic3_http/app.py | 18 ++++++++++++++---- mimic3-http/mimic3_http/args.py | 3 +++ mimic3-http/mimic3_http/templates/index.html | 3 +++ pyinstaller/mimic3.py | 12 ++++++++++-- 5 files changed, 31 insertions(+), 7 deletions(-) diff --git a/Dockerfile.binary b/Dockerfile.binary index 66079dd..c33289c 100644 --- a/Dockerfile.binary +++ b/Dockerfile.binary @@ -102,7 +102,7 @@ RUN .venv/bin/pyinstaller \ --collect-data "gruut_lang_sw" \ --collect-data 'espeak_phonemizer' \ --collect-data 'phonemes2ids' \ - --hidden-import 'swagger-ui-py' \ + --hidden-import 'swagger_ui' \ --collect-data 'mimic3_tts' \ --collect-data 'mimic3_http' \ pyinstaller/mimic3_server.py diff --git a/mimic3-http/mimic3_http/app.py b/mimic3-http/mimic3_http/app.py index b774c3d..20b503b 100644 --- a/mimic3-http/mimic3_http/app.py +++ b/mimic3-http/mimic3_http/app.py @@ -176,10 +176,12 @@ def get_app(args: argparse.Namespace, mimic3: Mimic3TextToSpeechSystem, temp_dir """CSS static endpoint.""" return await send_from_directory(_CSS_DIR, filename) + show_openapi = True + @app.route("/") async def app_index(): """Main page.""" - return await render_template("index.html") + return await render_template("index.html", show_openapi=show_openapi) @app.route("/api/tts", methods=["GET", "POST"]) async def app_tts() -> Response: @@ -275,9 +277,17 @@ def get_app(args: argparse.Namespace, mimic3: Mimic3TextToSpeechSystem, temp_dir return Response(wav_bytes, mimetype="audio/wav") # Swagger UI - api_doc( - app, config_path=_DIR / "swagger.yaml", url_prefix="/openapi", title="Mimic 3" - ) + try: + api_doc( + app, + config_path=_DIR / "swagger.yaml", + url_prefix="/openapi", + title="Mimic 3", + ) + except Exception: + # Fails with PyInstaller for some reason + _LOGGER.exception("Error setting up swagger UI page") + show_openapi = False @app.errorhandler(Exception) async def handle_error(err) -> typing.Tuple[str, int]: diff --git a/mimic3-http/mimic3_http/args.py b/mimic3-http/mimic3_http/args.py index 4a95485..4809e5f 100644 --- a/mimic3-http/mimic3_http/args.py +++ b/mimic3-http/mimic3_http/args.py @@ -72,6 +72,9 @@ def get_args() -> argparse.Namespace: parser.add_argument( "--debug", action="store_true", help="Print DEBUG messages to console" ) + parser.add_argument( + "--server", action="store_true", help="Dummy argument from PyInstaller script" + ) parser.add_argument( "--version", action="store_true", help="Print version to console and exit" ) diff --git a/mimic3-http/mimic3_http/templates/index.html b/mimic3-http/mimic3_http/templates/index.html index bc51555..2c29001 100644 --- a/mimic3-http/mimic3_http/templates/index.html +++ b/mimic3-http/mimic3_http/templates/index.html @@ -54,8 +54,11 @@
+ + {% if show_openapi %}

API + {% endif %}
diff --git a/pyinstaller/mimic3.py b/pyinstaller/mimic3.py index 26dfff8..5724428 100644 --- a/pyinstaller/mimic3.py +++ b/pyinstaller/mimic3.py @@ -15,7 +15,15 @@ # along with this program. If not, see . # """Stub for PyInstaller""" +import sys -from mimic3_tts.__main__ import main -main() +if (len(sys.argv) > 1) and (sys.argv[1] == "--server"): + from mimic3_http.__main__ import main as http_main + + sys.argv = [sys.argv[0]] + sys.argv[2:] + http_main() +else: + from mimic3_tts.__main__ import main as tts_main + + tts_main()