Themivito's picture
Update app.py
9b27a5b verified
Raw History Blame Contribute Delete
2.05 kB
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)