neerajkalyank commited on
Commit
a2cd40e
·
verified ·
1 Parent(s): 8686aa9

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +63 -18
app.py CHANGED
@@ -31,8 +31,14 @@ except Exception as e:
31
  print(f"Salesforce connection failed: {str(e)}")
32
  raise
33
 
34
- # Valid milestones
35
  VALID_MILESTONES = ["Planning", "Foundation", "Walls Erected", "Completed"]
 
 
 
 
 
 
36
 
37
  # Adjust the timezone to your local timezone
38
  local_timezone = pytz.timezone("Asia/Kolkata")
@@ -42,9 +48,13 @@ def process_image(images, project_name):
42
  try:
43
  if not images or len(images) == 0:
44
  return "Error: Please upload at least one image to proceed.", "Pending", "", "", 0
 
 
45
 
46
  # Process each image
47
- milestones = []
 
 
48
  for image in images:
49
  img = Image.open(image)
50
  image_size_mb = os.path.getsize(image) / (1024 * 1024)
@@ -80,23 +90,44 @@ def process_image(images, project_name):
80
  except Exception as e:
81
  return f"Error: Failed to upload image to Salesforce - {str(e)}", "Failure", "", "", 0
82
 
83
- # Mock AI model to detect milestone based on image content
84
- img_bytes = img.tobytes()
85
- img_hash = int(hashlib.sha256(img_bytes).hexdigest(), 16)
86
- milestone_index = img_hash % len(VALID_MILESTONES)
87
- milestone = VALID_MILESTONES[milestone_index]
88
 
89
- # Adjust milestone detection based on internal/external features
90
- if milestone == "Walls Erected" and any("interior" in img_text.lower() for img_text in [os.path.basename(image)] if img_text):
91
- if any(keyword in str(os.path.basename(image)).lower() for keyword in ["electrical", "plumbing", "wiring", "pipes"]):
92
- milestone = "Completed"
93
- else:
94
- milestone = "Walls Erected"
 
 
 
 
 
95
 
96
- milestones.append(milestone)
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
97
 
98
- # Determine overall milestone (most advanced detected)
99
- final_milestone = max(set(milestones), key=milestones.count) if milestones else "Planning"
100
  milestone_completion_map = {
101
  "Planning": 10,
102
  "Foundation": 30,
@@ -164,6 +195,9 @@ def process_image(images, project_name):
164
  completed_html = "".join([f'<li style="color: green;">✔ {task}</li>' for task in completed_tasks])
165
  not_completed_html = "".join([f'<li style="color: red;">✘ {task}</li>' for task in not_completed_tasks])
166
 
 
 
 
167
  result_html = f"""
168
  <div style="font-family: Arial, sans-serif; padding: 20px; background-color: #f9f9f9; border-radius: 10px;">
169
  <h2 style="color: #2c3e50; text-align: center;">Project Summary</h2>
@@ -179,6 +213,11 @@ def process_image(images, project_name):
179
  </div>
180
  </div>
181
 
 
 
 
 
 
182
  <h3 style="color: #2c3e50;">Milestone Timeline</h3>
183
  <div style="display: flex; justify-content: space-between; align-items: center; margin-bottom: 20px;">
184
  <span style="color: {'#2ecc71' if final_milestone == 'Planning' else '#bdc3c7'};">Planning</span>
@@ -264,7 +303,7 @@ with gr.Blocks(css="""
264
  background-color: #2ecc71;
265
  border-radius: 5px;
266
  }
267
- progress::-webkit-progress-bar {
268
  background-color: #ecf0f1;
269
  border-radius: 5px;
270
  }
@@ -279,6 +318,12 @@ with gr.Blocks(css="""
279
  }
280
  """) as demo:
281
  gr.Markdown("<h1 class='title'>Construction Progress Analyzer</h1>")
 
 
 
 
 
 
282
  with gr.Row():
283
  image_input = gr.Files(type="filepath", label="Upload Construction Site Photos (JPG/PNG, ≤ 20MB)")
284
  project_name_input = gr.Textbox(label="Project Name (Required)", placeholder="e.g. Project_12345")
@@ -295,4 +340,4 @@ with gr.Blocks(css="""
295
  outputs=[output_html, upload_status, milestone, progress]
296
  )
297
 
298
- demo.launch(share=True)
 
31
  print(f"Salesforce connection failed: {str(e)}")
32
  raise
33
 
34
+ # Valid milestones in sequential order
35
  VALID_MILESTONES = ["Planning", "Foundation", "Walls Erected", "Completed"]
36
+ MILESTONE_WEIGHTS = {
37
+ "Planning": 1,
38
+ "Foundation": 2,
39
+ "Walls Erected": 3,
40
+ "Completed": 4
41
+ }
42
 
43
  # Adjust the timezone to your local timezone
44
  local_timezone = pytz.timezone("Asia/Kolkata")
 
48
  try:
49
  if not images or len(images) == 0:
50
  return "Error: Please upload at least one image to proceed.", "Pending", "", "", 0
51
+ if len(images) < 2:
52
+ return "Error: Please upload at least one indoor and one outdoor image for accurate milestone detection.", "Pending", "", "", 0
53
 
54
  # Process each image
55
+ image_milestones = []
56
+ image_types = []
57
+
58
  for image in images:
59
  img = Image.open(image)
60
  image_size_mb = os.path.getsize(image) / (1024 * 1024)
 
90
  except Exception as e:
91
  return f"Error: Failed to upload image to Salesforce - {str(e)}", "Failure", "", "", 0
92
 
93
+ # Classify image as indoor or outdoor based on filename
94
+ filename_lower = os.path.basename(image).lower()
95
+ is_indoor = any(keyword in filename_lower for keyword in ["indoor", "interior", "inside"])
96
+ image_type = "Indoor" if is_indoor else "Outdoor"
97
+ image_types.append(image_type)
98
 
99
+ # Enhanced milestone detection logic
100
+ # Use filename keywords to simulate content analysis
101
+ milestone = "Planning" # Default
102
+ if any(keyword in filename_lower for keyword in ["site", "clearing", "planning", "design"]):
103
+ milestone = "Planning"
104
+ elif any(keyword in filename_lower for keyword in ["foundation", "footing", "slab", "excavation"]):
105
+ milestone = "Foundation"
106
+ elif any(keyword in filename_lower for keyword in ["wall", "structure", "beam", "column", "facade"]):
107
+ milestone = "Walls Erected"
108
+ elif any(keyword in filename_lower for keyword in ["electrical", "plumbing", "hvac", "finish", "completed"]):
109
+ milestone = "Completed"
110
 
111
+ # Adjust milestone based on image type
112
+ if image_type == "Indoor" and milestone in ["Planning", "Foundation"]:
113
+ milestone = "Walls Erected" # Indoor images imply at least walls are up
114
+ elif image_type == "Outdoor" and milestone == "Completed":
115
+ milestone = "Walls Erected" # Outdoor completion needs indoor confirmation
116
+
117
+ image_milestones.append(milestone)
118
+
119
+ # Validate and aggregate milestones
120
+ if not any(t == "Indoor" for t in image_types) or not any(t == "Outdoor" for t in image_types):
121
+ return "Error: Both indoor and outdoor images are required for accurate milestone detection.", "Pending", "", "", 0
122
+
123
+ # Ensure sequential milestone logic
124
+ max_milestone_index = max(MILESTONE_WEIGHTS[m] for m in image_milestones)
125
+ final_milestone = [m for m, w in MILESTONE_WEIGHTS.items() if w == max_milestone_index][0]
126
+
127
+ # If "Completed" is detected, ensure indoor images confirm it
128
+ if final_milestone == "Completed" and not any(m == "Completed" and t == "Indoor" for m, t in zip(image_milestones, image_types)):
129
+ final_milestone = "Walls Erected"
130
 
 
 
131
  milestone_completion_map = {
132
  "Planning": 10,
133
  "Foundation": 30,
 
195
  completed_html = "".join([f'<li style="color: green;">✔ {task}</li>' for task in completed_tasks])
196
  not_completed_html = "".join([f'<li style="color: red;">✘ {task}</li>' for task in not_completed_tasks])
197
 
198
+ # Enhanced result HTML with image type feedback
199
+ image_summary = "".join([f'<li>{os.path.basename(img)} ({img_type}): {milestone}</li>' for img, img_type, milestone in zip(images, image_types, image_milestones)])
200
+
201
  result_html = f"""
202
  <div style="font-family: Arial, sans-serif; padding: 20px; background-color: #f9f9f9; border-radius: 10px;">
203
  <h2 style="color: #2c3e50; text-align: center;">Project Summary</h2>
 
213
  </div>
214
  </div>
215
 
216
+ <h3 style="color: #2c3e50;">Image Analysis</h3>
217
+ <ul style="padding-left: 20px; margin-bottom: 20px;">
218
+ {image_summary}
219
+ </ul>
220
+
221
  <h3 style="color: #2c3e50;">Milestone Timeline</h3>
222
  <div style="display: flex; justify-content: space-between; align-items: center; margin-bottom: 20px;">
223
  <span style="color: {'#2ecc71' if final_milestone == 'Planning' else '#bdc3c7'};">Planning</span>
 
303
  background-color: #2ecc71;
304
  border-radius: 5px;
305
  }
306
+ .gradio-container progress::-webkit-progress-bar {
307
  background-color: #ecf0f1;
308
  border-radius: 5px;
309
  }
 
318
  }
319
  """) as demo:
320
  gr.Markdown("<h1 class='title'>Construction Progress Analyzer</h1>")
321
+ gr.Markdown("""
322
+ <p style='text-align: center; color: #34495e;'>
323
+ Upload at least one indoor and one outdoor image of the construction site (JPG/PNG, ≤ 20MB each).<br>
324
+ Use descriptive filenames (e.g., 'outdoor_foundation.jpg', 'indoor_electrical.jpg') for best results.
325
+ </p>
326
+ """)
327
  with gr.Row():
328
  image_input = gr.Files(type="filepath", label="Upload Construction Site Photos (JPG/PNG, ≤ 20MB)")
329
  project_name_input = gr.Textbox(label="Project Name (Required)", placeholder="e.g. Project_12345")
 
340
  outputs=[output_html, upload_status, milestone, progress]
341
  )
342
 
343
+ demo.launch(share=True)