Spaces:
Sleeping
Sleeping
| import streamlit as st | |
| import requests | |
| # --- Constants --- | |
| FRAMEWORK_OPTIONS = { | |
| "GRI": "Global Reporting Initiative", | |
| "SASB": "Sustainability Accounting Standards Board", | |
| "TCFD": "Task Force on Climate-related Financial Disclosures", | |
| "IIRC": "International Integrated Reporting Council", | |
| "Custom": "Kế hoạch tùy chỉnh theo mục tiêu" | |
| } | |
| # --- Backend Logic --- | |
| def generate_sustainability_plan(profile, objectives, framework, lang_code): | |
| try: | |
| groq_api_key = st.secrets["GROQ_API_KEY"] | |
| except FileNotFoundError: | |
| return "Error: GROQ_API_KEY secret not found. Please add it to your Streamlit secrets." | |
| prompt = f""" | |
| You are an expert sustainability advisor. Based on the following organization profile and ESG objectives, generate a comprehensive, actionable sustainability plan strictly aligned with the {framework} framework. | |
| Organization Profile: | |
| - Geographic area: {profile['province']} | |
| - Sector: {profile['sector']} | |
| - Size: {profile['size']} | |
| - Key operations: {profile['operations']} | |
| ESG Objectives: | |
| - Environmental: {objectives['environmental']} | |
| - Social: {objectives['social']} | |
| - Governance: {objectives['governance']} | |
| Please structure the plan with sections matching the ESG pillars, include clear recommended actions, timelines, KPIs, and assign each action to a responsible role where possible. Respond in {'English' if lang_code == 'en' else 'Vietnamese'}. | |
| """ | |
| api_url = "https://api.groq.com/openai/v1/chat/completions" | |
| headers = {"Authorization": f"Bearer {groq_api_key}", "Content-Type": "application/json"} | |
| payload = { | |
| "model": "llama3-8b-8192", | |
| "messages": [{"role": "user", "content": prompt}], | |
| "max_tokens": 2048, | |
| "temperature": 0.6 | |
| } | |
| try: | |
| response = requests.post(api_url, json=payload, headers=headers) | |
| response.raise_for_status() | |
| return response.json()["choices"][0]["message"]["content"] | |
| except requests.exceptions.RequestException as e: | |
| return f"Error: API request failed: {e}" | |
| except (KeyError, IndexError) as e: | |
| return f"Error: Invalid API response format: {e}" | |
| # --- Streamlit UI --- | |
| def show_module2_ui(): | |
| """Renders the Streamlit UI for Module 2: Sustainability Planner.""" | |
| st.subheader("Tinh chỉnh kế hoạch phát triển bền vững chi tiết") | |
| st.markdown( | |
| """ | |
| Module này giúp bạn xây dựng một kế hoạch hành động ESG chi tiết, phù hợp với bối cảnh | |
| và mục tiêu của tổ chức, dựa trên các framework quốc tế. | |
| """ | |
| ) | |
| # --- User Inputs --- | |
| st.markdown("#### **Thông tin tổ chức**") | |
| col1, col2 = st.columns(2) | |
| with col1: | |
| province = st.text_input("Tỉnh/Thành phố", "TP. Hồ Chí Minh") | |
| sector = st.text_input("Lĩnh vực hoạt động", "Công nghệ thông tin") | |
| with col2: | |
| size = st.selectbox("Quy mô", ["<50 nhân viên", "50-250 nhân viên", "250-1000 nhân viên", ">1000 nhân viên"]) | |
| operations = st.text_area("Hoạt động chính", "Phát triển phần mềm, tư vấn giải pháp CNTT.") | |
| st.markdown("#### **Mục tiêu ESG**") | |
| col1, col2, col3 = st.columns(3) | |
| with col1: | |
| environmental_obj = st.text_area("Mục tiêu Môi trường (E)", "Giảm 15% lượng khí thải carbon trong 2 năm.") | |
| with col2: | |
| social_obj = st.text_area("Mục tiêu Xã hội (S)", "Tăng tỷ lệ nữ giới trong vai trò lãnh đạo lên 40%.") | |
| with col3: | |
| governance_obj = st.text_area("Mục tiêu Quản trị (G)", "Thành lập ủy ban giám sát ESG độc lập.") | |
| st.markdown("#### **Tùy chọn Kế hoạch**") | |
| col1, col2 = st.columns(2) | |
| with col1: | |
| framework = st.selectbox( | |
| "📊 Chọn Framework", | |
| options=list(FRAMEWORK_OPTIONS.keys()), | |
| help="Chọn framework để xây dựng kế hoạch." | |
| ) | |
| with col2: | |
| lang_code = st.selectbox( | |
| "🌐 Chọn Ngôn ngữ", | |
| options=["vi", "en"], | |
| format_func=lambda x: "Tiếng Việt" if x == "vi" else "English", | |
| help="Chọn ngôn ngữ cho kế hoạch." | |
| ) | |
| # --- Generation --- | |
| if st.button("Tạo Kế hoạch Hành động", use_container_width=True): | |
| profile = { | |
| 'province': province, | |
| 'sector': sector, | |
| 'size': size, | |
| 'operations': operations | |
| } | |
| objectives = { | |
| 'environmental': environmental_obj, | |
| 'social': social_obj, | |
| 'governance': governance_obj | |
| } | |
| with st.spinner("Đang xây dựng kế hoạch, vui lòng chờ..."): | |
| generated_plan = generate_sustainability_plan( | |
| profile, objectives, framework, lang_code | |
| ) | |
| st.session_state.generated_plan_m2 = generated_plan | |
| # --- Display Result --- | |
| if "generated_plan_m2" in st.session_state: | |
| st.markdown("---") | |
| st.subheader("Kế hoạch Phát triển Bền vững đã tạo") | |
| st.markdown(st.session_state.generated_plan_m2) | |
| st.download_button( | |
| label="Tải kế hoạch về máy", | |
| data=st.session_state.generated_plan_m2, | |
| file_name=f"sustainability_plan_{framework}.md", | |
| mime="text/markdown", | |
| use_container_width=True, | |
| ) | |