capradeepgujaran
commited on
Commit
•
1de2d2a
1
Parent(s):
bb5413b
Update app.py
Browse files
app.py
CHANGED
@@ -76,7 +76,7 @@ def analyze_construction_media(media):
|
|
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. "
|
@@ -85,77 +85,106 @@ def analyze_construction_media(media):
|
|
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 |
-
|
89 |
-
|
90 |
-
|
91 |
-
|
92 |
-
|
93 |
-
|
94 |
-
|
95 |
-
|
96 |
-
|
97 |
-
|
98 |
-
|
99 |
-
|
100 |
-
|
101 |
-
|
102 |
-
|
103 |
-
|
|
|
|
|
|
|
|
|
104 |
{
|
105 |
-
"
|
106 |
-
"
|
107 |
-
|
108 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
109 |
}
|
110 |
]
|
111 |
-
|
112 |
-
|
113 |
-
|
114 |
-
|
115 |
-
|
116 |
-
|
117 |
-
|
118 |
-
|
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 |
-
|
148 |
-
|
149 |
-
|
150 |
-
|
151 |
-
|
152 |
-
|
153 |
-
|
154 |
-
|
155 |
-
|
156 |
-
|
157 |
-
|
158 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
159 |
|
160 |
logger.info("Analysis completed successfully")
|
161 |
return results
|
|
|
76 |
return [("No input", "Error: Please upload images or a video for analysis.")]
|
77 |
|
78 |
try:
|
79 |
+
logger.info(f"Starting analysis of {len(media)} files")
|
80 |
results = []
|
81 |
|
82 |
instruction = ("You are an AI assistant specialized in analyzing images for safety issues. "
|
|
|
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 |
+
try:
|
89 |
+
file_path = file.name # Get the file path
|
90 |
+
logger.info(f"Processing file {i+1}/{len(media)}: {file_path}")
|
91 |
+
|
92 |
+
if not os.path.exists(file_path):
|
93 |
+
logger.error(f"File does not exist: {file_path}")
|
94 |
+
results.append((f"File {i+1} analysis", f"Error: File does not exist: {file_path}"))
|
95 |
+
continue
|
96 |
+
|
97 |
+
file_type = os.path.splitext(file_path)[1][1:].lower()
|
98 |
+
|
99 |
+
if file_type in ['jpg', 'jpeg', 'png', 'gif']:
|
100 |
+
# Handle image
|
101 |
+
try:
|
102 |
+
image = Image.open(file_path)
|
103 |
+
logger.info(f"Successfully opened image: {file_path}")
|
104 |
+
resized_image = resize_image(image)
|
105 |
+
image_data_url = f"data:image/png;base64,{encode_image(resized_image)}"
|
106 |
+
|
107 |
+
messages = [
|
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": image_data_url
|
119 |
+
}
|
120 |
+
}
|
121 |
+
]
|
122 |
}
|
123 |
]
|
124 |
+
|
125 |
+
completion = client.chat.completions.create(
|
126 |
+
model="llama-3.2-90b-vision-preview",
|
127 |
+
messages=messages,
|
128 |
+
temperature=0.7,
|
129 |
+
max_tokens=1000,
|
130 |
+
top_p=1,
|
131 |
+
stream=False,
|
132 |
+
stop=None
|
133 |
+
)
|
134 |
+
result = completion.choices[0].message.content
|
135 |
+
results.append((f"Image {i+1} analysis", result))
|
136 |
+
logger.info(f"Successfully analyzed image {i+1}")
|
137 |
+
except Exception as img_error:
|
138 |
+
logger.error(f"Error processing image {i+1}: {str(img_error)}")
|
139 |
+
results.append((f"Image {i+1} analysis", f"Error processing image: {str(img_error)}"))
|
140 |
+
|
141 |
+
elif file_type in ['mp4', 'avi', 'mov', 'wmv']:
|
142 |
+
# Handle video
|
143 |
+
try:
|
144 |
+
frames = extract_frames_from_video(file_path)
|
145 |
+
logger.info(f"Extracted {len(frames)} frames from video: {file_path}")
|
146 |
+
for j, frame in enumerate(frames):
|
147 |
+
image_data_url = f"data:image/png;base64,{encode_image(frame)}"
|
148 |
+
messages = [
|
|
|
149 |
{
|
150 |
+
"role": "user",
|
151 |
+
"content": [
|
152 |
+
{
|
153 |
+
"type": "text",
|
154 |
+
"text": f"{instruction}\n\nAnalyze this frame from a video (File {i+1}/{len(media)}, Frame {j+1}/{len(frames)}). First, determine if it's a construction site. If it is, explain what you observe, focusing on safety aspects. If it's not, briefly describe what you see."
|
155 |
+
},
|
156 |
+
{
|
157 |
+
"type": "image_url",
|
158 |
+
"image_url": {
|
159 |
+
"url": image_data_url
|
160 |
+
}
|
161 |
+
}
|
162 |
+
]
|
163 |
}
|
164 |
]
|
165 |
+
completion = client.chat.completions.create(
|
166 |
+
model="llama-3.2-90b-vision-preview",
|
167 |
+
messages=messages,
|
168 |
+
temperature=0.7,
|
169 |
+
max_tokens=1000,
|
170 |
+
top_p=1,
|
171 |
+
stream=False,
|
172 |
+
stop=None
|
173 |
+
)
|
174 |
+
result = completion.choices[0].message.content
|
175 |
+
results.append((f"Video {i+1}, Frame {j+1} analysis", result))
|
176 |
+
logger.info(f"Successfully analyzed video {i+1}")
|
177 |
+
except Exception as vid_error:
|
178 |
+
logger.error(f"Error processing video {i+1}: {str(vid_error)}")
|
179 |
+
results.append((f"Video {i+1} analysis", f"Error processing video: {str(vid_error)}"))
|
180 |
+
|
181 |
+
else:
|
182 |
+
logger.warning(f"Unsupported file type: {file_type}")
|
183 |
+
results.append((f"File {i+1} analysis", f"Unsupported file type: {file_type}"))
|
184 |
+
|
185 |
+
except Exception as file_error:
|
186 |
+
logger.error(f"Error processing file {i+1}: {str(file_error)}")
|
187 |
+
results.append((f"File {i+1} analysis", f"Error processing file: {str(file_error)}"))
|
188 |
|
189 |
logger.info("Analysis completed successfully")
|
190 |
return results
|