Spaces:
Running
Running
| import streamlit as st | |
| from agents.greenwash_utils import extract_text, chunk_text, analyze_chunk | |
| import json | |
| def show_module6_ui(): | |
| """Renders the Streamlit UI for Module 6: Greenwashing Check.""" | |
| st.subheader("Phân tích và phát hiện Greenwashing") | |
| st.markdown( | |
| """ | |
| Tải lên báo cáo bền vững (PDF, DOCX) hoặc dán một đoạn văn bản để AI phân tích, | |
| xác định các tuyên bố có khả năng gây hiểu lầm (greenwashing). | |
| """ | |
| ) | |
| # --- User Inputs --- | |
| uploaded_file = st.file_uploader( | |
| "Tải lên báo cáo ESG (PDF, DOCX)", | |
| type=["pdf", "docx"] | |
| ) | |
| manual_text = st.text_area( | |
| "Hoặc dán một đoạn văn bản (tuyên bố, quảng cáo...) vào đây:", | |
| height=200, | |
| placeholder="Ví dụ: Sản phẩm của chúng tôi 100% thân thiện với môi trường..." | |
| ) | |
| # --- Analysis --- | |
| if st.button("Phân tích Greenwashing", use_container_width=True): | |
| text_to_analyze = "" | |
| if uploaded_file: | |
| with st.spinner(f"Đang trích xuất văn bản từ file {uploaded_file.name}..."): | |
| text_to_analyze = extract_text(uploaded_file) | |
| elif manual_text: | |
| text_to_analyze = manual_text | |
| else: | |
| st.warning("Vui lòng tải file lên hoặc dán văn bản để phân tích.") | |
| st.stop() | |
| if "Error:" in text_to_analyze: | |
| st.error(text_to_analyze) | |
| st.stop() | |
| chunks = chunk_text(text_to_analyze) | |
| if not chunks: | |
| st.warning("Không tìm thấy nội dung văn bản để phân tích.") | |
| st.stop() | |
| all_issues = [] | |
| progress_bar = st.progress(0, text="Bắt đầu phân tích...") | |
| for i, chunk in enumerate(chunks): | |
| progress_text = f"Đang phân tích phần {i + 1}/{len(chunks)}..." | |
| progress_bar.progress((i + 1) / len(chunks), text=progress_text) | |
| # Analyze the chunk (API key is now handled by the utility function) | |
| issues = analyze_chunk(chunk) | |
| if isinstance(issues, list): | |
| all_issues.extend(issues) | |
| progress_bar.empty() | |
| st.session_state.greenwash_results_m6 = all_issues | |
| # --- Display Results --- | |
| if "greenwash_results_m6" in st.session_state: | |
| st.markdown("---") | |
| st.subheader("Kết quả Phân tích Greenwashing") | |
| results = st.session_state.greenwash_results_m6 | |
| if not results: | |
| st.success("Không tìm thấy dấu hiệu greenwashing đáng kể trong văn bản.") | |
| else: | |
| st.info(f"Tìm thấy {len(results)} vấn đề tiềm ẩn.") | |
| for idx, issue in enumerate(results, 1): | |
| issue_type = issue.get('type', 'Không xác định') | |
| with st.expander(f"Vấn đề {idx}: **{issue_type}**"): | |
| st.markdown(f"> **Trích đoạn:** *...{issue.get('excerpt', 'N/A')}...*") | |
| st.write("**Giải thích:**", issue.get("explanation", "")) | |
| st.write("**Gợi ý:**", issue.get("suggestion", "")) | |
| st.download_button( | |
| label="Tải kết quả phân tích", | |
| data=json.dumps(results, indent=2, ensure_ascii=False), | |
| file_name="greenwashing_analysis.json", | |
| mime="application/json", | |
| use_container_width=True, | |
| ) | |