Spaces:
Runtime error
Runtime error
Download app.py from Themivito/DeveloperGuidelines: direct link, hf CLI and curl.
- Browser
- Download file 1.97 kB
-
https://huggingface.co/spaces/Themivito/DeveloperGuidelines/resolve/dd2abc67130307cab8f9b8caaa8533db1981e92f/app.py
- Command line
-
hf download hf://spaces/Themivito/DeveloperGuidelines@dd2abc67130307cab8f9b8caaa8533db1981e92f/app.py
-
curl -L -o app.py https://huggingface.co/spaces/Themivito/DeveloperGuidelines/resolve/dd2abc67130307cab8f9b8caaa8533db1981e92f/app.py
1.97 kB
| 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 = client.chat.completions.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: | |
| 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() | |