Spaces:
Running
Running
| import streamlit as st | |
| import requests | |
| # --- Backend Logic --- | |
| def evaluate_esg_score(esg_report_text, lang_code, benchmarks=None): | |
| 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." | |
| benchmark_text = f"Compare the results to these benchmarks for the sector: {benchmarks}" if benchmarks else '' | |
| prompt = f""" | |
| You are an independent ESG scoring analyst. Based on the following *actual, implemented* ESG report, rate the organization's performance in each ESG pillar (Environmental, Social, Governance), and provide an overall ESG score (out of 100). | |
| For each pillar, score from 0-100 and provide a rationale. Summarize major strengths and weaknesses based on the data. | |
| {benchmark_text} | |
| Be transparent about scoring logic (e.g., low environmental score if no GHG disclosure; high governance score if independent board, etc). | |
| ESG Report: | |
| \"\"\"{esg_report_text}\"\"\" | |
| Format your answer as: | |
| - Environmental Score: X/100 | |
| Explanation: ... | |
| - Social Score: X/100 | |
| Explanation: ... | |
| - Governance Score: X/100 | |
| Explanation: ... | |
| - **Overall ESG Score:** X/100 | |
| Summary: ... | |
| 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": 1500, | |
| "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_module5_ui(): | |
| """Renders the Streamlit UI for Module 5: ESG Scoring.""" | |
| st.subheader("Đánh giá và chấm điểm báo cáo ESG") | |
| st.markdown( | |
| """ | |
| Module này hoạt động như một nhà phân tích độc lập, chấm điểm báo cáo ESG của bạn | |
| trên thang điểm 100 và cung cấp lý giải chi tiết cho từng trụ cột. | |
| """ | |
| ) | |
| # --- User Inputs --- | |
| esg_report_text = st.text_area( | |
| "📝 Dán nội dung báo cáo ESG của bạn vào đây", | |
| height=300, | |
| placeholder="Ví dụ: Báo cáo bền vững năm 2023 của công ty ABC..." | |
| ) | |
| col1, col2 = st.columns(2) | |
| with col1: | |
| benchmarks = st.text_area( | |
| "📈 Nhập benchmarks (tùy chọn)", | |
| height=150, | |
| help="Cung cấp các chỉ số tham chiếu của ngành để so sánh. Ví dụ: 'Tỷ lệ phát thải trung bình ngành: 1.2 tấn CO2/sản phẩm'" | |
| ) | |
| with col2: | |
| lang_code = st.selectbox( | |
| "🌐 Chọn Ngôn ngữ Phân tích", | |
| options=["vi", "en"], | |
| format_func=lambda x: "Tiếng Việt" if x == "vi" else "English", | |
| key="module5_lang" | |
| ) | |
| # --- Generation --- | |
| if st.button("Chấm điểm ESG", use_container_width=True): | |
| if not esg_report_text: | |
| st.warning("Vui lòng dán nội dung báo cáo của bạn.") | |
| else: | |
| with st.spinner("Đang phân tích và chấm điểm, vui lòng chờ..."): | |
| scoring_result = evaluate_esg_score( | |
| esg_report_text, lang_code, benchmarks if benchmarks else None | |
| ) | |
| st.session_state.scoring_result_m5 = scoring_result | |
| # --- Display Result --- | |
| if "scoring_result_m5" in st.session_state: | |
| st.markdown("---") | |
| st.subheader("Kết quả Chấm điểm ESG") | |
| st.markdown(st.session_state.scoring_result_m5) | |
| st.download_button( | |
| label="Tải kết quả chấm điểm", | |
| data=st.session_state.scoring_result_m5, | |
| file_name="esg_score_analysis.md", | |
| mime="text/markdown", | |
| use_container_width=True, | |
| ) | |