Spaces:
Runtime error
Runtime error
Update app.py
Browse files
app.py
CHANGED
|
@@ -70,7 +70,7 @@ def process_image(image, project_name):
|
|
| 70 |
if not str(image).lower().endswith(('.jpg', '.jpeg', '.png')):
|
| 71 |
return "Error: Only JPG/PNG images are supported.", "Failure", "", "", 0
|
| 72 |
|
| 73 |
-
# Save image to
|
| 74 |
upload_dir = "public_uploads"
|
| 75 |
os.makedirs(upload_dir, exist_ok=True)
|
| 76 |
unique_id = datetime.now().strftime("%Y%m%d%H%M%S")
|
|
@@ -78,14 +78,23 @@ def process_image(image, project_name):
|
|
| 78 |
saved_image_path = os.path.join(upload_dir, image_filename)
|
| 79 |
shutil.copy(image, saved_image_path)
|
| 80 |
|
| 81 |
-
#
|
| 82 |
-
|
| 83 |
-
|
| 84 |
-
|
| 85 |
-
|
| 86 |
-
|
| 87 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 88 |
|
|
|
|
| 89 |
milestone, percent_complete, confidence_score = mock_ai_model(img)
|
| 90 |
|
| 91 |
record = {
|
|
@@ -95,9 +104,10 @@ def process_image(image, project_name):
|
|
| 95 |
"Last_Updated_On__c": datetime.now().isoformat(),
|
| 96 |
"Upload_Status__c": "Success",
|
| 97 |
"Comments__c": f"AI Prediction: {milestone} with {confidence_score*100}% confidence",
|
| 98 |
-
"Last_Updated_Image__c":
|
| 99 |
}
|
| 100 |
|
|
|
|
| 101 |
try:
|
| 102 |
sf.Construction__c.create(record)
|
| 103 |
except Exception as e:
|
|
|
|
| 70 |
if not str(image).lower().endswith(('.jpg', '.jpeg', '.png')):
|
| 71 |
return "Error: Only JPG/PNG images are supported.", "Failure", "", "", 0
|
| 72 |
|
| 73 |
+
# Save image to a temporary file
|
| 74 |
upload_dir = "public_uploads"
|
| 75 |
os.makedirs(upload_dir, exist_ok=True)
|
| 76 |
unique_id = datetime.now().strftime("%Y%m%d%H%M%S")
|
|
|
|
| 78 |
saved_image_path = os.path.join(upload_dir, image_filename)
|
| 79 |
shutil.copy(image, saved_image_path)
|
| 80 |
|
| 81 |
+
# Upload to Salesforce as a ContentVersion
|
| 82 |
+
try:
|
| 83 |
+
with open(saved_image_path, 'rb') as file:
|
| 84 |
+
content_version_data = {
|
| 85 |
+
'Title': image_filename,
|
| 86 |
+
'PathOnClient': image_filename,
|
| 87 |
+
'VersionData': file,
|
| 88 |
+
'FirstPublishLocationId': 'your_sobject_id', # Replace with the appropriate Salesforce object ID (e.g., Project or Construction)
|
| 89 |
+
}
|
| 90 |
+
content_version = sf.ContentVersion.create(content_version_data)
|
| 91 |
+
|
| 92 |
+
# Generate download URL for the uploaded image
|
| 93 |
+
file_url = f"https://{sf.sf_instance}/sfc/servlet.shepherd/version/download/{content_version['id']}"
|
| 94 |
+
except Exception as e:
|
| 95 |
+
return f"Error: Failed to upload image to Salesforce - {str(e)}", "Failure", "", "", 0
|
| 96 |
|
| 97 |
+
# AI prediction
|
| 98 |
milestone, percent_complete, confidence_score = mock_ai_model(img)
|
| 99 |
|
| 100 |
record = {
|
|
|
|
| 104 |
"Last_Updated_On__c": datetime.now().isoformat(),
|
| 105 |
"Upload_Status__c": "Success",
|
| 106 |
"Comments__c": f"AI Prediction: {milestone} with {confidence_score*100}% confidence",
|
| 107 |
+
"Last_Updated_Image__c": file_url
|
| 108 |
}
|
| 109 |
|
| 110 |
+
# Insert record into Salesforce
|
| 111 |
try:
|
| 112 |
sf.Construction__c.create(record)
|
| 113 |
except Exception as e:
|