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| 1 |
+
import streamlit as st
|
| 2 |
+
import requests
|
| 3 |
+
import os
|
| 4 |
+
from PIL import Image
|
| 5 |
+
import numpy as np
|
| 6 |
+
from transformers import pipeline
|
| 7 |
+
from simple_salesforce import Salesforce
|
| 8 |
+
import io
|
| 9 |
+
import time
|
| 10 |
+
|
| 11 |
+
# Set up page title and layout
|
| 12 |
+
st.title("Construction Project Progress Tracker")
|
| 13 |
+
st.write("Upload a photo of your construction site, and the AI will tell you the progress!")
|
| 14 |
+
|
| 15 |
+
# Salesforce connection (replace with your credentials)
|
| 16 |
+
sf = Salesforce(
|
| 17 |
+
username='your_salesforce_username',
|
| 18 |
+
password='your_salesforce_password',
|
| 19 |
+
security_token='your_security_token',
|
| 20 |
+
domain='login' # Use 'test' for sandbox
|
| 21 |
+
)
|
| 22 |
+
|
| 23 |
+
# Hugging Face model (replace with your actual model for construction milestone detection)
|
| 24 |
+
# For demo, we use a placeholder image classification model
|
| 25 |
+
model = pipeline("image-classification", model="microsoft/resnet-50")
|
| 26 |
+
|
| 27 |
+
# Function to validate photo size (< 20MB)
|
| 28 |
+
def validate_photo_size(image_file):
|
| 29 |
+
max_size_mb = 20
|
| 30 |
+
image_file.seek(0, os.SEEK_END)
|
| 31 |
+
file_size_mb = image_file.tell() / (1024 * 1024) # Convert bytes to MB
|
| 32 |
+
image_file.seek(0) # Reset file pointer
|
| 33 |
+
return file_size_mb <= max_size_mb
|
| 34 |
+
|
| 35 |
+
# Function to process image with AI and predict milestone
|
| 36 |
+
def predict_milestone(image):
|
| 37 |
+
try:
|
| 38 |
+
# Simulate AI processing time (ensure < 5 seconds)
|
| 39 |
+
start_time = time.time()
|
| 40 |
+
|
| 41 |
+
# Process image with Hugging Face model
|
| 42 |
+
predictions = model(image)
|
| 43 |
+
|
| 44 |
+
# Placeholder logic: Map model output to construction milestones
|
| 45 |
+
# Replace with actual milestone mapping based on your trained model
|
| 46 |
+
milestone = predictions[0]["label"] # Example: "positive" -> "Walls Erected"
|
| 47 |
+
confidence = predictions[0]["score"]
|
| 48 |
+
|
| 49 |
+
# Map model output to construction milestones (customize this)
|
| 50 |
+
milestone_map = {
|
| 51 |
+
"positive": "Walls Erected",
|
| 52 |
+
"negative": "Foundation Completed",
|
| 53 |
+
# Add more mappings based on your model
|
| 54 |
+
}
|
| 55 |
+
completion_map = {
|
| 56 |
+
"positive": 60.00, # Example: Walls = 60% complete
|
| 57 |
+
"negative": 20.00, # Example: Foundation = 20% complete
|
| 58 |
+
}
|
| 59 |
+
|
| 60 |
+
predicted_milestone = milestone_map.get(milestone, "Unknown Milestone")
|
| 61 |
+
completion_percentage = completion_map.get(milestone, 0.00)
|
| 62 |
+
|
| 63 |
+
processing_time = time.time() - start_time
|
| 64 |
+
if processing_time > 5:
|
| 65 |
+
return None, None, "AI took too long to process (> 5 seconds)."
|
| 66 |
+
|
| 67 |
+
return predicted_milestone, completion_percentage, None
|
| 68 |
+
except Exception as e:
|
| 69 |
+
return None, None, f"AI failed to process the image: {str(e)}"
|
| 70 |
+
|
| 71 |
+
# Function to upload image to Salesforce and get a URL
|
| 72 |
+
def upload_image_to_salesforce(image_file, project_id):
|
| 73 |
+
try:
|
| 74 |
+
# Placeholder: Simulate uploading image to Salesforce ContentVersion
|
| 75 |
+
# Replace with actual Salesforce file upload API call
|
| 76 |
+
image_url = "https://your-salesforce-file-url.com/example-image.jpg" # Simulated URL
|
| 77 |
+
return image_url, None
|
| 78 |
+
except Exception as e:
|
| 79 |
+
return None, f"Failed to upload image to Salesforce: {str(e)}"
|
| 80 |
+
|
| 81 |
+
# Function to update Salesforce Construction_Project__c object
|
| 82 |
+
def update_salesforce_record(project_id, milestone, percentage, image_url, status, comments):
|
| 83 |
+
try:
|
| 84 |
+
sf.Construction_Project__c.update(project_id, {
|
| 85 |
+
'Current_Milestone__c': milestone,
|
| 86 |
+
'Completion_Percentage__c': percentage,
|
| 87 |
+
'Last_Updated_Image__c': image_url,
|
| 88 |
+
'Last_Updated_On__c': time.strftime('%Y-%m-%dT%H:%M:%SZ', time.gmtime()),
|
| 89 |
+
'Upload_Status__c': status,
|
| 90 |
+
'Comments__c': comments
|
| 91 |
+
})
|
| 92 |
+
return None
|
| 93 |
+
except Exception as e:
|
| 94 |
+
return f"Failed to update Salesforce: {str(e)}"
|
| 95 |
+
|
| 96 |
+
# Streamlit UI
|
| 97 |
+
with st.form(key="photo_upload_form"):
|
| 98 |
+
project_id = st.text_input("Enter Project ID (e.g., Sunshine Apartments)", "Sunshine Apartments")
|
| 99 |
+
uploaded_file = st.file_uploader("Upload a Construction Photo", type=["jpg", "jpeg", "png"])
|
| 100 |
+
submit_button = st.form_submit_button(label="Upload and Analyze")
|
| 101 |
+
|
| 102 |
+
if submit_button and uploaded_file is not None:
|
| 103 |
+
# Validate photo size
|
| 104 |
+
if not validate_photo_size(uploaded_file):
|
| 105 |
+
st.error("Photo is too large! Please upload a photo smaller than 20MB.")
|
| 106 |
+
else:
|
| 107 |
+
# Display the uploaded image
|
| 108 |
+
image = Image.open(uploaded_file)
|
| 109 |
+
st.image(image, caption="Uploaded Construction Photo", use_column_width=True)
|
| 110 |
+
|
| 111 |
+
# Process the image with AI
|
| 112 |
+
milestone, percentage, error = predict_milestone(image)
|
| 113 |
+
|
| 114 |
+
if error:
|
| 115 |
+
st.error(error)
|
| 116 |
+
# Update Salesforce with failure status
|
| 117 |
+
error_message = update_salesforce_record(
|
| 118 |
+
project_id=project_id,
|
| 119 |
+
milestone=None,
|
| 120 |
+
percentage=0.00,
|
| 121 |
+
image_url=None,
|
| 122 |
+
status="Failure",
|
| 123 |
+
comments=error
|
| 124 |
+
)
|
| 125 |
+
if error_message:
|
| 126 |
+
st.error(error_message)
|
| 127 |
+
else:
|
| 128 |
+
# Upload image to Salesforce
|
| 129 |
+
image_url, upload_error = upload_image_to_salesforce(uploaded_file, project_id)
|
| 130 |
+
|
| 131 |
+
if upload_error:
|
| 132 |
+
st.error(upload_error)
|
| 133 |
+
# Update Salesforce with failure status
|
| 134 |
+
error_message = update_salesforce_record(
|
| 135 |
+
project_id=project_id,
|
| 136 |
+
milestone=milestone,
|
| 137 |
+
percentage=percentage,
|
| 138 |
+
image_url=None,
|
| 139 |
+
status="Failure",
|
| 140 |
+
comments=upload_error
|
| 141 |
+
)
|
| 142 |
+
if error_message:
|
| 143 |
+
st.error(error_message)
|
| 144 |
+
else:
|
| 145 |
+
# Update Salesforce with success
|
| 146 |
+
st.success(f"AI Result: Milestone = {milestone}, Completion = {percentage}%")
|
| 147 |
+
error_message = update_salesforce_record(
|
| 148 |
+
project_id=project_id,
|
| 149 |
+
milestone=milestone,
|
| 150 |
+
percentage=percentage,
|
| 151 |
+
image_url=image_url,
|
| 152 |
+
status="Success",
|
| 153 |
+
comments="Photo processed successfully"
|
| 154 |
+
)
|
| 155 |
+
if error_message:
|
| 156 |
+
st.error(error_message)
|
| 157 |
+
else:
|
| 158 |
+
st.success("Progress saved to Salesforce!")
|
| 159 |
+
else:
|
| 160 |
+
st.info("Please upload a photo to analyze.")
|