Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
|
@@ -5,7 +5,6 @@ import torch
|
|
| 5 |
import numpy as np
|
| 6 |
from ultralytics import YOLO
|
| 7 |
import time
|
| 8 |
-
import base64
|
| 9 |
from simple_salesforce import Salesforce
|
| 10 |
|
| 11 |
# ==========================
|
|
@@ -69,19 +68,31 @@ def violations_to_text(violations):
|
|
| 69 |
|
| 70 |
def generate_violation_text_file(violations):
|
| 71 |
try:
|
|
|
|
|
|
|
|
|
|
|
|
|
| 72 |
text_filename = f"violations_{int(time.time())}.txt"
|
| 73 |
text_path = os.path.join(STATIC_OUTPUT_DIR, text_filename)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 74 |
with open(text_path, "w") as f:
|
| 75 |
-
f.write(
|
| 76 |
-
|
| 77 |
-
f.write(violations_to_text(violations))
|
| 78 |
print(f"✅ Text file generated at {text_path}")
|
|
|
|
|
|
|
|
|
|
| 79 |
public_url = f"https://huggingface.co/spaces/PrashanthB461/AI_Safety_Demo1/resolve/main/static/output/{text_filename}"
|
| 80 |
print(f"✅ Text file URL: {public_url}")
|
| 81 |
-
return text_path, public_url
|
| 82 |
except Exception as e:
|
| 83 |
print(f"❌ Error generating text file: {e}")
|
| 84 |
-
return "", ""
|
| 85 |
|
| 86 |
def push_report_to_salesforce(score, violations, text_filename):
|
| 87 |
try:
|
|
@@ -103,7 +114,7 @@ def push_report_to_salesforce(score, violations, text_filename):
|
|
| 103 |
print(f"Attempting to create Salesforce record with data: {record_data}")
|
| 104 |
record = sf.Safety_Video_Report__c.create(record_data)
|
| 105 |
print(f"✅ Salesforce record created with Id: {record.get('id')}")
|
| 106 |
-
return record.get(
|
| 107 |
except Exception as e:
|
| 108 |
print(f"❌ Salesforce record creation failed: {e}")
|
| 109 |
return None
|
|
@@ -193,7 +204,7 @@ def process_video(video_data, frame_skip=5, max_frames=100):
|
|
| 193 |
os.remove(video_path)
|
| 194 |
|
| 195 |
score = calculate_safety_score(violations)
|
| 196 |
-
text_path, text_url = generate_violation_text_file(violations)
|
| 197 |
|
| 198 |
if text_path:
|
| 199 |
report_id = push_report_to_salesforce(score, violations, text_path)
|
|
@@ -207,7 +218,8 @@ def process_video(video_data, frame_skip=5, max_frames=100):
|
|
| 207 |
"snapshots": snapshots,
|
| 208 |
"score": score,
|
| 209 |
"salesforce_record_id": report_id,
|
| 210 |
-
"violation_details_url": text_url
|
|
|
|
| 211 |
}
|
| 212 |
|
| 213 |
except Exception as e:
|
|
@@ -218,6 +230,7 @@ def process_video(video_data, frame_skip=5, max_frames=100):
|
|
| 218 |
"score": 0,
|
| 219 |
"salesforce_record_id": None,
|
| 220 |
"violation_details_url": "",
|
|
|
|
| 221 |
"error": str(e)
|
| 222 |
}
|
| 223 |
|
|
@@ -227,7 +240,7 @@ def process_video(video_data, frame_skip=5, max_frames=100):
|
|
| 227 |
def gradio_interface(video_file):
|
| 228 |
try:
|
| 229 |
if not video_file:
|
| 230 |
-
return {"error": "Please upload a video file."}, "", [], ""
|
| 231 |
|
| 232 |
with open(video_file, "rb") as f:
|
| 233 |
video_data = f.read()
|
|
@@ -238,11 +251,12 @@ def gradio_interface(video_file):
|
|
| 238 |
f"Safety Score: {result.get('score', 0)}%",
|
| 239 |
result.get("snapshots", []),
|
| 240 |
f"Salesforce Record ID: {result.get('salesforce_record_id', '')}",
|
| 241 |
-
f"Violation Details URL: {result.get('violation_details_url', '')}"
|
|
|
|
| 242 |
)
|
| 243 |
except Exception as e:
|
| 244 |
print(f"❌ Error in gradio_interface: {e}")
|
| 245 |
-
return {"error": str(e)}, "", [], "", ""
|
| 246 |
|
| 247 |
interface = gr.Interface(
|
| 248 |
fn=gradio_interface,
|
|
@@ -252,7 +266,8 @@ interface = gr.Interface(
|
|
| 252 |
gr.Textbox(label="Compliance Score"),
|
| 253 |
gr.JSON(label="Snapshots"),
|
| 254 |
gr.Textbox(label="Salesforce Record ID"),
|
| 255 |
-
gr.Textbox(label="Violation Details URL")
|
|
|
|
| 256 |
],
|
| 257 |
title="Worksite Safety Violation Analyzer",
|
| 258 |
description="Upload short site videos to detect safety violations (e.g., no helmet, no harness, unsafe posture)."
|
|
|
|
| 5 |
import numpy as np
|
| 6 |
from ultralytics import YOLO
|
| 7 |
import time
|
|
|
|
| 8 |
from simple_salesforce import Salesforce
|
| 9 |
|
| 10 |
# ==========================
|
|
|
|
| 68 |
|
| 69 |
def generate_violation_text_file(violations):
|
| 70 |
try:
|
| 71 |
+
# Ensure the directory is writable
|
| 72 |
+
if not os.access(STATIC_OUTPUT_DIR, os.W_OK):
|
| 73 |
+
raise PermissionError(f"Cannot write to directory: {STATIC_OUTPUT_DIR}")
|
| 74 |
+
|
| 75 |
text_filename = f"violations_{int(time.time())}.txt"
|
| 76 |
text_path = os.path.join(STATIC_OUTPUT_DIR, text_filename)
|
| 77 |
+
|
| 78 |
+
# Generate the content
|
| 79 |
+
content = "Worksite Safety Violation Details\n" + "=" * 40 + "\n"
|
| 80 |
+
content += violations_to_text(violations)
|
| 81 |
+
|
| 82 |
+
# Write the file
|
| 83 |
with open(text_path, "w") as f:
|
| 84 |
+
f.write(content)
|
| 85 |
+
|
|
|
|
| 86 |
print(f"✅ Text file generated at {text_path}")
|
| 87 |
+
if not os.path.exists(text_path):
|
| 88 |
+
raise FileNotFoundError(f"Text file was not created at {text_path}")
|
| 89 |
+
|
| 90 |
public_url = f"https://huggingface.co/spaces/PrashanthB461/AI_Safety_Demo1/resolve/main/static/output/{text_filename}"
|
| 91 |
print(f"✅ Text file URL: {public_url}")
|
| 92 |
+
return text_path, public_url, content
|
| 93 |
except Exception as e:
|
| 94 |
print(f"❌ Error generating text file: {e}")
|
| 95 |
+
return "", "", "Error: Could not generate violation details."
|
| 96 |
|
| 97 |
def push_report_to_salesforce(score, violations, text_filename):
|
| 98 |
try:
|
|
|
|
| 114 |
print(f"Attempting to create Salesforce record with data: {record_data}")
|
| 115 |
record = sf.Safety_Video_Report__c.create(record_data)
|
| 116 |
print(f"✅ Salesforce record created with Id: {record.get('id')}")
|
| 117 |
+
return record.get('id')
|
| 118 |
except Exception as e:
|
| 119 |
print(f"❌ Salesforce record creation failed: {e}")
|
| 120 |
return None
|
|
|
|
| 204 |
os.remove(video_path)
|
| 205 |
|
| 206 |
score = calculate_safety_score(violations)
|
| 207 |
+
text_path, text_url, text_content = generate_violation_text_file(violations)
|
| 208 |
|
| 209 |
if text_path:
|
| 210 |
report_id = push_report_to_salesforce(score, violations, text_path)
|
|
|
|
| 218 |
"snapshots": snapshots,
|
| 219 |
"score": score,
|
| 220 |
"salesforce_record_id": report_id,
|
| 221 |
+
"violation_details_url": text_url,
|
| 222 |
+
"violation_details_content": text_content
|
| 223 |
}
|
| 224 |
|
| 225 |
except Exception as e:
|
|
|
|
| 230 |
"score": 0,
|
| 231 |
"salesforce_record_id": None,
|
| 232 |
"violation_details_url": "",
|
| 233 |
+
"violation_details_content": "Error: Could not process video.",
|
| 234 |
"error": str(e)
|
| 235 |
}
|
| 236 |
|
|
|
|
| 240 |
def gradio_interface(video_file):
|
| 241 |
try:
|
| 242 |
if not video_file:
|
| 243 |
+
return {"error": "Please upload a video file."}, "", [], "", "", "No file uploaded."
|
| 244 |
|
| 245 |
with open(video_file, "rb") as f:
|
| 246 |
video_data = f.read()
|
|
|
|
| 251 |
f"Safety Score: {result.get('score', 0)}%",
|
| 252 |
result.get("snapshots", []),
|
| 253 |
f"Salesforce Record ID: {result.get('salesforce_record_id', '')}",
|
| 254 |
+
f"Violation Details URL: {result.get('violation_details_url', '')}",
|
| 255 |
+
result.get("violation_details_content", "No content available.")
|
| 256 |
)
|
| 257 |
except Exception as e:
|
| 258 |
print(f"❌ Error in gradio_interface: {e}")
|
| 259 |
+
return {"error": str(e)}, "", [], "", "", "Error in processing."
|
| 260 |
|
| 261 |
interface = gr.Interface(
|
| 262 |
fn=gradio_interface,
|
|
|
|
| 266 |
gr.Textbox(label="Compliance Score"),
|
| 267 |
gr.JSON(label="Snapshots"),
|
| 268 |
gr.Textbox(label="Salesforce Record ID"),
|
| 269 |
+
gr.Textbox(label="Violation Details URL"),
|
| 270 |
+
gr.Textbox(label="Violation Details Content (Preview)")
|
| 271 |
],
|
| 272 |
title="Worksite Safety Violation Analyzer",
|
| 273 |
description="Upload short site videos to detect safety violations (e.g., no helmet, no harness, unsafe posture)."
|