import sys import os import io from flask import Flask, request, jsonify, render_template_string # Add project root to path sys.path.append(os.path.abspath(os.path.join(os.path.dirname(__file__), '../../'))) from src.model.rag_runner import RAGRunner app = Flask(__name__) # Initialize RAG Runner (Global) # Note: This will load the model, which takes time. print("Initializing Chatbot Engine...") chatbot = RAGRunner() print("Chatbot Engine Ready!") HTML_TEMPLATE = """ NIHE AI Smart Assistant
N

NIHE Assistant v2.5

Trực tuyến
Xin chào! Tôi là **trợ lý thông minh của NIHE**. Tôi có thể hỗ trợ bạn về thông tin dịch bệnh, tiêm chủng và các quy trình y tế. Hôm nay tôi có thể giúp gì cho bạn?
Trợ lý đang soạn câu trả lời...
""" @app.route('/') def home(): return render_template_string(HTML_TEMPLATE) @app.route('/chat', methods=['POST']) def chat(): """API endpoint for chat.""" print("Received chat request", flush=True) data = request.json print(f"Data received: {data}", flush=True) query = data.get('query', '') if not query: return jsonify({'response': 'Vui lòng nhập câu hỏi.'}) try: response = chatbot.run(query) return jsonify({'response': response}) except Exception as e: print(f"Error executing RAG: {e}", flush=True) return jsonify({'response': f"Lỗi hệ thống: {str(e)}"}), 500 if __name__ == '__main__': # Threaded=True to handle multiple requests (and keep-alives) better app.run(host='0.0.0.0', port=5000, threaded=True)