Spaces:
Sleeping
Sleeping
| """ | |
| Main entry point for the ESG AI web application. | |
| This script exposes a single Streamlit application that aggregates all of the | |
| individual modules implemented in this repository. Each module focuses on a | |
| distinct portion of the ESG workflow (e.g. template generation, planning, | |
| report generation, compliance checking, score evaluation and greenwashing | |
| analysis) and exposes its own top‑level UI function. The `app.py` file | |
| provides a simple sidebar for users to switch between these modules and | |
| delegates rendering to the appropriate function. Because the module | |
| implementations live in separate files, this entry point remains light | |
| weight and easy to maintain. | |
| To add a new module to the application simply implement a function that | |
| presents the user interface (for example `show_module1_ui`), place it in a | |
| Python file at the top level of the repository (e.g. `module1_template.py`) | |
| and add an entry for it to the `MODULE_REGISTRY` dictionary below. | |
| """ | |
| import importlib | |
| import streamlit as st | |
| # --------------------------------------------------------------------------- | |
| # Module registry | |
| # | |
| # The keys are the human friendly names that will appear in the sidebar | |
| # selectbox. The values are tuples containing the import path and the name | |
| # of the function to call. New modules should be registered here. If a | |
| # module cannot be imported at runtime a friendly error will be displayed. | |
| # --------------------------------------------------------------------------- | |
| MODULE_REGISTRY = { | |
| "1. Tạo mẫu kế hoạch bền vững": ("agents.module1_template", "show_module1_ui"), | |
| "2. Tinh chỉnh kế hoạch bền vững": ("agents.module2_planner", "show_module2_ui"), | |
| "3. Tạo báo cáo ESG tự động": ("agents.module3_report", "show_module3_ui"), | |
| "4. Kiểm tra tuân thủ đa khung": ("agents.module4_compliance", "show_module4_ui"), | |
| "5. Đánh giá và điểm số ESG": ("agents.module5_score", "show_module5_ui"), | |
| "6. Kiểm tra greenwashing": ("agents.module6_greenwash", "show_module6_ui"), | |
| } | |
| def _load_module_ui(module_name: str, function_name: str): | |
| """Attempt to import and retrieve the UI function from a module. | |
| Parameters | |
| ---------- | |
| module_name: str | |
| The module to import. Should be a top level Python file without | |
| `.py` extension, for example ``module3_report``. | |
| function_name: str | |
| The name of the function within the module that renders the UI. | |
| Returns | |
| ------- | |
| callable | |
| A callable function if import succeeded. Otherwise a lambda that | |
| displays a warning. | |
| """ | |
| try: | |
| module = importlib.import_module(module_name) | |
| except ModuleNotFoundError as exc: | |
| def _missing_ui(exc=exc): | |
| st.warning( | |
| f"Không thể tải module '{module_name}'. Vui lòng kiểm tra rằng " | |
| f"file '{module_name}.py' tồn tại trong thư mục dự án và chứa " | |
| f"một hàm có tên '{function_name}'.\n\nChi tiết lỗi: {exc}" | |
| ) | |
| return _missing_ui | |
| # module loaded, try to get the function | |
| ui_func = getattr(module, function_name, None) | |
| if ui_func is None: | |
| def _missing_func(): | |
| st.warning( | |
| f"Module '{module_name}' được tìm thấy nhưng không chứa hàm " | |
| f"'{function_name}'. Vui lòng định nghĩa hàm giao diện đúng tên." | |
| ) | |
| return _missing_func | |
| return ui_func | |
| def main() -> None: | |
| """Main function executed by Streamlit to run the multi‑module application.""" | |
| st.set_page_config( | |
| page_title="ESG AI Web Application", | |
| layout="wide", | |
| initial_sidebar_state="expanded", | |
| ) | |
| st.sidebar.title("📘 Chức năng ESG") | |
| module_key = st.sidebar.selectbox( | |
| label="Chọn chức năng", | |
| options=list(MODULE_REGISTRY.keys()), | |
| index=0, | |
| ) | |
| # Unpack module path and function name | |
| module_name, function_name = MODULE_REGISTRY[module_key] | |
| # Display header for the chosen module | |
| st.title(module_key) | |
| # Resolve and call UI function | |
| ui_function = _load_module_ui(module_name, function_name) | |
| try: | |
| ui_function() | |
| except Exception as err: | |
| st.error( | |
| "Đã xảy ra lỗi khi chạy module. Vui lòng kiểm tra lại mã nguồn."\ | |
| f"\n\nChi tiết lỗi: {err}" | |
| ) | |
| if __name__ == "__main__": | |
| main() | |