Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
|
@@ -2,12 +2,11 @@ import gradio as gr
|
|
| 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 |
from dotenv import load_dotenv
|
|
|
|
|
|
|
| 11 |
|
| 12 |
# Load environment variables from .env file
|
| 13 |
load_dotenv()
|
|
@@ -26,22 +25,30 @@ def validate_photo_size(image_file):
|
|
| 26 |
def predict_milestone(image):
|
| 27 |
try:
|
| 28 |
start_time = time.time()
|
|
|
|
| 29 |
model = pipeline("image-classification", model="microsoft/resnet-50")
|
| 30 |
predictions = model(image)
|
| 31 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 32 |
confidence = predictions[0]["score"]
|
| 33 |
|
|
|
|
| 34 |
milestone_map = {
|
| 35 |
-
"
|
| 36 |
-
"
|
| 37 |
}
|
| 38 |
completion_map = {
|
| 39 |
-
"
|
| 40 |
-
"
|
| 41 |
}
|
| 42 |
|
| 43 |
-
|
| 44 |
-
|
|
|
|
| 45 |
|
| 46 |
processing_time = time.time() - start_time
|
| 47 |
if processing_time > 5:
|
|
@@ -51,17 +58,20 @@ def predict_milestone(image):
|
|
| 51 |
except Exception as e:
|
| 52 |
return None, None, f"AI failed to process the image: {str(e)}"
|
| 53 |
|
| 54 |
-
# Placeholder for image upload to Salesforce (
|
| 55 |
def upload_image_to_salesforce(image, project_name):
|
| 56 |
try:
|
|
|
|
| 57 |
image_url = f"https://your-salesforce-instance.com/file/{project_name}.jpg"
|
| 58 |
return image_url, None
|
| 59 |
except Exception as e:
|
| 60 |
return None, f"Failed to upload image to Salesforce: {str(e)}"
|
| 61 |
|
| 62 |
-
# Function to update Salesforce
|
| 63 |
def update_salesforce_record(sf, project_name, milestone, percentage, image_url, status, comments):
|
| 64 |
try:
|
|
|
|
|
|
|
| 65 |
query = f"SELECT Id FROM Construction_Progress__c WHERE Name = '{project_name}'"
|
| 66 |
result = sf.query(query)
|
| 67 |
|
|
@@ -70,15 +80,17 @@ def update_salesforce_record(sf, project_name, milestone, percentage, image_url,
|
|
| 70 |
|
| 71 |
record_id = result['records'][0]['Id']
|
| 72 |
|
|
|
|
| 73 |
sf.Construction_Progress__c.update(record_id, {
|
| 74 |
-
'Current_Milestone__c': milestone,
|
| 75 |
-
'Completion_Percentage__c': percentage,
|
| 76 |
-
'Last_Updated_Image__c': image_url,
|
| 77 |
'Last_Updated_On__c': time.strftime('%Y-%m-%dT%H:%M:%SZ', time.gmtime()),
|
| 78 |
'Upload_Status__c': status,
|
| 79 |
-
'Comments__c': comments
|
| 80 |
})
|
| 81 |
|
|
|
|
| 82 |
updated_query = f"""
|
| 83 |
SELECT Current_Milestone__c, Last_Updated_Image__c, Last_Updated_On__c, Upload_Status__c
|
| 84 |
FROM Construction_Progress__c WHERE Id = '{record_id}'
|
|
@@ -104,20 +116,23 @@ def process_construction_photo(project_name, image):
|
|
| 104 |
if not project_name or not image:
|
| 105 |
return None, "Please provide a project name and upload a photo."
|
| 106 |
|
|
|
|
| 107 |
try:
|
| 108 |
sf = Salesforce(
|
| 109 |
username=os.getenv('SALESFORCE_USERNAME'),
|
| 110 |
password=os.getenv('SALESFORCE_PASSWORD'),
|
| 111 |
security_token=os.getenv('SALESFORCE_SECURITY_TOKEN'),
|
| 112 |
-
domain=os.getenv('SALESFORCE_DOMAIN')
|
| 113 |
)
|
| 114 |
except Exception as e:
|
| 115 |
return None, f"Failed to connect to Salesforce: {str(e)}"
|
| 116 |
|
|
|
|
| 117 |
is_valid, error = validate_photo_size(image)
|
| 118 |
if not is_valid:
|
| 119 |
return None, error or "Photo is too large! Please upload a photo smaller than 20MB."
|
| 120 |
|
|
|
|
| 121 |
milestone, percentage, error = predict_milestone(image)
|
| 122 |
|
| 123 |
if error:
|
|
@@ -139,6 +154,7 @@ def process_construction_photo(project_name, image):
|
|
| 139 |
error_text += f"{field}: {value}\n"
|
| 140 |
return None, error_text
|
| 141 |
|
|
|
|
| 142 |
image_url, upload_error = upload_image_to_salesforce(image, project_name)
|
| 143 |
|
| 144 |
if upload_error:
|
|
@@ -160,6 +176,7 @@ def process_construction_photo(project_name, image):
|
|
| 160 |
error_text += f"{field}: {value}\n"
|
| 161 |
return None, error_text
|
| 162 |
|
|
|
|
| 163 |
fields, error_message = update_salesforce_record(
|
| 164 |
sf=sf,
|
| 165 |
project_name=project_name,
|
|
@@ -173,6 +190,7 @@ def process_construction_photo(project_name, image):
|
|
| 173 |
if error_message:
|
| 174 |
return None, f"Salesforce Error: {error_message}"
|
| 175 |
|
|
|
|
| 176 |
result_text = f"Success! Milestone: {milestone}, Completion: {percentage}%\nProgress saved to Salesforce!\n\nSalesforce Fields:\n"
|
| 177 |
for field, value in fields.items():
|
| 178 |
result_text += f"{field}: {value}\n"
|
|
@@ -195,4 +213,4 @@ iface = gr.Interface(
|
|
| 195 |
)
|
| 196 |
|
| 197 |
if __name__ == "__main__":
|
| 198 |
-
iface.launch()
|
|
|
|
| 2 |
import requests
|
| 3 |
import os
|
| 4 |
from PIL import Image
|
|
|
|
|
|
|
|
|
|
| 5 |
import io
|
| 6 |
import time
|
| 7 |
from dotenv import load_dotenv
|
| 8 |
+
from simple_salesforce import Salesforce
|
| 9 |
+
from transformers import pipeline
|
| 10 |
|
| 11 |
# Load environment variables from .env file
|
| 12 |
load_dotenv()
|
|
|
|
| 25 |
def predict_milestone(image):
|
| 26 |
try:
|
| 27 |
start_time = time.time()
|
| 28 |
+
# Use a simpler model for demo purposes; replace with a construction-specific model if available
|
| 29 |
model = pipeline("image-classification", model="microsoft/resnet-50")
|
| 30 |
predictions = model(image)
|
| 31 |
+
|
| 32 |
+
# Debugging: Print predictions to understand the output
|
| 33 |
+
print("Model predictions:", predictions)
|
| 34 |
+
|
| 35 |
+
# Assuming the model returns labels like 'building' or 'construction', map them accordingly
|
| 36 |
+
top_prediction = predictions[0]["label"]
|
| 37 |
confidence = predictions[0]["score"]
|
| 38 |
|
| 39 |
+
# Updated milestone mapping based on possible ResNet-50 labels
|
| 40 |
milestone_map = {
|
| 41 |
+
"building": "Walls Erected",
|
| 42 |
+
"structure": "Foundation Completed",
|
| 43 |
}
|
| 44 |
completion_map = {
|
| 45 |
+
"building": 60.00,
|
| 46 |
+
"structure": 20.00,
|
| 47 |
}
|
| 48 |
|
| 49 |
+
# Fallback to a default if the label isn't in the map
|
| 50 |
+
predicted_milestone = milestone_map.get(top_prediction, "Unknown Milestone")
|
| 51 |
+
completion_percentage = completion_map.get(top_prediction, 0.00)
|
| 52 |
|
| 53 |
processing_time = time.time() - start_time
|
| 54 |
if processing_time > 5:
|
|
|
|
| 58 |
except Exception as e:
|
| 59 |
return None, None, f"AI failed to process the image: {str(e)}"
|
| 60 |
|
| 61 |
+
# Placeholder for image upload to Salesforce (replace with actual logic if needed)
|
| 62 |
def upload_image_to_salesforce(image, project_name):
|
| 63 |
try:
|
| 64 |
+
# Simulate uploading the image to Salesforce (replace with actual Salesforce file upload logic)
|
| 65 |
image_url = f"https://your-salesforce-instance.com/file/{project_name}.jpg"
|
| 66 |
return image_url, None
|
| 67 |
except Exception as e:
|
| 68 |
return None, f"Failed to upload image to Salesforce: {str(e)}"
|
| 69 |
|
| 70 |
+
# Function to update Salesforce custom object record
|
| 71 |
def update_salesforce_record(sf, project_name, milestone, percentage, image_url, status, comments):
|
| 72 |
try:
|
| 73 |
+
# Ensure the custom object name matches your Salesforce setup
|
| 74 |
+
# Replace 'Construction_Progress__c' with the correct API name of your custom object
|
| 75 |
query = f"SELECT Id FROM Construction_Progress__c WHERE Name = '{project_name}'"
|
| 76 |
result = sf.query(query)
|
| 77 |
|
|
|
|
| 80 |
|
| 81 |
record_id = result['records'][0]['Id']
|
| 82 |
|
| 83 |
+
# Update the record with the correct field API names
|
| 84 |
sf.Construction_Progress__c.update(record_id, {
|
| 85 |
+
'Current_Milestone__c': milestone if milestone else 'N/A',
|
| 86 |
+
'Completion_Percentage__c': float(percentage) if percentage else 0.0,
|
| 87 |
+
'Last_Updated_Image__c': image_url if image_url else 'N/A',
|
| 88 |
'Last_Updated_On__c': time.strftime('%Y-%m-%dT%H:%M:%SZ', time.gmtime()),
|
| 89 |
'Upload_Status__c': status,
|
| 90 |
+
'Comments__c': comments if comments else 'No comments'
|
| 91 |
})
|
| 92 |
|
| 93 |
+
# Fetch the updated record
|
| 94 |
updated_query = f"""
|
| 95 |
SELECT Current_Milestone__c, Last_Updated_Image__c, Last_Updated_On__c, Upload_Status__c
|
| 96 |
FROM Construction_Progress__c WHERE Id = '{record_id}'
|
|
|
|
| 116 |
if not project_name or not image:
|
| 117 |
return None, "Please provide a project name and upload a photo."
|
| 118 |
|
| 119 |
+
# Authenticate with Salesforce
|
| 120 |
try:
|
| 121 |
sf = Salesforce(
|
| 122 |
username=os.getenv('SALESFORCE_USERNAME'),
|
| 123 |
password=os.getenv('SALESFORCE_PASSWORD'),
|
| 124 |
security_token=os.getenv('SALESFORCE_SECURITY_TOKEN'),
|
| 125 |
+
domain=os.getenv('SALESFORCE_DOMAIN', 'login') # Default to 'login' if not specified
|
| 126 |
)
|
| 127 |
except Exception as e:
|
| 128 |
return None, f"Failed to connect to Salesforce: {str(e)}"
|
| 129 |
|
| 130 |
+
# Validate photo size
|
| 131 |
is_valid, error = validate_photo_size(image)
|
| 132 |
if not is_valid:
|
| 133 |
return None, error or "Photo is too large! Please upload a photo smaller than 20MB."
|
| 134 |
|
| 135 |
+
# Predict milestone using AI
|
| 136 |
milestone, percentage, error = predict_milestone(image)
|
| 137 |
|
| 138 |
if error:
|
|
|
|
| 154 |
error_text += f"{field}: {value}\n"
|
| 155 |
return None, error_text
|
| 156 |
|
| 157 |
+
# Upload image to Salesforce
|
| 158 |
image_url, upload_error = upload_image_to_salesforce(image, project_name)
|
| 159 |
|
| 160 |
if upload_error:
|
|
|
|
| 176 |
error_text += f"{field}: {value}\n"
|
| 177 |
return None, error_text
|
| 178 |
|
| 179 |
+
# Update Salesforce record
|
| 180 |
fields, error_message = update_salesforce_record(
|
| 181 |
sf=sf,
|
| 182 |
project_name=project_name,
|
|
|
|
| 190 |
if error_message:
|
| 191 |
return None, f"Salesforce Error: {error_message}"
|
| 192 |
|
| 193 |
+
# Prepare success message
|
| 194 |
result_text = f"Success! Milestone: {milestone}, Completion: {percentage}%\nProgress saved to Salesforce!\n\nSalesforce Fields:\n"
|
| 195 |
for field, value in fields.items():
|
| 196 |
result_text += f"{field}: {value}\n"
|
|
|
|
| 213 |
)
|
| 214 |
|
| 215 |
if __name__ == "__main__":
|
| 216 |
+
iface.launch()
|