File size: 3,664 Bytes
1bae8a9 f2d290a 06f1827 1bae8a9 06f1827 749da80 8113f74 0f76e1a bad139d 1bae8a9 bad139d 14d453c bad139d 1bae8a9 14d453c 749da80 1bae8a9 9d1b6e4 bad139d 0147c61 1bae8a9 |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 |
from flask import Flask, render_template, request, jsonify, send_file
import requests
from dotenv import load_dotenv
import os
from io import BytesIO
import tempfile
# import namespaces
from azure.core.credentials import AzureKeyCredential
from azure.ai.language.questionanswering import QuestionAnsweringClient
from azure.cognitiveservices.speech import SpeechConfig, SpeechSynthesizer, AudioConfig
from azure.cognitiveservices.speech.audio import AudioOutputConfig
# Create a Flask app
app = Flask(__name__)
# Azure Bot Service configuration
AZURE_BOT_ENDPOINT = "https://iti109-sectionb.cognitiveservices.azure.com/"
AZURE_BOT_KEY = "2ou0CMAjUutj0D4In8U8AkxEIXtCrvYFOBMhqSW4rZ7x6yZ033GdJQQJ99ALACqBBLyXJ3w3AAAaACOGtVJj"
# Get Configuration Settings
load_dotenv()
ai_endpoint = os.getenv('AI_SERVICE_ENDPOINT')
ai_key = os.getenv('AI_SERVICE_KEY')
ai_project_name = os.getenv('QA_PROJECT_NAME')
ai_deployment_name = os.getenv('QA_DEPLOYMENT_NAME')
speech_key = os.getenv('SPEECH_KEY')
speech_region = os.getenv('SPEECH_REGION')
# Create client using endpoint and key
credential = AzureKeyCredential(ai_key)
ai_client = QuestionAnsweringClient(endpoint=ai_endpoint, credential=credential)
# Web Interface
@app.route('/')
def home():
return render_template('index.html') # HTML file for the web interface
@app.route('/ask', methods=['POST'])
def ask_bot():
user_question = request.json.get("question", "") # Get the question from the request
if not user_question:
return jsonify({"error": "No question provided"}), 400 # Return an error if no question is provided
try:
# Get the answer from the bot
response = ai_client.get_answers(question=user_question,
project_name=ai_project_name,
deployment_name=ai_deployment_name)
# Get the answer from the response
bot_response = response.answers[0].answer if response.answers else "No response from bot"
# Text-to-Speech
speech_config = SpeechConfig(subscription=speech_key, region=speech_region) # Create a speech config
#audio_config = AudioConfig(filename="./response.wav") # Save the audio to a file
with tempfile.NamedTemporaryFile(suffix=".wav", delete=False) as temp_audio_file:
audio_config = AudioConfig(filename=temp_audio_file.name)
synthesizer = SpeechSynthesizer(speech_config=speech_config, audio_config=audio_config)
synthesizer.speak_text(bot_response)
print("Dir content: ", os.listdir("."))
#audio_output_config = AudioOutputConfig(use_default_speaker=True) # Correct usage
#audio_config = AudioConfig(use_default_microphone=True) # Use the default microphone
synthesizer = SpeechSynthesizer(speech_config=speech_config, audio_config=audio_config) # Create a synthesizer
#synthesizer.speak_text(bot_response)
try:
synthesizer.speak_text(bot_response)
print("Audio file created successfully!")
except Exception as e:
print(f"Error generating audio: {e}")
# Return the answer from the bot
return jsonify({"answer": bot_response, "audio": temp_audio_file.name})
#return jsonify({"answer": bot_response})
except requests.exceptions.RequestException as e:
return jsonify({"error": str(e)}), 500
# Return the audio file
@app.route('/response.wav')
def get_audio():
return send_file(temp_audio_file.name, mimetype="audio/wav")
if __name__ == '__main__':
app.run(debug=True)
|