|
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 |
|
|
|
|
|
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 |
|
|
|
|
|
app = Flask(__name__) |
|
|
|
|
|
AZURE_BOT_ENDPOINT = "https://iti109-sectionb.cognitiveservices.azure.com/" |
|
AZURE_BOT_KEY = "2ou0CMAjUutj0D4In8U8AkxEIXtCrvYFOBMhqSW4rZ7x6yZ033GdJQQJ99ALACqBBLyXJ3w3AAAaACOGtVJj" |
|
|
|
|
|
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') |
|
|
|
|
|
credential = AzureKeyCredential(ai_key) |
|
ai_client = QuestionAnsweringClient(endpoint=ai_endpoint, credential=credential) |
|
|
|
|
|
@app.route('/') |
|
def home(): |
|
return render_template('index.html') |
|
|
|
@app.route('/ask', methods=['POST']) |
|
def ask_bot(): |
|
user_question = request.json.get("question", "") |
|
|
|
if not user_question: |
|
return jsonify({"error": "No question provided"}), 400 |
|
|
|
try: |
|
|
|
response = ai_client.get_answers(question=user_question, |
|
project_name=ai_project_name, |
|
deployment_name=ai_deployment_name) |
|
|
|
|
|
bot_response = response.answers[0].answer if response.answers else "No response from bot" |
|
|
|
|
|
speech_config = SpeechConfig(subscription=speech_key, region=speech_region) |
|
|
|
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(".")) |
|
|
|
|
|
synthesizer = SpeechSynthesizer(speech_config=speech_config, audio_config=audio_config) |
|
|
|
|
|
try: |
|
synthesizer.speak_text(bot_response) |
|
print("Audio file created successfully!") |
|
except Exception as e: |
|
print(f"Error generating audio: {e}") |
|
|
|
|
|
return jsonify({"answer": bot_response, "audio": temp_audio_file.name}) |
|
|
|
except requests.exceptions.RequestException as e: |
|
return jsonify({"error": str(e)}), 500 |
|
|
|
|
|
@app.route('/response.wav') |
|
def get_audio(): |
|
return send_file(temp_audio_file.name, mimetype="audio/wav") |
|
|
|
if __name__ == '__main__': |
|
app.run(debug=True) |
|
|