Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
|
@@ -157,26 +157,27 @@ def push_report_to_salesforce(violations, score, pdf_path, pdf_file):
|
|
| 157 |
pdf_url = f"{CONFIG['PUBLIC_URL_BASE']}{os.path.basename(pdf_path)}" if pdf_path else ""
|
| 158 |
|
| 159 |
record_data = {
|
| 160 |
-
"Compliance_Score__c":
|
| 161 |
-
|
| 162 |
-
|
| 163 |
-
|
| 164 |
-
|
| 165 |
-
|
| 166 |
-
|
| 167 |
-
|
| 168 |
-
|
|
|
|
| 169 |
|
| 170 |
-
|
| 171 |
-
|
| 172 |
-
|
| 173 |
-
|
| 174 |
-
|
| 175 |
|
| 176 |
-
|
| 177 |
-
|
| 178 |
-
|
| 179 |
-
|
| 180 |
|
| 181 |
# ==========================
|
| 182 |
# Safety Score Calculation
|
|
@@ -279,26 +280,35 @@ def process_video(video_data):
|
|
| 279 |
# ==========================
|
| 280 |
def gradio_interface(video_file):
|
| 281 |
if not video_file:
|
| 282 |
-
return "No
|
| 283 |
try:
|
| 284 |
with open(video_file, "rb") as f:
|
| 285 |
video_data = f.read()
|
| 286 |
result = process_video(video_data)
|
| 287 |
-
|
| 288 |
-
#
|
| 289 |
-
|
| 290 |
-
|
| 291 |
-
|
| 292 |
-
|
| 293 |
-
|
| 294 |
-
|
| 295 |
-
|
| 296 |
-
|
| 297 |
-
|
| 298 |
-
|
| 299 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 300 |
return (
|
| 301 |
-
|
| 302 |
f"Safety Score: {result['score']}%",
|
| 303 |
snapshots_text,
|
| 304 |
f"Salesforce Record ID: {result['salesforce_record_id'] or 'N/A'}",
|
|
@@ -307,15 +317,15 @@ def gradio_interface(video_file):
|
|
| 307 |
)
|
| 308 |
except Exception as e:
|
| 309 |
logger.error(f"Error in Gradio interface: {e}")
|
| 310 |
-
return f"Error: {str(e)}", "", "
|
| 311 |
|
| 312 |
interface = gr.Interface(
|
| 313 |
fn=gradio_interface,
|
| 314 |
inputs=gr.Video(label="Upload Site Video"),
|
| 315 |
outputs=[
|
| 316 |
-
gr.
|
| 317 |
gr.Textbox(label="Compliance Score"),
|
| 318 |
-
gr.
|
| 319 |
gr.Textbox(label="Salesforce Record ID"),
|
| 320 |
gr.Textbox(label="Violation Details URL"),
|
| 321 |
gr.Textbox(label="Violation Details Content")
|
|
|
|
| 157 |
pdf_url = f"{CONFIG['PUBLIC_URL_BASE']}{os.path.basename(pdf_path)}" if pdf_path else ""
|
| 158 |
|
| 159 |
record_data = {
|
| 160 |
+
"Compliance_Score__c": scorea) {
|
| 161 |
+
"Compliance_Score__c": score,
|
| 162 |
+
"Violations_Found__c": len(violations),
|
| 163 |
+
"Violations_Details__c": violations_text,
|
| 164 |
+
"Status__c": "Pending",
|
| 165 |
+
"PDF_Report_URL__c": pdf_url
|
| 166 |
+
}
|
| 167 |
+
record = sf.Safety_Video_Report__c.create(record_data)
|
| 168 |
+
record_id = record["id"]
|
| 169 |
+
logger.info(f"Salesforce record created: {record_id}")
|
| 170 |
|
| 171 |
+
if pdf_file:
|
| 172 |
+
uploaded_url = upload_pdf_to_salesforce(sf, pdf_file, record_id)
|
| 173 |
+
if uploaded_url:
|
| 174 |
+
sf.Safety_Video_Report__c.update(record_id, {"PDF_Report_URL__c": uploaded_url})
|
| 175 |
+
pdf_url = uploaded_url
|
| 176 |
|
| 177 |
+
return record_id, pdf_url
|
| 178 |
+
except Exception as e:
|
| 179 |
+
logger.error(f"Salesforce record creation failed: {e}")
|
| 180 |
+
return None, ""
|
| 181 |
|
| 182 |
# ==========================
|
| 183 |
# Safety Score Calculation
|
|
|
|
| 280 |
# ==========================
|
| 281 |
def gradio_interface(video_file):
|
| 282 |
if not video_file:
|
| 283 |
+
return "No file uploaded.", "", "No file uploaded.", "", "", "No file uploaded."
|
| 284 |
try:
|
| 285 |
with open(video_file, "rb") as f:
|
| 286 |
video_data = f.read()
|
| 287 |
result = process_video(video_data)
|
| 288 |
+
|
| 289 |
+
# Format violations as a Markdown table
|
| 290 |
+
violation_table = "No violations detected."
|
| 291 |
+
if result["violations"]:
|
| 292 |
+
header = "| Violation | Timestamp | Confidence | Bounding Box |\n"
|
| 293 |
+
separator = "|---------------|-----------|------------|--------------------------|\n"
|
| 294 |
+
rows = []
|
| 295 |
+
for v in result["violations"]:
|
| 296 |
+
# Simplify violation names (e.g., "no_helmet" to "helmet")
|
| 297 |
+
violation_name = v["violation"].replace("no_", "").replace("unsafe_", "")
|
| 298 |
+
row = f"| {violation_name:<13} | {v['timestamp']:.2f}s | {v['confidence']:.2f} | {v['bounding_box']} |"
|
| 299 |
+
rows.append(row)
|
| 300 |
+
violation ladies = header + separator + "\n".join(rows)
|
| 301 |
+
|
| 302 |
+
# Format snapshots as a bullet list
|
| 303 |
+
snapshots_text = "No snapshots captured."
|
| 304 |
+
if result["snapshots"]:
|
| 305 |
+
snapshots_text = "\n".join(
|
| 306 |
+
f"- Snapshot for {s['violation'].replace('no_', '').replace('unsafe_', '')} at frame {s['frame']}: {s['snapshot_url']}"
|
| 307 |
+
for s in result["snapshots"]
|
| 308 |
+
)
|
| 309 |
+
|
| 310 |
return (
|
| 311 |
+
violation_table,
|
| 312 |
f"Safety Score: {result['score']}%",
|
| 313 |
snapshots_text,
|
| 314 |
f"Salesforce Record ID: {result['salesforce_record_id'] or 'N/A'}",
|
|
|
|
| 317 |
)
|
| 318 |
except Exception as e:
|
| 319 |
logger.error(f"Error in Gradio interface: {e}")
|
| 320 |
+
return f"Error: {str(e)}", "", "Error in processing.", "", "", "Error in processing."
|
| 321 |
|
| 322 |
interface = gr.Interface(
|
| 323 |
fn=gradio_interface,
|
| 324 |
inputs=gr.Video(label="Upload Site Video"),
|
| 325 |
outputs=[
|
| 326 |
+
gr.Markdown(label="Detected Safety Violations"),
|
| 327 |
gr.Textbox(label="Compliance Score"),
|
| 328 |
+
gr.Markdown(label="Snapshots"),
|
| 329 |
gr.Textbox(label="Salesforce Record ID"),
|
| 330 |
gr.Textbox(label="Violation Details URL"),
|
| 331 |
gr.Textbox(label="Violation Details Content")
|