Update app.py
Browse files
app.py
CHANGED
|
@@ -70,9 +70,9 @@ def extract_frames_from_video(video, frame_points=[0, 0.5, 1], max_size=(800, 80
|
|
| 70 |
cap.release()
|
| 71 |
return frames
|
| 72 |
|
| 73 |
-
def
|
| 74 |
-
if not
|
| 75 |
-
logger.warning("No
|
| 76 |
return [("No input", "Error: Please upload images or a video for analysis.")]
|
| 77 |
|
| 78 |
try:
|
|
@@ -84,72 +84,52 @@ def analyze_construction_image(images=None, video=None):
|
|
| 84 |
"If it does, identify any safety issues or hazards, categorize them, and provide a detailed description, "
|
| 85 |
"and suggest steps to resolve them. If it's not a construction site, simply state that")
|
| 86 |
|
| 87 |
-
|
| 88 |
-
|
| 89 |
-
|
|
|
|
|
|
|
|
|
|
| 90 |
resized_image = resize_image(image)
|
| 91 |
image_data_url = f"data:image/png;base64,{encode_image(resized_image)}"
|
| 92 |
-
|
| 93 |
-
|
| 94 |
-
|
| 95 |
-
|
| 96 |
-
|
| 97 |
-
|
| 98 |
-
|
| 99 |
-
|
| 100 |
-
|
| 101 |
-
"type": "image_url",
|
| 102 |
-
"image_url": {
|
| 103 |
-
"url": image_data_url
|
| 104 |
-
}
|
| 105 |
-
}
|
| 106 |
-
]
|
| 107 |
-
}
|
| 108 |
-
]
|
| 109 |
-
completion = client.chat.completions.create(
|
| 110 |
-
model="llama-3.2-90b-vision-preview",
|
| 111 |
-
messages=messages,
|
| 112 |
-
temperature=0.7,
|
| 113 |
-
max_tokens=1000,
|
| 114 |
-
top_p=1,
|
| 115 |
-
stream=False,
|
| 116 |
-
stop=None
|
| 117 |
-
)
|
| 118 |
-
result = completion.choices[0].message.content
|
| 119 |
-
results.append((f"Image {i+1} analysis", result))
|
| 120 |
|
| 121 |
-
|
| 122 |
-
|
| 123 |
-
|
| 124 |
-
|
| 125 |
-
|
| 126 |
-
|
| 127 |
-
|
| 128 |
-
|
| 129 |
-
|
| 130 |
-
|
| 131 |
-
|
| 132 |
-
|
| 133 |
-
{
|
| 134 |
-
"type": "image_url",
|
| 135 |
-
"image_url": {
|
| 136 |
-
"url": image_data_url
|
| 137 |
-
}
|
| 138 |
}
|
| 139 |
-
|
| 140 |
-
|
| 141 |
-
|
| 142 |
-
|
| 143 |
-
|
| 144 |
-
|
| 145 |
-
|
| 146 |
-
|
| 147 |
-
|
| 148 |
-
|
| 149 |
-
|
| 150 |
-
|
| 151 |
-
|
| 152 |
-
|
|
|
|
| 153 |
|
| 154 |
logger.info("Analysis completed successfully")
|
| 155 |
return results
|
|
@@ -268,22 +248,19 @@ with gr.Blocks(css=custom_css, theme=gr.themes.Soft()) as iface:
|
|
| 268 |
"""
|
| 269 |
)
|
| 270 |
|
| 271 |
-
#
|
| 272 |
with gr.Row():
|
| 273 |
-
|
| 274 |
-
image_input = gr.File(label="Upload Construction Site Images", file_count="multiple", type="filepath", elem_classes="image-container")
|
| 275 |
-
with gr.Column(scale=1):
|
| 276 |
-
video_input = gr.Video(label="Upload Construction Site Video", elem_classes="image-container")
|
| 277 |
|
| 278 |
-
#
|
| 279 |
with gr.Row():
|
| 280 |
analyze_button = gr.Button("π Analyze Safety Hazards", elem_classes="analyze-button")
|
| 281 |
|
| 282 |
-
#
|
| 283 |
with gr.Row():
|
| 284 |
chatbot = gr.Chatbot(label="Safety Analysis Results and Expert Chat", elem_classes="chatbot")
|
| 285 |
|
| 286 |
-
#
|
| 287 |
with gr.Row():
|
| 288 |
msg = gr.Textbox(
|
| 289 |
label="Ask about safety measures or regulations",
|
|
@@ -292,15 +269,14 @@ with gr.Blocks(css=custom_css, theme=gr.themes.Soft()) as iface:
|
|
| 292 |
elem_classes="chat-input"
|
| 293 |
)
|
| 294 |
|
| 295 |
-
#
|
| 296 |
with gr.Row():
|
| 297 |
clear = gr.Button("ποΈ Clear Chat", elem_classes="clear-button")
|
| 298 |
download_button = gr.Button("π₯ Download Report", elem_classes="download-button")
|
| 299 |
|
| 300 |
-
#
|
| 301 |
report_file = gr.File(label="Download Safety Analysis Report")
|
| 302 |
|
| 303 |
-
|
| 304 |
def update_chat(history, new_messages):
|
| 305 |
history = history or []
|
| 306 |
for title, content in new_messages:
|
|
@@ -308,8 +284,8 @@ with gr.Blocks(css=custom_css, theme=gr.themes.Soft()) as iface:
|
|
| 308 |
return history
|
| 309 |
|
| 310 |
analyze_button.click(
|
| 311 |
-
|
| 312 |
-
inputs=[
|
| 313 |
outputs=[chatbot],
|
| 314 |
postprocess=lambda x: update_chat(chatbot.value, x)
|
| 315 |
)
|
|
@@ -317,7 +293,6 @@ with gr.Blocks(css=custom_css, theme=gr.themes.Soft()) as iface:
|
|
| 317 |
msg.submit(chat_about_image, [msg, chatbot], [msg, chatbot])
|
| 318 |
clear.click(lambda: None, None, chatbot, queue=False)
|
| 319 |
|
| 320 |
-
|
| 321 |
download_button.click(
|
| 322 |
download_report,
|
| 323 |
inputs=[chatbot],
|
|
@@ -337,4 +312,4 @@ if __name__ == "__main__":
|
|
| 337 |
except Exception as e:
|
| 338 |
logger.error(f"Error when trying to launch the interface: {str(e)}")
|
| 339 |
logger.error(traceback.format_exc())
|
| 340 |
-
print("Failed to launch the Gradio interface. Please check the logs for more information.")
|
|
|
|
| 70 |
cap.release()
|
| 71 |
return frames
|
| 72 |
|
| 73 |
+
def analyze_construction_media(media):
|
| 74 |
+
if not media:
|
| 75 |
+
logger.warning("No media provided")
|
| 76 |
return [("No input", "Error: Please upload images or a video for analysis.")]
|
| 77 |
|
| 78 |
try:
|
|
|
|
| 84 |
"If it does, identify any safety issues or hazards, categorize them, and provide a detailed description, "
|
| 85 |
"and suggest steps to resolve them. If it's not a construction site, simply state that")
|
| 86 |
|
| 87 |
+
for i, file in enumerate(media):
|
| 88 |
+
file_type = file.name.split('.')[-1].lower()
|
| 89 |
+
|
| 90 |
+
if file_type in ['jpg', 'jpeg', 'png', 'gif']:
|
| 91 |
+
# Handle image
|
| 92 |
+
image = Image.open(file.name)
|
| 93 |
resized_image = resize_image(image)
|
| 94 |
image_data_url = f"data:image/png;base64,{encode_image(resized_image)}"
|
| 95 |
+
content_type = "image"
|
| 96 |
+
elif file_type in ['mp4', 'avi', 'mov', 'wmv']:
|
| 97 |
+
# Handle video
|
| 98 |
+
frames = extract_frames_from_video(file.name)
|
| 99 |
+
image_data_url = f"data:image/png;base64,{encode_image(frames[0])}" # Use first frame
|
| 100 |
+
content_type = "video"
|
| 101 |
+
else:
|
| 102 |
+
results.append((f"File {i+1} analysis", f"Unsupported file type: {file_type}"))
|
| 103 |
+
continue
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 104 |
|
| 105 |
+
messages = [
|
| 106 |
+
{
|
| 107 |
+
"role": "user",
|
| 108 |
+
"content": [
|
| 109 |
+
{
|
| 110 |
+
"type": "text",
|
| 111 |
+
"text": f"{instruction}\n\nAnalyze this {content_type} (File {i+1}/{len(media)}). First, determine if it's a construction site. If it is, explain the {content_type} in detail, focusing on safety aspects. If it's not, briefly describe what you see."
|
| 112 |
+
},
|
| 113 |
+
{
|
| 114 |
+
"type": "image_url",
|
| 115 |
+
"image_url": {
|
| 116 |
+
"url": image_data_url
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 117 |
}
|
| 118 |
+
}
|
| 119 |
+
]
|
| 120 |
+
}
|
| 121 |
+
]
|
| 122 |
+
completion = client.chat.completions.create(
|
| 123 |
+
model="llama-3.2-90b-vision-preview",
|
| 124 |
+
messages=messages,
|
| 125 |
+
temperature=0.7,
|
| 126 |
+
max_tokens=1000,
|
| 127 |
+
top_p=1,
|
| 128 |
+
stream=False,
|
| 129 |
+
stop=None
|
| 130 |
+
)
|
| 131 |
+
result = completion.choices[0].message.content
|
| 132 |
+
results.append((f"File {i+1} analysis", result))
|
| 133 |
|
| 134 |
logger.info("Analysis completed successfully")
|
| 135 |
return results
|
|
|
|
| 248 |
"""
|
| 249 |
)
|
| 250 |
|
| 251 |
+
# Combined upload for images and videos
|
| 252 |
with gr.Row():
|
| 253 |
+
media_input = gr.File(label="Upload Construction Site Images or Videos", file_count="multiple", type="filepath", elem_classes="image-container")
|
|
|
|
|
|
|
|
|
|
| 254 |
|
| 255 |
+
# Analyze Safety Hazards Button
|
| 256 |
with gr.Row():
|
| 257 |
analyze_button = gr.Button("π Analyze Safety Hazards", elem_classes="analyze-button")
|
| 258 |
|
| 259 |
+
# Chat Interface (Safety Analysis Results)
|
| 260 |
with gr.Row():
|
| 261 |
chatbot = gr.Chatbot(label="Safety Analysis Results and Expert Chat", elem_classes="chatbot")
|
| 262 |
|
| 263 |
+
# Question Bar
|
| 264 |
with gr.Row():
|
| 265 |
msg = gr.Textbox(
|
| 266 |
label="Ask about safety measures or regulations",
|
|
|
|
| 269 |
elem_classes="chat-input"
|
| 270 |
)
|
| 271 |
|
| 272 |
+
# Clear Chat and Download Report Buttons
|
| 273 |
with gr.Row():
|
| 274 |
clear = gr.Button("ποΈ Clear Chat", elem_classes="clear-button")
|
| 275 |
download_button = gr.Button("π₯ Download Report", elem_classes="download-button")
|
| 276 |
|
| 277 |
+
# File component to handle the download
|
| 278 |
report_file = gr.File(label="Download Safety Analysis Report")
|
| 279 |
|
|
|
|
| 280 |
def update_chat(history, new_messages):
|
| 281 |
history = history or []
|
| 282 |
for title, content in new_messages:
|
|
|
|
| 284 |
return history
|
| 285 |
|
| 286 |
analyze_button.click(
|
| 287 |
+
analyze_construction_media,
|
| 288 |
+
inputs=[media_input],
|
| 289 |
outputs=[chatbot],
|
| 290 |
postprocess=lambda x: update_chat(chatbot.value, x)
|
| 291 |
)
|
|
|
|
| 293 |
msg.submit(chat_about_image, [msg, chatbot], [msg, chatbot])
|
| 294 |
clear.click(lambda: None, None, chatbot, queue=False)
|
| 295 |
|
|
|
|
| 296 |
download_button.click(
|
| 297 |
download_report,
|
| 298 |
inputs=[chatbot],
|
|
|
|
| 312 |
except Exception as e:
|
| 313 |
logger.error(f"Error when trying to launch the interface: {str(e)}")
|
| 314 |
logger.error(traceback.format_exc())
|
| 315 |
+
print("Failed to launch the Gradio interface. Please check the logs for more information.")
|