Fix PyInstaller crash with swagger ui page

This commit is contained in:
Michael Hansen 2022-04-06 18:45:27 -04:00
commit 3ccf2c7ab3
5 changed files with 31 additions and 7 deletions

View file

@ -102,7 +102,7 @@ RUN .venv/bin/pyinstaller \
--collect-data "gruut_lang_sw" \ --collect-data "gruut_lang_sw" \
--collect-data 'espeak_phonemizer' \ --collect-data 'espeak_phonemizer' \
--collect-data 'phonemes2ids' \ --collect-data 'phonemes2ids' \
--hidden-import 'swagger-ui-py' \ --hidden-import 'swagger_ui' \
--collect-data 'mimic3_tts' \ --collect-data 'mimic3_tts' \
--collect-data 'mimic3_http' \ --collect-data 'mimic3_http' \
pyinstaller/mimic3_server.py pyinstaller/mimic3_server.py

View file

@ -176,10 +176,12 @@ def get_app(args: argparse.Namespace, mimic3: Mimic3TextToSpeechSystem, temp_dir
"""CSS static endpoint.""" """CSS static endpoint."""
return await send_from_directory(_CSS_DIR, filename) return await send_from_directory(_CSS_DIR, filename)
show_openapi = True
@app.route("/") @app.route("/")
async def app_index(): async def app_index():
"""Main page.""" """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"]) @app.route("/api/tts", methods=["GET", "POST"])
async def app_tts() -> Response: 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") return Response(wav_bytes, mimetype="audio/wav")
# Swagger UI # Swagger UI
api_doc( try:
app, config_path=_DIR / "swagger.yaml", url_prefix="/openapi", title="Mimic 3" 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) @app.errorhandler(Exception)
async def handle_error(err) -> typing.Tuple[str, int]: async def handle_error(err) -> typing.Tuple[str, int]:

View file

@ -72,6 +72,9 @@ def get_args() -> argparse.Namespace:
parser.add_argument( parser.add_argument(
"--debug", action="store_true", help="Print DEBUG messages to console" "--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( parser.add_argument(
"--version", action="store_true", help="Print version to console and exit" "--version", action="store_true", help="Print version to console and exit"
) )

View file

@ -54,8 +54,11 @@
</div> </div>
<div class="col-auto"> <div class="col-auto">
<button id="speak-button" name="speak" class="btn btn-lg btn-primary" alt="Generate speech">Speak</button> <button id="speak-button" name="speak" class="btn btn-lg btn-primary" alt="Generate speech">Speak</button>
{% if show_openapi %}
<br/><br /> <br/><br />
<a href="/openapi/" title="OpenAPI page" target="_blank" class="badge badge-info">API</a> <a href="/openapi/" title="OpenAPI page" target="_blank" class="badge badge-info">API</a>
{% endif %}
</div> </div>
</div> </div>
<div class="row mt-3"> <div class="row mt-3">

View file

@ -15,7 +15,15 @@
# along with this program. If not, see <http://www.gnu.org/licenses/>. # along with this program. If not, see <http://www.gnu.org/licenses/>.
# #
"""Stub for PyInstaller""" """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()