File size: 2,951 Bytes
10b5661 4ec8ad4 10b5661 c8ee59e 4ec8ad4 10b5661 c8ee59e 4ec8ad4 10b5661 c8ee59e 10b5661 c8ee59e 10b5661 c8ee59e 10b5661 4ec8ad4 10b5661 4ec8ad4 c8ee59e 10b5661 4ec8ad4 10b5661 |
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 |
import os
import base64
import gradio as gr
from PIL import Image
import io
from groq import Groq
# Load environment variables
GROQ_API_KEY = os.environ.get("GROQ_API_KEY")
# Initialize Groq client
client = Groq(api_key=GROQ_API_KEY)
def analyze_construction_image(image):
if image is None:
return "Error: No image uploaded", "", ""
try:
# Convert PIL Image to base64
buffered = io.BytesIO()
image.save(buffered, format="PNG")
img_str = base64.b64encode(buffered.getvalue()).decode()
image_data_url = f"data:image/png;base64,{img_str}"
# Prepare the message for Groq API
completion = client.chat.completions.create(
model="llama-3.2-11b-vision-preview",
messages=[
{
"role": "system",
"content": "You are an AI assistant specialized in analyzing construction site images. Identify issues, categorize them, and provide steps to resolve them."
},
{
"role": "user",
"content": [
{
"type": "text",
"text": "Analyze this construction image. Identify the snag category, provide a detailed snag description, and list steps to desnag."
},
{
"type": "image_url",
"image_url": {
"url": image_data_url
}
}
]
}
],
temperature=0.7,
max_tokens=300,
top_p=1,
stream=False,
stop=None
)
result = completion.choices[0].message.content
# Parse the result
lines = result.split('\n')
snag_category = lines[0] if len(lines) > 0 else "N/A"
snag_description = lines[1] if len(lines) > 1 else "N/A"
desnag_steps = "\n".join(lines[2:]) if len(lines) > 2 else "N/A"
return snag_category, snag_description, desnag_steps
except Exception as e:
return f"Error: {str(e)}", "", ""
# Create the Gradio interface
iface = gr.Interface(
fn=analyze_construction_image,
inputs=gr.Image(type="pil", label="Upload Construction Image"),
outputs=[
gr.Textbox(label="Snag Category"),
gr.Textbox(label="Snag Description"),
gr.Textbox(label="Steps to Desnag")
],
title="Construction Image Analyzer (Llama 3.2 Vision via Groq)",
description="Upload a construction site image to identify issues and get desnag steps using Llama 3.2 Vision technology through Groq API.",
examples=[
["example_image1.jpg"],
["example_image2.jpg"]
],
cache_examples=True,
theme="default"
)
# Launch the app
if __name__ == "__main__":
iface.launch() |