File size: 2,656 Bytes
804a2b5
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
f8336d8
804a2b5
 
67841d5
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
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)  # Enable CORS for all routes

    @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}"

                # Save the audio to a file
                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:
        # Parse GET parameters
            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)

                # Use an in-memory buffer to store the audio
                buffer = BytesIO()
                sf.write(buffer, audio_array, 44100, format='WAV')  # Write audio to buffer in WAV format
                buffer.seek(0)  # Reset buffer pointer to the beginning

                # Return the binary data with appropriate headers
                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)