capradeepgujaran commited on
Commit
1de2d2a
1 Parent(s): bb5413b

Update app.py

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