# file: app.py import gradio as gr import time from rag_pipeline import initialize_components, generate_response # --- KHỞI TẠO CÁC THÀNH PHẦN (CHỈ CHẠY 1 LẦN) --- start_time = time.time() print("Bắt đầu khởi tạo ứng dụng Chatbot Luật Giao thông...") DATA_PATH = "data/luat_chi_tiet_output_openai_sdk_final_cleaned.json" COMPONENTS = initialize_components(DATA_PATH) end_time = time.time() print(f"✅ Ứng dụng đã sẵn sàng! Thời gian khởi tạo: {end_time - start_time:.2f} giây.") # ---------------------------------------------------- def chat_interface(query, history): """ Hàm xử lý logic cho giao diện chat của Gradio. """ print(f"Nhận được câu hỏi từ người dùng: '{query}'") # Gọi hàm generate_response với query và các thành phần đã được khởi tạo response = generate_response(query, COMPONENTS) return response # --- GIAO DIỆN GRADIO --- with gr.Blocks(theme=gr.themes.Soft(), title="Chatbot Luật Giao thông Việt Nam") as demo: gr.Markdown( """ # ⚖️ Chatbot Luật Giao thông Việt Nam Hỏi đáp về các quy định, mức phạt trong luật giao thông đường bộ dựa trên cơ sở dữ liệu được cung cấp. *Lưu ý: Đây là một sản phẩm demo. Thông tin chỉ mang tính chất tham khảo.* """ ) chatbot = gr.Chatbot(label="Chatbot", height=500) msg = gr.Textbox(label="Nhập câu hỏi của bạn", placeholder="Ví dụ: Vượt đèn đỏ bị phạt bao nhiêu tiền?") clear = gr.ClearButton([msg, chatbot]) def respond(message, chat_history): bot_message = chat_interface(message, chat_history) chat_history.append((message, bot_message)) return "", chat_history msg.submit(respond, [msg, chatbot], [msg, chatbot]) gr.Examples( examples=[ "Phương tiện giao thông đường bộ gồm những loại nào?", "Vượt đèn đỏ phạt bao nhiêu tiền đối với xe máy?", "Nồng độ cồn cho phép khi lái xe ô tô là bao nhiêu?", "Đi sai làn đường bị trừ mấy điểm bằng lái?", ], inputs=msg ) if __name__ == "__main__": demo.launch()