Update app.py
Browse files
app.py
CHANGED
|
@@ -31,63 +31,47 @@ except Exception as e:
|
|
| 31 |
print(f"Salesforce connection failed: {str(e)}")
|
| 32 |
raise
|
| 33 |
|
| 34 |
-
# Updated Milestone Percentages
|
| 35 |
milestone_percentage_map = {
|
| 36 |
-
"Excavation and Foundation": 10,
|
| 37 |
-
"Structural Framework": 40,
|
| 38 |
-
"Roofing": 70,
|
| 39 |
-
"Exterior Work": 85,
|
| 40 |
-
"Interior Work": 95,
|
| 41 |
-
"Final Completion": 100
|
| 42 |
}
|
| 43 |
|
| 44 |
# Adjust the timezone to your local timezone
|
| 45 |
local_timezone = pytz.timezone("Asia/Kolkata")
|
| 46 |
|
| 47 |
-
# AI model prediction
|
| 48 |
def mock_ai_model(image):
|
| 49 |
img = image.convert("RGB")
|
| 50 |
max_size = 1024
|
| 51 |
img.thumbnail((max_size, max_size), Image.Resampling.LANCZOS)
|
| 52 |
|
| 53 |
-
|
| 54 |
-
|
| 55 |
-
gray_scale = [sum(pixel) / 3 for pixel in img_data] # Simple grayscale conversion
|
| 56 |
-
avg_brightness = sum(gray_scale) / len(gray_scale)
|
| 57 |
|
| 58 |
-
|
| 59 |
-
|
| 60 |
-
|
| 61 |
-
milestone = "Structural Framework"
|
| 62 |
-
elif avg_brightness > 200: # Brighter image might suggest advanced stages
|
| 63 |
-
milestone = "Exterior Work"
|
| 64 |
-
else:
|
| 65 |
-
milestone_index = int(hashlib.sha256(img.tobytes()).hexdigest(), 16) % len(milestone_percentage_map)
|
| 66 |
-
milestone = list(milestone_percentage_map.keys())[milestone_index]
|
| 67 |
|
| 68 |
-
|
| 69 |
-
confidence_raw = 0.85 + ((hashlib.sha256(img.tobytes()).hexdigest() % 1000) / 1000) * (0.95 - 0.85)
|
| 70 |
confidence_score = round(confidence_raw, 2)
|
| 71 |
|
| 72 |
-
return milestone,
|
| 73 |
|
| 74 |
-
# Image processing and Salesforce upload
|
| 75 |
def process_image(image, project_name, project_type):
|
| 76 |
try:
|
| 77 |
-
if not image
|
| 78 |
-
return "Error: Please upload an image
|
| 79 |
|
| 80 |
# Open the image from the file path
|
| 81 |
-
img = Image.open(image)
|
| 82 |
milestone, percent_complete, confidence_score = mock_ai_model(img)
|
| 83 |
|
| 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"
|
| 93 |
os.makedirs(upload_dir, exist_ok=True)
|
|
@@ -112,158 +96,30 @@ def process_image(image, project_name, project_type):
|
|
| 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", "", "",
|
| 116 |
-
|
| 117 |
-
# Detailed Completion Breakdown
|
| 118 |
-
completion_details = {
|
| 119 |
-
"Excavation and Foundation": {
|
| 120 |
-
"completed": [
|
| 121 |
-
"Site clearing and leveling completed.",
|
| 122 |
-
"Excavation for foundation finished.",
|
| 123 |
-
"Foundation concrete poured and cured."
|
| 124 |
-
],
|
| 125 |
-
"not_completed": [
|
| 126 |
-
"Structural framework not started.",
|
| 127 |
-
"Roofing and exterior work pending.",
|
| 128 |
-
"Interior work not initiated."
|
| 129 |
-
]
|
| 130 |
-
},
|
| 131 |
-
"Structural Framework": {
|
| 132 |
-
"completed": [
|
| 133 |
-
"Foundation fully set and inspected.",
|
| 134 |
-
"Concrete columns and beams erected.",
|
| 135 |
-
"Floor slabs poured and cured."
|
| 136 |
-
],
|
| 137 |
-
"not_completed": [
|
| 138 |
-
"Roofing installation not started.",
|
| 139 |
-
"Exterior walls and windows pending.",
|
| 140 |
-
"Interior work not begun."
|
| 141 |
-
]
|
| 142 |
-
},
|
| 143 |
-
"Roofing": {
|
| 144 |
-
"completed": [
|
| 145 |
-
"Structural framework completed.",
|
| 146 |
-
"Roof structure installed and secured.",
|
| 147 |
-
"Weatherproofing applied."
|
| 148 |
-
],
|
| 149 |
-
"not_completed": [
|
| 150 |
-
"Exterior cladding and windows not installed.",
|
| 151 |
-
"Interior work still pending.",
|
| 152 |
-
"Final finishes not started."
|
| 153 |
-
]
|
| 154 |
-
},
|
| 155 |
-
"Exterior Work": {
|
| 156 |
-
"completed": [
|
| 157 |
-
"Roofing fully completed.",
|
| 158 |
-
"Exterior walls, windows, and doors installed.",
|
| 159 |
-
"Cladding and exterior finishes applied."
|
| 160 |
-
],
|
| 161 |
-
"not_completed": [
|
| 162 |
-
"Interior electrical and plumbing pending.",
|
| 163 |
-
"Interior finishes not started.",
|
| 164 |
-
"Final inspections not conducted."
|
| 165 |
-
]
|
| 166 |
-
},
|
| 167 |
-
"Interior Work": {
|
| 168 |
-
"completed": [
|
| 169 |
-
"Exterior work fully completed.",
|
| 170 |
-
"Interior walls, electrical, and plumbing installed.",
|
| 171 |
-
"HVAC systems set up."
|
| 172 |
-
],
|
| 173 |
-
"not_completed": [
|
| 174 |
-
"Final flooring and painting pending.",
|
| 175 |
-
"Fixtures and fittings not installed.",
|
| 176 |
-
"Final approvals not obtained."
|
| 177 |
-
]
|
| 178 |
-
},
|
| 179 |
-
"Final Completion": {
|
| 180 |
-
"completed": [
|
| 181 |
-
"All construction and interior work finished.",
|
| 182 |
-
"Flooring, painting, and fixtures installed.",
|
| 183 |
-
"Final inspections and approvals obtained."
|
| 184 |
-
],
|
| 185 |
-
"not_completed": [
|
| 186 |
-
"No pending tasks."
|
| 187 |
-
]
|
| 188 |
-
}
|
| 189 |
-
}
|
| 190 |
-
|
| 191 |
-
# Format the completed and not completed tasks as HTML for collapsible sections
|
| 192 |
-
completed_tasks = completion_details[milestone]["completed"]
|
| 193 |
-
not_completed_tasks = completion_details[milestone]["not_completed"]
|
| 194 |
-
|
| 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 |
-
# 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>
|
| 203 |
-
<div style="display: flex; justify-content: space-around; margin-bottom: 20px;">
|
| 204 |
-
<div style="text-align: center;">
|
| 205 |
-
<h3 style="color: #34495e;">Detected Milestone</h3>
|
| 206 |
-
<p style="font-size: 18px; font-weight: bold;">{milestone}</p>
|
| 207 |
-
</div>
|
| 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 |
|
| 220 |
-
|
| 221 |
-
<div style="display: flex; justify-content: space-between; align-items: center; margin-bottom: 20px;">
|
| 222 |
-
<span style="color: {'#2ecc71' if milestone == 'Excavation and Foundation' else '#bdc3c7'};">Excavation and Foundation</span>
|
| 223 |
-
<span style="color: {'#2ecc71' if milestone == 'Structural Framework' else '#bdc3c7'};">Structural Framework</span>
|
| 224 |
-
<span style="color: {'#2ecc71' if milestone == 'Roofing' else '#bdc3c7'};">Roofing</span>
|
| 225 |
-
<span style="color: {'#2ecc71' if milestone == 'Exterior Work' else '#bdc3c7'};">Exterior Work</span>
|
| 226 |
-
<span style="color: {'#2ecc71' if milestone == 'Interior Work' else '#bdc3c7'};">Interior Work</span>
|
| 227 |
-
<span style="color: {'#2ecc71' if milestone == 'Final Completion' else '#bdc3c7'};">Final Completion</span>
|
| 228 |
-
</div>
|
| 229 |
-
|
| 230 |
-
<details style="margin-bottom: 20px;">
|
| 231 |
-
<summary style="color: #2c3e50; font-weight: bold;">Completed Tasks</summary>
|
| 232 |
-
<ul style="padding-left: 20px;">
|
| 233 |
-
{completed_html}
|
| 234 |
-
</ul>
|
| 235 |
-
</details>
|
| 236 |
-
|
| 237 |
-
<details style="margin-bottom: 20px;">
|
| 238 |
-
<summary style="color: #2c3e50; font-weight: bold;">Not Completed Tasks</summary>
|
| 239 |
-
<ul style="padding-left: 20px;">
|
| 240 |
-
{not_completed_html}
|
| 241 |
-
</ul>
|
| 242 |
-
</details>
|
| 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:]
|
| 249 |
|
|
|
|
| 250 |
record = {
|
| 251 |
"Name__c": project_name,
|
| 252 |
"Project_Type__c": project_type,
|
| 253 |
-
"Completion_Percentage__c": percent_complete,
|
| 254 |
-
"Current_Milestone__c": milestone,
|
| 255 |
"Last_Updated_On__c": local_time,
|
| 256 |
"Upload_Status__c": "Success",
|
| 257 |
"Comments__c": f"Project {project_name} at {milestone} with {percent_complete}% completion",
|
| 258 |
-
"Last_Updated_Image__c": file_url
|
| 259 |
}
|
| 260 |
|
| 261 |
try:
|
| 262 |
-
sf.Construction__c.create(record)
|
| 263 |
except Exception as e:
|
| 264 |
-
return f"Error: Failed to update Salesforce - {str(e)}", "Failure", "", "",
|
| 265 |
|
| 266 |
-
return
|
| 267 |
|
| 268 |
except Exception as e:
|
| 269 |
return f"Error: {str(e)}", "Failure", "", "", "0%"
|
|
@@ -326,19 +182,15 @@ with gr.Blocks(css="""
|
|
| 326 |
project_type_input = gr.Dropdown(label="Project Type", choices=["House", "Apartment"], value="House")
|
| 327 |
project_name_input = gr.Textbox(label="Project Name (Required)", placeholder="e.g. Project_12345")
|
| 328 |
|
| 329 |
-
# Handle single image upload
|
| 330 |
-
image_input = gr.Image(type="filepath", label="Upload Construction Site Photo (JPG/PNG, ≤ 20MB)")
|
| 331 |
submit_button = gr.Button("Process Image")
|
| 332 |
output_html = gr.HTML(label="Result")
|
| 333 |
-
|
| 334 |
-
milestone = gr.Textbox(label="Detected Milestone")
|
| 335 |
-
confidence = gr.Textbox(label="Confidence Score")
|
| 336 |
-
progress = gr.Textbox(label="Completion Percentage", interactive=False)
|
| 337 |
-
|
| 338 |
submit_button.click(
|
| 339 |
fn=process_image,
|
| 340 |
inputs=[image_input, project_name_input, project_type_input],
|
| 341 |
-
outputs=[output_html
|
| 342 |
)
|
| 343 |
|
| 344 |
-
demo.launch(share=True)
|
|
|
|
| 31 |
print(f"Salesforce connection failed: {str(e)}")
|
| 32 |
raise
|
| 33 |
|
| 34 |
+
# Updated Milestone Percentages (reflecting the percentage ranges)
|
| 35 |
milestone_percentage_map = {
|
| 36 |
+
"Excavation and Foundation": 10, # Stage 1: Excavation and Foundation = 0% to 10%
|
| 37 |
+
"Structural Framework": 40, # Stage 2: Structural Framework = 10% to 40%
|
| 38 |
+
"Roofing": 70, # Stage 3: Roofing = 40% to 70%
|
| 39 |
+
"Exterior Work": 85, # Stage 4: Exterior Work = 70% to 85%
|
| 40 |
+
"Interior Work": 95, # Stage 5: Interior Work = 85% to 95%
|
| 41 |
+
"Final Completion": 100 # Stage 6: Final Completion = 90% to 100%
|
| 42 |
}
|
| 43 |
|
| 44 |
# Adjust the timezone to your local timezone
|
| 45 |
local_timezone = pytz.timezone("Asia/Kolkata")
|
| 46 |
|
| 47 |
+
# AI model prediction (for simulation)
|
| 48 |
def mock_ai_model(image):
|
| 49 |
img = image.convert("RGB")
|
| 50 |
max_size = 1024
|
| 51 |
img.thumbnail((max_size, max_size), Image.Resampling.LANCZOS)
|
| 52 |
|
| 53 |
+
img_bytes = img.tobytes()
|
| 54 |
+
img_hash = int(hashlib.sha256(img_bytes).hexdigest(), 16)
|
|
|
|
|
|
|
| 55 |
|
| 56 |
+
# Assign milestone based on hash (for simulation)
|
| 57 |
+
milestone_index = img_hash % len(milestone_percentage_map)
|
| 58 |
+
milestone = list(milestone_percentage_map.keys())[milestone_index]
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 59 |
|
| 60 |
+
confidence_raw = 0.85 + ((img_hash % 1000) / 1000) * (0.95 - 0.85)
|
|
|
|
| 61 |
confidence_score = round(confidence_raw, 2)
|
| 62 |
|
| 63 |
+
return milestone, milestone_percentage_map[milestone], confidence_score
|
| 64 |
|
| 65 |
+
# Image processing and Salesforce upload
|
| 66 |
def process_image(image, project_name, project_type):
|
| 67 |
try:
|
| 68 |
+
if not image:
|
| 69 |
+
return "Error: Please upload an image to proceed.", "Pending", "", "", 0
|
| 70 |
|
| 71 |
# Open the image from the file path
|
| 72 |
+
img = Image.open(image) # image is now the file path string
|
| 73 |
milestone, percent_complete, confidence_score = mock_ai_model(img)
|
| 74 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 75 |
# Save image to public folder temporarily before uploading to Salesforce
|
| 76 |
upload_dir = "public_uploads"
|
| 77 |
os.makedirs(upload_dir, exist_ok=True)
|
|
|
|
| 96 |
content_version_id = content_version_result['id']
|
| 97 |
file_url = f"https://sathkruthatechsolutionspri8-dev-ed.develop.lightning.force.com/{content_version_id}"
|
| 98 |
except Exception as e:
|
| 99 |
+
return f"Error: Failed to upload image to Salesforce - {str(e)}", "Failure", "", "", 0
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 100 |
|
| 101 |
+
# Create Salesforce record for construction project
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 102 |
now = datetime.now(local_timezone)
|
| 103 |
+
local_time = now.strftime("%Y-%m-%dT%H:%M:%S") + now.strftime("%z")[:-2] + ":" + now.strftime("%z")[-2:]
|
| 104 |
|
| 105 |
+
# Create Salesforce record
|
| 106 |
record = {
|
| 107 |
"Name__c": project_name,
|
| 108 |
"Project_Type__c": project_type,
|
| 109 |
+
"Completion_Percentage__c": percent_complete, # Correct field for percentage
|
| 110 |
+
"Current_Milestone__c": milestone, # Correct field for milestone (Picklist)
|
| 111 |
"Last_Updated_On__c": local_time,
|
| 112 |
"Upload_Status__c": "Success",
|
| 113 |
"Comments__c": f"Project {project_name} at {milestone} with {percent_complete}% completion",
|
| 114 |
+
"Last_Updated_Image__c": file_url # Correct field for image URL
|
| 115 |
}
|
| 116 |
|
| 117 |
try:
|
| 118 |
+
sf.Construction__c.create(record) # Creating a record in Salesforce
|
| 119 |
except Exception as e:
|
| 120 |
+
return f"Error: Failed to update Salesforce - {str(e)}", "Failure", "", "", 0
|
| 121 |
|
| 122 |
+
return f"Image processed successfully. {milestone} - {percent_complete}% completion", "Success", milestone, f"Confidence Score: {confidence_score}", f"{percent_complete}%"
|
| 123 |
|
| 124 |
except Exception as e:
|
| 125 |
return f"Error: {str(e)}", "Failure", "", "", "0%"
|
|
|
|
| 182 |
project_type_input = gr.Dropdown(label="Project Type", choices=["House", "Apartment"], value="House")
|
| 183 |
project_name_input = gr.Textbox(label="Project Name (Required)", placeholder="e.g. Project_12345")
|
| 184 |
|
| 185 |
+
# Handle single image upload (no need for multiple)
|
| 186 |
+
image_input = gr.Image(type="filepath", label="Upload Construction Site Photo (JPG/PNG, ≤ 20MB)") # Single image upload
|
| 187 |
submit_button = gr.Button("Process Image")
|
| 188 |
output_html = gr.HTML(label="Result")
|
| 189 |
+
|
|
|
|
|
|
|
|
|
|
|
|
|
| 190 |
submit_button.click(
|
| 191 |
fn=process_image,
|
| 192 |
inputs=[image_input, project_name_input, project_type_input],
|
| 193 |
+
outputs=[output_html]
|
| 194 |
)
|
| 195 |
|
| 196 |
+
demo.launch(share=True)
|