chandra7799 commited on
Commit
6c6a1e4
·
verified ·
1 Parent(s): c26aa17

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +86 -33
app.py CHANGED
@@ -34,7 +34,7 @@ except Exception as e:
34
  # Valid milestones
35
  VALID_MILESTONES = ["Foundation", "Walls Erected", "Planning", "Completed"]
36
 
37
- # Adjust the timezone to your local timezone (replace 'Asia/Kolkata' with your timezone if needed)
38
  local_timezone = pytz.timezone("Asia/Kolkata")
39
 
40
  # Deterministic AI prediction with fixed confidence and percent
@@ -98,8 +98,6 @@ def process_image(image, project_name):
98
  try:
99
  content_version_result = sf.ContentVersion.create(content_version)
100
  content_version_id = content_version_result['id']
101
-
102
- # Generate the public URL for the uploaded file in Salesforce
103
  file_url = f"https://sathkruthatechsolutionspri8-dev-ed.develop.lightning.force.com/{content_version_id}"
104
  except Exception as e:
105
  return f"Error: Failed to upload image to Salesforce - {str(e)}", "Failure", "", "", 0
@@ -161,55 +159,88 @@ def process_image(image, project_name):
161
  }
162
  }
163
 
164
- # Get the detailed completion information based on the detected milestone
165
- completed_work = "\n".join([f"{idx+1}. {task}" for idx, task in enumerate(completion_details[milestone]["completed"])])
166
- not_completed_work = "\n".join([f"{idx+1}. {task}" for idx, task in enumerate(completion_details[milestone]["not_completed"])])
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
167
 
168
  # Adjust the current time to local timezone with proper ISO 8601 format
169
  now = datetime.now(local_timezone)
170
- # Format the datetime with a colon in the timezone offset (e.g., +05:30)
171
  local_time = now.strftime("%Y-%m-%dT%H:%M:%S") + now.strftime("%z")[:-2] + ":" + now.strftime("%z")[-2:]
172
 
173
- # Create the Salesforce record with the image URL and AI prediction
174
  record = {
175
  "Name__c": project_name,
176
  "Current_Milestone__c": milestone,
177
  "Completion_Percentage__c": percent_complete,
178
- "Last_Updated_On__c": local_time, # Corrected format for Salesforce
179
  "Upload_Status__c": "Success",
180
- "Comments__c": f"{milestone} with {confidence_score*100}% confidence", # Removed "AI Prediction:"
181
  "Last_Updated_Image__c": file_url
182
  }
183
 
184
- # Upload the record to Salesforce
185
  try:
186
  sf.Construction__c.create(record)
187
  except Exception as e:
188
  return f"Error: Failed to update Salesforce - {str(e)}", "Failure", "", "", 0
189
 
190
- # Return the detailed result in the desired format
191
- result = f"""
192
- Completed:
193
-
194
- {completed_work}
195
-
196
- Not Completed:
197
-
198
- {not_completed_work}
199
-
200
- Confidence Score: {confidence_score * 100}%
201
- """
202
-
203
- return result, "Success", milestone, f"Confidence Score: {confidence_score}", f"{percent_complete}%"
204
 
205
  except Exception as e:
206
  return f"Error: {str(e)}", "Failure", "", "", "0%"
207
 
208
- # Gradio UI with added styling
209
  with gr.Blocks(css="""
210
  .gradio-container {
211
  background-color: #f0f4f8;
212
- font-family: Arial;
213
  }
214
  .title {
215
  color: #2c3e50;
@@ -229,19 +260,41 @@ with gr.Blocks(css="""
229
  .gradio-container .button {
230
  display: block;
231
  margin: 0 auto;
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
232
  }
233
- .result-textbox {
234
- white-space: pre-wrap !important;
235
- text-align: left !important;
236
  }
237
  """) as demo:
238
- gr.Markdown("<h1 class='title'></h1>")
239
  with gr.Row():
240
  image_input = gr.Image(type="filepath", label="Upload Construction Site Photo (JPG/PNG, ≤ 20MB)")
241
  project_name_input = gr.Textbox(label="Project Name (Required)", placeholder="e.g. Project_12345")
242
 
243
  submit_button = gr.Button("Process Image")
244
- output_text = gr.Textbox(label="Result", elem_classes="result-textbox")
245
  upload_status = gr.Textbox(label="Upload Status")
246
  milestone = gr.Textbox(label="Detected Milestone")
247
  confidence = gr.Textbox(label="Confidence Score")
@@ -250,7 +303,7 @@ with gr.Blocks(css="""
250
  submit_button.click(
251
  fn=process_image,
252
  inputs=[image_input, project_name_input],
253
- outputs=[output_text, upload_status, milestone, confidence, progress]
254
  )
255
 
256
  demo.launch(share=True)
 
34
  # Valid milestones
35
  VALID_MILESTONES = ["Foundation", "Walls Erected", "Planning", "Completed"]
36
 
37
+ # Adjust the timezone to your local timezone
38
  local_timezone = pytz.timezone("Asia/Kolkata")
39
 
40
  # Deterministic AI prediction with fixed confidence and percent
 
98
  try:
99
  content_version_result = sf.ContentVersion.create(content_version)
100
  content_version_id = content_version_result['id']
 
 
101
  file_url = f"https://sathkruthatechsolutionspri8-dev-ed.develop.lightning.force.com/{content_version_id}"
102
  except Exception as e:
103
  return f"Error: Failed to upload image to Salesforce - {str(e)}", "Failure", "", "", 0
 
159
  }
160
  }
161
 
162
+ # Format the completed and not completed tasks as HTML for collapsible sections
163
+ completed_tasks = completion_details[milestone]["completed"]
164
+ not_completed_tasks = completion_details[milestone]["not_completed"]
165
+
166
+ completed_html = "".join([f'<li style="color: green;">✔ {task}</li>' for task in completed_tasks])
167
+ not_completed_html = "".join([f'<li style="color: red;">✘ {task}</li>' for task in not_completed_tasks])
168
+
169
+ # Create HTML for the output with collapsible sections and progress bar
170
+ result_html = f"""
171
+ <div style="font-family: Arial, sans-serif; padding: 20px; background-color: #f9f9f9; border-radius: 10px;">
172
+ <h2 style="color: #2c3e50; text-align: center;">Project Summary</h2>
173
+ <div style="display: flex; justify-content: space-around; margin-bottom: 20px;">
174
+ <div style="text-align: center;">
175
+ <h3 style="color: #34495e;">Detected Milestone</h3>
176
+ <p style="font-size: 18px; font-weight: bold;">{milestone}</p>
177
+ </div>
178
+ <div style="text-align: center;">
179
+ <h3 style="color: #34495e;">Completion</h3>
180
+ <progress value="{percent_complete}" max="100" style="width: 200px; height: 20px;"></progress>
181
+ <p>{percent_complete}%</p>
182
+ </div>
183
+ <div style="text-align: center;">
184
+ <h3 style="color: #34495e;">Confidence Score</h3>
185
+ <p style="font-size: 18px; font-weight: bold;">{confidence_score * 100}%</p>
186
+ <small>The AI is {confidence_score * 100}% confident that this image represents the {milestone} stage.</small>
187
+ </div>
188
+ </div>
189
+
190
+ <h3 style="color: #2c3e50;">Milestone Timeline</h3>
191
+ <div style="display: flex; justify-content: space-between; align-items: center; margin-bottom: 20px;">
192
+ <span style="color: {'#2ecc71' if milestone == 'Planning' else '#bdc3c7'};">Planning</span>
193
+ <span style="color: {'#2ecc71' if milestone == 'Foundation' else '#bdc3c7'};">Foundation</span>
194
+ <span style="color: {'#2ecc71' if milestone == 'Walls Erected' else '#bdc3c7'};">Walls Erected</span>
195
+ <span style="color: {'#2ecc71' if milestone == 'Completed' else '#bdc3c7'};">Completed</span>
196
+ </div>
197
+
198
+ <details style="margin-bottom: 20px;">
199
+ <summary style="color: #2c3e50; font-weight: bold;">Completed Tasks</summary>
200
+ <ul style="padding-left: 20px;">
201
+ {completed_html}
202
+ </ul>
203
+ </details>
204
+
205
+ <details style="margin-bottom: 20px;">
206
+ <summary style="color: #2c3e50; font-weight: bold;">Not Completed Tasks</summary>
207
+ <ul style="padding-left: 20px;">
208
+ {not_completed_html}
209
+ </ul>
210
+ </details>
211
+ </div>
212
+ """
213
 
214
  # Adjust the current time to local timezone with proper ISO 8601 format
215
  now = datetime.now(local_timezone)
 
216
  local_time = now.strftime("%Y-%m-%dT%H:%M:%S") + now.strftime("%z")[:-2] + ":" + now.strftime("%z")[-2:]
217
 
218
+ # Create the Salesforce record
219
  record = {
220
  "Name__c": project_name,
221
  "Current_Milestone__c": milestone,
222
  "Completion_Percentage__c": percent_complete,
223
+ "Last_Updated_On__c": local_time,
224
  "Upload_Status__c": "Success",
225
+ "Comments__c": f"{milestone} with {confidence_score*100}% confidence",
226
  "Last_Updated_Image__c": file_url
227
  }
228
 
 
229
  try:
230
  sf.Construction__c.create(record)
231
  except Exception as e:
232
  return f"Error: Failed to update Salesforce - {str(e)}", "Failure", "", "", 0
233
 
234
+ return result_html, "Success", milestone, f"Confidence Score: {confidence_score}", f"{percent_complete}%"
 
 
 
 
 
 
 
 
 
 
 
 
 
235
 
236
  except Exception as e:
237
  return f"Error: {str(e)}", "Failure", "", "", "0%"
238
 
239
+ # Gradio UI with enhanced styling
240
  with gr.Blocks(css="""
241
  .gradio-container {
242
  background-color: #f0f4f8;
243
+ font-family: Arial, sans-serif;
244
  }
245
  .title {
246
  color: #2c3e50;
 
260
  .gradio-container .button {
261
  display: block;
262
  margin: 0 auto;
263
+ background-color: #3498db;
264
+ color: white;
265
+ border: none;
266
+ padding: 10px 20px;
267
+ border-radius: 5px;
268
+ cursor: pointer;
269
+ }
270
+ .gradio-container .button:hover {
271
+ background-color: #2980b9;
272
+ }
273
+ progress::-webkit-progress-value {
274
+ background-color: #2ecc71;
275
+ border-radius: 5px;
276
+ }
277
+ progress::-webkit-progress-bar {
278
+ background-color: #ecf0f1;
279
+ border-radius: 5px;
280
+ }
281
+ details summary {
282
+ cursor: pointer;
283
+ padding: 10px;
284
+ background-color: #ecf0f1;
285
+ border-radius: 5px;
286
  }
287
+ details ul {
288
+ margin-top: 10px;
 
289
  }
290
  """) as demo:
291
+ gr.Markdown("<h1 class='title'>Construction Progress Analyzer</h1>")
292
  with gr.Row():
293
  image_input = gr.Image(type="filepath", label="Upload Construction Site Photo (JPG/PNG, ≤ 20MB)")
294
  project_name_input = gr.Textbox(label="Project Name (Required)", placeholder="e.g. Project_12345")
295
 
296
  submit_button = gr.Button("Process Image")
297
+ output_html = gr.HTML(label="Result") # Changed to HTML for richer output
298
  upload_status = gr.Textbox(label="Upload Status")
299
  milestone = gr.Textbox(label="Detected Milestone")
300
  confidence = gr.Textbox(label="Confidence Score")
 
303
  submit_button.click(
304
  fn=process_image,
305
  inputs=[image_input, project_name_input],
306
+ outputs=[output_html, upload_status, milestone, confidence, progress]
307
  )
308
 
309
  demo.launch(share=True)