Update app.py
Browse files
app.py
CHANGED
@@ -70,13 +70,13 @@ 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:
|
79 |
-
logger.info(
|
80 |
results = []
|
81 |
|
82 |
instruction = ("You are an AI assistant specialized in analyzing images for safety issues. "
|
@@ -84,114 +84,84 @@ def analyze_construction_media(media):
|
|
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(
|
88 |
-
|
89 |
-
|
90 |
-
|
91 |
-
|
92 |
-
|
93 |
-
|
94 |
-
|
95 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
96 |
|
97 |
-
|
98 |
-
|
99 |
-
|
100 |
-
|
101 |
-
|
102 |
-
|
103 |
-
|
104 |
-
|
105 |
-
|
106 |
-
|
107 |
-
|
108 |
-
|
109 |
-
"role": "user",
|
110 |
-
"content": [
|
111 |
-
{
|
112 |
-
"type": "text",
|
113 |
-
"text": f"{instruction}\n\nAnalyze this image (File {i+1}/{len(media)}). First, determine if it's a construction site. If it is, explain the image in detail, focusing on safety aspects. If it's not, briefly describe what you see."
|
114 |
-
},
|
115 |
-
{
|
116 |
-
"type": "image_url",
|
117 |
-
"image_url": {
|
118 |
-
"url": f"data:image/png;base64,{image_base64}"
|
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 |
-
frames = extract_frames_from_video(file_path)
|
148 |
-
logger.info(f"Extracted {len(frames)} frames from video: {file_path}")
|
149 |
-
for j, frame in enumerate(frames):
|
150 |
-
frame_base64 = encode_image(frame)
|
151 |
-
logger.info(f"Video {i+1}, Frame {j+1} encoded, size: {len(frame_base64)} bytes")
|
152 |
-
|
153 |
-
messages = [
|
154 |
{
|
155 |
-
"
|
156 |
-
"
|
157 |
-
|
158 |
-
|
159 |
-
|
160 |
-
|
161 |
-
|
162 |
-
|
163 |
-
"image_url": {
|
164 |
-
"url": f"data:image/png;base64,{frame_base64}"
|
165 |
-
}
|
166 |
-
}
|
167 |
-
]
|
168 |
}
|
169 |
]
|
170 |
-
|
171 |
-
|
172 |
-
|
173 |
-
|
174 |
-
|
175 |
-
|
176 |
-
|
177 |
-
|
178 |
-
|
179 |
-
|
180 |
-
|
181 |
-
|
182 |
-
|
183 |
-
logger.error(f"Error processing video {i+1}: {str(vid_error)}")
|
184 |
-
logger.error(traceback.format_exc())
|
185 |
-
results.append((f"Video {i+1} analysis", f"Error processing video: {str(vid_error)}"))
|
186 |
-
|
187 |
-
else:
|
188 |
-
logger.warning(f"Unsupported file type: {file_type}")
|
189 |
-
results.append((f"File {i+1} analysis", f"Unsupported file type: {file_type}"))
|
190 |
-
|
191 |
-
except Exception as file_error:
|
192 |
-
logger.error(f"Error processing file {i+1}: {str(file_error)}")
|
193 |
-
logger.error(traceback.format_exc())
|
194 |
-
results.append((f"File {i+1} analysis", f"Error processing file: {str(file_error)}"))
|
195 |
|
196 |
logger.info("Analysis completed successfully")
|
197 |
return results
|
@@ -310,9 +280,9 @@ with gr.Blocks(css=custom_css, theme=gr.themes.Soft()) as iface:
|
|
310 |
"""
|
311 |
)
|
312 |
|
313 |
-
#
|
314 |
with gr.Row():
|
315 |
-
|
316 |
|
317 |
# Analyze Safety Hazards Button
|
318 |
with gr.Row():
|
@@ -346,8 +316,8 @@ with gr.Blocks(css=custom_css, theme=gr.themes.Soft()) as iface:
|
|
346 |
return history
|
347 |
|
348 |
analyze_button.click(
|
349 |
-
|
350 |
-
inputs=[
|
351 |
outputs=[chatbot],
|
352 |
postprocess=lambda x: update_chat(chatbot.value, x)
|
353 |
)
|
|
|
70 |
cap.release()
|
71 |
return frames
|
72 |
|
73 |
+
def analyze_mixed_input(input_files):
|
74 |
+
if not input_files:
|
75 |
+
logger.warning("No input files provided")
|
76 |
return [("No input", "Error: Please upload images or a video for analysis.")]
|
77 |
|
78 |
try:
|
79 |
+
logger.info("Starting analysis")
|
80 |
results = []
|
81 |
|
82 |
instruction = ("You are an AI assistant specialized in analyzing images for safety issues. "
|
|
|
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(input_files):
|
88 |
+
file_type = file.name.split('.')[-1].lower()
|
89 |
+
if file_type in ['jpg', 'jpeg', 'png', 'bmp']:
|
90 |
+
# Process image
|
91 |
+
image = Image.open(file.name)
|
92 |
+
resized_image = resize_image(image)
|
93 |
+
image_data_url = f"data:image/png;base64,{encode_image(resized_image)}"
|
94 |
+
content_type = "image"
|
95 |
+
elif file_type in ['mp4', 'avi', 'mov', 'webm']:
|
96 |
+
# Process video
|
97 |
+
frames = extract_frames_from_video(file.name)
|
98 |
+
image_data_url = f"data:image/png;base64,{encode_image(frames[0])}" # Use the first frame
|
99 |
+
content_type = "video"
|
100 |
+
else:
|
101 |
+
results.append((f"File {i+1} analysis", f"Unsupported file type: {file_type}"))
|
102 |
+
continue
|
103 |
|
104 |
+
messages = [
|
105 |
+
{
|
106 |
+
"role": "user",
|
107 |
+
"content": [
|
108 |
+
{
|
109 |
+
"type": "text",
|
110 |
+
"text": f"{instruction}\n\nAnalyze this {content_type} (File {i+1}/{len(input_files)}). 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."
|
111 |
+
},
|
112 |
+
{
|
113 |
+
"type": "image_url",
|
114 |
+
"image_url": {
|
115 |
+
"url": image_data_url
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
116 |
}
|
117 |
+
}
|
118 |
+
]
|
119 |
+
}
|
120 |
+
]
|
121 |
+
completion = client.chat.completions.create(
|
122 |
+
model="llama-3.2-90b-vision-preview",
|
123 |
+
messages=messages,
|
124 |
+
temperature=0.7,
|
125 |
+
max_tokens=1000,
|
126 |
+
top_p=1,
|
127 |
+
stream=False,
|
128 |
+
stop=None
|
129 |
+
)
|
130 |
+
result = completion.choices[0].message.content
|
131 |
+
results.append((f"File {i+1} analysis ({content_type})", result))
|
132 |
+
|
133 |
+
# If it's a video, analyze additional frames
|
134 |
+
if content_type == "video" and len(frames) > 1:
|
135 |
+
for j, frame in enumerate(frames[1:], start=2):
|
136 |
+
image_data_url = f"data:image/png;base64,{encode_image(frame)}"
|
137 |
+
messages = [
|
138 |
+
{
|
139 |
+
"role": "user",
|
140 |
+
"content": [
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
141 |
{
|
142 |
+
"type": "text",
|
143 |
+
"text": f"{instruction}\n\nAnalyze this additional frame from the video (File {i+1}, Frame {j}/{len(frames)}). Focus on any new or changed safety aspects compared to the previous frame."
|
144 |
+
},
|
145 |
+
{
|
146 |
+
"type": "image_url",
|
147 |
+
"image_url": {
|
148 |
+
"url": image_data_url
|
149 |
+
}
|
|
|
|
|
|
|
|
|
|
|
150 |
}
|
151 |
]
|
152 |
+
}
|
153 |
+
]
|
154 |
+
completion = client.chat.completions.create(
|
155 |
+
model="llama-3.2-90b-vision-preview",
|
156 |
+
messages=messages,
|
157 |
+
temperature=0.7,
|
158 |
+
max_tokens=1000,
|
159 |
+
top_p=1,
|
160 |
+
stream=False,
|
161 |
+
stop=None
|
162 |
+
)
|
163 |
+
result = completion.choices[0].message.content
|
164 |
+
results.append((f"File {i+1} analysis (video frame {j})", result))
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
165 |
|
166 |
logger.info("Analysis completed successfully")
|
167 |
return results
|
|
|
280 |
"""
|
281 |
)
|
282 |
|
283 |
+
# Single upload window for both images and videos
|
284 |
with gr.Row():
|
285 |
+
input_files = gr.File(label="Upload Construction Site Images or Videos", file_count="multiple", type="file", elem_classes="image-container")
|
286 |
|
287 |
# Analyze Safety Hazards Button
|
288 |
with gr.Row():
|
|
|
316 |
return history
|
317 |
|
318 |
analyze_button.click(
|
319 |
+
analyze_mixed_input,
|
320 |
+
inputs=[input_files],
|
321 |
outputs=[chatbot],
|
322 |
postprocess=lambda x: update_chat(chatbot.value, x)
|
323 |
)
|