Update app.py
Browse files
app.py
CHANGED
|
@@ -102,7 +102,9 @@ def analyze_construction_media(media):
|
|
| 102 |
with Image.open(file_path) as image:
|
| 103 |
logger.info(f"Successfully opened image: {file_path}")
|
| 104 |
resized_image = resize_image(image)
|
| 105 |
-
|
|
|
|
|
|
|
| 106 |
|
| 107 |
messages = [
|
| 108 |
{
|
|
@@ -122,6 +124,7 @@ def analyze_construction_media(media):
|
|
| 122 |
}
|
| 123 |
]
|
| 124 |
|
|
|
|
| 125 |
completion = client.chat.completions.create(
|
| 126 |
model="llama-3.2-90b-vision-preview",
|
| 127 |
messages=messages,
|
|
@@ -132,58 +135,25 @@ def analyze_construction_media(media):
|
|
| 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 |
-
|
| 144 |
-
|
| 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")
|
|
@@ -192,6 +162,7 @@ def analyze_construction_media(media):
|
|
| 192 |
logger.error(f"Error during analysis: {str(e)}")
|
| 193 |
logger.error(traceback.format_exc())
|
| 194 |
return [("Analysis error", f"Error during analysis: {str(e)}")]
|
|
|
|
| 195 |
|
| 196 |
def chat_about_image(message, chat_history):
|
| 197 |
try:
|
|
|
|
| 102 |
with Image.open(file_path) as image:
|
| 103 |
logger.info(f"Successfully opened image: {file_path}")
|
| 104 |
resized_image = resize_image(image)
|
| 105 |
+
image_data = encode_image(resized_image)
|
| 106 |
+
logger.info(f"Image {i+1} encoded, size: {len(image_data)} bytes")
|
| 107 |
+
image_data_url = f"data:image/png;base64,{image_data}"
|
| 108 |
|
| 109 |
messages = [
|
| 110 |
{
|
|
|
|
| 124 |
}
|
| 125 |
]
|
| 126 |
|
| 127 |
+
logger.info(f"Sending request to AI model for image {i+1}")
|
| 128 |
completion = client.chat.completions.create(
|
| 129 |
model="llama-3.2-90b-vision-preview",
|
| 130 |
messages=messages,
|
|
|
|
| 135 |
stop=None
|
| 136 |
)
|
| 137 |
result = completion.choices[0].message.content
|
| 138 |
+
logger.info(f"Received response from AI model for image {i+1}")
|
| 139 |
results.append((f"Image {i+1} analysis", result))
|
| 140 |
logger.info(f"Successfully analyzed image {i+1}")
|
| 141 |
except Exception as img_error:
|
| 142 |
logger.error(f"Error processing image {i+1}: {str(img_error)}")
|
| 143 |
+
logger.error(traceback.format_exc())
|
| 144 |
results.append((f"Image {i+1} analysis", f"Error processing image: {str(img_error)}"))
|
| 145 |
|
| 146 |
elif file_type in ['mp4', 'avi', 'mov', 'wmv']:
|
| 147 |
+
# Handle video (keep the existing video processing code)
|
| 148 |
+
...
|
| 149 |
+
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 150 |
else:
|
| 151 |
logger.warning(f"Unsupported file type: {file_type}")
|
| 152 |
results.append((f"File {i+1} analysis", f"Unsupported file type: {file_type}"))
|
| 153 |
|
| 154 |
except Exception as file_error:
|
| 155 |
logger.error(f"Error processing file {i+1}: {str(file_error)}")
|
| 156 |
+
logger.error(traceback.format_exc())
|
| 157 |
results.append((f"File {i+1} analysis", f"Error processing file: {str(file_error)}"))
|
| 158 |
|
| 159 |
logger.info("Analysis completed successfully")
|
|
|
|
| 162 |
logger.error(f"Error during analysis: {str(e)}")
|
| 163 |
logger.error(traceback.format_exc())
|
| 164 |
return [("Analysis error", f"Error during analysis: {str(e)}")]
|
| 165 |
+
|
| 166 |
|
| 167 |
def chat_about_image(message, chat_history):
|
| 168 |
try:
|