Spaces:
Sleeping
Sleeping
| import gradio as gr | |
| import pandas as pd | |
| from fpdf import FPDF | |
| import os | |
| # Function to generate the EIA report | |
| def generate_eia_report(file): | |
| try: | |
| # Check the file extension and load the file with the appropriate engine | |
| file_extension = os.path.splitext(file.name)[1].lower() | |
| if file_extension == '.xlsx': | |
| # Attempt to read the file with the openpyxl engine | |
| try: | |
| df = pd.read_excel(file, engine='openpyxl') | |
| except Exception as e: | |
| return f"Error reading .xlsx file: {str(e)}", None | |
| elif file_extension == '.xls': | |
| # Attempt to read the file with the xlrd engine | |
| try: | |
| df = pd.read_excel(file, engine='xlrd') | |
| except Exception as e: | |
| return f"Error reading .xls file: {str(e)}", None | |
| else: | |
| return "Unsupported file format. Please upload a valid Excel file (.xlsx or .xls).", None | |
| # Prepare a PDF report | |
| pdf = FPDF() | |
| pdf.set_auto_page_break(auto=True, margin=15) | |
| pdf.add_page() | |
| pdf.set_font("Arial", size=12) | |
| # Add title | |
| pdf.set_font("Arial", style="B", size=16) | |
| pdf.cell(0, 10, "ENVIRONMENT IMPACT ASSESSMENT REPORT", ln=True, align="C") | |
| pdf.ln(10) | |
| # Add table of contents | |
| pdf.set_font("Arial", style="B", size=14) | |
| pdf.cell(0, 10, "TABLE OF CONTENTS", ln=True) | |
| pdf.set_font("Arial", size=12) | |
| table_of_contents = [ | |
| "EXECUTIVE SUMMARY", | |
| "PROJECT TITLE AND LOCATION", | |
| "PROJECT PROPONENT", | |
| "THE EIA STUDY CONSULTANT", | |
| "BRIEF DESCRIPTION OF THE PROJECT", | |
| "ENVIRONMENTAL CONDITIONS OF THE SITE", | |
| "MAJOR IMPACTS & PROPOSED MITIGATION", | |
| "PROPOSED ENVIRONMENTAL MONITORING", | |
| "CHAPTER # 1: INTRODUCTION", | |
| "CHAPTER # 2: DESCRIPTION OF PROJECT", | |
| "CHAPTER # 3: ENVIRONMENTAL & SOCIAL BASELINE CONDITIONS", | |
| "CHAPTER # 4: POTENTIAL ENVIRONMENTAL IMPACTS & MITIGATION MEASURES", | |
| "CHAPTER # 5: ENVIRONMENTAL MANAGEMENT AND MONITORING PROGRAM", | |
| "CHAPTER # 6: STAKEHOLDERS AND PUBLIC CONSULTATION", | |
| "CHAPTER # 7: CONCLUSION & RECOMMENDATIONS", | |
| "REFERENCES", | |
| "GLOSSARY", | |
| ] | |
| for idx, item in enumerate(table_of_contents, 1): | |
| pdf.cell(0, 10, f"{idx}. {item}", ln=True) | |
| pdf.ln(10) | |
| # Add content dynamically from the Excel file | |
| pdf.set_font("Arial", style="B", size=14) | |
| pdf.cell(0, 10, "EXECUTIVE SUMMARY", ln=True) | |
| pdf.set_font("Arial", size=12) | |
| summary = df.get("Executive Summary", "No data provided.") | |
| if isinstance(summary, pd.Series): | |
| summary = summary.iloc[0] # Take the first entry if it's a series | |
| pdf.multi_cell(0, 10, str(summary)) | |
| # Example: Add more sections from the Excel data | |
| sections = ["Project Title", "Project Proponent", "Environmental Conditions"] | |
| for section in sections: | |
| pdf.add_page() | |
| pdf.set_font("Arial", style="B", size=14) | |
| pdf.cell(0, 10, section, ln=True) | |
| pdf.set_font("Arial", size=12) | |
| content = df.get(section, "No data provided.") | |
| if isinstance(content, pd.Series): | |
| content = content.iloc[0] # Take the first entry if it's a series | |
| pdf.multi_cell(0, 10, str(content)) | |
| # Save the PDF to a local path that Gradio can access | |
| output_file = "EIA_Report.pdf" # Save in the current working directory | |
| pdf.output(output_file) | |
| return "Report generated successfully!", output_file | |
| except Exception as e: | |
| return f"Error occurred: {str(e)}", None | |
| # Gradio interface | |
| def process_file(file): | |
| status, pdf_path = generate_eia_report(file) | |
| if pdf_path: | |
| return status, pdf_path | |
| else: | |
| return status, None | |
| with gr.Blocks() as demo: | |
| gr.Markdown("# Environmental Impact Assessment Tool with PDF Report") | |
| with gr.Row(): | |
| with gr.Column(): | |
| file_input = gr.File(label="Upload EIA Excel File", file_types=[".xlsx", ".xls"]) | |
| submit_button = gr.Button("Submit") | |
| with gr.Column(): | |
| status_output = gr.Textbox(label="Status") | |
| file_output = gr.File(label="Generated EIA Report (PDF)") | |
| submit_button.click(process_file, inputs=[file_input], outputs=[status_output, file_output]) | |
| demo.launch() | |