capradeepgujaran commited on
Commit
6f31c1c
·
verified ·
1 Parent(s): e8734eb

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +55 -80
app.py CHANGED
@@ -70,9 +70,9 @@ 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 analyze_construction_image(images=None, video=None):
74
- if not images and video is None:
75
- logger.warning("No images or video provided")
76
  return [("No input", "Error: Please upload images or a video for analysis.")]
77
 
78
  try:
@@ -84,72 +84,52 @@ def analyze_construction_image(images=None, video=None):
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
- if images:
88
- for i, image_file in enumerate(images):
89
- image = Image.open(image_file.name)
 
 
 
90
  resized_image = resize_image(image)
91
  image_data_url = f"data:image/png;base64,{encode_image(resized_image)}"
92
- messages = [
93
- {
94
- "role": "user",
95
- "content": [
96
- {
97
- "type": "text",
98
- "text": f"{instruction}\n\nAnalyze this image (Image {i+1}/{len(images)}). 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."
99
- },
100
- {
101
- "type": "image_url",
102
- "image_url": {
103
- "url": image_data_url
104
- }
105
- }
106
- ]
107
- }
108
- ]
109
- completion = client.chat.completions.create(
110
- model="llama-3.2-90b-vision-preview",
111
- messages=messages,
112
- temperature=0.7,
113
- max_tokens=1000,
114
- top_p=1,
115
- stream=False,
116
- stop=None
117
- )
118
- result = completion.choices[0].message.content
119
- results.append((f"Image {i+1} analysis", result))
120
 
121
- if video:
122
- frames = extract_frames_from_video(video)
123
- for i, frame in enumerate(frames):
124
- image_data_url = f"data:image/png;base64,{encode_image(frame)}"
125
- messages = [
126
- {
127
- "role": "user",
128
- "content": [
129
- {
130
- "type": "text",
131
- "text": f"{instruction}\n\nAnalyze this frame from a video (Frame {i+1}/{len(frames)}). First, explain the video and then 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."
132
- },
133
- {
134
- "type": "image_url",
135
- "image_url": {
136
- "url": image_data_url
137
- }
138
  }
139
- ]
140
- }
141
- ]
142
- completion = client.chat.completions.create(
143
- model="llama-3.2-90b-vision-preview",
144
- messages=messages,
145
- temperature=0.7,
146
- max_tokens=1000,
147
- top_p=1,
148
- stream=False,
149
- stop=None
150
- )
151
- result = completion.choices[0].message.content
152
- results.append((f"Video frame {i+1} analysis", result))
 
153
 
154
  logger.info("Analysis completed successfully")
155
  return results
@@ -268,22 +248,19 @@ with gr.Blocks(css=custom_css, theme=gr.themes.Soft()) as iface:
268
  """
269
  )
270
 
271
- # First row: Upload Image and Video side by side
272
  with gr.Row():
273
- with gr.Column(scale=1):
274
- image_input = gr.File(label="Upload Construction Site Images", file_count="multiple", type="filepath", elem_classes="image-container")
275
- with gr.Column(scale=1):
276
- video_input = gr.Video(label="Upload Construction Site Video", elem_classes="image-container")
277
 
278
- # Second row: Analyze Safety Hazards Button
279
  with gr.Row():
280
  analyze_button = gr.Button("🔍 Analyze Safety Hazards", elem_classes="analyze-button")
281
 
282
- # Third row: Chat Interface (Safety Analysis Results)
283
  with gr.Row():
284
  chatbot = gr.Chatbot(label="Safety Analysis Results and Expert Chat", elem_classes="chatbot")
285
 
286
- # Fourth row: Question Bar
287
  with gr.Row():
288
  msg = gr.Textbox(
289
  label="Ask about safety measures or regulations",
@@ -292,15 +269,14 @@ with gr.Blocks(css=custom_css, theme=gr.themes.Soft()) as iface:
292
  elem_classes="chat-input"
293
  )
294
 
295
- # Fifth row: Clear Chat and Download Report Buttons
296
  with gr.Row():
297
  clear = gr.Button("🗑️ Clear Chat", elem_classes="clear-button")
298
  download_button = gr.Button("📥 Download Report", elem_classes="download-button")
299
 
300
- # Add a file component to handle the download
301
  report_file = gr.File(label="Download Safety Analysis Report")
302
 
303
-
304
  def update_chat(history, new_messages):
305
  history = history or []
306
  for title, content in new_messages:
@@ -308,8 +284,8 @@ with gr.Blocks(css=custom_css, theme=gr.themes.Soft()) as iface:
308
  return history
309
 
310
  analyze_button.click(
311
- analyze_construction_image,
312
- inputs=[image_input, video_input],
313
  outputs=[chatbot],
314
  postprocess=lambda x: update_chat(chatbot.value, x)
315
  )
@@ -317,7 +293,6 @@ with gr.Blocks(css=custom_css, theme=gr.themes.Soft()) as iface:
317
  msg.submit(chat_about_image, [msg, chatbot], [msg, chatbot])
318
  clear.click(lambda: None, None, chatbot, queue=False)
319
 
320
-
321
  download_button.click(
322
  download_report,
323
  inputs=[chatbot],
@@ -337,4 +312,4 @@ if __name__ == "__main__":
337
  except Exception as e:
338
  logger.error(f"Error when trying to launch the interface: {str(e)}")
339
  logger.error(traceback.format_exc())
340
- print("Failed to launch the Gradio interface. Please check the logs for more information.")
 
70
  cap.release()
71
  return frames
72
 
73
+ def analyze_construction_media(media):
74
+ if not media:
75
+ logger.warning("No media provided")
76
  return [("No input", "Error: Please upload images or a video for analysis.")]
77
 
78
  try:
 
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(media):
88
+ file_type = file.name.split('.')[-1].lower()
89
+
90
+ if file_type in ['jpg', 'jpeg', 'png', 'gif']:
91
+ # Handle image
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
+ content_type = "image"
96
+ elif file_type in ['mp4', 'avi', 'mov', 'wmv']:
97
+ # Handle video
98
+ frames = extract_frames_from_video(file.name)
99
+ image_data_url = f"data:image/png;base64,{encode_image(frames[0])}" # Use first frame
100
+ content_type = "video"
101
+ else:
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
+ "type": "text",
111
+ "text": f"{instruction}\n\nAnalyze this {content_type} (File {i+1}/{len(media)}). 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."
112
+ },
113
+ {
114
+ "type": "image_url",
115
+ "image_url": {
116
+ "url": image_data_url
 
 
 
 
 
117
  }
118
+ }
119
+ ]
120
+ }
121
+ ]
122
+ completion = client.chat.completions.create(
123
+ model="llama-3.2-90b-vision-preview",
124
+ messages=messages,
125
+ temperature=0.7,
126
+ max_tokens=1000,
127
+ top_p=1,
128
+ stream=False,
129
+ stop=None
130
+ )
131
+ result = completion.choices[0].message.content
132
+ results.append((f"File {i+1} analysis", result))
133
 
134
  logger.info("Analysis completed successfully")
135
  return results
 
248
  """
249
  )
250
 
251
+ # Combined upload for images and videos
252
  with gr.Row():
253
+ media_input = gr.File(label="Upload Construction Site Images or Videos", file_count="multiple", type="filepath", elem_classes="image-container")
 
 
 
254
 
255
+ # Analyze Safety Hazards Button
256
  with gr.Row():
257
  analyze_button = gr.Button("🔍 Analyze Safety Hazards", elem_classes="analyze-button")
258
 
259
+ # Chat Interface (Safety Analysis Results)
260
  with gr.Row():
261
  chatbot = gr.Chatbot(label="Safety Analysis Results and Expert Chat", elem_classes="chatbot")
262
 
263
+ # Question Bar
264
  with gr.Row():
265
  msg = gr.Textbox(
266
  label="Ask about safety measures or regulations",
 
269
  elem_classes="chat-input"
270
  )
271
 
272
+ # Clear Chat and Download Report Buttons
273
  with gr.Row():
274
  clear = gr.Button("🗑️ Clear Chat", elem_classes="clear-button")
275
  download_button = gr.Button("📥 Download Report", elem_classes="download-button")
276
 
277
+ # File component to handle the download
278
  report_file = gr.File(label="Download Safety Analysis Report")
279
 
 
280
  def update_chat(history, new_messages):
281
  history = history or []
282
  for title, content in new_messages:
 
284
  return history
285
 
286
  analyze_button.click(
287
+ analyze_construction_media,
288
+ inputs=[media_input],
289
  outputs=[chatbot],
290
  postprocess=lambda x: update_chat(chatbot.value, x)
291
  )
 
293
  msg.submit(chat_about_image, [msg, chatbot], [msg, chatbot])
294
  clear.click(lambda: None, None, chatbot, queue=False)
295
 
 
296
  download_button.click(
297
  download_report,
298
  inputs=[chatbot],
 
312
  except Exception as e:
313
  logger.error(f"Error when trying to launch the interface: {str(e)}")
314
  logger.error(traceback.format_exc())
315
+ print("Failed to launch the Gradio interface. Please check the logs for more information.")