File size: 6,288 Bytes
8eacbda
 
 
 
 
 
 
 
63d7c1e
8eacbda
b1c79c4
 
 
e95d9e4
 
b1c79c4
 
e95d9e4
 
b1c79c4
 
 
 
 
e95d9e4
 
b1c79c4
 
e95d9e4
 
b1c79c4
e95d9e4
 
b1c79c4
 
 
e95d9e4
 
 
 
 
b1c79c4
e95d9e4
b1c79c4
e95d9e4
b1c79c4
 
 
e95d9e4
b1c79c4
e95d9e4
b1c79c4
e95d9e4
b1c79c4
e95d9e4
b1c79c4
e95d9e4
b1c79c4
 
 
 
 
 
 
 
 
e95d9e4
b1c79c4
 
 
 
e95d9e4
b1c79c4
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
e95d9e4
b1c79c4
e95d9e4
b1c79c4
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
e95d9e4
b1c79c4
e95d9e4
b1c79c4
e95d9e4
b1c79c4
 
 
 
e95d9e4
 
b1c79c4
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
---
license: mit
title: Construction Site Safety Analyzer
sdk: gradio
emoji: 🏢
colorFrom: blue
colorTo: green
short_description: Enhance workplace safety and compliance with AI
sdk_version: 5.1.0
---
## 🏗️ Construction Site Safety Analyzer

Enhance workplace safety and compliance with AI-powered image analysis using Llama 3.2 90B Vision and expert chat assistance.

### Overview

This application leverages the power of Groq's AI models to analyze construction site images, identify safety issues, and provide detailed descriptions along with recommended resolutions. The Gradio interface allows users to upload images and interact with the system through a chatbot, making it easy to ask questions and get answers regarding safety measures and regulations.

### Features

- **Image Upload**: Users can upload construction site images either as a file path or a PIL Image object.
- **AI Analysis**: The application uses advanced computer vision techniques to detect and categorize safety issues within the images.
- **Chat Interaction**: A built-in chatbot provides detailed responses to questions related to the analyzed images, offering insights and guidance on safety measures and regulations.
- **Custom CSS**: The interface is styled with custom CSS to enhance readability and usability.

### Installation

To run this application, you need to have Python installed on your system. Additionally, ensure that you have the required dependencies installed. You can install these dependencies using pip:

```bash
pip install gradio pillow groq-python
```

### Environment Variables

Before running the application, make sure to set the `GROQ_API_KEY` environment variable. This key is necessary for authenticating requests to the Groq API.

```bash
export GROQ_API_KEY=your_groq_api_key_here
```

### Running the Application

To launch the application, simply execute the main script:

```bash
python main.py
```

The application will start a local server, and you can access it via your web browser at `http://localhost:7860`.

### Code Explanation

#### Imports and Setup

The code begins by importing necessary libraries and setting up logging for debugging purposes. It also loads the `GROQ_API_KEY` from the environment variables and initializes a Groq client.

```python
import os
import base64
import gradio as gr
from PIL import Image
import io
import json
from groq import Groq
import logging

# Set up logging
logging.basicConfig(level=logging.DEBUG)
logger = logging.getLogger(__name__)
```

#### Helper Functions

The `encode_image` function encodes an image into a base64 string, which is useful for sending images over APIs. The `analyze_construction_image` function handles the main logic of image analysis, including sending requests to the Groq API and processing the results.

```python
def encode_image(image):
    try:
        if isinstance(image, str):
            with open(image, "rb") as image_file:
                return base64.b64encode(image_file.read()).decode('utf-8')
        elif isinstance(image, Image.Image):
            buffered = io.BytesIO()
            image.save(buffered, format="PNG")
            return base64.b64encode(buffered.getvalue()).decode('utf-8')
        else:
            raise ValueError(f"Unsupported image type: {type(image)}")
    except Exception as e:
        logger.error(f"Error encoding image: {str(e)}")
        raise

def analyze_construction_image(image):
    if image is None:
        logger.warning("No image provided")
        return [("No image uploaded", "Error: Please upload an image for analysis.")]
    ...
```

#### Gradio Interface

The Gradio interface is created using the `gr.Blocks` context manager. It includes a form for uploading images, a button to trigger analysis, and a chatbot for displaying results and handling user queries.

```python
with gr.Blocks(css=custom_css, theme=gr.themes.Soft()) as iface:
    gr.HTML(
        """
        <div class="container">
            <div class="header">
                <h1>🏗️ Construction Site Safety Analyzer</h1>
            </div>
            <p class="subheader">Enhance workplace safety and compliance with AI-powered image analysis using Llama 3.2 90B Vision and expert chat assistance.</p>
        </div>
        """
    )
    
    with gr.Row():
        with gr.Column(scale=1):
            image_input = gr.Image(type="pil", label="Upload Construction Site Image", elem_classes="image-container")
            analyze_button = gr.Button("🔍 Analyze Safety Hazards", elem_classes="analyze-button")
        with gr.Column(scale=2):
            with gr.Group(elem_classes="chat-container"):
                chatbot = gr.Chatbot(label="Safety Analysis Results and Expert Chat", elem_classes="chatbot")
                with gr.Row(elem_classes="input-row"):
                    msg = gr.Textbox(
                        label="Ask about safety measures or regulations",
                        placeholder="E.g., 'What OSHA guidelines apply to this hazard?'",
                        show_label=False,
                        elem_classes="chat-input"
                    )
                    clear = gr.Button("🗑️ Clear", elem_classes="clear-button")

    def update_chat(history, new_message):
        history = history or []
        history.append(new_message)
        return history

    analyze_button.click(
        analyze_construction_image,
        inputs=[image_input],
        outputs=[chatbot],
        postprocess=lambda x: update_chat(chatbot.value, x[0])
    )

    msg.submit(chat_about_image, [msg, chatbot], [msg, chatbot])
    clear.click(lambda: None, None, chatbot, queue=False)

    gr.HTML(
        """
        <div class="groq-badge">Powered by Groq</div>
        """
    )
```

#### Launching the App

Finally, the application is launched with the `iface.launch()` method, which starts a local development server.

```python
if __name__ == "__main__":
    iface.launch(debug=True)
```

### Conclusion

This Construction Site Safety Analyzer is a powerful tool for enhancing workplace safety and compliance through AI-driven image analysis and expert chat assistance. With its intuitive interface and advanced capabilities, it helps users quickly identify and address potential hazards on construction sites.