File tree Expand file tree Collapse file tree 1 file changed +19
-0
lines changed
Expand file tree Collapse file tree 1 file changed +19
-0
lines changed Original file line number Diff line number Diff line change 88import traceback
99from flask import send_file
1010import io
11+ from gtts import gTTS
1112
1213# --- Logging Setup ---
1314logging .basicConfig (level = logging .INFO ,
@@ -176,6 +177,24 @@ def api_delete_chat(chat_id):
176177 os .remove (path )
177178 return jsonify ({'success' : True })
178179
180+ @app .route ('/tts' , methods = ['POST' ])
181+ def text_to_speech ():
182+ data = request .get_json ()
183+ text = data .get ('text' , '' )
184+ if not text :
185+ return jsonify ({'error' : 'No text provided' }), 400
186+ try :
187+ # Generate speech using gTTS
188+ tts = gTTS (text = text , lang = 'en' )
189+ mp3_fp = io .BytesIO ()
190+ tts .write_to_fp (mp3_fp )
191+ mp3_fp .seek (0 )
192+ # Return the audio file as response
193+ return send_file (mp3_fp , mimetype = 'audio/mpeg' , as_attachment = False , download_name = 'speech.mp3' )
194+ except Exception as e :
195+ logger .error (f"Error generating speech: { e } " , exc_info = True )
196+ return jsonify ({'error' : 'Failed to generate speech' }), 500
197+
179198if __name__ == '__main__' :
180199 if orchestrator :
181200 logger .info ("Starting Flask development server..." )
You can’t perform that action at this time.
0 commit comments