Upload Web-Chatbot-Speech-En-app.py
Browse files
Web-Chatbot-Speech-En-app.py
CHANGED
@@ -31,9 +31,6 @@ speech_region = os.getenv('SPEECH_REGION')
|
|
31 |
credential = AzureKeyCredential(ai_key)
|
32 |
ai_client = QuestionAnsweringClient(endpoint=ai_endpoint, credential=credential)
|
33 |
|
34 |
-
# Directory to store audio files
|
35 |
-
AUDIO_DIR = "static/audio"
|
36 |
-
|
37 |
# Web Interface
|
38 |
@app.route('/')
|
39 |
def home():
|
@@ -58,8 +55,10 @@ def ask_bot():
|
|
58 |
# Text-to-Speech
|
59 |
speech_config = SpeechConfig(subscription=speech_key, region=speech_region) # Create a speech config
|
60 |
#audio_config = AudioConfig(filename="./response.wav") # Save the audio to a file
|
61 |
-
|
62 |
-
|
|
|
|
|
63 |
print("Dir content: ", os.listdir("."))
|
64 |
#audio_output_config = AudioOutputConfig(use_default_speaker=True) # Correct usage
|
65 |
#audio_config = AudioConfig(use_default_microphone=True) # Use the default microphone
|
@@ -73,15 +72,15 @@ def ask_bot():
|
|
73 |
print(f"Error generating audio: {e}")
|
74 |
|
75 |
# Return the answer from the bot
|
76 |
-
|
77 |
-
return jsonify({"answer": bot_response
|
78 |
except requests.exceptions.RequestException as e:
|
79 |
return jsonify({"error": str(e)}), 500
|
80 |
|
81 |
# Return the audio file
|
82 |
@app.route('/response.wav')
|
83 |
def get_audio():
|
84 |
-
return send_file(
|
85 |
|
86 |
if __name__ == '__main__':
|
87 |
app.run(debug=True)
|
|
|
31 |
credential = AzureKeyCredential(ai_key)
|
32 |
ai_client = QuestionAnsweringClient(endpoint=ai_endpoint, credential=credential)
|
33 |
|
|
|
|
|
|
|
34 |
# Web Interface
|
35 |
@app.route('/')
|
36 |
def home():
|
|
|
55 |
# Text-to-Speech
|
56 |
speech_config = SpeechConfig(subscription=speech_key, region=speech_region) # Create a speech config
|
57 |
#audio_config = AudioConfig(filename="./response.wav") # Save the audio to a file
|
58 |
+
with tempfile.NamedTemporaryFile(suffix=".wav", delete=False) as temp_audio_file:
|
59 |
+
audio_config = AudioConfig(filename=temp_audio_file.name)
|
60 |
+
synthesizer = SpeechSynthesizer(speech_config=speech_config, audio_config=audio_config)
|
61 |
+
synthesizer.speak_text(bot_response)
|
62 |
print("Dir content: ", os.listdir("."))
|
63 |
#audio_output_config = AudioOutputConfig(use_default_speaker=True) # Correct usage
|
64 |
#audio_config = AudioConfig(use_default_microphone=True) # Use the default microphone
|
|
|
72 |
print(f"Error generating audio: {e}")
|
73 |
|
74 |
# Return the answer from the bot
|
75 |
+
return jsonify({"answer": bot_response, "audio": temp_audio_file.name})
|
76 |
+
#return jsonify({"answer": bot_response})
|
77 |
except requests.exceptions.RequestException as e:
|
78 |
return jsonify({"error": str(e)}), 500
|
79 |
|
80 |
# Return the audio file
|
81 |
@app.route('/response.wav')
|
82 |
def get_audio():
|
83 |
+
return send_file("./response.wav", mimetype="audio/wav")
|
84 |
|
85 |
if __name__ == '__main__':
|
86 |
app.run(debug=True)
|