swahili-tts-model / server.py
mosha255's picture
Initial commit
fc37b9e unverified
raw
history blame
1.36 kB
from flask import Flask, send_from_directory, request, jsonify
from flask_cors import CORS
app = Flask(__name__)
CORS(app) # Enable CORS for all routes
# Import the TTS class
from tts import TTS
import numpy as np
import onnxruntime as ort
from pathlib import Path
import datetime
from config import Config
@app.route('/')
def index():
return 'Server is up!'
@app.route('/text_to_speech', methods=['POST'])
def fingerprint_verify():
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}"
# Save the audio to a file
TTS.save_audio(audio_array, file_path)
audio_url = f"{Config.BASE_URL}/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('/audio/<path:path>')
def send_audio(path):
return send_from_directory('outputs', path)
if __name__ == '__main__':
app.run(host=Config.HOST, port=Config.PORT, debug=Config.DEBUG)