File size: 1,198 Bytes
89d9b0d | 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 | 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', '')
# Make request to the chat API
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) |