Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
|
@@ -84,12 +84,14 @@ def create_pdf_report(coverage_percent, extracted_texts, annotated_image_path, o
|
|
| 84 |
|
| 85 |
def upload_image_and_get_url(image_path):
|
| 86 |
"""
|
| 87 |
-
|
| 88 |
-
|
| 89 |
"""
|
| 90 |
-
|
| 91 |
-
|
| 92 |
-
|
|
|
|
|
|
|
| 93 |
|
| 94 |
def save_record_to_salesforce(annotated_image_url, coverage_percent, original_image_pil, compliance_threshold=80):
|
| 95 |
sf = Salesforce(
|
|
@@ -99,10 +101,12 @@ def save_record_to_salesforce(annotated_image_url, coverage_percent, original_im
|
|
| 99 |
domain=os.environ.get('SF_DOMAIN', 'login') # 'test' for sandbox
|
| 100 |
)
|
| 101 |
|
| 102 |
-
# Encode original image to base64 string for storage
|
| 103 |
buffered = io.BytesIO()
|
| 104 |
original_image_pil.save(buffered, format="JPEG")
|
| 105 |
-
|
|
|
|
|
|
|
| 106 |
|
| 107 |
compliance_status = 'Pass' if coverage_percent >= compliance_threshold else 'Fail'
|
| 108 |
technician_id = os.environ.get('SF_TECHNICIAN_ID') # Salesforce UserId lookup
|
|
@@ -113,7 +117,7 @@ def save_record_to_salesforce(annotated_image_url, coverage_percent, original_im
|
|
| 113 |
'Name': record_name,
|
| 114 |
'Annotated_Image__c': annotated_image_url,
|
| 115 |
'Coverage_Percentage__c': round(coverage_percent, 2),
|
| 116 |
-
'Original_Image__c':
|
| 117 |
'Compliance_Status__c': compliance_status,
|
| 118 |
'Technician_ID__c': technician_id,
|
| 119 |
'Verified_On__c': datetime.utcnow().isoformat()
|
|
@@ -151,7 +155,7 @@ def process_image(input_img, brightness_threshold=150):
|
|
| 151 |
temp_pdf_file.close()
|
| 152 |
create_pdf_report(coverage_percent, extracted_texts, annotated_img_path, temp_pdf_file.name)
|
| 153 |
|
| 154 |
-
# Upload annotated image and get URL
|
| 155 |
annotated_image_url = upload_image_and_get_url(annotated_img_path)
|
| 156 |
|
| 157 |
# Save record in Salesforce
|
|
|
|
| 84 |
|
| 85 |
def upload_image_and_get_url(image_path):
|
| 86 |
"""
|
| 87 |
+
Instead of uploading, encode image file as base64 data URI
|
| 88 |
+
and return as a valid URL string to store in Salesforce.
|
| 89 |
"""
|
| 90 |
+
with open(image_path, "rb") as img_file:
|
| 91 |
+
img_bytes = img_file.read()
|
| 92 |
+
encoded_str = base64.b64encode(img_bytes).decode('utf-8')
|
| 93 |
+
data_uri = f"data:image/jpeg;base64,{encoded_str}"
|
| 94 |
+
return data_uri
|
| 95 |
|
| 96 |
def save_record_to_salesforce(annotated_image_url, coverage_percent, original_image_pil, compliance_threshold=80):
|
| 97 |
sf = Salesforce(
|
|
|
|
| 101 |
domain=os.environ.get('SF_DOMAIN', 'login') # 'test' for sandbox
|
| 102 |
)
|
| 103 |
|
| 104 |
+
# Encode original image to base64 data URI string for storage
|
| 105 |
buffered = io.BytesIO()
|
| 106 |
original_image_pil.save(buffered, format="JPEG")
|
| 107 |
+
original_img_bytes = buffered.getvalue()
|
| 108 |
+
original_img_b64 = base64.b64encode(original_img_bytes).decode('utf-8')
|
| 109 |
+
original_img_data_uri = f"data:image/jpeg;base64,{original_img_b64}"
|
| 110 |
|
| 111 |
compliance_status = 'Pass' if coverage_percent >= compliance_threshold else 'Fail'
|
| 112 |
technician_id = os.environ.get('SF_TECHNICIAN_ID') # Salesforce UserId lookup
|
|
|
|
| 117 |
'Name': record_name,
|
| 118 |
'Annotated_Image__c': annotated_image_url,
|
| 119 |
'Coverage_Percentage__c': round(coverage_percent, 2),
|
| 120 |
+
'Original_Image__c': original_img_data_uri,
|
| 121 |
'Compliance_Status__c': compliance_status,
|
| 122 |
'Technician_ID__c': technician_id,
|
| 123 |
'Verified_On__c': datetime.utcnow().isoformat()
|
|
|
|
| 155 |
temp_pdf_file.close()
|
| 156 |
create_pdf_report(coverage_percent, extracted_texts, annotated_img_path, temp_pdf_file.name)
|
| 157 |
|
| 158 |
+
# Upload annotated image and get URL (now base64 data URI)
|
| 159 |
annotated_image_url = upload_image_and_get_url(annotated_img_path)
|
| 160 |
|
| 161 |
# Save record in Salesforce
|