TTS / app.py
AdarshJi's picture
Create app.py
c9f1429 verified
raw
history blame
906 Bytes
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:
file_path = r"main.mp3"
if os.path.exists(file_path):
os.remove(file_path)
communicate = edge_tts.Communicate(text, voice='en-GB-SoniaNeural', 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')
audio_base64 = asyncio.run(TextToAudioFile(text))
return jsonify({'audio': audio_base64}), 200
if __name__ == '__main__':
app.run(app)