Create app.py
Browse files
app.py
ADDED
@@ -0,0 +1,42 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
import os
|
2 |
+
import base64
|
3 |
+
import uvicorn
|
4 |
+
import json
|
5 |
+
from flask import Flask, request, jsonify
|
6 |
+
from flask import Response, stream_with_context
|
7 |
+
import edge_tts
|
8 |
+
import asyncio
|
9 |
+
|
10 |
+
app = Flask(__name__)
|
11 |
+
|
12 |
+
|
13 |
+
async def TextToAudioFile(text) -> str:
|
14 |
+
|
15 |
+
|
16 |
+
file_path = r"main.mp3"
|
17 |
+
|
18 |
+
if os.path.exists(file_path):
|
19 |
+
os.remove(file_path)
|
20 |
+
|
21 |
+
communicate = edge_tts.Communicate(text, voice='en-GB-SoniaNeural', pitch='+5Hz', rate='+10%')
|
22 |
+
await communicate.save(file_path)
|
23 |
+
|
24 |
+
with open(file_path, 'rb') as audio_file:
|
25 |
+
audio_data = audio_file.read()
|
26 |
+
audio_base64 = base64.b64encode(audio_data).decode('utf-8')
|
27 |
+
|
28 |
+
return audio_base64
|
29 |
+
|
30 |
+
|
31 |
+
|
32 |
+
|
33 |
+
|
34 |
+
@app.route('/tts', methods=['POST'])
|
35 |
+
def tts():
|
36 |
+
data = request.get_json()
|
37 |
+
text = data.get('text')
|
38 |
+
audio_base64 = asyncio.run(TextToAudioFile(text))
|
39 |
+
return jsonify({'audio': audio_base64}), 200
|
40 |
+
|
41 |
+
if __name__ == '__main__':
|
42 |
+
app.run(app)
|