surah 1
अप्रैल 29, 2024
0
from flask import Flask, request, render_template_string, send_file
import requests
import json
import base64
import os
app = Flask(__name__)
API_KEY = 'YOUR_GOOGLE_CLOUD_API_KEY'
TTS_URL = 'https://texttospeech.googleapis.com/v1/text:synthesize'
HTML_TEMPLATE = '''
AI Voice Text-to-Speech
'''
@app.route('/')
def index():
return render_template_string(HTML_TEMPLATE)
@app.route('/convert', methods=['POST'])
def convert():
text = request.json.get('text')
headers = {
'Content-Type': 'application/json',
'Authorization': f'Bearer {API_KEY}'
}
data = {
'input': {'text': text},
'voice': {'languageCode': 'en-US', 'ssmlGender': 'FEMALE'},
'audioConfig': {'audioEncoding': 'MP3'}
}
response = requests.post(TTS_URL, headers=headers, json=data)
audio_content = response.json()['audioContent']
audio_data = base64.b64decode(audio_content)
output_file = 'output.mp3'
with open(output_file, 'wb') as out:
out.write(audio_data)
return send_file(output_file, mimetype='audio/mpeg')
if __name__ == '__main__':
app.run(debug=True)