CoolFace
Apppublic

Themivito/DeveloperGuidelines1

sourceHugging Faceupdated 2y agoView on Hugging Face
0likes
app.py64 linesDownload Raw Back to root
1import gradio as gr2import PyPDF23import openai4 5# Set your OpenAI API key6OPENAI_API_KEY = "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"7 8# Initialize OpenAI client9client = openai.OpenAI(api_key=OPENAI_API_KEY)10 11def extract_text_from_pdf(pdf_file):12    """Extracts text from an uploaded PDF."""13    text = ""14    try:15        pdf_reader = PyPDF2.PdfReader(pdf_file)16        for page in pdf_reader.pages:17            text += page.extract_text() + "\n"18    except Exception as e:19        return f"Error extracting text: {str(e)}"20    return text21 22def generate_checklist(pdf_file):23    """Processes PDF and generates a structured checklist using OpenAI."""24    extracted_text = extract_text_from_pdf(pdf_file)25 26    if not extracted_text.strip():27        return "Error: Could not extract any meaningful text from the PDF."28 29    prompt = f"""30    The following text contains developer design guidelines for a housing estate.31    Please extract the key compliance areas and format them into a structured checklist.32    Ensure it is clear, easy to follow, and covers all major compliance points.33 34    PDF Text:35    {extracted_text}36 37    Structured Checklist:38    """39 40    try:41        response = client.chat.completions.create(42            model="gpt-3.5-turbo",43            messages=[{"role": "system", "content": prompt}],44            temperature=0.3,45            max_tokens=80046        )47        checklist = response.choices[0].message.content48    except Exception as e:49        return f"Error generating checklist: {str(e)}"50 51    return checklist52 53# Define Gradio UI54interface = gr.Interface(55    fn=generate_checklist,56    inputs=gr.File(type="filepath"),57    outputs="text",58    title="Developer Guidelines Checklist Generator",59    description="Upload an estate developer guidelines PDF, and this tool will generate a structured checklist.",60)61 62# Launch the Gradio app63if __name__ == "__main__":64    interface.launch(share=True)