Rekham1110 commited on
Commit
e2579fa
·
verified ·
1 Parent(s): 05507a8

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +12 -9
app.py CHANGED
@@ -51,19 +51,24 @@ milestone_percentage_map = {
51
  # Adjust the timezone to your local timezone
52
  local_timezone = pytz.timezone("Asia/Kolkata")
53
 
54
- # AI model prediction (for simulation)
55
  def mock_ai_model(image):
56
  img = image.convert("RGB")
57
  max_size = 1024
58
  img.thumbnail((max_size, max_size), Image.Resampling.LANCZOS)
59
 
 
 
 
 
 
 
 
60
  img_bytes = img.tobytes()
61
  img_hash = int(hashlib.sha256(img_bytes).hexdigest(), 16)
62
 
63
- # Assign milestone based on hash (for simulation)
64
  milestone_index = img_hash % len(milestone_percentage_map)
65
  milestone = list(milestone_percentage_map.keys())[milestone_index]
66
-
67
  confidence_raw = 0.85 + ((img_hash % 1000) / 1000) * (0.95 - 0.85)
68
  confidence_score = round(confidence_raw, 2)
69
 
@@ -141,11 +146,11 @@ def process_image(images, project_name, project_type):
141
  record = {
142
  "Name__c": project_name,
143
  "Project_Type__c": project_type,
144
- "Completion_Percentage__c": total_percent_complete, # Updated to total percentage
145
  "Current_Milestone__c": all_milestones_str,
146
  "Last_Updated_On__c": local_time,
147
  "Upload_Status__c": "Success",
148
- "Comments__c": f"Project {project_name} at {dominant_milestone or 'no milestone detected'} with {total_percent_complete}% completion",
149
  "Last_Updated_Image__c": dominant_image_url or ""
150
  }
151
 
@@ -159,13 +164,11 @@ def process_image(images, project_name, project_type):
159
  for result in results:
160
  output_html += f"<li>{result}</li>"
161
  output_html += "</ul>"
162
- output_html += f"<p><strong>Project Record Created:</strong> {project_name} - {dominant_milestone or 'No milestone detected'} (Total: {total_percent_complete}%, Milestones: {all_milestones_str})</p>"
163
  output_html += f"<p><strong>Note:</strong> Only the image with the highest completion percentage is stored in Salesforce due to field length constraints.</p>"
164
- if not dominant_milestone:
165
- output_html += "<p><strong>Warning:</strong> No valid milestone was set as dominant due to restricted picklist constraints (prior to conversion).</p>"
166
  output_html += "</div>"
167
 
168
- return output_html, "Success", dominant_milestone or "", f"Max Confidence: {max([conf for _, conf in milestone_confidences], default=0)}", f"{total_percent_complete}%"
169
 
170
  except Exception as e:
171
  return f"<p style='color: red;'>Error: {str(e)}</p>", "Failure", "", "", "0%"
 
51
  # Adjust the timezone to your local timezone
52
  local_timezone = pytz.timezone("Asia/Kolkata")
53
 
54
+ # Enhanced AI model prediction with heuristic for completion
55
  def mock_ai_model(image):
56
  img = image.convert("RGB")
57
  max_size = 1024
58
  img.thumbnail((max_size, max_size), Image.Resampling.LANCZOS)
59
 
60
+ # Simple heuristic for Final Completion (100%)
61
+ img_data = list(img.getdata())
62
+ color_variation = max(max(pixel) - min(pixel) for pixel in img_data) # Measure color uniformity
63
+ if color_variation < 20 and all(sum(pixel) / 3 > 200 for pixel in img_data[:1000]): # Bright and uniform
64
+ return "Final Completion", 100, 0.95
65
+
66
+ # Fallback to hash-based simulation
67
  img_bytes = img.tobytes()
68
  img_hash = int(hashlib.sha256(img_bytes).hexdigest(), 16)
69
 
 
70
  milestone_index = img_hash % len(milestone_percentage_map)
71
  milestone = list(milestone_percentage_map.keys())[milestone_index]
 
72
  confidence_raw = 0.85 + ((img_hash % 1000) / 1000) * (0.95 - 0.85)
73
  confidence_score = round(confidence_raw, 2)
74
 
 
146
  record = {
147
  "Name__c": project_name,
148
  "Project_Type__c": project_type,
149
+ "Completion_Percentage__c": total_percent_complete,
150
  "Current_Milestone__c": all_milestones_str,
151
  "Last_Updated_On__c": local_time,
152
  "Upload_Status__c": "Success",
153
+ "Comments__c": f"Project {project_name} with {total_percent_complete}% completion",
154
  "Last_Updated_Image__c": dominant_image_url or ""
155
  }
156
 
 
164
  for result in results:
165
  output_html += f"<li>{result}</li>"
166
  output_html += "</ul>"
167
+ output_html += f"<p><strong>Project Record Created:</strong> {project_name} (Total: {total_percent_complete}%)</p>"
168
  output_html += f"<p><strong>Note:</strong> Only the image with the highest completion percentage is stored in Salesforce due to field length constraints.</p>"
 
 
169
  output_html += "</div>"
170
 
171
+ return output_html, "Success", "", f"Max Confidence: {max([conf for _, conf in milestone_confidences], default=0)}", f"{total_percent_complete}%"
172
 
173
  except Exception as e:
174
  return f"<p style='color: red;'>Error: {str(e)}</p>", "Failure", "", "", "0%"