capradeepgujaran commited on
Commit
5b19aee
β€’
1 Parent(s): d4ad3f6

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +104 -106
app.py CHANGED
@@ -70,106 +70,101 @@ 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_mixed_input(input_files):
74
- if not input_files:
75
- logger.warning("No input files provided")
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. "
83
- "Your task is first to explain what you see in the image and determine if the image shows a construction site. "
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(input_files):
88
- file_type = file.name.split('.')[-1].lower()
89
- if file_type in ['jpg', 'jpeg', 'png', 'bmp']:
90
- # Process image
91
- image = Image.open(file.name)
92
- resized_image = resize_image(image)
93
- image_data_url = f"data:image/png;base64,{encode_image(resized_image)}"
94
- content_type = "image"
95
- elif file_type in ['mp4', 'avi', 'mov', 'webm']:
96
- # Process video
97
- frames = extract_frames_from_video(file.name)
98
- image_data_url = f"data:image/png;base64,{encode_image(Image.fromarray(frames[0]))}" # Use the first frame
99
- content_type = "video"
100
- else:
101
- results.append((f"File {i+1} analysis", f"Unsupported file type: {file_type}"))
102
- continue
103
 
104
- messages = [
 
 
 
105
  {
106
- "role": "user",
107
- "content": [
108
- {
109
- "type": "text",
110
- "text": f"{instruction}\n\nAnalyze this {content_type} (File {i+1}/{len(input_files)}). 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."
111
- },
112
- {
113
- "type": "image_url",
114
- "image_url": {
115
- "url": image_data_url
116
- }
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 ({content_type})", result))
 
 
 
 
 
 
 
 
 
 
 
133
 
134
- # If it's a video, analyze additional frames
135
- if content_type == "video" and len(frames) > 1:
136
- for j, frame in enumerate(frames[1:], start=2):
137
- frame_data_url = f"data:image/png;base64,{encode_image(Image.fromarray(frame))}"
138
- messages = [
139
- {
140
- "role": "user",
141
- "content": [
142
- {
143
- "type": "text",
144
- "text": f"{instruction}\n\nAnalyze this additional frame from the video (File {i+1}, Frame {j}/{len(frames)}). Focus on any new or changed safety aspects compared to the previous frame."
145
- },
146
- {
147
- "type": "image_url",
148
- "image_url": {
149
- "url": frame_data_url
150
- }
151
- }
152
- ]
153
  }
154
- ]
155
- completion = client.chat.completions.create(
156
- model="llama-3.2-90b-vision-preview",
157
- messages=messages,
158
- temperature=0.7,
159
- max_tokens=1000,
160
- top_p=1,
161
- stream=False,
162
- stop=None
163
- )
164
- result = completion.choices[0].message.content
165
- results.append((f"File {i+1} analysis (video frame {j})", result))
 
 
 
 
166
 
167
- logger.info("Analysis completed successfully")
168
- return results
169
- except Exception as e:
170
- logger.error(f"Error during analysis: {str(e)}")
171
- logger.error(traceback.format_exc())
172
- return [("Analysis error", f"Error during analysis: {str(e)}")]
173
 
174
 
175
  def chat_about_image(message, chat_history):
@@ -281,24 +276,19 @@ with gr.Blocks(css=custom_css, theme=gr.themes.Soft()) as iface:
281
  """
282
  )
283
 
284
- # Single upload window for both images and videos
285
  with gr.Row():
286
- input_files = gr.File(
287
- label="Upload Construction Site Images or Videos",
288
- file_count="multiple",
289
- type="filepath", # Changed from "file" to "filepath"
290
- elem_classes="image-container"
291
- )
292
 
293
- # Analyze Safety Hazards Button
294
  with gr.Row():
295
  analyze_button = gr.Button("πŸ” Analyze Safety Hazards", elem_classes="analyze-button")
296
 
297
- # Chat Interface (Safety Analysis Results)
298
  with gr.Row():
299
  chatbot = gr.Chatbot(label="Safety Analysis Results and Expert Chat", elem_classes="chatbot")
300
 
301
- # Question Bar
302
  with gr.Row():
303
  msg = gr.Textbox(
304
  label="Ask about safety measures or regulations",
@@ -307,14 +297,21 @@ with gr.Blocks(css=custom_css, theme=gr.themes.Soft()) as iface:
307
  elem_classes="chat-input"
308
  )
309
 
310
- # Clear Chat and Download Report Buttons
311
  with gr.Row():
312
  clear = gr.Button("πŸ—‘οΈ Clear Chat", elem_classes="clear-button")
313
  download_button = gr.Button("πŸ“₯ Download Report", elem_classes="download-button")
314
 
315
- # File component to handle the download
316
  report_file = gr.File(label="Download Safety Analysis Report")
317
 
 
 
 
 
 
 
 
318
  def update_chat(history, new_messages):
319
  history = history or []
320
  for title, content in new_messages:
@@ -322,8 +319,8 @@ with gr.Blocks(css=custom_css, theme=gr.themes.Soft()) as iface:
322
  return history
323
 
324
  analyze_button.click(
325
- analyze_mixed_input,
326
- inputs=[input_files],
327
  outputs=[chatbot],
328
  postprocess=lambda x: update_chat(chatbot.value, x)
329
  )
@@ -343,10 +340,11 @@ with gr.Blocks(css=custom_css, theme=gr.themes.Soft()) as iface:
343
  """
344
  )
345
 
 
346
  if __name__ == "__main__":
347
  try:
348
  iface.launch(debug=True)
349
  except Exception as e:
350
  logger.error(f"Error when trying to launch the interface: {str(e)}")
351
  logger.error(traceback.format_exc())
352
- print("Failed to launch the Gradio interface. Please check the logs for more information.")
 
70
  cap.release()
71
  return frames
72
 
73
+ def analyze_file(file):
74
+ """Analyze a single file (image or video)"""
 
 
 
75
  try:
76
+ file_type = file.name.split('.')[-1].lower()
77
+ if file_type in ['jpg', 'jpeg', 'png', 'bmp']:
78
+ return analyze_image(file)
79
+ elif file_type in ['mp4', 'avi', 'mov', 'webm']:
80
+ return analyze_video(file)
81
+ else:
82
+ return "Unsupported file type. Please upload an image or video file."
83
+ except Exception as e:
84
+ logger.error(f"Error analyzing file: {str(e)}")
85
+ return f"Error analyzing file: {str(e)}"
86
 
87
+ def analyze_image(image_file):
88
+ image = Image.open(image_file.name)
89
+ resized_image = resize_image(image)
90
+ image_data_url = f"data:image/png;base64,{encode_image(resized_image)}"
91
+
92
+ instruction = ("You are an AI assistant specialized in analyzing images for safety issues. "
93
+ "Your task is first to explain what you see in the image and determine if the image shows a construction site. "
94
+ "If it does, identify any safety issues or hazards, categorize them, and provide a detailed description, "
95
+ "and suggest steps to resolve them. If it's not a construction site, simply state that")
 
 
 
 
 
 
 
96
 
97
+ messages = [
98
+ {
99
+ "role": "user",
100
+ "content": [
101
  {
102
+ "type": "text",
103
+ "text": f"{instruction}\n\nAnalyze this image. 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."
104
+ },
105
+ {
106
+ "type": "image_url",
107
+ "image_url": {
108
+ "url": image_data_url
109
+ }
 
 
 
 
 
110
  }
111
  ]
112
+ }
113
+ ]
114
+
115
+ completion = client.chat.completions.create(
116
+ model="llama-3.2-90b-vision-preview",
117
+ messages=messages,
118
+ temperature=0.7,
119
+ max_tokens=1000,
120
+ top_p=1,
121
+ stream=False,
122
+ stop=None
123
+ )
124
+
125
+ return completion.choices[0].message.content
126
+
127
+ def analyze_video(video_file):
128
+ frames = extract_frames_from_video(video_file.name)
129
+ results = []
130
+
131
+ instruction = ("You are an AI assistant specialized in analyzing images for safety issues. "
132
+ "Your task is first to explain what you see in the image and determine if the image shows a construction site. "
133
+ "If it does, identify any safety issues or hazards, categorize them, and provide a detailed description, "
134
+ "and suggest steps to resolve them. If it's not a construction site, simply state that")
135
 
136
+ for i, frame in enumerate(frames):
137
+ image_data_url = f"data:image/png;base64,{encode_image(frame)}"
138
+ messages = [
139
+ {
140
+ "role": "user",
141
+ "content": [
142
+ {
143
+ "type": "text",
144
+ "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."
145
+ },
146
+ {
147
+ "type": "image_url",
148
+ "image_url": {
149
+ "url": image_data_url
 
 
 
 
 
150
  }
151
+ }
152
+ ]
153
+ }
154
+ ]
155
+ completion = client.chat.completions.create(
156
+ model="llama-3.2-90b-vision-preview",
157
+ messages=messages,
158
+ temperature=0.7,
159
+ max_tokens=1000,
160
+ top_p=1,
161
+ stream=False,
162
+ stop=None
163
+ )
164
+ results.append(f"Frame {i+1} analysis:\n{completion.choices[0].message.content}\n\n")
165
+
166
+ return "\n".join(results)
167
 
 
 
 
 
 
 
168
 
169
 
170
  def chat_about_image(message, chat_history):
 
276
  """
277
  )
278
 
279
+ # First row: Combined file upload for images and videos
280
  with gr.Row():
281
+ file_input = gr.File(label="Upload Construction Site Images or Videos", file_count="multiple", type="file", elem_classes="file-container")
 
 
 
 
 
282
 
283
+ # Second row: Analyze Safety Hazards Button
284
  with gr.Row():
285
  analyze_button = gr.Button("πŸ” Analyze Safety Hazards", elem_classes="analyze-button")
286
 
287
+ # Third row: Chat Interface (Safety Analysis Results)
288
  with gr.Row():
289
  chatbot = gr.Chatbot(label="Safety Analysis Results and Expert Chat", elem_classes="chatbot")
290
 
291
+ # Fourth row: Question Bar
292
  with gr.Row():
293
  msg = gr.Textbox(
294
  label="Ask about safety measures or regulations",
 
297
  elem_classes="chat-input"
298
  )
299
 
300
+ # Fifth row: Clear Chat and Download Report Buttons
301
  with gr.Row():
302
  clear = gr.Button("πŸ—‘οΈ Clear Chat", elem_classes="clear-button")
303
  download_button = gr.Button("πŸ“₯ Download Report", elem_classes="download-button")
304
 
305
+ # Add a file component to handle the download
306
  report_file = gr.File(label="Download Safety Analysis Report")
307
 
308
+ def process_files(files):
309
+ results = []
310
+ for file in files:
311
+ result = analyze_file(file)
312
+ results.append((file.name, result))
313
+ return results
314
+
315
  def update_chat(history, new_messages):
316
  history = history or []
317
  for title, content in new_messages:
 
319
  return history
320
 
321
  analyze_button.click(
322
+ process_files,
323
+ inputs=[file_input],
324
  outputs=[chatbot],
325
  postprocess=lambda x: update_chat(chatbot.value, x)
326
  )
 
340
  """
341
  )
342
 
343
+ # Launch the app
344
  if __name__ == "__main__":
345
  try:
346
  iface.launch(debug=True)
347
  except Exception as e:
348
  logger.error(f"Error when trying to launch the interface: {str(e)}")
349
  logger.error(traceback.format_exc())
350
+ print("Failed to launch the Gradio interface. Please check the logs for more information.")