File size: 16,922 Bytes
10b5661
 
4ec8ad4
230a814
10b5661
85d2f78
c8ee59e
e2524e7
00759b9
 
16d08c3
2799c43
e8734eb
e2524e7
 
 
 
4ec8ad4
10b5661
 
e2524e7
 
 
10b5661
c8ee59e
 
4ec8ad4
85d2f78
e2524e7
 
 
 
 
 
 
 
00759b9
 
 
 
e2524e7
 
 
 
 
85d2f78
16d08c3
 
 
e47ba84
16d08c3
 
 
230a814
 
16d08c3
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
e47ba84
 
10b5661
5b19aee
 
e47ba84
5b19aee
e47ba84
5b19aee
 
 
e47ba84
 
8bded3a
e47ba84
5b19aee
 
 
 
e47ba84
aa93317
e47ba84
 
1de2d2a
5b19aee
 
 
 
0b8f58d
5b19aee
e47ba84
5b19aee
 
 
 
 
 
0b8f58d
 
5b19aee
 
 
 
 
 
 
 
 
 
 
 
 
 
 
e47ba84
5b19aee
 
 
e47ba84
aa93317
e47ba84
 
0b8f58d
5b19aee
 
 
 
 
 
 
 
e47ba84
5b19aee
 
 
 
 
0b8f58d
5b19aee
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
88efb3f
e47ba84
2d89b4e
 
e47ba84
2d89b4e
 
 
 
 
 
 
 
 
 
 
64a9ffc
2d89b4e
 
 
 
 
 
 
 
 
 
 
 
 
 
 
4ec8ad4
e47ba84
2799c43
e47ba84
2799c43
e47ba84
2799c43
 
 
 
 
 
 
 
 
 
 
 
e47ba84
2799c43
e47ba84
2799c43
e47ba84
2799c43
e47ba84
86cf2b9
 
 
 
 
 
 
f51d4f8
417694d
fa4f3c7
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
91132d3
fa4f3c7
 
 
 
 
 
 
91132d3
 
fa4f3c7
 
 
 
 
 
91132d3
fa4f3c7
 
 
 
91132d3
 
 
 
fa4f3c7
 
91132d3
 
 
c53efd6
91132d3
c53efd6
91132d3
9dd6b86
c53efd6
 
 
 
91132d3
 
 
 
 
 
 
 
 
 
a0b0991
 
91132d3
 
a0b0991
 
d57cf4a
 
 
 
 
 
fa4f3c7
d57cf4a
fa4f3c7
 
 
91132d3
fa4f3c7
f51d4f8
 
fa4f3c7
 
 
 
 
91132d3
fa4f3c7
 
d57cf4a
fa4f3c7
91132d3
fa4f3c7
 
 
 
 
 
91132d3
fa4f3c7
 
91132d3
 
a0b0991
 
f51d4f8
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
c53efd6
 
 
 
 
 
a9a0441
91132d3
 
a9a0441
 
91132d3
a9a0441
 
 
 
 
 
91132d3
a9a0441
 
91132d3
a9a0441
 
 
 
 
91132d3
a9a0441
417694d
 
91132d3
417694d
 
 
 
 
fa4f3c7
 
417694d
 
 
 
91132d3
d57cf4a
 
 
 
 
 
 
 
 
 
 
 
 
 
91132d3
fa4f3c7
 
 
 
 
 
 
 
 
d57cf4a
 
 
 
 
 
fa4f3c7
 
 
 
 
 
 
 
 
 
 
 
 
d57cf4a
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
91132d3
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
c53efd6
 
2799c43
fa4f3c7
 
a9a0441
d57cf4a
 
fa4f3c7
 
 
 
d57cf4a
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
2799c43
91132d3
10b5661
1f0ad8d
2799c43
1f0ad8d
2799c43
1f0ad8d
f51d4f8
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
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
import os
import base64
import gradio as gr
from PIL import Image, ImageOps
import io
import json
from groq import Groq
import logging
import cv2
import numpy as np
import traceback
from datetime import datetime
import tempfile

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

# Load environment variables
GROQ_API_KEY = os.environ.get("GROQ_API_KEY")
if not GROQ_API_KEY:
    logger.error("GROQ_API_KEY is not set in environment variables")
    raise ValueError("GROQ_API_KEY is not set")

# Initialize Groq client
client = Groq(api_key=GROQ_API_KEY)

def encode_image(image):
    try:
        if isinstance(image, str):  # If image is a file path
            with open(image, "rb") as image_file:
                return base64.b64encode(image_file.read()).decode('utf-8')
        elif isinstance(image, Image.Image):  # If image is a PIL Image
            buffered = io.BytesIO()
            image.save(buffered, format="PNG")
            return base64.b64encode(buffered.getvalue()).decode('utf-8')
        elif isinstance(image, np.ndarray):  # If image is a numpy array (from video)
            is_success, buffer = cv2.imencode(".png", image)
            if is_success:
                return base64.b64encode(buffer).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 resize_image(image, max_size=(800, 800)):
    """Resize image to avoid exceeding the API size limits."""
    try:
        image.thumbnail(max_size, Image.Resampling.LANCZOS)
        return image
    except Exception as e:
        logger.error(f"Error resizing image: {str(e)}")
        raise
        
def extract_frames_from_video(video, frame_points=[0, 0.5, 1], max_size=(800, 800)):
    """Extract key frames from the video at specific time points."""
    cap = cv2.VideoCapture(video)
    frame_count = int(cap.get(cv2.CAP_PROP_FRAME_COUNT))
    fps = int(cap.get(cv2.CAP_PROP_FPS))
    duration = frame_count / fps

    frames = []
    for time_point in frame_points:
        cap.set(cv2.CAP_PROP_POS_MSEC, time_point * duration * 1000)
        ret, frame = cap.read()
        if ret:
            resized_frame = cv2.resize(frame, max_size)
            frames.append(resized_frame)
    cap.release()
    return frames

def detect_snags(file):
    """Detect snags in a single file (image or video)"""
    try:
        file_type = file.name.split('.')[-1].lower()
        if file_type in ['jpg', 'jpeg', 'png', 'bmp']:
            return detect_snags_in_image(file)
        elif file_type in ['mp4', 'avi', 'mov', 'webm']:
            return detect_snags_in_video(file)
        else:
            return "Unsupported file type. Please upload an image or video file."
    except Exception as e:
        logger.error(f"Error detecting snags: {str(e)}")
        return f"Error detecting snags: {str(e)}"

def detect_snags_in_image(image_file):
    image = Image.open(image_file.name)
    resized_image = resize_image(image)
    image_data_url = f"data:image/png;base64,{encode_image(resized_image)}"
    
    instruction = ("You are an AI assistant specialized in detecting snags in construction sites. "
                   "Your task is to analyze the image and describe what you see in the image. Then identify any construction defects, unfinished work, "
                   "or quality issues. List each snag, categorize it, and provide a brief description. "
                   "If no snags are detected, state that the area appears to be free of visible issues.")

    messages = [
        {
            "role": "user",
            "content": [
                {
                    "type": "text",
                    "text": f"{instruction}\n\nAnalyze this image for construction snags and provide a detailed report."
                },
                {
                    "type": "image_url",
                    "image_url": {
                        "url": image_data_url
                    }
                }
            ]
        }
    ]
    
    completion = client.chat.completions.create(
        model="llama-3.2-90b-vision-preview",
        messages=messages,
        temperature=0.7,
        max_tokens=1000,
        top_p=1,
        stream=False,
        stop=None
    )
    
    return completion.choices[0].message.content

def detect_snags_in_video(video_file):
    frames = extract_frames_from_video(video_file.name)
    results = []
    
    instruction = ("You are an AI assistant specialized in detecting snags in construction sites. "
                   "Your task is to analyze the video frame and describe what you see in the video. Then identify any construction defects, unfinished work, "
                   "or quality issues. List each snag, categorize it, and provide a brief description. "
                   "If no snags are detected, state that the area appears to be free of visible issues.")

    for i, frame in enumerate(frames):
        image_data_url = f"data:image/png;base64,{encode_image(frame)}"
        messages = [
            {
                "role": "user",
                "content": [
                    {
                        "type": "text",
                        "text": f"{instruction}\n\nAnalyze this frame from a video (Frame {i+1}/{len(frames)}) for construction snags and provide a detailed report."
                    },
                    {
                        "type": "image_url",
                        "image_url": {
                            "url": image_data_url
                        }
                    }
                ]
            }
        ]
        completion = client.chat.completions.create(
            model="llama-3.2-90b-vision-preview",
            messages=messages,
            temperature=0.7,
            max_tokens=1000,
            top_p=1,
            stream=False,
            stop=None
        )
        results.append(f"Frame {i+1} analysis:\n{completion.choices[0].message.content}\n\n")
    
    return "\n".join(results)

def chat_about_snags(message, chat_history):
    try:
        messages = [
            {"role": "system", "content": "You are an AI assistant specialized in analyzing construction site snags and answering questions about them. Use the information from the initial analysis to answer user queries."},
        ]
        
        for human, ai in chat_history:
            if human:
                messages.append({"role": "user", "content": human})
            if ai:
                messages.append({"role": "assistant", "content": ai})
        
        messages.append({"role": "user", "content": message})
        
        completion = client.chat.completions.create(
            model="llama-3.2-90b-vision-preview",
            messages=messages,
            temperature=0.7,
            max_tokens=500,
            top_p=1,
            stream=False,
            stop=None
        )
        
        response = completion.choices[0].message.content
        chat_history.append((message, response))
        
        return "", chat_history
    except Exception as e:
        logger.error(f"Error during chat: {str(e)}")
        return "", chat_history + [(message, f"Error: {str(e)}")]

def generate_snag_report(chat_history):
    """
    Generate a snag report from the chat history.
    """
    report = "Construction Site Snag Detection Report\n"
    report += "=" * 40 + "\n"
    report += f"Generated on: {datetime.now().strftime('%Y-%m-%d %H:%M:%S')}\n\n"

    for i, (user, ai) in enumerate(chat_history, 1):
        if user:
            report += f"Query {i}:\n{user}\n\n"
        if ai:
            report += f"Analysis {i}:\n{ai}\n\n"
        report += "-" * 40 + "\n"

    return report

def download_snag_report(chat_history):
    """
    Generate and provide a download link for the snag report.
    """
    report = generate_snag_report(chat_history)
    timestamp = datetime.now().strftime("%Y%m%d_%H%M%S")
    filename = f"snag_detection_report_{timestamp}.txt"
    
    with tempfile.NamedTemporaryFile(mode="w", delete=False, suffix=".txt") as temp_file:
        temp_file.write(report)
        temp_file_path = temp_file.name
    
    return temp_file_path

# Custom CSS for improved styling
custom_css = """
:root {
    --primary-color: #FF6B35;
    --secondary-color: #004E89;
    --background-color: #F0F4F8;
    --text-color: #333333;
    --border-color: #CCCCCC;
}

body {
    font-family: 'Arial', sans-serif;
    background-color: var(--background-color);
    color: var(--text-color);
}

.container {
    max-width: 1200px;
    margin: auto;
    padding: 2rem;
    background-color: white;
    border-radius: 10px;
    box-shadow: 0 4px 6px rgba(0, 0, 0, 0.1);
}

.header {
    text-align: center;
    margin-bottom: 2rem;
    padding-bottom: 1rem;
    border-bottom: 2px solid var(--primary-color);
}

.header h1 {
    color: var(--secondary-color);
    font-size: 2.5rem;
    margin-bottom: 0.5rem;
}

.subheader {
    color: var(--text-color);
    font-size: 1.1rem;
    line-height: 1.4;
    margin-bottom: 1.5rem;
    text-align: center;
}

.file-upload-container {
    border: 2px dashed var(--primary-color);
    border-radius: 10px;
    padding: 1rem;
    text-align: center;
    margin-bottom: 1rem;
    background-color: #FFF5E6;
    height: 120px;
    display: flex;
    flex-direction: column;
    justify-content: center;
    align-items: center;
}

.analyze-button {
    background-color: var(--primary-color) !important;
    color: white !important;
    font-size: 1.1rem !important;
    padding: 0.75rem 1.5rem !important;
    border-radius: 5px !important;
    width: 100%;
    transition: background-color 0.3s ease;
}

.analyze-button:hover {
    background-color: #E85A2A !important;
}

.info-row {
    display: flex;
    gap: 1rem;
    margin-bottom: 1.5rem;
}

.info-box {
    flex: 1;
    background-color: #E6F3FF;
    border: 1px solid var(--secondary-color);
    border-radius: 5px;
    padding: 1rem;
    font-size: 0.9rem;
    height: 200px;
    overflow-y: auto;
}

.info-box h4 {
    color: var(--secondary-color);
    margin-top: 0;
    margin-bottom: 0.5rem;
}

.info-box ul, .info-box ol {
    margin: 0;
    padding-left: 1.5rem;
}

.tag {
    display: inline-block;
    background-color: var(--primary-color);
    color: white;
    padding: 0.25rem 0.5rem;
    border-radius: 3px;
    font-size: 0.8rem;
    margin-right: 0.5rem;
    margin-bottom: 0.5rem;
}

.section-title {
    color: var(--secondary-color);
    font-size: 1.5rem;
    margin-top: 2rem;
    margin-bottom: 1rem;
    border-bottom: 2px solid var(--primary-color);
    padding-bottom: 0.5rem;
}

.chatbot {
    border: 1px solid var(--border-color);
    border-radius: 10px;
    padding: 1rem;
    height: 400px;
    overflow-y: auto;
    background-color: white;
}

.chat-input {
    border: 1px solid var(--border-color);
    border-radius: 5px;
    padding: 0.75rem;
    width: 100%;
    font-size: 1rem;
}

.clear-button, .download-button {
    background-color: var(--secondary-color) !important;
    color: white !important;
    font-size: 1rem !important;
    padding: 0.5rem 1rem !important;
    border-radius: 5px !important;
    transition: background-color 0.3s ease;
}

.clear-button:hover, .download-button:hover {
    background-color: #003D6E !important;
}

.download-report-container {
    height: 60px;
    display: flex;
    align-items: center;
}

.footer {
    margin-top: 2rem;
    padding-top: 1rem;
    border-top: 2px solid var(--primary-color);
    display: flex;
    justify-content: space-between;
    align-items: center;
}

.groq-badge {
    background-color: var(--secondary-color);
    color: white;
    padding: 8px 15px;
    border-radius: 5px;
    font-weight: bold;
    font-size: 1rem;
    display: inline-block;
}

.model-info {
    color: var(--text-color);
    font-size: 0.9rem;
}
"""

# Create the Gradio interface
with gr.Blocks(css=custom_css, theme=gr.themes.Soft()) as iface:
    gr.HTML(
        """
        <div class="container">
            <div class="header">
                <h1>🏗️ Construction Site Snag Detector</h1>
                <p class="subheader">Enhance quality control and project management with AI-powered snag detection. Upload images or videos of your construction site to identify defects, unfinished work, and quality issues.</p>
            </div>
        """
    )
    
    with gr.Row():
        gr.HTML('<h3 class="section-title">Upload Files</h3>')
    
    with gr.Row():
        file_input = gr.File(
            label="Upload Construction Site Images or Videos",
            file_count="multiple",
            type="filepath",
            elem_classes="file-upload-container"
        )
    
    with gr.Row():
        analyze_button = gr.Button("🔍 Detect Snags", elem_classes="analyze-button")
    
    with gr.Row(elem_classes="info-row"):
        with gr.Column(scale=1):
            gr.HTML(
                """
                <div class="info-box">
                    <h4>Supported File Types:</h4>
                    <ul>
                        <li>Images: JPG, JPEG, PNG, BMP</li>
                        <li>Videos: MP4, AVI, MOV, WEBM</li>
                    </ul>
                </div>
                """
            )
        
        with gr.Column(scale=1):
            gr.HTML(
                """
                <div class="info-box">
                    <h4>Common Snags:</h4>
                    <div>
                        <span class="tag">Cracks</span>
                        <span class="tag">Leaks</span>
                        <span class="tag">Uneven Surfaces</span>
                        <span class="tag">Incomplete Work</span>
                        <span class="tag">Poor Finishes</span>
                        <span class="tag">Misalignments</span>
                    </div>
                </div>
                """
            )
        
        with gr.Column(scale=1):
            gr.HTML(
                """
                <div class="info-box">
                    <h4>How to use:</h4>
                    <ol>
                        <li>Upload images or videos of your construction site</li>
                        <li>Click "Detect Snags" to analyze the files</li>
                        <li>Review the detected snags in the chat area</li>
                        <li>Ask follow-up questions about the snags or request more information</li>
                        <li>Download a comprehensive report for your records</li>
                    </ol>
                </div>
                """
            )
    
    gr.HTML('<h3 class="section-title">Snag Detection Results</h3>')
    chatbot = gr.Chatbot(
        label="Snag Detection Results and Expert Chat",
        elem_classes="chatbot",
        show_share_button=False,
        show_copy_button=False
    )
    
    with gr.Row():
        msg = gr.Textbox(
            label="Ask about detected snags or quality issues",
            placeholder="E.g., 'What are the most critical snags detected?'",
            show_label=False,
            elem_classes="chat-input"
        )

    with gr.Row():
        clear = gr.Button("🗑️ Clear Chat", elem_classes="clear-button")
        download_button = gr.Button("📥 Download Report", elem_classes="download-button")

    with gr.Row(elem_classes="download-report-container"):
        report_file = gr.File(label="Download Snag Detection Report")

    gr.HTML(
        """
        <div class="footer">
            <div class="groq-badge">Powered by Groq</div>
            <div class="model-info">Model: llama-3.2-90b-vision-preview</div>
        </div>
        """
    )

    def process_files(files):
        results = []
        for file in files:
            result = detect_snags(file)
            results.append((file.name, result))
        return results

    def update_chat(history, new_messages):
        history = history or []
        for title, content in new_messages:
            history.append((None, f"File: {title}\n\n{content}"))
        return history

    analyze_button.click(
        process_files,
        inputs=[file_input],
        outputs=[chatbot],
        postprocess=lambda x: update_chat(chatbot.value, x)
    )

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

    download_button.click(
        download_snag_report,
        inputs=[chatbot],
        outputs=[report_file]
    )

# Launch the app
if __name__ == "__main__":
    try:
        iface.launch(debug=True)
    except Exception as e:
        logger.error(f"Error when trying to launch the interface: {str(e)}")
        logger.error(traceback.format_exc())
        print("Failed to launch the Gradio interface. Please check the logs for more information.")