capradeepgujaran's picture
Update app.py
c17d63e verified
raw
history blame
8.43 kB
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__)
# 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')
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 [(None, "Error: No image uploaded")], "Error: No image uploaded"
try:
logger.info("Starting image analysis")
image_data_url = f"data:image/png;base64,{encode_image(image)}"
logger.debug("Image encoded successfully")
messages = [
{
"role": "user",
"content": [
{
"type": "text",
"text": "Analyze this construction site image. Identify any safety issues or hazards, categorize them, provide a detailed description, and suggest steps to resolve them."
},
{
"type": "image_url",
"image_url": {
"url": image_data_url
}
}
]
}
]
logger.info("Sending request to Groq API")
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
)
logger.info("Received response from Groq API")
result = completion.choices[0].message.content
logger.debug(f"Raw API response: {result}")
if not result:
logger.warning("Received empty response from API")
return [(None, "Error: Received empty response from API")], "Error: Received empty response from API"
# Parse the result
analysis_text = "Safety and Hazard Analysis of the Construction Site Image:\n\n"
categories = result.split("Category")[1:] # Split by categories
if not categories:
logger.warning("No categories found in the response")
return [(None, "Error: Unable to parse the response. No categories found.")], "Error: Unable to parse the response"
for category in categories:
lines = category.split("\n")
category_name = lines[0].split(":")[1].strip() if ":" in lines[0] else lines[0].strip()
analysis_text += f"Category: {category_name}\n"
description = next((line.split("Description:")[1].strip() for line in lines if "Description:" in line), "N/A")
hazard = next((line.split("Hazard:")[1].strip() for line in lines if "Hazard:" in line), "N/A")
resolution_steps = [line.strip() for line in lines if line.strip().startswith("*")]
analysis_text += f"Description: {description}\n"
analysis_text += f"Hazard: {hazard}\n"
analysis_text += "Resolution Steps:\n"
for step in resolution_steps:
analysis_text += f"{step}\n"
analysis_text += "\n"
logger.info("Analysis completed successfully")
return [(None, analysis_text)], ""
except Exception as e:
logger.error(f"Error during image analysis: {str(e)}")
logger.error(traceback.format_exc())
error_message = f"Error during analysis: {str(e)}. Please try again or contact support if the issue persists."
return [(None, error_message)], error_message
def chat_about_image(message, chat_history):
try:
# Prepare the conversation history for the API
messages = [
{"role": "system", "content": "You are an AI assistant specialized in analyzing construction site images and answering questions about them. Use the information from the initial analysis to answer user queries."},
]
# Add chat history to messages
for human, ai in chat_history:
if human:
messages.append({"role": "user", "content": human})
if ai:
messages.append({"role": "assistant", "content": ai})
# Add the new user message
messages.append({"role": "user", "content": message})
# Make API call
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)}")]
# Custom CSS for improved styling
custom_css = """
.container { max-width: 1000px; margin: auto; padding-top: 1.5rem; }
.header { text-align: center; margin-bottom: 2rem; }
.header h1 { color: #2c3e50; font-size: 2.5rem; }
.subheader { color: #34495e; font-size: 1.2rem; margin-bottom: 2rem; }
.image-container { border: 2px dashed #3498db; border-radius: 10px; padding: 1rem; text-align: center; }
.analyze-button { background-color: #2ecc71 !important; color: white !important; }
.clear-button { background-color: #e74c3c !important; color: white !important; }
.chatbot { border: 1px solid #bdc3c7; border-radius: 10px; padding: 1rem; height: 400px; overflow-y: auto; }
.chat-input { border: 1px solid #bdc3c7; border-radius: 5px; padding: 0.5rem; }
.groq-badge { position: fixed; bottom: 10px; right: 10px; background-color: #f39c12; color: white; padding: 5px 10px; border-radius: 5px; font-weight: bold; }
"""
# 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 Safety Analyzer</h1>
</div>
<p class="subheader">Enhance workplace safety and compliance with AI-powered image analysis 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):
chatbot = gr.Chatbot(label="Safety Analysis Results and Expert Chat", elem_classes="chatbot")
with gr.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 Chat", elem_classes="clear-button")
analyze_button.click(
analyze_construction_image,
inputs=[image_input],
outputs=[chatbot]
)
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>
"""
)
# Launch the app
if __name__ == "__main__":
iface.launch(debug=True)