Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
|
@@ -126,43 +126,65 @@ def calculate_safety_score(violations):
|
|
| 126 |
base_score -= penalties.get(v["violation"], 0)
|
| 127 |
return max(base_score, 0)
|
| 128 |
|
| 129 |
-
# ==========================
|
| 130 |
-
# Send Results to Salesforce
|
| 131 |
-
# ==========================
|
| 132 |
def send_to_salesforce(violations, score, video_path):
|
| 133 |
-
#
|
| 134 |
-
|
| 135 |
-
|
| 136 |
-
|
| 137 |
-
|
| 138 |
-
status = "Reviewed" # Update this based on the processing state
|
| 139 |
|
| 140 |
-
#
|
| 141 |
-
|
| 142 |
|
|
|
|
| 143 |
violations_details = "\n".join([f"Frame {v['frame']}: {v['violation']} (Confidence: {v['confidence']})" for v in violations])
|
| 144 |
|
|
|
|
| 145 |
data = {
|
| 146 |
-
'Site__c': site_name,
|
| 147 |
-
'Uploaded_By__c': uploaded_by,
|
| 148 |
-
'Upload_Date__c': upload_date,
|
| 149 |
'Status__c': status,
|
| 150 |
'Compliance_Score__c': score,
|
| 151 |
'Violations_Found__c': len(violations),
|
| 152 |
'Violations_Details__c': violations_details,
|
| 153 |
-
'Video_File__c':
|
| 154 |
-
# Optionally add a URL for the PDF report
|
| 155 |
'PDF_Report_URL__c': "http://path_to_pdf_report" # Replace with actual PDF report URL if available
|
| 156 |
}
|
| 157 |
|
| 158 |
try:
|
| 159 |
-
# Insert new record in Salesforce
|
| 160 |
result = sf.Safety_Video_Reports__c.create(data)
|
| 161 |
print(f"✅ Successfully created new report in Salesforce with ID: {result['id']}")
|
| 162 |
except Exception as e:
|
| 163 |
print(f"❌ Error creating/updating record in Salesforce: {e}")
|
| 164 |
|
| 165 |
# ==========================
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 166 |
# Gradio Interface
|
| 167 |
# ==========================
|
| 168 |
def gradio_interface(video_file):
|
|
|
|
| 126 |
base_score -= penalties.get(v["violation"], 0)
|
| 127 |
return max(base_score, 0)
|
| 128 |
|
|
|
|
|
|
|
|
|
|
| 129 |
def send_to_salesforce(violations, score, video_path):
|
| 130 |
+
# Dynamic values (e.g., from user input)
|
| 131 |
+
site_name = "Construction Site 1" # Replace with dynamic data from your app
|
| 132 |
+
uploaded_by = "JohnDoe" # Replace with actual user info (e.g., from session or UI)
|
| 133 |
+
upload_date = "2025-05-08T12:00:00Z" # Replace with actual upload date
|
| 134 |
+
status = "Reviewed" # Update based on video processing result
|
|
|
|
| 135 |
|
| 136 |
+
# Video file upload
|
| 137 |
+
file_id = upload_video_to_salesforce(video_path) # Upload the video and get the file ID
|
| 138 |
|
| 139 |
+
# Violations details: Frame number, violation type, confidence level
|
| 140 |
violations_details = "\n".join([f"Frame {v['frame']}: {v['violation']} (Confidence: {v['confidence']})" for v in violations])
|
| 141 |
|
| 142 |
+
# Data to be inserted into Salesforce
|
| 143 |
data = {
|
| 144 |
+
'Site__c': site_name,
|
| 145 |
+
'Uploaded_By__c': uploaded_by,
|
| 146 |
+
'Upload_Date__c': upload_date,
|
| 147 |
'Status__c': status,
|
| 148 |
'Compliance_Score__c': score,
|
| 149 |
'Violations_Found__c': len(violations),
|
| 150 |
'Violations_Details__c': violations_details,
|
| 151 |
+
'Video_File__c': file_id, # This is the file ID from ContentVersion
|
|
|
|
| 152 |
'PDF_Report_URL__c': "http://path_to_pdf_report" # Replace with actual PDF report URL if available
|
| 153 |
}
|
| 154 |
|
| 155 |
try:
|
| 156 |
+
# Insert a new record in Salesforce
|
| 157 |
result = sf.Safety_Video_Reports__c.create(data)
|
| 158 |
print(f"✅ Successfully created new report in Salesforce with ID: {result['id']}")
|
| 159 |
except Exception as e:
|
| 160 |
print(f"❌ Error creating/updating record in Salesforce: {e}")
|
| 161 |
|
| 162 |
# ==========================
|
| 163 |
+
# Function to Upload Video to Salesforce
|
| 164 |
+
# ==========================
|
| 165 |
+
def upload_video_to_salesforce(video_path):
|
| 166 |
+
# Open the video file and read it as a binary file
|
| 167 |
+
with open(video_path, 'rb') as file:
|
| 168 |
+
video_data = file.read()
|
| 169 |
+
|
| 170 |
+
# Upload the video as a ContentVersion record in Salesforce
|
| 171 |
+
content_version_data = {
|
| 172 |
+
'Title': 'Safety Video', # Title of the file
|
| 173 |
+
'PathOnClient': 'safety_video.mp4', # Path of the file on the client's system
|
| 174 |
+
'VersionData': video_data, # Video file content as binary
|
| 175 |
+
'FirstPublishLocationId': 'your_salesforce_library_id', # Library ID (if required, or leave blank)
|
| 176 |
+
}
|
| 177 |
+
|
| 178 |
+
try:
|
| 179 |
+
# Create a new ContentVersion record to upload the video
|
| 180 |
+
content_version = sf.ContentVersion.create(content_version_data)
|
| 181 |
+
file_id = content_version['Id'] # This is the ContentDocument ID
|
| 182 |
+
print(f"✅ Video uploaded to Salesforce with File ID: {file_id}")
|
| 183 |
+
return file_id
|
| 184 |
+
except Exception as e:
|
| 185 |
+
print(f"❌ Error uploading video to Salesforce: {e}")
|
| 186 |
+
return None
|
| 187 |
+
# ==========================
|
| 188 |
# Gradio Interface
|
| 189 |
# ==========================
|
| 190 |
def gradio_interface(video_file):
|