|
|
from flask import Flask, render_template, request, jsonify |
|
|
import requests |
|
|
import json |
|
|
|
|
|
app = Flask(__name__) |
|
|
|
|
|
@app.route('/') |
|
|
def index(): |
|
|
return render_template('index.html') |
|
|
|
|
|
@app.route('/chat', methods=['POST']) |
|
|
def chat(): |
|
|
try: |
|
|
data = request.get_json() |
|
|
query = data.get('query', '') |
|
|
|
|
|
|
|
|
response = requests.post( |
|
|
'http://127.0.0.1:8000/generate_response', |
|
|
json={"query": query}, |
|
|
headers={'Content-Type': 'application/json'}, |
|
|
timeout=30 |
|
|
) |
|
|
|
|
|
if response.status_code == 200: |
|
|
result = response.json() |
|
|
return jsonify({"answer": result.get("answer", "No response received")}) |
|
|
else: |
|
|
return jsonify({"answer": "Sorry, I'm having trouble connecting to the AI service."}), 500 |
|
|
|
|
|
except requests.exceptions.RequestException as e: |
|
|
return jsonify({"answer": "Connection error. Please make sure the AI service is running."}), 500 |
|
|
except Exception as e: |
|
|
return jsonify({"answer": "An unexpected error occurred."}), 500 |
|
|
|
|
|
if __name__ == '__main__': |
|
|
app.run(debug=True, port=5000) |