Spaces:
Build error
Build error
Create app.py
Browse files
app.py
ADDED
|
@@ -0,0 +1,62 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import gradio as gr
|
| 2 |
+
import PyPDF2
|
| 3 |
+
import openai
|
| 4 |
+
|
| 5 |
+
# Set your OpenAI API key
|
| 6 |
+
OPENAI_API_KEY = "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"
|
| 7 |
+
openai.api_key = OPENAI_API_KEY
|
| 8 |
+
|
| 9 |
+
def extract_text_from_pdf(pdf_file):
|
| 10 |
+
"""Extracts text from an uploaded PDF."""
|
| 11 |
+
text = ""
|
| 12 |
+
try:
|
| 13 |
+
pdf_reader = PyPDF2.PdfReader(pdf_file)
|
| 14 |
+
for page in pdf_reader.pages:
|
| 15 |
+
text += page.extract_text() + "\n"
|
| 16 |
+
except Exception as e:
|
| 17 |
+
return f"Error extracting text: {str(e)}"
|
| 18 |
+
return text
|
| 19 |
+
|
| 20 |
+
def generate_checklist(pdf_file):
|
| 21 |
+
"""Processes PDF and generates a structured checklist using OpenAI."""
|
| 22 |
+
extracted_text = extract_text_from_pdf(pdf_file)
|
| 23 |
+
|
| 24 |
+
if not extracted_text.strip():
|
| 25 |
+
return "Error: Could not extract any meaningful text from the PDF."
|
| 26 |
+
|
| 27 |
+
prompt = f"""
|
| 28 |
+
The following text contains developer design guidelines for a housing estate.
|
| 29 |
+
Please extract the key compliance areas and format them into a structured checklist.
|
| 30 |
+
Ensure it is clear, easy to follow, and covers all major compliance points.
|
| 31 |
+
|
| 32 |
+
PDF Text:
|
| 33 |
+
{extracted_text}
|
| 34 |
+
|
| 35 |
+
Structured Checklist:
|
| 36 |
+
"""
|
| 37 |
+
|
| 38 |
+
try:
|
| 39 |
+
response = openai.ChatCompletion.create(
|
| 40 |
+
model="gpt-4",
|
| 41 |
+
messages=[{"role": "system", "content": prompt}],
|
| 42 |
+
temperature=0.3,
|
| 43 |
+
max_tokens=800
|
| 44 |
+
)
|
| 45 |
+
checklist = response["choices"][0]["message"]["content"]
|
| 46 |
+
except Exception as e:
|
| 47 |
+
return f"Error generating checklist: {str(e)}"
|
| 48 |
+
|
| 49 |
+
return checklist
|
| 50 |
+
|
| 51 |
+
# Define Gradio UI
|
| 52 |
+
interface = gr.Interface(
|
| 53 |
+
fn=generate_checklist,
|
| 54 |
+
inputs=gr.File(type="filepath"),
|
| 55 |
+
outputs="text",
|
| 56 |
+
title="Developer Guidelines Checklist Generator",
|
| 57 |
+
description="Upload an estate developer guidelines PDF, and this tool will generate a structured checklist.",
|
| 58 |
+
)
|
| 59 |
+
|
| 60 |
+
# Launch the Gradio app
|
| 61 |
+
if __name__ == "__main__":
|
| 62 |
+
interface.launch(share=True)
|