wookimchye commited on
Commit
bad139d
·
verified ·
1 Parent(s): deb9e23

Update Web-Chatbot-Speech-En-app.py

Browse files
Files changed (1) hide show
  1. Web-Chatbot-Speech-En-app.py +16 -9
Web-Chatbot-Speech-En-app.py CHANGED
@@ -52,22 +52,29 @@ def ask_bot():
52
 
53
  # Text-to-Speech
54
  speech_config = SpeechConfig(subscription=speech_key, region=speech_region) # Create a speech config
55
- #audio_config = AudioConfig(filename="response.wav") # Save the audio to a file
56
- audio_config = AudioOutputConfig(use_default_speaker=True) # Use the default speaker
57
- audio_config = AudioConfig(use_default_microphone=True) # Use the default microphone
 
58
  synthesizer = SpeechSynthesizer(speech_config=speech_config, audio_config=audio_config) # Create a synthesizer
59
- synthesizer.speak_text(bot_response)
 
 
 
 
 
 
60
 
61
  # Return the answer from the bot
62
- #return jsonify({"answer": bot_response, "audio": "/response.wav"})
63
- return jsonify({"answer": bot_response})
64
  except requests.exceptions.RequestException as e:
65
  return jsonify({"error": str(e)}), 500
66
 
67
  # Return the audio file
68
- #@app.route('/response.wav')
69
- #def get_audio():
70
- # return send_file("response.wav", mimetype="audio/wav")
71
 
72
  if __name__ == '__main__':
73
  app.run(debug=True)
 
52
 
53
  # Text-to-Speech
54
  speech_config = SpeechConfig(subscription=speech_key, region=speech_region) # Create a speech config
55
+ audio_config = AudioConfig(filename="./response.wav") # Save the audio to a file
56
+ print(os.listdir("."))
57
+ #audio_config = AudioOutputConfig(use_default_speaker=True) # Use the default speaker
58
+ #audio_config = AudioConfig(use_default_microphone=True) # Use the default microphone
59
  synthesizer = SpeechSynthesizer(speech_config=speech_config, audio_config=audio_config) # Create a synthesizer
60
+ #synthesizer.speak_text(bot_response)
61
+
62
+ try:
63
+ synthesizer.speak_text(bot_response)
64
+ print("Audio file created successfully!")
65
+ except Exception as e:
66
+ print(f"Error generating audio: {e}")
67
 
68
  # Return the answer from the bot
69
+ return jsonify({"answer": bot_response, "audio": "./response.wav"})
70
+ #return jsonify({"answer": bot_response})
71
  except requests.exceptions.RequestException as e:
72
  return jsonify({"error": str(e)}), 500
73
 
74
  # Return the audio file
75
+ @app.route('./response.wav')
76
+ def get_audio():
77
+ return send_file("response.wav", mimetype="audio/wav")
78
 
79
  if __name__ == '__main__':
80
  app.run(debug=True)