capradeepgujaran commited on
Commit
d4ad3f6
1 Parent(s): a393b01

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +10 -8
app.py CHANGED
@@ -84,16 +84,18 @@ def analyze_mixed_input(input_files):
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_path in enumerate(input_files):
88
- file_type = file_path.split('.')[-1].lower()
89
  if file_type in ['jpg', 'jpeg', 'png', 'bmp']:
90
  # Process image
91
- image_data = encode_image(file_path)
 
 
92
  content_type = "image"
93
  elif file_type in ['mp4', 'avi', 'mov', 'webm']:
94
  # Process video
95
- frames = extract_frames_from_video(file_path)
96
- image_data = encode_image(frames[0]) # Use the first frame
97
  content_type = "video"
98
  else:
99
  results.append((f"File {i+1} analysis", f"Unsupported file type: {file_type}"))
@@ -110,7 +112,7 @@ def analyze_mixed_input(input_files):
110
  {
111
  "type": "image_url",
112
  "image_url": {
113
- "url": f"data:image/jpeg;base64,{image_data}"
114
  }
115
  }
116
  ]
@@ -132,7 +134,7 @@ def analyze_mixed_input(input_files):
132
  # If it's a video, analyze additional frames
133
  if content_type == "video" and len(frames) > 1:
134
  for j, frame in enumerate(frames[1:], start=2):
135
- frame_data = encode_image(frame)
136
  messages = [
137
  {
138
  "role": "user",
@@ -144,7 +146,7 @@ def analyze_mixed_input(input_files):
144
  {
145
  "type": "image_url",
146
  "image_url": {
147
- "url": f"data:image/jpeg;base64,{frame_data}"
148
  }
149
  }
150
  ]
 
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}"))
 
112
  {
113
  "type": "image_url",
114
  "image_url": {
115
+ "url": image_data_url
116
  }
117
  }
118
  ]
 
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",
 
146
  {
147
  "type": "image_url",
148
  "image_url": {
149
+ "url": frame_data_url
150
  }
151
  }
152
  ]