Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
|
@@ -11,7 +11,6 @@ from reportlab.lib.utils import ImageReader
|
|
| 11 |
import base64
|
| 12 |
from PIL import Image
|
| 13 |
from simple_salesforce import Salesforce, SalesforceMalformedRequest
|
| 14 |
-
import requests
|
| 15 |
|
| 16 |
# ==========================
|
| 17 |
# Configuration
|
|
@@ -86,18 +85,12 @@ def push_report_to_salesforce(score, violations, pdf_url):
|
|
| 86 |
violations_count = len(violations)
|
| 87 |
violations_details = violations_to_text(violations)
|
| 88 |
|
| 89 |
-
# Verify PDF URL accessibility
|
| 90 |
-
print(f"🔍 Verifying PDF URL: {pdf_url}")
|
| 91 |
-
response = requests.head(pdf_url, timeout=5)
|
| 92 |
-
if response.status_code != 200:
|
| 93 |
-
print(f"⚠️ PDF URL not accessible: {response.status_code}")
|
| 94 |
-
pdf_url = "" # Fallback to empty URL to avoid saving invalid link
|
| 95 |
-
|
| 96 |
# Create the record in the custom object Safety_Video_Report__c
|
| 97 |
-
print(f"
|
| 98 |
record = sf.Safety_Video_Report__c.create({
|
| 99 |
"Compliance_Score__c": score,
|
| 100 |
"Violations_Found__c": violations_count,
|
|
|
|
| 101 |
"Violations_Details__c": violations_details,
|
| 102 |
"Status__c": "Pending",
|
| 103 |
"PDF_Report_URL__c": pdf_url
|
|
@@ -163,8 +156,6 @@ def generate_pdf_report(violations, snapshots, score):
|
|
| 163 |
img = ImageReader(local_img_path)
|
| 164 |
c.drawImage(img, 50, y - 100, width=200, height=150)
|
| 165 |
y -= 170
|
| 166 |
-
else:
|
| 167 |
-
print(f"⚠️ Snapshot not found at {local_img_path}")
|
| 168 |
|
| 169 |
if y < 50:
|
| 170 |
c.showPage()
|
|
@@ -173,11 +164,10 @@ def generate_pdf_report(violations, snapshots, score):
|
|
| 173 |
c.save()
|
| 174 |
print(f"✅ PDF generated at {pdf_path}")
|
| 175 |
|
| 176 |
-
# Verify file
|
| 177 |
-
for _ in range(
|
| 178 |
if os.path.exists(pdf_path):
|
| 179 |
break
|
| 180 |
-
print(f"⏳ Waiting for PDF at {pdf_path}")
|
| 181 |
time.sleep(1)
|
| 182 |
else:
|
| 183 |
raise Exception(f"PDF file not found at {pdf_path} after retries")
|
|
@@ -186,16 +176,6 @@ def generate_pdf_report(violations, snapshots, score):
|
|
| 186 |
pdf_url = f"{BASE_PUBLIC_URL}{pdf_filename}"
|
| 187 |
print(f"📎 Public PDF URL: {pdf_url}")
|
| 188 |
|
| 189 |
-
# Verify URL accessibility
|
| 190 |
-
try:
|
| 191 |
-
response = requests.head(pdf_url, timeout=5)
|
| 192 |
-
if response.status_code == 200:
|
| 193 |
-
print(f"✅ PDF URL is accessible")
|
| 194 |
-
else:
|
| 195 |
-
print(f"⚠️ PDF URL not accessible: HTTP {response.status_code}")
|
| 196 |
-
except requests.RequestException as e:
|
| 197 |
-
print(f"⚠️ Failed to verify PDF URL: {e}")
|
| 198 |
-
|
| 199 |
return pdf_path, pdf_url
|
| 200 |
|
| 201 |
except Exception as e:
|
|
@@ -259,7 +239,6 @@ def process_video(video_data, frame_skip=5, max_frames=100):
|
|
| 259 |
"frame": frame_count,
|
| 260 |
"snapshot_url": f"{BASE_PUBLIC_URL}{snapshot_filename}"
|
| 261 |
})
|
| 262 |
-
print(f"📸 Snapshot saved at {snapshot_path}")
|
| 263 |
else:
|
| 264 |
print(f"⚠️ Snapshot not saved at {snapshot_path}")
|
| 265 |
|
|
@@ -328,9 +307,9 @@ def gradio_interface(video_file):
|
|
| 328 |
return (
|
| 329 |
violation_text,
|
| 330 |
f"Safety Score: {result.get('score', 0)}%",
|
|
|
|
| 331 |
result.get("pdf_base64", ""),
|
| 332 |
result.get("snapshots", []),
|
| 333 |
-
f"PDF URL: {result.get('pdf_url', '')}",
|
| 334 |
f"Salesforce Record ID: {result.get('salesforce_record_id', '')}"
|
| 335 |
)
|
| 336 |
except Exception as e:
|
|
|
|
| 11 |
import base64
|
| 12 |
from PIL import Image
|
| 13 |
from simple_salesforce import Salesforce, SalesforceMalformedRequest
|
|
|
|
| 14 |
|
| 15 |
# ==========================
|
| 16 |
# Configuration
|
|
|
|
| 85 |
violations_count = len(violations)
|
| 86 |
violations_details = violations_to_text(violations)
|
| 87 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 88 |
# Create the record in the custom object Safety_Video_Report__c
|
| 89 |
+
print(f"Creating Salesforce record with compliance score: {score} and violations found: {violations_count}")
|
| 90 |
record = sf.Safety_Video_Report__c.create({
|
| 91 |
"Compliance_Score__c": score,
|
| 92 |
"Violations_Found__c": violations_count,
|
| 93 |
+
"Violfilepath: app.py
|
| 94 |
"Violations_Details__c": violations_details,
|
| 95 |
"Status__c": "Pending",
|
| 96 |
"PDF_Report_URL__c": pdf_url
|
|
|
|
| 156 |
img = ImageReader(local_img_path)
|
| 157 |
c.drawImage(img, 50, y - 100, width=200, height=150)
|
| 158 |
y -= 170
|
|
|
|
|
|
|
| 159 |
|
| 160 |
if y < 50:
|
| 161 |
c.showPage()
|
|
|
|
| 164 |
c.save()
|
| 165 |
print(f"✅ PDF generated at {pdf_path}")
|
| 166 |
|
| 167 |
+
# Verify the file exists with retry
|
| 168 |
+
for _ in range(3):
|
| 169 |
if os.path.exists(pdf_path):
|
| 170 |
break
|
|
|
|
| 171 |
time.sleep(1)
|
| 172 |
else:
|
| 173 |
raise Exception(f"PDF file not found at {pdf_path} after retries")
|
|
|
|
| 176 |
pdf_url = f"{BASE_PUBLIC_URL}{pdf_filename}"
|
| 177 |
print(f"📎 Public PDF URL: {pdf_url}")
|
| 178 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 179 |
return pdf_path, pdf_url
|
| 180 |
|
| 181 |
except Exception as e:
|
|
|
|
| 239 |
"frame": frame_count,
|
| 240 |
"snapshot_url": f"{BASE_PUBLIC_URL}{snapshot_filename}"
|
| 241 |
})
|
|
|
|
| 242 |
else:
|
| 243 |
print(f"⚠️ Snapshot not saved at {snapshot_path}")
|
| 244 |
|
|
|
|
| 307 |
return (
|
| 308 |
violation_text,
|
| 309 |
f"Safety Score: {result.get('score', 0)}%",
|
| 310 |
+
f"PDF URL: {result.get('pdf_url', '')}",
|
| 311 |
result.get("pdf_base64", ""),
|
| 312 |
result.get("snapshots", []),
|
|
|
|
| 313 |
f"Salesforce Record ID: {result.get('salesforce_record_id', '')}"
|
| 314 |
)
|
| 315 |
except Exception as e:
|