File size: 2,407 Bytes
a037056
 
 
 
 
 
 
 
 
6eef8d3
a037056
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
# main.py
import os
import gradio as gr
from PIL import Image
import io
import base64
from groq import Groq

# Initialize Groq client with API key (set this as a secret in HF Spaces)
client = Groq(api_key=os.environ.get("construction"))

# Helper: Convert PIL Image to base64
def image_to_base64(image):
    buffered = io.BytesIO()
    image.save(buffered, format="JPEG")
    return base64.b64encode(buffered.getvalue()).decode()

# Prompt for model
SYSTEM_PROMPT = """
You are a helpful civil engineering assistant. The user uploads an image showing some construction damage such as cracks, water leakage, or pipe failure. Based on the image, give:

1. Likely issue  
2. Possible solution  
3. Tools or materials needed  
4. Estimated time to fix

Use simple, helpful, practical language.
"""

# Chatbot logic
def analyze_image(image, history):
    if image is None:
        return history + [("User", "No image uploaded."), ("Bot", "Please upload a damage photo.")]

    base64_img = image_to_base64(image)
    image_url = f"data:image/jpeg;base64,{base64_img}"

    try:
        response = client.chat.completions.create(
            model="meta-llama/llama-4-scout-17b-16e-instruct",
            messages=[
                {"role": "system", "content": SYSTEM_PROMPT},
                {"role": "user", "content": [
                    {"type": "text", "text": "Please analyze this image and give advice on the damage."},
                    {"type": "image_url", "image_url": {"url": image_url}}
                ]}
            ],
            temperature=0.7,
            max_tokens=512
        )

        reply = response.choices[0].message.content
        history.append(("User", "Uploaded image"))
        history.append(("Bot", reply))
        return history
    except Exception as e:
        return history + [("Bot", f"❌ Error: {str(e)}")]

# Gradio Interface
with gr.Blocks() as demo:
    gr.Markdown("## 🛠️ Construction Damage Assistant\nUpload a photo of damage to get repair advice.")

    with gr.Row():
        with gr.Column(scale=1):
            image_input = gr.Image(type="pil", label="Upload Damage Image")

        with gr.Column(scale=2):
            chatbot = gr.Chatbot(label="Repair Suggestions", height=450)

    state = gr.State([])
    submit_btn = gr.Button("Analyze")

    submit_btn.click(fn=analyze_image, inputs=[image_input, state], outputs=chatbot)

demo.launch()