Rekham1110 commited on
Commit
22f76f1
·
verified ·
1 Parent(s): 3d29558

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +12 -12
app.py CHANGED
@@ -51,7 +51,6 @@ def mock_ai_model(image):
51
  img.thumbnail((max_size, max_size), Image.Resampling.LANCZOS)
52
 
53
  # Basic rule-based milestone detection (simplified placeholder)
54
- # Check for structural elements (columns, rebar) vs. exterior features
55
  img_data = list(img.getdata())
56
  gray_scale = [sum(pixel) / 3 for pixel in img_data] # Simple grayscale conversion
57
  avg_brightness = sum(gray_scale) / len(gray_scale)
@@ -75,8 +74,8 @@ def mock_ai_model(image):
75
  # Image processing and Salesforce upload with detailed output
76
  def process_image(image, project_name, project_type):
77
  try:
78
- if not image:
79
- return "Error: Please upload an image to proceed.", "Pending", "", "", 0
80
 
81
  # Open the image from the file path
82
  img = Image.open(image)
@@ -85,9 +84,9 @@ def process_image(image, project_name, project_type):
85
  # Image size and format validation
86
  image_size_mb = os.path.getsize(image) / (1024 * 1024)
87
  if image_size_mb > 20:
88
- return "Error: Image size exceeds 20MB.", "Failure", "", "", 0
89
  if not str(image).lower().endswith(('.jpg', '.jpeg', '.png')):
90
- return "Error: Only JPG/PNG images are supported.", "Failure", "", "", 0
91
 
92
  # Save image to public folder temporarily before uploading to Salesforce
93
  upload_dir = "public_uploads"
@@ -113,7 +112,7 @@ def process_image(image, project_name, project_type):
113
  content_version_id = content_version_result['id']
114
  file_url = f"https://sathkruthatechsolutionspri8-dev-ed.develop.lightning.force.com/{content_version_id}"
115
  except Exception as e:
116
- return f"Error: Failed to upload image to Salesforce - {str(e)}", "Failure", "", "", 0
117
 
118
  # Detailed Completion Breakdown
119
  completion_details = {
@@ -197,6 +196,7 @@ def process_image(image, project_name, project_type):
197
  not_completed_html = "".join([f'<li style="color: red;">✘ {task}</li>' for task in not_completed_tasks])
198
 
199
  # Create HTML for the output with collapsible sections and progress bar
 
200
  result_html = f"""
201
  <div style="font-family: Arial, sans-serif; padding: 20px; background-color: #f9f9f9; border-radius: 10px;">
202
  <h2 style="color: #2c3e50; text-align: center;">Project Summary</h2>
@@ -208,12 +208,12 @@ def process_image(image, project_name, project_type):
208
  <div style="text-align: center;">
209
  <h3 style="color: #34495e;">Completion</h3>
210
  <progress value="{percent_complete}" max="100" style="width: 200px; height: 20px;"></progress>
211
- <p>{percent_complete}%</p>
212
  </div>
213
  <div style="text-align: center;">
214
  <h3 style="color: #34495e;">Confidence Score</h3>
215
- <p style="font-size: 18px; font-weight: bold;">{confidence_score * 100}%</p>
216
- <small>The AI is {confidence_score * 100}% confident that this image represents the {milestone} stage.</small>
217
  </div>
218
  </div>
219
 
@@ -243,9 +243,9 @@ def process_image(image, project_name, project_type):
243
  </div>
244
  """
245
 
246
- # Create Salesforce record with current date and time
247
  now = datetime.now(local_timezone)
248
- local_time = now.strftime("%Y-%m-%dT%H:%M:%S") + now.strftime("%z")[:-2] + ":" + now.strftime("%z")[-2:] # 2025-06-30T17:07:00+05:30
249
 
250
  record = {
251
  "Name__c": project_name,
@@ -261,7 +261,7 @@ def process_image(image, project_name, project_type):
261
  try:
262
  sf.Construction__c.create(record)
263
  except Exception as e:
264
- return f"Error: Failed to update Salesforce - {str(e)}", "Failure", "", "", 0
265
 
266
  return result_html, "Success", milestone, f"Confidence Score: {confidence_score}", f"{percent_complete}%"
267
 
 
51
  img.thumbnail((max_size, max_size), Image.Resampling.LANCZOS)
52
 
53
  # Basic rule-based milestone detection (simplified placeholder)
 
54
  img_data = list(img.getdata())
55
  gray_scale = [sum(pixel) / 3 for pixel in img_data] # Simple grayscale conversion
56
  avg_brightness = sum(gray_scale) / len(gray_scale)
 
74
  # Image processing and Salesforce upload with detailed output
75
  def process_image(image, project_name, project_type):
76
  try:
77
+ if not image or not project_name or not project_type:
78
+ return "Error: Please upload an image and provide a project name and type.", "Pending", "", "", "0%"
79
 
80
  # Open the image from the file path
81
  img = Image.open(image)
 
84
  # Image size and format validation
85
  image_size_mb = os.path.getsize(image) / (1024 * 1024)
86
  if image_size_mb > 20:
87
+ return "Error: Image size exceeds 20MB.", "Failure", "", "", "0%"
88
  if not str(image).lower().endswith(('.jpg', '.jpeg', '.png')):
89
+ return "Error: Only JPG/PNG images are supported.", "Failure", "", "", "0%"
90
 
91
  # Save image to public folder temporarily before uploading to Salesforce
92
  upload_dir = "public_uploads"
 
112
  content_version_id = content_version_result['id']
113
  file_url = f"https://sathkruthatechsolutionspri8-dev-ed.develop.lightning.force.com/{content_version_id}"
114
  except Exception as e:
115
+ return f"Error: Failed to upload image to Salesforce - {str(e)}", "Failure", "", "", "0%"
116
 
117
  # Detailed Completion Breakdown
118
  completion_details = {
 
196
  not_completed_html = "".join([f'<li style="color: red;">✘ {task}</li>' for task in not_completed_tasks])
197
 
198
  # Create HTML for the output with collapsible sections and progress bar
199
+ # Escape % signs to prevent string formatting errors
200
  result_html = f"""
201
  <div style="font-family: Arial, sans-serif; padding: 20px; background-color: #f9f9f9; border-radius: 10px;">
202
  <h2 style="color: #2c3e50; text-align: center;">Project Summary</h2>
 
208
  <div style="text-align: center;">
209
  <h3 style="color: #34495e;">Completion</h3>
210
  <progress value="{percent_complete}" max="100" style="width: 200px; height: 20px;"></progress>
211
+ <p>{percent_complete}%%</p> <!-- Escaped % with %% -->
212
  </div>
213
  <div style="text-align: center;">
214
  <h3 style="color: #34495e;">Confidence Score</h3>
215
+ <p style="font-size: 18px; font-weight: bold;">{confidence_score * 100}%%</p> <!-- Escaped % with %% -->
216
+ <small>The AI is {confidence_score * 100}%% confident that this image represents the {milestone} stage.</small>
217
  </div>
218
  </div>
219
 
 
243
  </div>
244
  """
245
 
246
+ # Create Salesforce record with current date and time (05:09 PM IST, June 30, 2025)
247
  now = datetime.now(local_timezone)
248
+ local_time = now.strftime("%Y-%m-%dT%H:%M:%S") + now.strftime("%z")[:-2] + ":" + now.strftime("%z")[-2:] # 2025-06-30T17:09:00+05:30
249
 
250
  record = {
251
  "Name__c": project_name,
 
261
  try:
262
  sf.Construction__c.create(record)
263
  except Exception as e:
264
+ return f"Error: Failed to update Salesforce - {str(e)}", "Failure", "", "", "0%"
265
 
266
  return result_html, "Success", milestone, f"Confidence Score: {confidence_score}", f"{percent_complete}%"
267