File size: 15,135 Bytes
f7ec189 521c424 f7ec189 9afe9d3 521c424 f7ec189 a2cd40e 32efefd a2cd40e f7ec189 6c6a1e4 9afe9d3 7b362b3 32efefd 521c424 07bdf30 89aed7b a2cd40e 32efefd a2cd40e 32efefd 89aed7b 32efefd 89aed7b 32efefd 45365c7 32efefd 89aed7b 32efefd a2cd40e 32efefd a2cd40e 32efefd a2cd40e 32efefd 638a209 32efefd 638a209 d1970ee 6e86a94 57b48e6 6e86a94 57b48e6 6e86a94 d1970ee 6e86a94 57b48e6 6e86a94 57b48e6 6e86a94 d1970ee 6e86a94 57b48e6 6e86a94 57b48e6 6e86a94 d1970ee 6e86a94 aae87f3 6e86a94 aae87f3 6e86a94 d1970ee 32efefd 6c6a1e4 a2cd40e 6c6a1e4 32efefd 6c6a1e4 a2cd40e 6c6a1e4 32efefd 6c6a1e4 d1970ee 3847a1d c5c136a 9afe9d3 521c424 32efefd 521c424 6c6a1e4 521c424 32efefd 638a209 521c424 89aed7b 521c424 8686aa9 807468f 521c424 89aed7b f7ec189 8686aa9 f92cfec 6c6a1e4 f92cfec 6c6a1e4 a2cd40e 6c6a1e4 f92cfec 6c6a1e4 c26aa17 f92cfec 6c6a1e4 a2cd40e f7ec189 07bdf30 f7ec189 8686aa9 f7ec189 c26aa17 f7ec189 8686aa9 f7ec189 a2cd40e |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 |
import gradio as gr
from PIL import Image
import os
from dotenv import load_dotenv
from simple_salesforce import Salesforce
from datetime import datetime
import hashlib
import shutil
import base64
import pytz
# Load environment variables
load_dotenv()
SF_USERNAME = os.getenv("SF_USERNAME")
SF_PASSWORD = os.getenv("SF_PASSWORD")
SF_SECURITY_TOKEN = os.getenv("SF_SECURITY_TOKEN")
# Validate Salesforce credentials
if not all([SF_USERNAME, SF_PASSWORD, SF_SECURITY_TOKEN]):
raise ValueError("Missing Salesforce credentials. Set SF_USERNAME, SF_PASSWORD, and SF_SECURITY_TOKEN in environment variables.")
# Initialize Salesforce connection
try:
sf = Salesforce(
username=SF_USERNAME,
password=SF_PASSWORD,
security_token=SF_SECURITY_TOKEN,
domain='login'
)
except Exception as e:
print(f"Salesforce connection failed: {str(e)}")
raise
# Valid milestones in sequential order
VALID_MILESTONES = ["Planning", "Foundation", "Walls Erected", "Completed"]
MILESTONE_WEIGHTS = {
"Planning": 1,
"Foundation": 2,
"Walls Erected": 3,
"Completed": 4
}
# Adjust the timezone to your local timezone
local_timezone = pytz.timezone("Asia/Kolkata")
# Image processing and Salesforce upload
def process_image(images, project_name):
try:
if not images or len(images) == 0:
return "Error: Please upload at least one image to proceed.", "Pending", "", "", 0
if len(images) < 2:
return "Error: Please upload at least one indoor and one outdoor image for accurate milestone detection.", "Pending", "", "", 0
# Process each image
image_milestones = []
image_types = []
for image in images:
img = Image.open(image)
image_size_mb = os.path.getsize(image) / (1024 * 1024)
if image_size_mb > 20:
return "Error: One or more images exceed 20MB.", "Failure", "", "", 0
if not str(image).lower().endswith(('.jpg', '.jpeg', '.png')):
return "Error: Only JPG/PNG images are supported.", "Failure", "", "", 0
# Save image to public folder temporarily before uploading to Salesforce
upload_dir = "public_uploads"
os.makedirs(upload_dir, exist_ok=True)
unique_id = datetime.now().strftime("%Y%m%d%H%M%S")
image_filename = f"{unique_id}_{os.path.basename(image)}"
saved_image_path = os.path.join(upload_dir, image_filename)
shutil.copy(image, saved_image_path)
# Convert image to base64 before uploading to Salesforce
with open(saved_image_path, 'rb') as image_file:
image_data = base64.b64encode(image_file.read()).decode('utf-8')
# Create the ContentVersion record in Salesforce
content_version = {
'Title': image_filename,
'PathOnClient': saved_image_path,
'VersionData': image_data
}
# Upload the file to Salesforce
try:
content_version_result = sf.ContentVersion.create(content_version)
content_version_id = content_version_result['id']
file_url = f"https://sathkruthatechsolutionspri8-dev-ed.develop.lightning.force.com/{content_version_id}"
except Exception as e:
return f"Error: Failed to upload image to Salesforce - {str(e)}", "Failure", "", "", 0
# Classify image as indoor or outdoor based on filename
filename_lower = os.path.basename(image).lower()
is_indoor = any(keyword in filename_lower for keyword in ["indoor", "interior", "inside"])
image_type = "Indoor" if is_indoor else "Outdoor"
image_types.append(image_type)
# Enhanced milestone detection logic
# Use filename keywords to simulate content analysis
milestone = "Planning" # Default
if any(keyword in filename_lower for keyword in ["site", "clearing", "planning", "design"]):
milestone = "Planning"
elif any(keyword in filename_lower for keyword in ["foundation", "footing", "slab", "excavation"]):
milestone = "Foundation"
elif any(keyword in filename_lower for keyword in ["wall", "structure", "beam", "column", "facade"]):
milestone = "Walls Erected"
elif any(keyword in filename_lower for keyword in ["electrical", "plumbing", "hvac", "finish", "completed"]):
milestone = "Completed"
# Adjust milestone based on image type
if image_type == "Indoor" and milestone in ["Planning", "Foundation"]:
milestone = "Walls Erected" # Indoor images imply at least walls are up
elif image_type == "Outdoor" and milestone == "Completed":
milestone = "Walls Erected" # Outdoor completion needs indoor confirmation
image_milestones.append(milestone)
# Validate and aggregate milestones
if not any(t == "Indoor" for t in image_types) or not any(t == "Outdoor" for t in image_types):
return "Error: Both indoor and outdoor images are required for accurate milestone detection.", "Pending", "", "", 0
# Ensure sequential milestone logic
max_milestone_index = max(MILESTONE_WEIGHTS[m] for m in image_milestones)
final_milestone = [m for m, w in MILESTONE_WEIGHTS.items() if w == max_milestone_index][0]
# If "Completed" is detected, ensure indoor images confirm it
if final_milestone == "Completed" and not any(m == "Completed" and t == "Indoor" for m, t in zip(image_milestones, image_types)):
final_milestone = "Walls Erected"
milestone_completion_map = {
"Planning": 10,
"Foundation": 30,
"Walls Erected": 50,
"Completed": 100,
}
percent_complete = milestone_completion_map.get(final_milestone, 0)
completion_details = {
"Planning": {
"completed": [
"Initial project outline and objectives have been established.",
"Preliminary designs and architectural plans are drafted.",
"Stakeholder meetings and initial approvals are completed."
],
"not_completed": [
"Detailed construction plans and blueprints are pending finalization.",
"Permits and regulatory approvals are yet to be obtained.",
"Contractor selection and procurement processes are not yet complete."
]
},
"Foundation": {
"completed": [
"Site preparation, including clearing and leveling, is finished.",
"Excavation for the foundation has been completed.",
"Concrete pouring for the foundation, including footings and slabs, is done.",
"Initial structural inspections for the foundation have been passed."
],
"not_completed": [
"Plumbing and electrical groundwork installations are pending.",
"Backfilling and site grading around the foundation are not yet done.",
"Above-ground structural work, such as columns and walls, has not started."
]
},
"Walls Erected": {
"completed": [
"The concrete framework, including columns and beams, is in place.",
"All structural walls have been erected and stabilized.",
"Temporary scaffolding and safety measures are installed for ongoing work.",
"Initial inspections for structural integrity have been completed."
],
"not_completed": [
"Roofing installation and weatherproofing are pending.",
"Windows, doors, and exterior cladding are not yet installed.",
"Interior walls, electrical, and plumbing systems are still to be implemented."
]
},
"Completed": {
"completed": [
"The concrete framework, including columns, beams, and floor slabs, is fully constructed.",
"Exterior walls, windows, and cladding are installed, completing the building's facade.",
"Interior work, including electrical, plumbing, and HVAC systems, is fully implemented.",
"Finishing touches, such as flooring, painting, and fixtures, are completed.",
"All phases of the project are finished, including final inspections and approvals."
],
"not_completed": [
"There should be no more pending work as the project is fully completed."
]
}
}
completed_tasks = completion_details[final_milestone]["completed"]
not_completed_tasks = completion_details[final_milestone]["not_completed"]
completed_html = "".join([f'<li style="color: green;">✔ {task}</li>' for task in completed_tasks])
not_completed_html = "".join([f'<li style="color: red;">✘ {task}</li>' for task in not_completed_tasks])
# Enhanced result HTML with image type feedback
image_summary = "".join([f'<li>{os.path.basename(img)} ({img_type}): {milestone}</li>' for img, img_type, milestone in zip(images, image_types, image_milestones)])
result_html = f"""
<div style="font-family: Arial, sans-serif; padding: 20px; background-color: #f9f9f9; border-radius: 10px;">
<h2 style="color: #2c3e50; text-align: center;">Project Summary</h2>
<div style="display: flex; justify-content: space-around; margin-bottom: 20px;">
<div style="text-align: center;">
<h3 style="color: #34495e;">Detected Milestone</h3>
<p style="font-size: 18px; font-weight: bold;">{final_milestone}</p>
</div>
<div style="text-align: center;">
<h3 style="color: #34495e;">Completion</h3>
<progress value="{percent_complete}" max="100" style="width: 200px; height: 20px;"></progress>
<p>{percent_complete}%</p>
</div>
</div>
<h3 style="color: #2c3e50;">Image Analysis</h3>
<ul style="padding-left: 20px; margin-bottom: 20px;">
{image_summary}
</ul>
<h3 style="color: #2c3e50;">Milestone Timeline</h3>
<div style="display: flex; justify-content: space-between; align-items: center; margin-bottom: 20px;">
<span style="color: {'#2ecc71' if final_milestone == 'Planning' else '#bdc3c7'};">Planning</span>
<span style="color: {'#2ecc71' if final_milestone == 'Foundation' else '#bdc3c7'};">Foundation</span>
<span style="color: {'#2ecc71' if final_milestone == 'Walls Erected' else '#bdc3c7'};">Walls Erected</span>
<span style="color: {'#2ecc71' if final_milestone == 'Completed' else '#bdc3c7'};">Completed</span>
</div>
<details style="margin-bottom: 20px;">
<summary style="color: #2c3e50; font-weight: bold;">Completed Tasks</summary>
<ul style="padding-left: 20px;">
{completed_html}
</ul>
</details>
<details style="margin-bottom: 20px;">
<summary style="color: #2c3e50; font-weight: bold;">Not Completed Tasks</summary>
<ul style="padding-left: 20px;">
{not_completed_html}
</ul>
</details>
</div>
"""
now = datetime.now(local_timezone)
local_time = now.strftime("%Y-%m-%dT%H:%M:%S") + now.strftime("%z")[:-2] + ":" + now.strftime("%z")[-2:]
record = {
"Name__c": project_name,
"Current_Milestone__c": final_milestone,
"Completion_Percentage__c": percent_complete,
"Last_Updated_On__c": local_time,
"Upload_Status__c": "Success",
"Comments__c": f"{final_milestone}",
"Last_Updated_Image__c": file_url
}
try:
sf.Construction__c.create(record)
except Exception as e:
return f"Error: Failed to update Salesforce - {str(e)}", "Failure", "", "", 0
return result_html, "Success", final_milestone, f"{percent_complete}%"
except Exception as e:
return f"Error: {str(e)}", "Failure", "", "", "0%"
# Gradio UI
with gr.Blocks(css="""
.gradio-container {
background-color: #f0f4f8;
font-family: Arial, sans-serif;
}
.title {
color: #2c3e50;
font-size: 24px;
text-align: center;
font-weight: bold;
}
.gradio-row {
text-align: center;
}
.gradio-container .output {
text-align: center;
}
.gradio-container .input {
text-align: center;
}
.gradio-container .button {
display: block;
margin: 0 auto;
background-color: #3498db;
color: white;
border: none;
padding: 10px 20px;
border-radius: 5px;
cursor: pointer;
}
.gradio-container .button:hover {
background-color: #2980b9;
}
progress::-webkit-progress-value {
background-color: #2ecc71;
border-radius: 5px;
}
.gradio-container progress::-webkit-progress-bar {
background-color: #ecf0f1;
border-radius: 5px;
}
details summary {
cursor: pointer;
padding: 10px;
background-color: #ecf0f1;
border-radius: 5px;
}
details ul {
margin-top: 10px;
}
""") as demo:
gr.Markdown("<h1 class='title'>Construction Progress Analyzer</h1>")
gr.Markdown("""
<p style='text-align: center; color: #34495e;'>
Upload at least one indoor and one outdoor image of the construction site (JPG/PNG, ≤ 20MB each).<br>
Use descriptive filenames (e.g., 'outdoor_foundation.jpg', 'indoor_electrical.jpg') for best results.
</p>
""")
with gr.Row():
image_input = gr.Files(type="filepath", label="Upload Construction Site Photos (JPG/PNG, ≤ 20MB)")
project_name_input = gr.Textbox(label="Project Name (Required)", placeholder="e.g. Project_12345")
submit_button = gr.Button("Process Image")
output_html = gr.HTML(label="Result")
upload_status = gr.Textbox(label="Upload Status")
milestone = gr.Textbox(label="Detected Milestone")
progress = gr.Textbox(label="Completion Percentage", interactive=False)
submit_button.click(
fn=process_image,
inputs=[image_input, project_name_input],
outputs=[output_html, upload_status, milestone, progress]
)
demo.launch(share=True) |