|
from io import BytesIO |
|
from flask import Flask, Response, send_from_directory, request, jsonify |
|
from flask_cors import CORS |
|
import datetime |
|
import soundfile as sf |
|
|
|
from config import Config |
|
from tts import TTS |
|
|
|
def create_api_app(): |
|
app = Flask(__name__) |
|
CORS(app) |
|
|
|
@app.route('/') |
|
def index(): |
|
return 'Server is up!' |
|
|
|
@app.route('/api/text_to_speech', methods=['POST']) |
|
def text_to_speech(): |
|
try: |
|
data = request.json |
|
input_text = data.get('text') |
|
|
|
try: |
|
audio_array = TTS.generate(input_text) |
|
|
|
now = datetime.datetime.now() |
|
now_str = now.strftime("%Y%m%d_%H%M%S") |
|
file_name = f"output_{now_str}.wav" |
|
file_path = f"./outputs/{file_name}" |
|
|
|
|
|
TTS.save_audio(audio_array, file_path) |
|
|
|
audio_url = f"{Config.BASE_URL}/api/audio/{file_name}"; |
|
|
|
return dict(success=True, audio_url=audio_url) |
|
except Exception as e: |
|
return dict(success=False, error=str(e)) |
|
except Exception as e: |
|
return dict(success=False, error=str(e)) |
|
|
|
@app.route('/api/text_to_speech', methods=['GET']) |
|
def text_to_speech2(): |
|
try: |
|
|
|
input_text = request.args.get('text') |
|
|
|
if not input_text: |
|
return jsonify(success=False, error="Missing 'text' parameter"), 400 |
|
|
|
|
|
try: |
|
audio_array = TTS.generate(input_text) |
|
|
|
|
|
buffer = BytesIO() |
|
sf.write(buffer, audio_array, 44100, format='WAV') |
|
buffer.seek(0) |
|
|
|
|
|
return Response( |
|
buffer.read(), |
|
mimetype="audio/wav", |
|
headers={ |
|
"Content-Disposition": "inline; filename=output.wav" |
|
} |
|
) |
|
except Exception as e: |
|
return dict(success=False, error=str(e)) |
|
except Exception as e: |
|
return dict(success=False, error=str(e)) |
|
|
|
@app.route('/api/audio/<path:path>') |
|
def send_audio(path): |
|
return send_from_directory('outputs', path) |
|
|
|
return app |
|
|
|
app = create_api_app() |
|
|
|
if __name__ == '__main__': |
|
app.run(host=Config.HOST, port=Config.PORT, debug=Config.DEBUG) |