Building Your Construction Planner App with Lyzr and OpenAI
Published: 2024-06-24 06:28:02

Construction project management is a complex task that involves meticulous planning and coordination of various activities, resources, and stakeholders. A typical construction project includes phases such as design, procurement, construction, and commissioning. Each phase requires detailed planning and scheduling to ensure timely completion within budget while maintaining quality standards. However, manual planning processes are often time-consuming, prone to errors, and lack the ability to dynamically adapt to changes and unforeseen issues.

Construction managers face significant challenges in creating comprehensive and efficient project plans that cover all aspects of construction, including material management, labor allocation, quality control, and budget management. The complexity increases with larger projects where multiple teams and resources must be synchronized effectively. Traditional methods often fail to provide the agility needed to respond to on-site changes and unexpected events, leading to delays, cost overruns, and compromised quality.

**Requirements**
Automated Analysis: The tool should analyze uploaded construction maps to extract essential details necessary for planning.
Weekly and Monthly Plans: It should generate detailed weekly and monthly construction schedules that align with project timelines and milestones.
Resource Management: The tool must include considerations for material management, labor management, quality control, and budget management.
User-Friendly Interface: Provide an intuitive and easy-to-use interface for construction managers to upload maps and receive planning outputs.
Adaptability: The planning tool should adapt to various types and sizes of construction projects, providing scalable solutions.
Expected Outcomes
Improved Planning Efficiency: Reduced time and effort required for manual planning, allowing managers to focus on strategic decision-making.
Enhanced Accuracy: Minimized planning errors and improved accuracy in scheduling and resource allocation.
Dynamic Adaptability: Increased ability to adapt to changes and unforeseen events, ensuring project continuity and minimizing delays.
Comprehensive Management: Better integration of material, labor, quality, and budget management into the planning process.
Step-by-Step Guide
**Setting Up the App:**

```
pip install lyzr-automata streamlit pillow
```
**gptvision.py**

```
from typing import Any, Dict, List
from lyzr_automata.ai_models.model_base import AIModel
from openai import OpenAI
from lyzr_automata.data_models import FileResponse
from lyzr_automata.utils.resource_handler import ResourceBox


class GPTVISION(AIModel):
    def __init__(self, api_key, parameters: Dict[str, Any]):
        self.parameters = parameters
        self.client = OpenAI(api_key=api_key)
        self.api_key = api_key

    def generate_text(
        self,
        task_id: str=None,
        system_persona: str=None,
        prompt: str=None,
        image_url: str=None,
        messages: List[dict] = None,
    ):
        if messages is None:
            messages = [
                {"role": "user", "content": [
                    {"type": "text",
                     "text": prompt
                     },
                    {
                      "type": "image_url",
                      "image_url": {
                        "url": f"data:image/jpeg;base64,{image_url}",
                      },
                    },
                    ]
                 },
            ]

        response = self.client.chat.completions.create(
                **self.parameters,
                model="gpt-4o",
                messages=messages,
                )

        return response.choices[0].message.content

    def generate_image(
        self, task_id: str, prompt: str, resource_box: ResourceBox
    ) -> FileResponse:
        pass

```
This code defines a class GPTVISION that inherits from Lyzr Automata'sAIModel. It interacts with the OpenAI API to generate text based on user prompts and potentially images.

It takes an API key and parameters during initialization.
The generate_text function builds a message containing the prompt and optionally an image URL.
It sends this message to the gpt-4o model for text completion and returns the generated response.
The generate_image function is not implemented here.
**App.py**

```
from gptvision import GPTVISION
import streamlit as st
from PIL import Image
import utils
import base64
import os
```
Imports necessary libraries like GPTVISION from gptvision, streamlit for the web app, PIL for image processing, and utility functions from utils.Imports necessary libraries like GPTVISION from gptvision, streamlit for the web app, PIL for image processing, and utility functions from utils.

**OpenAI API Key Validation:**

```
api = st.sidebar.text_input("Enter Your OPENAI API KEY HERE",type="password")

if api:
        openai_4o_model = GPTVISION(api_key=api,parameters={})
else:
        st.sidebar.error("Please Enter Your OPENAI API KEY")

```
Takes user input for their OpenAI API key through a password field in the sidebar.
Checks if the API key is entered.
If not, displays an error message in the sidebar asking the user to enter the key.
**Uploading Construction Map:**

```
data = "data"
os.makedirs(data, exist_ok=True)

def encode_image(image_path):
        with open(image_path, "rb") as image_file:
                return base64.b64encode(image_file.read()).decode('utf-8')
```
Creates a folder named data to store uploaded files.
Defines a function encode_image that takes an image path, opens the image in binary mode, and encodes it to base64 format for sending to the OpenAI API.
**GPT Model and Prompt:**

```
prompt = f"""
You are an expert Construction Manager.Your Task Is to Create Outline and Planning Schedule Based on Given Construction Map.
1/ Analyze Construction map and Get all Necessary details to curate Planning
2/ Create Weekly Plan and Monthly plan.
3/ Create Additional Consideration like Materials,Labor Management,Quality Control And Budget Management.

Output Requirement:
Analysis of Construction Map:
Weekly Plans:
Monthly Plans:
Additional Consideration:
"""
```
Defines a prompt for the OpenAI model. This prompt instructs the model to act as a construction manager and create a plan based on the uploaded map.

**Uploading and Processing:**

```
uploaded_files = st.file_uploader("Upload Your Construction Map", type=['png', 'jpg'])
if uploaded_files is not None:
        st.success(f"File uploaded: {uploaded_files.name}")
        file_path = utils.save_uploaded_file(uploaded_files)
        if file_path is not None:
                encoded_image = encode_image(file_path)
                planning = openai_4o_model.generate_text(prompt=prompt, image_url=encoded_image)
                st.markdown(planning)
```
Creates a file uploader for construction maps, accepting only PNG and JPG formats.
If a file is uploaded, displays a success message with the filename.
Uses utils.save_uploaded_file to save the uploaded file and get its path.
If the path is valid:
Encodes the image using the encode_image function.
Uses the GPTVISION model with the provided API key and parameters to generate text based on the prompt and encoded image.
Displays the generated planning information as markdown text.

try it now: [https://lyzr-construction-planner.streamlit.app/](https://lyzr-construction-planner.streamlit.app/)

For more information explore the website: [Lyzr](https://lyzr.ai)

Github: https://github.com/harshit-lyzr/construction_planner/](https://github.com/harshit-lyzr/construction_planner/)