From 65ca31eb698364950afefaf8568fd9e7429d4dfc Mon Sep 17 00:00:00 2001 From: hina ntki Date: Mon, 26 Aug 2024 22:37:58 +0900 Subject: [PATCH] =?UTF-8?q?=E6=9C=AC=E7=95=AA=E7=92=B0=E5=A2=83=E3=81=A8?= =?UTF-8?q?=E3=81=97=E3=81=A6=E8=B5=B7=E5=8B=95=E3=81=A7=E3=81=8D=E3=82=8B?= =?UTF-8?q?=E3=82=88=E3=81=86=E3=81=AB=E4=BF=AE=E6=AD=A3=E3=81=97=E3=81=A6?= =?UTF-8?q?=E3=81=BF=E3=82=8B?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- _voicepeak-api.service | 8 ++++---- app/__init__.py | 12 ++++++++++++ app.py => app/routes.py | 24 +++++++----------------- config.py | 30 ++++++++++++++++++++++++++++++ requirements.txt | 1 + run_dev.sh | 2 +- run_prod.sh | 4 ++++ wsgi.py | 7 +++++++ 8 files changed, 66 insertions(+), 22 deletions(-) create mode 100644 app/__init__.py rename app.py => app/routes.py (79%) create mode 100644 config.py create mode 100644 run_prod.sh create mode 100644 wsgi.py diff --git a/_voicepeak-api.service b/_voicepeak-api.service index de66b70..2c30831 100644 --- a/_voicepeak-api.service +++ b/_voicepeak-api.service @@ -1,14 +1,14 @@ [Unit] -Description=VOICEPEAK API Service +Description=Gunicorn instance to serve VoicePeak API After=network.target [Service] -User=your_user +User=your_username Group=your_group WorkingDirectory=/path/to/your/project Environment="PATH=/path/to/your/project/venv/bin" -ExecStart=/path/to/your/project/venv/bin/python /path/to/your/project/app.py +ExecStart=/path/to/your/project/run_prod.sh Restart=always [Install] -WantedBy=multi-user.target +WantedBy=multi-user.target \ No newline at end of file diff --git a/app/__init__.py b/app/__init__.py new file mode 100644 index 0000000..32d2b7f --- /dev/null +++ b/app/__init__.py @@ -0,0 +1,12 @@ +from flask import Flask +from config import Config + +def create_app(config_class=Config): + app = Flask(__name__) + app.config.from_object(config_class) + config_class.init_app(app) + + from app import routes + app.register_blueprint(routes.bp) + + return app \ No newline at end of file diff --git a/app.py b/app/routes.py similarity index 79% rename from app.py rename to app/routes.py index 1e1e1c0..4681826 100644 --- a/app.py +++ b/app/routes.py @@ -1,18 +1,11 @@ -from flask import Flask, request, jsonify, send_file +from flask import Blueprint, request, jsonify, send_file import subprocess import os import logging import uuid -from dotenv import load_dotenv +from flask import current_app -# .envファイルを読み込む -load_dotenv() - -app = Flask(__name__) - -# 環境変数から基本設定を取得 -VOICEPEAK_PATH = os.getenv('VOICEPEAK_PATH') -PROJECT_PATH = os.getenv('PROJECT_PATH') +bp = Blueprint('main', __name__) def _build_command_args(args): return [ @@ -26,7 +19,7 @@ def _build_command_args(args): def _synthesize_with_voicepeak(script, narrator, emotions, pitch, output_path, retries=3): args = { - 'voicepeak_path': VOICEPEAK_PATH, + 'voicepeak_path': current_app.config['VOICEPEAK_PATH'], 'script': script, 'narrator': narrator, 'output_path': output_path, @@ -52,7 +45,7 @@ def _synthesize_with_voicepeak(script, narrator, emotions, pitch, output_path, r logging.error(error_message) raise FileNotFoundError(error_message) -@app.route('/generate_voice', methods=['POST']) +@bp.route('/generate_voice', methods=['POST']) def generate_voice(): try: data = request.json @@ -70,7 +63,7 @@ def generate_voice(): emotions = f"happy={emotion_happy},sad={emotion_sad},angry={emotion_angry},fun={emotion_fun}" unique_id = str(uuid.uuid4()) - output_file = os.path.join(PROJECT_PATH, f'{unique_id}.wav') + output_file = os.path.join(current_app.config['PROJECT_PATH'], f'{unique_id}.wav') _synthesize_with_voicepeak(text, narrator, emotions, pitch, output_file) @@ -81,7 +74,4 @@ def generate_voice(): return response except Exception as e: - return jsonify({'error': str(e)}), 500 - -if __name__ == '__main__': - app.run(host='0.0.0.0', port=5000) + return jsonify({'error': str(e)}), 500 \ No newline at end of file diff --git a/config.py b/config.py new file mode 100644 index 0000000..8de966c --- /dev/null +++ b/config.py @@ -0,0 +1,30 @@ +import os +from dotenv import load_dotenv + +load_dotenv() + +class Config: + VOICEPEAK_PATH = os.getenv('VOICEPEAK_PATH') + PROJECT_PATH = os.getenv('PROJECT_PATH') + + # 開発環境かどうかの判定 + FLASK_ENV = os.getenv('FLASK_ENV', 'production') + + @classmethod + def init_app(cls, app): + # Flaskアプリケーションのデバッグモードを設定 + app.debug = cls.FLASK_ENV == 'development' + +class DevelopmentConfig(Config): + # 開発環境時の設定: 今回は特になし + pass + +class ProductionConfig(Config): + # 本番環境時の設定: 今回は特になし + pass + +config = { + 'development': DevelopmentConfig, + 'production': ProductionConfig, + 'default': Config +} \ No newline at end of file diff --git a/requirements.txt b/requirements.txt index 2d139d5..c372220 100644 --- a/requirements.txt +++ b/requirements.txt @@ -1,3 +1,4 @@ Flask requests python-dotenv +gunicorn \ No newline at end of file diff --git a/run_dev.sh b/run_dev.sh index 5a798b7..e555483 100644 --- a/run_dev.sh +++ b/run_dev.sh @@ -4,6 +4,6 @@ source venv/bin/activate # Flaskアプリケーションを起動 -export FLASK_APP=app.py +export FLASK_APP=app export FLASK_ENV=development flask run --host=0.0.0.0 --port=5000 diff --git a/run_prod.sh b/run_prod.sh new file mode 100644 index 0000000..2dc6b5e --- /dev/null +++ b/run_prod.sh @@ -0,0 +1,4 @@ +#!/bin/bash + +source venv/bin/activate +gunicorn --workers 2 --bind 0.0.0.0:5000 wsgi:app \ No newline at end of file diff --git a/wsgi.py b/wsgi.py new file mode 100644 index 0000000..bfc42ce --- /dev/null +++ b/wsgi.py @@ -0,0 +1,7 @@ +from app import create_app +from config import config + +app = create_app(config['production']) + +if __name__ == "__main__": + app.run() \ No newline at end of file