Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
|
@@ -46,14 +46,14 @@ except Exception as e:
|
|
| 46 |
raise
|
| 47 |
|
| 48 |
# ==========================
|
| 49 |
-
# Salesforce connection
|
| 50 |
# ==========================
|
| 51 |
def connect_to_salesforce():
|
| 52 |
sf = Salesforce(
|
| 53 |
-
username=
|
| 54 |
-
password=
|
| 55 |
-
security_token=
|
| 56 |
-
domain=
|
| 57 |
)
|
| 58 |
return sf
|
| 59 |
|
|
@@ -70,20 +70,22 @@ def push_report_to_salesforce(score, violations, pdf_filename):
|
|
| 70 |
violations_count = len(violations)
|
| 71 |
violations_details = violations_to_text(violations)
|
| 72 |
|
| 73 |
-
# Public URL base for your PDF reports on HuggingFace space or other hosting
|
| 74 |
PUBLIC_PDF_BASE_URL = "https://huggingface.co/spaces/PrashanthB461/AI_Safety_Demo1/resolve/main/output/"
|
| 75 |
-
|
| 76 |
pdf_url = f"{PUBLIC_PDF_BASE_URL}{os.path.basename(pdf_filename)}"
|
| 77 |
|
| 78 |
-
|
| 79 |
-
|
| 80 |
-
|
| 81 |
-
|
| 82 |
-
|
| 83 |
-
|
| 84 |
-
|
| 85 |
-
|
| 86 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
| 87 |
|
| 88 |
# ==========================
|
| 89 |
# Safety Score Calculation
|
|
@@ -109,15 +111,12 @@ def generate_pdf_report(violations, snapshots, score):
|
|
| 109 |
c = canvas.Canvas(pdf_path, pagesize=letter)
|
| 110 |
width, height = letter
|
| 111 |
|
| 112 |
-
# Title
|
| 113 |
c.setFont("Helvetica-Bold", 16)
|
| 114 |
c.drawString(50, height - 50, "Worksite Safety Compliance Report")
|
| 115 |
|
| 116 |
-
# Compliance Score
|
| 117 |
c.setFont("Helvetica", 12)
|
| 118 |
c.drawString(50, height - 80, f"Compliance Score: {score}%")
|
| 119 |
|
| 120 |
-
# Violations Table
|
| 121 |
y = height - 120
|
| 122 |
c.setFont("Helvetica-Bold", 12)
|
| 123 |
c.drawString(50, y, "Detected Violations:")
|
|
@@ -129,7 +128,6 @@ def generate_pdf_report(violations, snapshots, score):
|
|
| 129 |
c.drawString(50, y, text)
|
| 130 |
y -= 20
|
| 131 |
|
| 132 |
-
# Add snapshot if available and exists locally
|
| 133 |
snapshot = next((s for s in snapshots if s["frame"] == v["frame"] and s["violation"] == v["violation"]), None)
|
| 134 |
if snapshot:
|
| 135 |
local_img_path = os.path.join(OUTPUT_DIR, os.path.basename(snapshot["snapshot_url"]))
|
|
@@ -157,7 +155,6 @@ def generate_pdf_report(violations, snapshots, score):
|
|
| 157 |
def process_video(video_data, frame_skip=5, max_frames=100):
|
| 158 |
try:
|
| 159 |
print("Processing video data...")
|
| 160 |
-
# Save uploaded video data to a temporary file
|
| 161 |
video_path = os.path.join(OUTPUT_DIR, f"temp_{int(time.time())}.mp4")
|
| 162 |
with open(video_path, "wb") as f:
|
| 163 |
f.write(video_data)
|
|
@@ -182,7 +179,6 @@ def process_video(video_data, frame_skip=5, max_frames=100):
|
|
| 182 |
frame_count += 1
|
| 183 |
continue
|
| 184 |
|
| 185 |
-
# Model inference
|
| 186 |
results = model(frame, device=device)
|
| 187 |
|
| 188 |
for result in results:
|
|
@@ -201,7 +197,6 @@ def process_video(video_data, frame_skip=5, max_frames=100):
|
|
| 201 |
}
|
| 202 |
violations.append(violation)
|
| 203 |
|
| 204 |
-
# Save snapshot
|
| 205 |
snapshot_path = os.path.join(OUTPUT_DIR, f"snapshot_{frame_count}_{label}.jpg")
|
| 206 |
cv2.imwrite(snapshot_path, frame)
|
| 207 |
snapshots.append({
|
|
@@ -226,14 +221,12 @@ def process_video(video_data, frame_skip=5, max_frames=100):
|
|
| 226 |
score = calculate_safety_score(violations)
|
| 227 |
pdf_path = generate_pdf_report(violations, snapshots, score)
|
| 228 |
|
| 229 |
-
# Push report to Salesforce with public PDF URL
|
| 230 |
if pdf_path:
|
| 231 |
report_id = push_report_to_salesforce(score, violations, pdf_path)
|
| 232 |
print(f"Salesforce record created with Id: {report_id}")
|
| 233 |
else:
|
| 234 |
report_id = None
|
| 235 |
|
| 236 |
-
# Convert PDF to base64 for UI display (optional)
|
| 237 |
with open(pdf_path, "rb") as f:
|
| 238 |
pdf_base64 = base64.b64encode(f.read()).decode('utf-8')
|
| 239 |
|
|
|
|
| 46 |
raise
|
| 47 |
|
| 48 |
# ==========================
|
| 49 |
+
# Salesforce connection with hardcoded credentials
|
| 50 |
# ==========================
|
| 51 |
def connect_to_salesforce():
|
| 52 |
sf = Salesforce(
|
| 53 |
+
username="prashanth1ai@safety.com",
|
| 54 |
+
password="SaiPrash461",
|
| 55 |
+
security_token="AP4AQnPoidIKPvSvNEfAHyoK",
|
| 56 |
+
domain="login" # Use "test" if sandbox
|
| 57 |
)
|
| 58 |
return sf
|
| 59 |
|
|
|
|
| 70 |
violations_count = len(violations)
|
| 71 |
violations_details = violations_to_text(violations)
|
| 72 |
|
|
|
|
| 73 |
PUBLIC_PDF_BASE_URL = "https://huggingface.co/spaces/PrashanthB461/AI_Safety_Demo1/resolve/main/output/"
|
|
|
|
| 74 |
pdf_url = f"{PUBLIC_PDF_BASE_URL}{os.path.basename(pdf_filename)}"
|
| 75 |
|
| 76 |
+
try:
|
| 77 |
+
record = sf.Safety_Video_Report__c.create({
|
| 78 |
+
"Compliance_Score__c": score,
|
| 79 |
+
"Violations_Found__c": violations_count,
|
| 80 |
+
"Violations_Details__c": violations_details,
|
| 81 |
+
"Status__c": "Pending",
|
| 82 |
+
"PDF_Report_URL__c": pdf_url
|
| 83 |
+
})
|
| 84 |
+
print(f"✅ Salesforce record created with Id: {record.get('id')}")
|
| 85 |
+
return record.get("id")
|
| 86 |
+
except Exception as e:
|
| 87 |
+
print(f"❌ Salesforce record creation failed: {e}")
|
| 88 |
+
return None
|
| 89 |
|
| 90 |
# ==========================
|
| 91 |
# Safety Score Calculation
|
|
|
|
| 111 |
c = canvas.Canvas(pdf_path, pagesize=letter)
|
| 112 |
width, height = letter
|
| 113 |
|
|
|
|
| 114 |
c.setFont("Helvetica-Bold", 16)
|
| 115 |
c.drawString(50, height - 50, "Worksite Safety Compliance Report")
|
| 116 |
|
|
|
|
| 117 |
c.setFont("Helvetica", 12)
|
| 118 |
c.drawString(50, height - 80, f"Compliance Score: {score}%")
|
| 119 |
|
|
|
|
| 120 |
y = height - 120
|
| 121 |
c.setFont("Helvetica-Bold", 12)
|
| 122 |
c.drawString(50, y, "Detected Violations:")
|
|
|
|
| 128 |
c.drawString(50, y, text)
|
| 129 |
y -= 20
|
| 130 |
|
|
|
|
| 131 |
snapshot = next((s for s in snapshots if s["frame"] == v["frame"] and s["violation"] == v["violation"]), None)
|
| 132 |
if snapshot:
|
| 133 |
local_img_path = os.path.join(OUTPUT_DIR, os.path.basename(snapshot["snapshot_url"]))
|
|
|
|
| 155 |
def process_video(video_data, frame_skip=5, max_frames=100):
|
| 156 |
try:
|
| 157 |
print("Processing video data...")
|
|
|
|
| 158 |
video_path = os.path.join(OUTPUT_DIR, f"temp_{int(time.time())}.mp4")
|
| 159 |
with open(video_path, "wb") as f:
|
| 160 |
f.write(video_data)
|
|
|
|
| 179 |
frame_count += 1
|
| 180 |
continue
|
| 181 |
|
|
|
|
| 182 |
results = model(frame, device=device)
|
| 183 |
|
| 184 |
for result in results:
|
|
|
|
| 197 |
}
|
| 198 |
violations.append(violation)
|
| 199 |
|
|
|
|
| 200 |
snapshot_path = os.path.join(OUTPUT_DIR, f"snapshot_{frame_count}_{label}.jpg")
|
| 201 |
cv2.imwrite(snapshot_path, frame)
|
| 202 |
snapshots.append({
|
|
|
|
| 221 |
score = calculate_safety_score(violations)
|
| 222 |
pdf_path = generate_pdf_report(violations, snapshots, score)
|
| 223 |
|
|
|
|
| 224 |
if pdf_path:
|
| 225 |
report_id = push_report_to_salesforce(score, violations, pdf_path)
|
| 226 |
print(f"Salesforce record created with Id: {report_id}")
|
| 227 |
else:
|
| 228 |
report_id = None
|
| 229 |
|
|
|
|
| 230 |
with open(pdf_path, "rb") as f:
|
| 231 |
pdf_base64 = base64.b64encode(f.read()).decode('utf-8')
|
| 232 |
|