Update app.py
Browse files
app.py
CHANGED
@@ -92,44 +92,69 @@ def analyze_construction_media(media):
|
|
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 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
96 |
elif file_type in ['mp4', 'avi', 'mov', 'wmv']:
|
97 |
# Handle video
|
98 |
frames = extract_frames_from_video(file.name)
|
99 |
-
|
100 |
-
|
101 |
-
|
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 |
-
"
|
111 |
-
"
|
112 |
-
|
113 |
-
|
114 |
-
|
115 |
-
|
116 |
-
|
117 |
-
|
|
|
|
|
|
|
|
|
|
|
118 |
}
|
119 |
]
|
120 |
-
|
121 |
-
|
122 |
-
|
123 |
-
|
124 |
-
|
125 |
-
|
126 |
-
|
127 |
-
|
128 |
-
|
129 |
-
|
130 |
-
|
131 |
-
|
132 |
-
|
133 |
|
134 |
logger.info("Analysis completed successfully")
|
135 |
return results
|
|
|
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 |
+
messages = [
|
96 |
+
{
|
97 |
+
"role": "user",
|
98 |
+
"content": [
|
99 |
+
{
|
100 |
+
"type": "text",
|
101 |
+
"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."
|
102 |
+
},
|
103 |
+
{
|
104 |
+
"type": "image_url",
|
105 |
+
"image_url": {
|
106 |
+
"url": image_data_url
|
107 |
+
}
|
108 |
+
}
|
109 |
+
]
|
110 |
+
}
|
111 |
+
]
|
112 |
+
completion = client.chat.completions.create(
|
113 |
+
model="llama-3.2-90b-vision-preview",
|
114 |
+
messages=messages,
|
115 |
+
temperature=0.7,
|
116 |
+
max_tokens=1000,
|
117 |
+
top_p=1,
|
118 |
+
stream=False,
|
119 |
+
stop=None
|
120 |
+
)
|
121 |
+
result = completion.choices[0].message.content
|
122 |
+
results.append((f"Image {i+1} analysis", result))
|
123 |
elif file_type in ['mp4', 'avi', 'mov', 'wmv']:
|
124 |
# Handle video
|
125 |
frames = extract_frames_from_video(file.name)
|
126 |
+
for j, frame in enumerate(frames):
|
127 |
+
image_data_url = f"data:image/png;base64,{encode_image(frame)}"
|
128 |
+
messages = [
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
129 |
{
|
130 |
+
"role": "user",
|
131 |
+
"content": [
|
132 |
+
{
|
133 |
+
"type": "text",
|
134 |
+
"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."
|
135 |
+
},
|
136 |
+
{
|
137 |
+
"type": "image_url",
|
138 |
+
"image_url": {
|
139 |
+
"url": image_data_url
|
140 |
+
}
|
141 |
+
}
|
142 |
+
]
|
143 |
}
|
144 |
]
|
145 |
+
completion = client.chat.completions.create(
|
146 |
+
model="llama-3.2-90b-vision-preview",
|
147 |
+
messages=messages,
|
148 |
+
temperature=0.7,
|
149 |
+
max_tokens=1000,
|
150 |
+
top_p=1,
|
151 |
+
stream=False,
|
152 |
+
stop=None
|
153 |
+
)
|
154 |
+
result = completion.choices[0].message.content
|
155 |
+
results.append((f"Video {i+1}, Frame {j+1} analysis", result))
|
156 |
+
else:
|
157 |
+
results.append((f"File {i+1} analysis", f"Unsupported file type: {file_type}"))
|
158 |
|
159 |
logger.info("Analysis completed successfully")
|
160 |
return results
|