Spaces:
Sleeping
Sleeping
| import streamlit as st | |
| import requests | |
| import os | |
| # --- Constants --- | |
| TEMPLATE_DICT = { | |
| "GRI": "Global Reporting Initiative Standards", | |
| "SASB": "Sustainability Accounting Standards Board", | |
| "TCFD": "Task Force on Climate-related Financial Disclosures", | |
| "IIRC": "International Integrated Reporting Council", | |
| } | |
| # --- Main Function --- | |
| def generate_esg_template(framework_key, lang_code, template_dict): | |
| 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." | |
| template_info = template_dict.get(framework_key, "") | |
| if not template_info: | |
| return "Unsupported or unknown ESG framework." | |
| prompt = f""" | |
| You are an expert ESG specialist. Provide a detailed, professional ESG reporting template strictly aligned with the {framework_key} framework. Include headings, subheadings, and instructions clearly aligned with the {framework_key} standard requirements. | |
| Base information provided: "{template_info}" | |
| Respond clearly formatted as a template 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_module1_ui(): | |
| """Renders the Streamlit UI for Module 1: ESG Template Generation.""" | |
| st.subheader("Tạo mẫu báo cáo ESG theo các framework quốc tế") | |
| st.markdown( | |
| """ | |
| Module này giúp bạn nhanh chóng tạo ra các mẫu báo cáo ESG (Môi trường - Xã hội - Quản trị) | |
| dựa trên các framework báo cáo bền vững phổ biến nhất trên thế giới. | |
| """ | |
| ) | |
| # --- User Inputs --- | |
| col1, col2 = st.columns(2) | |
| with col1: | |
| framework_key = st.selectbox( | |
| "📊 Chọn Framework Báo cáo", | |
| options=list(TEMPLATE_DICT.keys()), | |
| help="Chọn framework bạn muốn tạo mẫu báo cáo.", | |
| ) | |
| 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 mẫu báo cáo.", | |
| ) | |
| # --- Generation --- | |
| if st.button("Tạo Mẫu Báo cáo", use_container_width=True): | |
| with st.spinner("Đang tạo mẫu báo cáo, vui lòng chờ..."): | |
| generated_template = generate_esg_template( | |
| framework_key, lang_code, TEMPLATE_DICT | |
| ) | |
| st.session_state.generated_template = generated_template | |
| # --- Display Result --- | |
| if "generated_template" in st.session_state: | |
| st.markdown("---") | |
| st.subheader("Mẫu Báo cáo ESG đã tạo") | |
| st.markdown(st.session_state.generated_template) | |
| st.download_button( | |
| label="Tải mẫu về máy", | |
| data=st.session_state.generated_template, | |
| file_name=f"esg_template_{framework_key}.md", | |
| mime="text/markdown", | |
| use_container_width=True, | |
| ) | |