本番環境として起動できるように修正してみる
This commit is contained in:
parent
eb9c7082f9
commit
65ca31eb69
@ -1,13 +1,13 @@
|
||||
[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]
|
||||
|
12
app/__init__.py
Normal file
12
app/__init__.py
Normal file
@ -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
|
@ -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)
|
||||
|
||||
@ -82,6 +75,3 @@ def generate_voice():
|
||||
|
||||
except Exception as e:
|
||||
return jsonify({'error': str(e)}), 500
|
||||
|
||||
if __name__ == '__main__':
|
||||
app.run(host='0.0.0.0', port=5000)
|
30
config.py
Normal file
30
config.py
Normal file
@ -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
|
||||
}
|
@ -1,3 +1,4 @@
|
||||
Flask
|
||||
requests
|
||||
python-dotenv
|
||||
gunicorn
|
@ -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
|
||||
|
4
run_prod.sh
Normal file
4
run_prod.sh
Normal file
@ -0,0 +1,4 @@
|
||||
#!/bin/bash
|
||||
|
||||
source venv/bin/activate
|
||||
gunicorn --workers 2 --bind 0.0.0.0:5000 wsgi:app
|
Loading…
Reference in New Issue
Block a user