File size: 4,571 Bytes
f5117c5
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
3b4fe45
 
 
f5117c5
 
 
 
 
 
 
 
 
3b4fe45
 
 
f5117c5
a490905
 
f5117c5
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
3b4fe45
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
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()