Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
|
@@ -1,9 +1,4 @@
|
|
| 1 |
-
import
|
| 2 |
-
import requests
|
| 3 |
-
import os
|
| 4 |
-
from PIL import Image
|
| 5 |
-
import numpy as np```python
|
| 6 |
-
import streamlit as st
|
| 7 |
import requests
|
| 8 |
import os
|
| 9 |
from PIL import Image
|
|
@@ -17,33 +12,16 @@ from dotenv import load_dotenv
|
|
| 17 |
# Load environment variables from .env file
|
| 18 |
load_dotenv()
|
| 19 |
|
| 20 |
-
# Set up page title and layout
|
| 21 |
-
st.title("Construction Project Progress Tracker")
|
| 22 |
-
st.write("Upload a photo of your construction site, and the AI will tell you the progress!")
|
| 23 |
-
|
| 24 |
-
# Salesforce connection using .env variables
|
| 25 |
-
try:
|
| 26 |
-
sf = Salesforce(
|
| 27 |
-
username=os.getenv('SALESFORCE_USERNAME'),
|
| 28 |
-
password=os.getenv('SALESFORCE_PASSWORD'),
|
| 29 |
-
security_token=os.getenv('SALESFORCE_SECURITY_TOKEN'),
|
| 30 |
-
domain=os.getenv('SALESFORCE_DOMAIN')
|
| 31 |
-
)
|
| 32 |
-
except Exception as e:
|
| 33 |
-
st.error(f"Failed to connect to Salesforce: {str(e)}")
|
| 34 |
-
st.stop()
|
| 35 |
-
|
| 36 |
-
# Hugging Face model (placeholder; replace with your custom-trained model)
|
| 37 |
-
# Using a demo image classification model for now
|
| 38 |
-
model = pipeline("image-classification", model="microsoft/resnet-50")
|
| 39 |
-
|
| 40 |
# Function to validate photo size (< 20MB)
|
| 41 |
def validate_photo_size(image_file):
|
| 42 |
max_size_mb = 20
|
| 43 |
-
image_file
|
| 44 |
-
|
| 45 |
-
|
| 46 |
-
|
|
|
|
|
|
|
|
|
|
| 47 |
|
| 48 |
# Function to process image with AI and predict milestone
|
| 49 |
def predict_milestone(image):
|
|
@@ -52,10 +30,10 @@ def predict_milestone(image):
|
|
| 52 |
start_time = time.time()
|
| 53 |
|
| 54 |
# Process image with Hugging Face model
|
|
|
|
| 55 |
predictions = model(image)
|
| 56 |
|
| 57 |
# Placeholder logic: Map model output to construction milestones
|
| 58 |
-
# Replace with actual milestone mapping based on your trained model
|
| 59 |
milestone = predictions[0]["label"] # Example: "positive" -> "Walls Erected"
|
| 60 |
confidence = predictions[0]["score"]
|
| 61 |
|
|
@@ -82,18 +60,16 @@ def predict_milestone(image):
|
|
| 82 |
return None, None, f"AI failed to process the image: {str(e)}"
|
| 83 |
|
| 84 |
# Function to upload image to Salesforce and get a URL
|
| 85 |
-
def upload_image_to_salesforce(
|
| 86 |
try:
|
| 87 |
# Placeholder: Simulate uploading image to Salesforce ContentVersion
|
| 88 |
-
# Replace with actual Salesforce file upload API call
|
| 89 |
-
# Example: Upload to ContentVersion and get ContentDocumentLink
|
| 90 |
image_url = f"https://your-salesforce-instance.com/file/{project_id}.jpg" # Simulated URL
|
| 91 |
return image_url, None
|
| 92 |
except Exception as e:
|
| 93 |
return None, f"Failed to upload image to Salesforce: {str(e)}"
|
| 94 |
|
| 95 |
# Function to update Salesforce Construction_Project__c object
|
| 96 |
-
def update_salesforce_record(project_id, milestone, percentage, image_url, status, comments):
|
| 97 |
try:
|
| 98 |
# Query to check if the project exists
|
| 99 |
query = f"SELECT Id FROM Construction_Project__c WHERE Name = '{project_id}'"
|
|
@@ -117,266 +93,88 @@ def update_salesforce_record(project_id, milestone, percentage, image_url, statu
|
|
| 117 |
except Exception as e:
|
| 118 |
return f"Failed to update Salesforce: {str(e)}"
|
| 119 |
|
| 120 |
-
#
|
| 121 |
-
|
| 122 |
-
|
| 123 |
-
|
| 124 |
-
|
| 125 |
-
|
| 126 |
-
if submit_button and uploaded_file is not None:
|
| 127 |
-
# Validate photo size
|
| 128 |
-
if not validate_photo_size(uploaded_file):
|
| 129 |
-
st.error("Photo is too large! Please upload a photo smaller than 20MB.")
|
| 130 |
-
else:
|
| 131 |
-
# Display the uploaded image
|
| 132 |
-
image = Image.open(uploaded_file)
|
| 133 |
-
st.image(image, caption="Uploaded Construction Photo", use_column_width=True)
|
| 134 |
-
|
| 135 |
-
# Process the image with AI
|
| 136 |
-
milestone, percentage, error = predict_milestone(image)
|
| 137 |
-
|
| 138 |
-
if error:
|
| 139 |
-
st.error(error)
|
| 140 |
-
# Update Salesforce with failure status
|
| 141 |
-
error_message = update_salesforce_record(
|
| 142 |
-
project_id=project_id,
|
| 143 |
-
milestone=None,
|
| 144 |
-
percentage=0.00,
|
| 145 |
-
image_url=None,
|
| 146 |
-
status="Failure",
|
| 147 |
-
comments=error
|
| 148 |
-
)
|
| 149 |
-
if error_message:
|
| 150 |
-
st.error(error_message)
|
| 151 |
-
else:
|
| 152 |
-
# Upload image to Salesforce
|
| 153 |
-
image_url, upload_error = upload_image_to_salesforce(uploaded_file, project_id)
|
| 154 |
-
|
| 155 |
-
if upload_error:
|
| 156 |
-
st.error(upload_error)
|
| 157 |
-
# Update Salesforce with failure status
|
| 158 |
-
error_message = update_salesforce_record(
|
| 159 |
-
project_id=project_id,
|
| 160 |
-
milestone=milestone,
|
| 161 |
-
percentage=percentage,
|
| 162 |
-
image_url=None,
|
| 163 |
-
status="Failure",
|
| 164 |
-
comments=upload_error
|
| 165 |
-
)
|
| 166 |
-
if error_message:
|
| 167 |
-
st.error(error_message)
|
| 168 |
-
else:
|
| 169 |
-
# Update Salesforce with success
|
| 170 |
-
st.success(f"AI Result: Milestone = {milestone}, Completion = {percentage}%")
|
| 171 |
-
error_message = update_salesforce_record(
|
| 172 |
-
project_id=project_id,
|
| 173 |
-
milestone=milestone,
|
| 174 |
-
percentage=percentage,
|
| 175 |
-
image_url=image_url,
|
| 176 |
-
status="Success",
|
| 177 |
-
comments="Photo processed successfully"
|
| 178 |
-
)
|
| 179 |
-
if error_message:
|
| 180 |
-
st.error(error_message)
|
| 181 |
-
else:
|
| 182 |
-
st.success("Progress saved to Salesforce!")
|
| 183 |
-
else:
|
| 184 |
-
st.info("Please upload a photo to analyze.")
|
| 185 |
-
```
|
| 186 |
-
|
| 187 |
-
---
|
| 188 |
-
|
| 189 |
-
#### **2. requirements.txt**
|
| 190 |
-
|
| 191 |
-
This lists all the Python libraries needed to run the app, including `torch` for Hugging Face and `python-dotenv` for the `.env` file.
|
| 192 |
-
|
| 193 |
-
<xaiArtifact artifact_id="9acd8b9b-5c22-4fef-a4cd-74849c7b0075" artifact_version_id="afecf7ad-ee42-4754-a130-3b34b8d1a974" title="requirements.txt" contentType="text/plain">
|
| 194 |
-
streamlit==1.39.0
|
| 195 |
-
transformers==4.44.2
|
| 196 |
-
simple-salesforce==1.12.6
|
| 197 |
-
pillow==10.4.0
|
| 198 |
-
numpy==1.26.4
|
| 199 |
-
requests==2.32.3
|
| 200 |
-
torch==2.4.1
|
| 201 |
-
python-dotenv==1.0.1
|
| 202 |
-
from transformers import pipeline
|
| 203 |
-
from simple_salesforce import Salesforceimport streamlit as st
|
| 204 |
-
import requests
|
| 205 |
-
import os
|
| 206 |
-
from PIL import Image
|
| 207 |
-
import numpy as np
|
| 208 |
-
from transformers import pipeline
|
| 209 |
-
from simple_salesforce import Salesforce
|
| 210 |
-
import io
|
| 211 |
-
import time
|
| 212 |
-
from dotenv import load_dotenv
|
| 213 |
-
|
| 214 |
-
# Load environment variables from .env file
|
| 215 |
-
load_dotenv()
|
| 216 |
-
|
| 217 |
-
# Set up page title and layout
|
| 218 |
-
st.title("Construction Project Progress Tracker")
|
| 219 |
-
st.write("Upload a photo of your construction site, and the AI will tell you the progress!")
|
| 220 |
-
|
| 221 |
-
# Salesforce connection using .env variables
|
| 222 |
-
sf = Salesforce(
|
| 223 |
-
username=os.getenv('SALESFORCE_USERNAME'),
|
| 224 |
-
password=os.getenv('SALESFORCE_PASSWORD'),
|
| 225 |
-
security_token=os.getenv('SALESFORCE_SECURITY_TOKEN'),
|
| 226 |
-
domain=os.getenv('SALESFORCE_DOMAIN')
|
| 227 |
-
)
|
| 228 |
-
|
| 229 |
-
# Rest of the app.py code remains the same...
|
| 230 |
-
import io
|
| 231 |
-
import time
|
| 232 |
-
|
| 233 |
-
# Set up page title and layout
|
| 234 |
-
st.title("Construction Project Progress Tracker")
|
| 235 |
-
st.write("Upload a photo of your construction site, and the AI will tell you the progress!")
|
| 236 |
-
|
| 237 |
-
# Salesforce connection (replace with your credentials)
|
| 238 |
-
sf = Salesforce(
|
| 239 |
-
username='your_salesforce_username',
|
| 240 |
-
password='your_salesforce_password',
|
| 241 |
-
security_token='your_security_token',
|
| 242 |
-
domain='login' # Use 'test' for sandbox
|
| 243 |
-
)
|
| 244 |
-
|
| 245 |
-
# Hugging Face model (replace with your actual model for construction milestone detection)
|
| 246 |
-
# For demo, we use a placeholder image classification model
|
| 247 |
-
model = pipeline("image-classification", model="microsoft/resnet-50")
|
| 248 |
-
|
| 249 |
-
# Function to validate photo size (< 20MB)
|
| 250 |
-
def validate_photo_size(image_file):
|
| 251 |
-
max_size_mb = 20
|
| 252 |
-
image_file.seek(0, os.SEEK_END)
|
| 253 |
-
file_size_mb = image_file.tell() / (1024 * 1024) # Convert bytes to MB
|
| 254 |
-
image_file.seek(0) # Reset file pointer
|
| 255 |
-
return file_size_mb <= max_size_mb
|
| 256 |
-
|
| 257 |
-
# Function to process image with AI and predict milestone
|
| 258 |
-
def predict_milestone(image):
|
| 259 |
try:
|
| 260 |
-
|
| 261 |
-
|
| 262 |
-
|
| 263 |
-
|
| 264 |
-
|
| 265 |
-
|
| 266 |
-
# Placeholder logic: Map model output to construction milestones
|
| 267 |
-
# Replace with actual milestone mapping based on your trained model
|
| 268 |
-
milestone = predictions[0]["label"] # Example: "positive" -> "Walls Erected"
|
| 269 |
-
confidence = predictions[0]["score"]
|
| 270 |
-
|
| 271 |
-
# Map model output to construction milestones (customize this)
|
| 272 |
-
milestone_map = {
|
| 273 |
-
"positive": "Walls Erected",
|
| 274 |
-
"negative": "Foundation Completed",
|
| 275 |
-
# Add more mappings based on your model
|
| 276 |
-
}
|
| 277 |
-
completion_map = {
|
| 278 |
-
"positive": 60.00, # Example: Walls = 60% complete
|
| 279 |
-
"negative": 20.00, # Example: Foundation = 20% complete
|
| 280 |
-
}
|
| 281 |
-
|
| 282 |
-
predicted_milestone = milestone_map.get(milestone, "Unknown Milestone")
|
| 283 |
-
completion_percentage = completion_map.get(milestone, 0.00)
|
| 284 |
-
|
| 285 |
-
processing_time = time.time() - start_time
|
| 286 |
-
if processing_time > 5:
|
| 287 |
-
return None, None, "AI took too long to process (> 5 seconds)."
|
| 288 |
-
|
| 289 |
-
return predicted_milestone, completion_percentage, None
|
| 290 |
except Exception as e:
|
| 291 |
-
return None,
|
| 292 |
|
| 293 |
-
# Function to upload image to Salesforce and get a URL
|
| 294 |
-
def upload_image_to_salesforce(image_file, project_id):
|
| 295 |
-
try:
|
| 296 |
-
# Placeholder: Simulate uploading image to Salesforce ContentVersion
|
| 297 |
-
# Replace with actual Salesforce file upload API call
|
| 298 |
-
image_url = "https://your-salesforce-file-url.com/example-image.jpg" # Simulated URL
|
| 299 |
-
return image_url, None
|
| 300 |
-
except Exception as e:
|
| 301 |
-
return None, f"Failed to upload image to Salesforce: {str(e)}"
|
| 302 |
-
|
| 303 |
-
# Function to update Salesforce Construction_Project__c object
|
| 304 |
-
def update_salesforce_record(project_id, milestone, percentage, image_url, status, comments):
|
| 305 |
-
try:
|
| 306 |
-
sf.Construction_Project__c.update(project_id, {
|
| 307 |
-
'Current_Milestone__c': milestone,
|
| 308 |
-
'Completion_Percentage__c': percentage,
|
| 309 |
-
'Last_Updated_Image__c': image_url,
|
| 310 |
-
'Last_Updated_On__c': time.strftime('%Y-%m-%dT%H:%M:%SZ', time.gmtime()),
|
| 311 |
-
'Upload_Status__c': status,
|
| 312 |
-
'Comments__c': comments
|
| 313 |
-
})
|
| 314 |
-
return None
|
| 315 |
-
except Exception as e:
|
| 316 |
-
return f"Failed to update Salesforce: {str(e)}"
|
| 317 |
-
|
| 318 |
-
# Streamlit UI
|
| 319 |
-
with st.form(key="photo_upload_form"):
|
| 320 |
-
project_id = st.text_input("Enter Project ID (e.g., Sunshine Apartments)", "Sunshine Apartments")
|
| 321 |
-
uploaded_file = st.file_uploader("Upload a Construction Photo", type=["jpg", "jpeg", "png"])
|
| 322 |
-
submit_button = st.form_submit_button(label="Upload and Analyze")
|
| 323 |
-
|
| 324 |
-
if submit_button and uploaded_file is not None:
|
| 325 |
# Validate photo size
|
| 326 |
-
|
| 327 |
-
|
| 328 |
-
|
| 329 |
-
|
| 330 |
-
|
| 331 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 332 |
|
| 333 |
-
|
| 334 |
-
|
| 335 |
-
|
| 336 |
-
if error:
|
| 337 |
-
st.error(error)
|
| 338 |
-
# Update Salesforce with failure status
|
| 339 |
-
error_message = update_salesforce_record(
|
| 340 |
-
project_id=project_id,
|
| 341 |
-
milestone=None,
|
| 342 |
-
percentage=0.00,
|
| 343 |
-
image_url=None,
|
| 344 |
-
status="Failure",
|
| 345 |
-
comments=error
|
| 346 |
-
)
|
| 347 |
-
if error_message:
|
| 348 |
-
st.error(error_message)
|
| 349 |
-
else:
|
| 350 |
-
# Upload image to Salesforce
|
| 351 |
-
image_url, upload_error = upload_image_to_salesforce(uploaded_file, project_id)
|
| 352 |
-
|
| 353 |
-
if upload_error:
|
| 354 |
-
st.error(upload_error)
|
| 355 |
-
# Update Salesforce with failure status
|
| 356 |
-
error_message = update_salesforce_record(
|
| 357 |
-
project_id=project_id,
|
| 358 |
-
milestone=milestone,
|
| 359 |
-
percentage=percentage,
|
| 360 |
-
image_url=None,
|
| 361 |
-
status="Failure",
|
| 362 |
-
comments=upload_error
|
| 363 |
-
)
|
| 364 |
-
if error_message:
|
| 365 |
-
st.error(error_message)
|
| 366 |
-
else:
|
| 367 |
-
# Update Salesforce with success
|
| 368 |
-
st.success(f"AI Result: Milestone = {milestone}, Completion = {percentage}%")
|
| 369 |
-
error_message = update_salesforce_record(
|
| 370 |
-
project_id=project_id,
|
| 371 |
-
milestone=milestone,
|
| 372 |
-
percentage=percentage,
|
| 373 |
-
image_url=image_url,
|
| 374 |
-
status="Success",
|
| 375 |
-
comments="Photo processed successfully"
|
| 376 |
-
)
|
| 377 |
-
if error_message:
|
| 378 |
-
st.error(error_message)
|
| 379 |
-
else:
|
| 380 |
-
st.success("Progress saved to Salesforce!")
|
| 381 |
-
else:
|
| 382 |
-
st.info("Please upload a photo to analyze.")
|
|
|
|
| 1 |
+
import gradio as gr
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 2 |
import requests
|
| 3 |
import os
|
| 4 |
from PIL import Image
|
|
|
|
| 12 |
# Load environment variables from .env file
|
| 13 |
load_dotenv()
|
| 14 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 15 |
# Function to validate photo size (< 20MB)
|
| 16 |
def validate_photo_size(image_file):
|
| 17 |
max_size_mb = 20
|
| 18 |
+
if isinstance(image_file, Image.Image):
|
| 19 |
+
# Convert PIL Image to bytes for size check
|
| 20 |
+
img_byte_arr = io.BytesIO()
|
| 21 |
+
image_file.save(img_byte_arr, format='JPEG')
|
| 22 |
+
file_size_mb = img_byte_arr.tell() / (1024 * 1024) # Convert bytes to MB
|
| 23 |
+
return file_size_mb <= max_size_mb, None
|
| 24 |
+
return False, "Invalid image format"
|
| 25 |
|
| 26 |
# Function to process image with AI and predict milestone
|
| 27 |
def predict_milestone(image):
|
|
|
|
| 30 |
start_time = time.time()
|
| 31 |
|
| 32 |
# Process image with Hugging Face model
|
| 33 |
+
model = pipeline("image-classification", model="microsoft/resnet-50")
|
| 34 |
predictions = model(image)
|
| 35 |
|
| 36 |
# Placeholder logic: Map model output to construction milestones
|
|
|
|
| 37 |
milestone = predictions[0]["label"] # Example: "positive" -> "Walls Erected"
|
| 38 |
confidence = predictions[0]["score"]
|
| 39 |
|
|
|
|
| 60 |
return None, None, f"AI failed to process the image: {str(e)}"
|
| 61 |
|
| 62 |
# Function to upload image to Salesforce and get a URL
|
| 63 |
+
def upload_image_to_salesforce(image, project_id):
|
| 64 |
try:
|
| 65 |
# Placeholder: Simulate uploading image to Salesforce ContentVersion
|
|
|
|
|
|
|
| 66 |
image_url = f"https://your-salesforce-instance.com/file/{project_id}.jpg" # Simulated URL
|
| 67 |
return image_url, None
|
| 68 |
except Exception as e:
|
| 69 |
return None, f"Failed to upload image to Salesforce: {str(e)}"
|
| 70 |
|
| 71 |
# Function to update Salesforce Construction_Project__c object
|
| 72 |
+
def update_salesforce_record(sf, project_id, milestone, percentage, image_url, status, comments):
|
| 73 |
try:
|
| 74 |
# Query to check if the project exists
|
| 75 |
query = f"SELECT Id FROM Construction_Project__c WHERE Name = '{project_id}'"
|
|
|
|
| 93 |
except Exception as e:
|
| 94 |
return f"Failed to update Salesforce: {str(e)}"
|
| 95 |
|
| 96 |
+
# Main Gradio function
|
| 97 |
+
def process_construction_photo(project_id, image):
|
| 98 |
+
if not project_id or not image:
|
| 99 |
+
return None, "Please provide a project ID and upload a photo."
|
| 100 |
+
|
| 101 |
+
# Connect to Salesforce
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 102 |
try:
|
| 103 |
+
sf = Salesforce(
|
| 104 |
+
username=os.getenv('SALESFORCE_USERNAME'),
|
| 105 |
+
password=os.getenv('SALESFORCE_PASSWORD'),
|
| 106 |
+
security_token=os.getenv('SALESFORCE_SECURITY_TOKEN'),
|
| 107 |
+
domain=os.getenv('SALESFORCE_DOMAIN')
|
| 108 |
+
)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 109 |
except Exception as e:
|
| 110 |
+
return None, f"Failed to connect to Salesforce: {str(e)}"
|
| 111 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 112 |
# Validate photo size
|
| 113 |
+
is_valid, error = validate_photo_size(image)
|
| 114 |
+
if not is_valid:
|
| 115 |
+
return None, error or "Photo is too large! Please upload a photo smaller than 20MB."
|
| 116 |
+
|
| 117 |
+
# Process the image with AI
|
| 118 |
+
milestone, percentage, error = predict_milestone(image)
|
| 119 |
+
|
| 120 |
+
if error:
|
| 121 |
+
error_message = update_salesforce_record(
|
| 122 |
+
sf=sf,
|
| 123 |
+
project_id=project_id,
|
| 124 |
+
milestone=None,
|
| 125 |
+
percentage=0.00,
|
| 126 |
+
image_url=None,
|
| 127 |
+
status="Failure",
|
| 128 |
+
comments=error
|
| 129 |
+
)
|
| 130 |
+
return None, f"AI Error: {error}\nSalesforce Error: {error_message}" if error_message else f"AI Error: {error}"
|
| 131 |
+
|
| 132 |
+
# Upload image to Salesforce
|
| 133 |
+
image_url, upload_error = upload_image_to_salesforce(image, project_id)
|
| 134 |
+
|
| 135 |
+
if upload_error:
|
| 136 |
+
error_message = update_salesforce_record(
|
| 137 |
+
sf=sf,
|
| 138 |
+
project_id=project_id,
|
| 139 |
+
milestone=milestone,
|
| 140 |
+
percentage=percentage,
|
| 141 |
+
image_url=None,
|
| 142 |
+
status="Failure",
|
| 143 |
+
comments=upload_error
|
| 144 |
+
)
|
| 145 |
+
return None, f"Upload Error: {upload_error}\nSalesforce Error: {error_message}" if error_message else f"Upload Error: {upload_error}"
|
| 146 |
+
|
| 147 |
+
# Update Salesforce with success
|
| 148 |
+
error_message = update_salesforce_record(
|
| 149 |
+
sf=sf,
|
| 150 |
+
project_id=project_id,
|
| 151 |
+
milestone=milestone,
|
| 152 |
+
percentage=percentage,
|
| 153 |
+
image_url=image_url,
|
| 154 |
+
status="Success",
|
| 155 |
+
comments="Photo processed successfully"
|
| 156 |
+
)
|
| 157 |
+
|
| 158 |
+
if error_message:
|
| 159 |
+
return None, f"Salesforce Error: {error_message}"
|
| 160 |
+
|
| 161 |
+
return image, f"Success! Milestone: {milestone}, Completion: {percentage}%\nProgress saved to Salesforce!"
|
| 162 |
+
|
| 163 |
+
# Gradio interface
|
| 164 |
+
iface = gr.Interface(
|
| 165 |
+
fn=process_construction_photo,
|
| 166 |
+
inputs=[
|
| 167 |
+
gr.Textbox(label="Project ID (e.g., Sunshine Apartments)", placeholder="Sunshine Apartments"),
|
| 168 |
+
gr.Image(type="pil", label="Upload a Construction Photo")
|
| 169 |
+
],
|
| 170 |
+
outputs=[
|
| 171 |
+
gr.Image(label="Uploaded Photo"),
|
| 172 |
+
gr.Textbox(label="Result")
|
| 173 |
+
],
|
| 174 |
+
title="Construction Project Progress Tracker",
|
| 175 |
+
description="Upload a photo of your construction site, and the AI will tell you the progress!"
|
| 176 |
+
)
|
| 177 |
|
| 178 |
+
# Launch the Gradio app
|
| 179 |
+
if __name__ == "__main__":
|
| 180 |
+
iface.launch()
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|