Rekham1110 commited on
Commit
afe6823
·
verified ·
1 Parent(s): d3d494a

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +16 -20
app.py CHANGED
@@ -68,7 +68,7 @@ def analyze_image_with_hf(image_path):
68
  def process_image(images, project_name):
69
  try:
70
  if not images or len(images) == 0:
71
- return "Error: Please upload at least one image to proceed.", "Pending", "", "", 0
72
 
73
  results_html = []
74
  upload_statuses = []
@@ -81,14 +81,14 @@ def process_image(images, project_name):
81
  img = Image.open(image)
82
  except Exception as e:
83
  if "cannot identify image file" in str(e):
84
- return f"Error: Unsupported image format '{os.path.basename(image)}'. Please use JPG, JPEG, PNG, or convert to a supported format.", "Failure", "", "", 0
85
- return f"Error: Failed to open image '{os.path.basename(image)}' - {str(e)}", "Failure", "", "", 0
86
 
87
  image_size_mb = os.path.getsize(image) / (1024 * 1024)
88
  if image_size_mb > 20:
89
- return "Error: One or more images exceed 20MB.", "Failure", "", "", 0
90
  if not str(image).lower().endswith(('.jpg', '.jpeg', '.png', '.avif')):
91
- return "Error: Only JPG, JPEG, PNG, or AVIF images are supported.", "Failure", "", "", 0
92
 
93
  upload_dir = "public_uploads"
94
  os.makedirs(upload_dir, exist_ok=True)
@@ -114,7 +114,7 @@ def process_image(images, project_name):
114
  if "STORAGE_LIMIT_EXCEEDED" in str(e):
115
  file_url = "Upload skipped due to storage limit"
116
  else:
117
- return f"Error: Failed to upload image to Salesforce - {str(e)}", "Failure", "", "", 0
118
 
119
  detected_elements = analyze_image_with_hf(image)
120
 
@@ -184,14 +184,10 @@ def process_image(images, project_name):
184
  completed_html = "".join([f'<li style="color: green;">✔ {task}</li>' for task in completed_tasks])
185
  not_completed_html = "".join([f'<li style="color: red;">✘ {task}</li>' for task in not_completed_tasks])
186
 
187
- # Add image preview to the HTML output
188
- image_preview_html = f'<img src="file://{saved_image_path}" style="max-width: 100%; height: auto; margin-top: 10px;" alt="Uploaded Image">'
189
-
190
  if final_milestone == "Completed":
191
  result_html = f"""
192
  <div style="font-family: Arial, sans-serif; padding: 20px; background-color: #f9f9f9; border-radius: 10px; margin-bottom: 20px;">
193
  <h2 style="color: #2c3e50; text-align: center;">Project Summary - {os.path.basename(image)}</h2>
194
- {image_preview_html}
195
  <div style="display: flex; justify-content: space-around; margin-bottom: 20px;">
196
  <div style="text-align: center;">
197
  <h3 style="color: #34495e;">Detected Milestone</h3>
@@ -217,14 +213,13 @@ def process_image(images, project_name):
217
  {completed_html}
218
  </ul>
219
  </details>
220
- <p style="color: green;">Project is fully completed as of 02:16 PM IST, June 20, 2025.</p>
221
  </div>
222
  """
223
  else:
224
  result_html = f"""
225
  <div style="font-family: Arial, sans-serif; padding: 20px; background-color: #f9f9f9; border-radius: 10px; margin-bottom: 20px;">
226
  <h2 style="color: #2c3e50; text-align: center;">Project Summary - {os.path.basename(image)}</h2>
227
- {image_preview_html}
228
  <div style="display: flex; justify-content: space-around; margin-bottom: 20px;">
229
  <div style="text-align: center;">
230
  <h3 style="color: #34495e;">Detected Milestone</h3>
@@ -256,7 +251,7 @@ def process_image(images, project_name):
256
  {not_completed_html}
257
  </ul>
258
  </details>
259
- <p style="color: orange;">Construction is in progress at {final_milestone} stage as of 02:16 PM IST, June 20, 2025.</p>
260
  </div>
261
  """
262
 
@@ -267,8 +262,8 @@ def process_image(images, project_name):
267
 
268
  combined_html = "<div>" + "".join(results_html) + "</div>"
269
 
270
- # Set current time to 02:16 PM IST, June 20, 2025
271
- now = local_timezone.localize(datetime(2025, 6, 20, 14, 16))
272
  local_time = now.strftime("%Y-%m-%dT%H:%M:%S") + now.strftime("%z")[:-2] + ":" + now.strftime("%z")[-2:]
273
 
274
  record = {
@@ -285,14 +280,14 @@ def process_image(images, project_name):
285
  if file_url and file_url != "Upload skipped due to storage limit":
286
  sf.Construction__c.create(record)
287
  except Exception as e:
288
- return f"Error: Failed to update Salesforce - {str(e)}", "Failure", "", "", 0
289
 
290
- return combined_html, ",".join(upload_statuses), ",".join(milestones), "", ",".join(progresses)
291
 
292
  except Exception as e:
293
- return f"Error: {str(e)}", "Failure", "", "", "0%"
294
 
295
- # Gradio UI with enhanced styling
296
  with gr.Blocks(css="""
297
  .gradio-container {
298
  background-color: #f0f4f8;
@@ -351,6 +346,7 @@ with gr.Blocks(css="""
351
 
352
  submit_button = gr.Button("Process Image")
353
  output_html = gr.HTML(label="Result")
 
354
  upload_status = gr.Textbox(label="Upload Status")
355
  milestone = gr.Textbox(label="Detected Milestone")
356
  confidence = gr.Textbox(label="Confidence Score") # Kept for compatibility, but unused
@@ -359,7 +355,7 @@ with gr.Blocks(css="""
359
  submit_button.click(
360
  fn=process_image,
361
  inputs=[image_input, project_name_input],
362
- outputs=[output_html, upload_status, milestone, confidence, progress]
363
  )
364
 
365
  demo.launch(share=True)
 
68
  def process_image(images, project_name):
69
  try:
70
  if not images or len(images) == 0:
71
+ return "Error: Please upload at least one image to proceed.", None, "Pending", "", "", 0
72
 
73
  results_html = []
74
  upload_statuses = []
 
81
  img = Image.open(image)
82
  except Exception as e:
83
  if "cannot identify image file" in str(e):
84
+ return f"Error: Unsupported image format '{os.path.basename(image)}'. Please use JPG, JPEG, PNG, or convert to a supported format.", None, "Failure", "", "", 0
85
+ return f"Error: Failed to open image '{os.path.basename(image)}' - {str(e)}", None, "Failure", "", "", 0
86
 
87
  image_size_mb = os.path.getsize(image) / (1024 * 1024)
88
  if image_size_mb > 20:
89
+ return "Error: One or more images exceed 20MB.", None, "Failure", "", "", 0
90
  if not str(image).lower().endswith(('.jpg', '.jpeg', '.png', '.avif')):
91
+ return "Error: Only JPG, JPEG, PNG, or AVIF images are supported.", None, "Failure", "", "", 0
92
 
93
  upload_dir = "public_uploads"
94
  os.makedirs(upload_dir, exist_ok=True)
 
114
  if "STORAGE_LIMIT_EXCEEDED" in str(e):
115
  file_url = "Upload skipped due to storage limit"
116
  else:
117
+ return f"Error: Failed to upload image to Salesforce - {str(e)}", None, "Failure", "", "", 0
118
 
119
  detected_elements = analyze_image_with_hf(image)
120
 
 
184
  completed_html = "".join([f'<li style="color: green;">✔ {task}</li>' for task in completed_tasks])
185
  not_completed_html = "".join([f'<li style="color: red;">✘ {task}</li>' for task in not_completed_tasks])
186
 
 
 
 
187
  if final_milestone == "Completed":
188
  result_html = f"""
189
  <div style="font-family: Arial, sans-serif; padding: 20px; background-color: #f9f9f9; border-radius: 10px; margin-bottom: 20px;">
190
  <h2 style="color: #2c3e50; text-align: center;">Project Summary - {os.path.basename(image)}</h2>
 
191
  <div style="display: flex; justify-content: space-around; margin-bottom: 20px;">
192
  <div style="text-align: center;">
193
  <h3 style="color: #34495e;">Detected Milestone</h3>
 
213
  {completed_html}
214
  </ul>
215
  </details>
216
+ <p style="color: green;">Project is fully completed as of 02:22 PM IST, June 20, 2025.</p>
217
  </div>
218
  """
219
  else:
220
  result_html = f"""
221
  <div style="font-family: Arial, sans-serif; padding: 20px; background-color: #f9f9f9; border-radius: 10px; margin-bottom: 20px;">
222
  <h2 style="color: #2c3e50; text-align: center;">Project Summary - {os.path.basename(image)}</h2>
 
223
  <div style="display: flex; justify-content: space-around; margin-bottom: 20px;">
224
  <div style="text-align: center;">
225
  <h3 style="color: #34495e;">Detected Milestone</h3>
 
251
  {not_completed_html}
252
  </ul>
253
  </details>
254
+ <p style="color: orange;">Construction is in progress at {final_milestone} stage as of 02:22 PM IST, June 20, 2025.</p>
255
  </div>
256
  """
257
 
 
262
 
263
  combined_html = "<div>" + "".join(results_html) + "</div>"
264
 
265
+ # Set current time to 02:22 PM IST, June 20, 2025
266
+ now = local_timezone.localize(datetime(2025, 6, 20, 14, 22))
267
  local_time = now.strftime("%Y-%m-%dT%H:%M:%S") + now.strftime("%z")[:-2] + ":" + now.strftime("%z")[-2:]
268
 
269
  record = {
 
280
  if file_url and file_url != "Upload skipped due to storage limit":
281
  sf.Construction__c.create(record)
282
  except Exception as e:
283
+ return f"Error: Failed to update Salesforce - {str(e)}", None, "Failure", "", "", 0
284
 
285
+ return combined_html, Image.open(saved_image_path) if images else None, ",".join(upload_statuses), ",".join(milestones), "", ",".join(progresses)
286
 
287
  except Exception as e:
288
+ return f"Error: {str(e)}", None, "Failure", "", "", "0%"
289
 
290
+ # Gradio UI with enhanced styling and image preview
291
  with gr.Blocks(css="""
292
  .gradio-container {
293
  background-color: #f0f4f8;
 
346
 
347
  submit_button = gr.Button("Process Image")
348
  output_html = gr.HTML(label="Result")
349
+ output_image = gr.Image(label="Uploaded Image Preview") # Added image preview component
350
  upload_status = gr.Textbox(label="Upload Status")
351
  milestone = gr.Textbox(label="Detected Milestone")
352
  confidence = gr.Textbox(label="Confidence Score") # Kept for compatibility, but unused
 
355
  submit_button.click(
356
  fn=process_image,
357
  inputs=[image_input, project_name_input],
358
+ outputs=[output_html, output_image, upload_status, milestone, confidence, progress]
359
  )
360
 
361
  demo.launch(share=True)