Skip to content

Commit ea03c28

Browse files
authored
Update web.py
1 parent 87ad5e5 commit ea03c28

File tree

1 file changed

+26
-1
lines changed

1 file changed

+26
-1
lines changed

web.py

Lines changed: 26 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,11 @@
88
import json
99
import sys
1010
import glob
11+
from gtts import gTTS
12+
import logging;
13+
import traceback;
14+
import io;
15+
from flask import send_file;
1116

1217
# Add debug print statements
1318
print("Starting web application...")
@@ -245,6 +250,26 @@ def ask():
245250
'chat_history': updated_history
246251
})
247252

253+
logger = logging.getLogger(__name__)
254+
255+
@app.route('/tts', methods=['POST'])
256+
def text_to_speech():
257+
data = request.get_json()
258+
text = data.get('text', '')
259+
if not text:
260+
return jsonify({'error': 'No text provided'}), 400
261+
try:
262+
# Generate speech using gTTS
263+
tts = gTTS(text=text, lang='en')
264+
mp3_fp = io.BytesIO()
265+
tts.write_to_fp(mp3_fp)
266+
mp3_fp.seek(0)
267+
# Return the audio file as response
268+
return send_file(mp3_fp, mimetype='audio/mpeg', as_attachment=False, download_name='speech.mp3')
269+
except Exception as e:
270+
logger.error(f"Error generating speech: {e}", exc_info=True)
271+
return jsonify({'error': 'Failed to generate speech'}), 500
272+
248273
if __name__ == '__main__':
249274
print("Starting Flask development server...")
250-
app.run(debug=True, host='0.0.0.0')
275+
app.run(debug=True, host='0.0.0.0')

0 commit comments

Comments
 (0)