|
from flask import Flask, render_template, request, jsonify
|
|
import requests
|
|
from dotenv import load_dotenv
|
|
import os
|
|
import json
|
|
|
|
|
|
from azure.core.credentials import AzureKeyCredential
|
|
from azure.ai.language.questionanswering import QuestionAnsweringClient
|
|
|
|
from azure.ai.translation.text import *
|
|
from azure.ai.translation.text.models import InputTextItem
|
|
|
|
app = Flask(__name__)
|
|
|
|
|
|
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')
|
|
|
|
translator_endpoint = os.getenv('TRANSLATOR_ENDPOINT')
|
|
translator_region = os.getenv('TRANSLATOR_REGION')
|
|
translator_key = os.getenv('TRANSLATOR_KEY')
|
|
|
|
|
|
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
|
|
|
|
|
|
detected_language = detect_language(user_question)
|
|
|
|
|
|
if detected_language != 'en':
|
|
|
|
translated_question = translate_text(user_question, 'en')
|
|
try:
|
|
response = ai_client.get_answers(question=translated_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"
|
|
|
|
|
|
translated_answer = translate_text(response.answers[0].answer, detected_language)
|
|
return jsonify({"answer": translated_answer})
|
|
except requests.exceptions.RequestException as e:
|
|
return jsonify({"error": str(e)}), 500
|
|
|
|
else:
|
|
|
|
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"
|
|
return jsonify({"answer": bot_response})
|
|
except requests.exceptions.RequestException as e:
|
|
return jsonify({"error": str(e)}), 500
|
|
|
|
|
|
def detect_language(question):
|
|
|
|
path = '/detect?api-version=3.0'
|
|
url = f"{translator_endpoint}{path}"
|
|
headers = {
|
|
'Ocp-Apim-Subscription-Key': translator_key,
|
|
'Ocp-Apim-Subscription-Region': translator_region,
|
|
'Content-type': 'application/json'
|
|
}
|
|
body = [{'text': question}]
|
|
|
|
response = requests.post(url, headers=headers, json=body)
|
|
response.raise_for_status()
|
|
detected_language = response.json()[0]['language']
|
|
return detected_language
|
|
|
|
def translate_text(question, target_language):
|
|
|
|
|
|
path = '/translate'
|
|
url = f"{translator_endpoint}{path}"
|
|
headers = {
|
|
'Ocp-Apim-Subscription-Key': translator_key,
|
|
'Ocp-Apim-Subscription-Region': translator_region,
|
|
'Content-type': 'application/json'
|
|
}
|
|
params = {'api-version': '3.0', 'to': target_language}
|
|
body = [{'text': question}]
|
|
|
|
response = requests.post(url, headers=headers, params=params, json=body)
|
|
response.raise_for_status()
|
|
translated_text = response.json()[0]['translations'][0]['text']
|
|
return translated_text
|
|
|
|
if __name__ == '__main__':
|
|
app.run(debug=True)
|
|
|