Spaces:
Runtime error
Runtime error
Update app.py
Browse files
app.py
CHANGED
|
@@ -60,23 +60,23 @@ def predict_milestone(image):
|
|
| 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,
|
| 64 |
try:
|
| 65 |
# Placeholder: Simulate uploading image to Salesforce ContentVersion
|
| 66 |
-
image_url = f"https://your-salesforce-instance.com/file/{
|
| 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,
|
| 73 |
try:
|
| 74 |
# Query to check if the project exists
|
| 75 |
-
query = f"SELECT Id FROM Construction_Project__c WHERE Name = '{
|
| 76 |
result = sf.query(query)
|
| 77 |
|
| 78 |
if result['totalSize'] == 0:
|
| 79 |
-
return f"No project found with
|
| 80 |
|
| 81 |
record_id = result['records'][0]['Id']
|
| 82 |
|
|
@@ -89,14 +89,29 @@ def update_salesforce_record(sf, project_id, milestone, percentage, image_url, s
|
|
| 89 |
'Upload_Status__c': status,
|
| 90 |
'Comments__c': comments
|
| 91 |
})
|
| 92 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 93 |
except Exception as e:
|
| 94 |
-
return f"Failed to update Salesforce: {str(e)}"
|
| 95 |
|
| 96 |
# Main Gradio function
|
| 97 |
-
def process_construction_photo(
|
| 98 |
-
if not
|
| 99 |
-
return None, "Please provide a project
|
| 100 |
|
| 101 |
# Connect to Salesforce
|
| 102 |
try:
|
|
@@ -118,36 +133,50 @@ def process_construction_photo(project_id, image):
|
|
| 118 |
milestone, percentage, error = predict_milestone(image)
|
| 119 |
|
| 120 |
if error:
|
| 121 |
-
error_message = update_salesforce_record(
|
| 122 |
sf=sf,
|
| 123 |
-
|
| 124 |
milestone=None,
|
| 125 |
percentage=0.00,
|
| 126 |
image_url=None,
|
| 127 |
status="Failure",
|
| 128 |
comments=error
|
| 129 |
)
|
| 130 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 131 |
|
| 132 |
# Upload image to Salesforce
|
| 133 |
-
image_url, upload_error = upload_image_to_salesforce(image,
|
| 134 |
|
| 135 |
if upload_error:
|
| 136 |
-
error_message = update_salesforce_record(
|
| 137 |
sf=sf,
|
| 138 |
-
|
| 139 |
milestone=milestone,
|
| 140 |
percentage=percentage,
|
| 141 |
image_url=None,
|
| 142 |
status="Failure",
|
| 143 |
comments=upload_error
|
| 144 |
)
|
| 145 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 146 |
|
| 147 |
# Update Salesforce with success
|
| 148 |
-
error_message = update_salesforce_record(
|
| 149 |
sf=sf,
|
| 150 |
-
|
| 151 |
milestone=milestone,
|
| 152 |
percentage=percentage,
|
| 153 |
image_url=image_url,
|
|
@@ -158,13 +187,18 @@ def process_construction_photo(project_id, image):
|
|
| 158 |
if error_message:
|
| 159 |
return None, f"Salesforce Error: {error_message}"
|
| 160 |
|
| 161 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 162 |
|
| 163 |
# Gradio interface
|
| 164 |
iface = gr.Interface(
|
| 165 |
fn=process_construction_photo,
|
| 166 |
inputs=[
|
| 167 |
-
gr.Textbox(label="Project
|
| 168 |
gr.Image(type="pil", label="Upload a Construction Photo")
|
| 169 |
],
|
| 170 |
outputs=[
|
|
|
|
| 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_name):
|
| 64 |
try:
|
| 65 |
# Placeholder: Simulate uploading image to Salesforce ContentVersion
|
| 66 |
+
image_url = f"https://your-salesforce-instance.com/file/{project_name}.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 and fetch fields
|
| 72 |
+
def update_salesforce_record(sf, project_name, 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_name}'"
|
| 76 |
result = sf.query(query)
|
| 77 |
|
| 78 |
if result['totalSize'] == 0:
|
| 79 |
+
return None, f"No project found with Name: {project_name}"
|
| 80 |
|
| 81 |
record_id = result['records'][0]['Id']
|
| 82 |
|
|
|
|
| 89 |
'Upload_Status__c': status,
|
| 90 |
'Comments__c': comments
|
| 91 |
})
|
| 92 |
+
|
| 93 |
+
# Fetch the updated record to get the specified fields
|
| 94 |
+
updated_query = f"SELECT Current_Milestone__c, Last_Updated_Image__c, Last_Updated_On__c, Upload_Status__c FROM Construction_Project__c WHERE Id = '{record_id}'"
|
| 95 |
+
updated_result = sf.query(updated_query)
|
| 96 |
+
|
| 97 |
+
if updated_result['totalSize'] == 0:
|
| 98 |
+
return None, "Failed to retrieve updated record."
|
| 99 |
+
|
| 100 |
+
record = updated_result['records'][0]
|
| 101 |
+
fields_output = {
|
| 102 |
+
'Current_Milestone__c': record.get('Current_Milestone__c', 'N/A'),
|
| 103 |
+
'Last_Updated_Image__c': record.get('Last_Updated_Image__c', 'N/A'),
|
| 104 |
+
'Last_Updated_On__c': record.get('Last_Updated_On__c', 'N/A'),
|
| 105 |
+
'Upload_Status__c': record.get('Upload_Status__c', 'N/A')
|
| 106 |
+
}
|
| 107 |
+
return fields_output, None
|
| 108 |
except Exception as e:
|
| 109 |
+
return None, f"Failed to update Salesforce: {str(e)}"
|
| 110 |
|
| 111 |
# Main Gradio function
|
| 112 |
+
def process_construction_photo(project_name, image):
|
| 113 |
+
if not project_name or not image:
|
| 114 |
+
return None, "Please provide a project name and upload a photo."
|
| 115 |
|
| 116 |
# Connect to Salesforce
|
| 117 |
try:
|
|
|
|
| 133 |
milestone, percentage, error = predict_milestone(image)
|
| 134 |
|
| 135 |
if error:
|
| 136 |
+
fields, error_message = update_salesforce_record(
|
| 137 |
sf=sf,
|
| 138 |
+
project_name=project_name,
|
| 139 |
milestone=None,
|
| 140 |
percentage=0.00,
|
| 141 |
image_url=None,
|
| 142 |
status="Failure",
|
| 143 |
comments=error
|
| 144 |
)
|
| 145 |
+
error_text = f"AI Error: {error}"
|
| 146 |
+
if error_message:
|
| 147 |
+
error_text += f"\nSalesforce Error: {error_message}"
|
| 148 |
+
if fields:
|
| 149 |
+
error_text += "\nUpdated Salesforce Fields:\n"
|
| 150 |
+
for field, value in fields.items():
|
| 151 |
+
error_text += f"{field}: {value}\n"
|
| 152 |
+
return None, error_text
|
| 153 |
|
| 154 |
# Upload image to Salesforce
|
| 155 |
+
image_url, upload_error = upload_image_to_salesforce(image, project_name)
|
| 156 |
|
| 157 |
if upload_error:
|
| 158 |
+
fields, error_message = update_salesforce_record(
|
| 159 |
sf=sf,
|
| 160 |
+
project_name=project_name,
|
| 161 |
milestone=milestone,
|
| 162 |
percentage=percentage,
|
| 163 |
image_url=None,
|
| 164 |
status="Failure",
|
| 165 |
comments=upload_error
|
| 166 |
)
|
| 167 |
+
error_text = f"Upload Error: {upload_error}"
|
| 168 |
+
if error_message:
|
| 169 |
+
error_text += f"\nSalesforce Error: {error_message}"
|
| 170 |
+
if fields:
|
| 171 |
+
error_text += "\nUpdated Salesforce Fields:\n"
|
| 172 |
+
for field, value in fields.items():
|
| 173 |
+
error_text += f"{field}: {value}\n"
|
| 174 |
+
return None, error_text
|
| 175 |
|
| 176 |
# Update Salesforce with success
|
| 177 |
+
fields, error_message = update_salesforce_record(
|
| 178 |
sf=sf,
|
| 179 |
+
project_name=project_name,
|
| 180 |
milestone=milestone,
|
| 181 |
percentage=percentage,
|
| 182 |
image_url=image_url,
|
|
|
|
| 187 |
if error_message:
|
| 188 |
return None, f"Salesforce Error: {error_message}"
|
| 189 |
|
| 190 |
+
# Prepare output with AI results and Salesforce fields
|
| 191 |
+
result_text = f"Success! Milestone: {milestone}, Completion: {percentage}%\nProgress saved to Salesforce!\n\nSalesforce Fields:\n"
|
| 192 |
+
for field, value in fields.items():
|
| 193 |
+
result_text += f"{field}: {value}\n"
|
| 194 |
+
|
| 195 |
+
return image, result_text
|
| 196 |
|
| 197 |
# Gradio interface
|
| 198 |
iface = gr.Interface(
|
| 199 |
fn=process_construction_photo,
|
| 200 |
inputs=[
|
| 201 |
+
gr.Textbox(label="Project Name (e.g., Sunshine Apartments)", placeholder="Sunshine Apartments"),
|
| 202 |
gr.Image(type="pil", label="Upload a Construction Photo")
|
| 203 |
],
|
| 204 |
outputs=[
|