File size: 962 Bytes
c9f1429
 
 
 
 
 
 
 
 
 
 
 
371feb6
c9f1429
 
 
 
 
 
 
371feb6
c9f1429
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
371feb6
 
c9f1429
 
 
 
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
import os
import base64
import uvicorn
import json
from flask import Flask, request, jsonify
from flask import Response, stream_with_context
import edge_tts
import asyncio

app = Flask(__name__)


async def TextToAudioFile(text:str,model:str) -> str:


    file_path = r"main.mp3"

    if os.path.exists(file_path):
        os.remove(file_path)

    communicate = edge_tts.Communicate(text, voice=model, pitch='+5Hz', rate='+10%')
    await communicate.save(file_path)

    with open(file_path, 'rb') as audio_file:
        audio_data = audio_file.read()
        audio_base64 = base64.b64encode(audio_data).decode('utf-8')

    return audio_base64





@app.route('/tts', methods=['POST'])
def tts():
    data = request.get_json()
    text = data.get('text')
    model = data.get('model',"en-GB-SoniaNeural")
    audio_base64 = asyncio.run(TextToAudioFile(text,model))
    return jsonify({'audio': audio_base64}), 200

if __name__ == '__main__':
    app.run(app)