File size: 2,051 Bytes
26c6bcb
 
 
 
 
 
5962753
 
 
26c6bcb
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
5962753
9b27a5b
26c6bcb
 
 
 
5962753
26c6bcb
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
5962753
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
import gradio as gr
import PyPDF2
import openai

# Set your OpenAI API key
OPENAI_API_KEY = "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"

# Initialize OpenAI client
client = openai.OpenAI(api_key=OPENAI_API_KEY)

def extract_text_from_pdf(pdf_file):
    """Extracts text from an uploaded PDF."""
    text = ""
    try:
        pdf_reader = PyPDF2.PdfReader(pdf_file)
        for page in pdf_reader.pages:
            text += page.extract_text() + "\n"
    except Exception as e:
        return f"Error extracting text: {str(e)}"
    return text

def generate_checklist(pdf_file):
    """Processes PDF and generates a structured checklist using OpenAI."""
    extracted_text = extract_text_from_pdf(pdf_file)

    if not extracted_text.strip():
        return "Error: Could not extract any meaningful text from the PDF."

    prompt = f"""
    The following text contains developer design guidelines for a housing estate.
    Please extract the key compliance areas and format them into a structured checklist.
    Ensure it is clear, easy to follow, and covers all major compliance points.

    PDF Text:
    {extracted_text}

    Structured Checklist:
    """

    try:
        response = client.chat.completions.create(
            model="gpt-3.5-turbo",
            messages=[{"role": "system", "content": prompt}],
            temperature=0.3,
            max_tokens=800
        )
        checklist = response.choices[0].message.content
    except Exception as e:
        return f"Error generating checklist: {str(e)}"

    return checklist

# Define Gradio UI
interface = gr.Interface(
    fn=generate_checklist,
    inputs=gr.File(type="filepath"),
    outputs="text",
    title="Developer Guidelines Checklist Generator",
    description="Upload an estate developer guidelines PDF, and this tool will generate a structured checklist.",
)

# Launch the Gradio app
if __name__ == "__main__":
    interface.launch(share=True)