File size: 2,134 Bytes
a666eb8
 
 
 
b692be2
 
a666eb8
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
d5cf9e4
abce102
ad22edc
 
 
 
 
a142e68
94b4fab
 
abce102
94b4fab
ad22edc
 
fc4b7eb
dd2abc6
a666eb8
 
 
 
a3b46b0
a666eb8
 
 
 
 
 
 
c3631f8
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
import gradio as gr
import PyPDF2
import openai

# Set up the OpenAI client properly
client = openai.Client(api_key="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")  # Use your new key here

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 = openai.ChatCompletion.create(
        model="gpt-4",
        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
except Exception as e:
    checklist = 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)