PrashanthB461 commited on
Commit
160244b
·
verified ·
1 Parent(s): 1a562e6

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +67 -65
app.py CHANGED
@@ -129,6 +129,73 @@ def generate_violation_pdf(violations, score):
129
  logger.error(f"Error generating PDF: {e}")
130
  return "", "", None
131
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
132
  def calculate_safety_score(violations):
133
  penalties = {
134
  "no_helmet": 25,
@@ -242,68 +309,3 @@ def process_video(video_data):
242
  "violation_details_url": "",
243
  "message": f"Error processing video: {e}"
244
  }
245
-
246
- def gradio_interface(video_file):
247
- if not video_file:
248
- return "No file uploaded.", "", "No file uploaded.", "", ""
249
- try:
250
- yield "Processing video... please wait.", "", "", "", ""
251
-
252
- with open(video_file, "rb") as f:
253
- video_data = f.read()
254
-
255
- result = process_video(video_data)
256
-
257
- if result.get("message"):
258
- yield result["message"], "", "", "", ""
259
- return
260
-
261
- violation_table = "No violations detected."
262
- if result["violations"]:
263
- header = "| Violation | Timestamp (s) | Confidence | Bounding Box |\n"
264
- separator = "|------------------------|---------------|------------|--------------------------|\n"
265
- rows = []
266
- violation_name_map = CONFIG["DISPLAY_NAMES"]
267
- for v in result["violations"]:
268
- display_name = violation_name_map.get(v["violation"], v["violation"])
269
- row = f"| {display_name:<22} | {v['timestamp']:.2f} | {v['confidence']:.2f} | {v['bounding_box']} |"
270
- rows.append(row)
271
- violation_table = header + separator + "\n".join(rows)
272
-
273
- snapshots_text = "No snapshots captured."
274
- if result["snapshots"]:
275
- violation_name_map = CONFIG["DISPLAY_NAMES"]
276
- snapshots_text = "\n".join(
277
- f"- Snapshot for {violation_name_map.get(s['violation'], s['violation'])} at frame {s['frame']}: ![]({s['snapshot_base64']})"
278
- for s in result["snapshots"]
279
- )
280
-
281
- yield (
282
- violation_table,
283
- f"Safety Score: {result['score']}%",
284
- snapshots_text,
285
- f"Salesforce Record ID: {result['salesforce_record_id'] or 'N/A'}",
286
- result["violation_details_url"] or "N/A"
287
- )
288
- except Exception as e:
289
- logger.error(f"Error in Gradio interface: {e}", exc_info=True)
290
- yield f"Error: {str(e)}", "", "Error in processing.", "", ""
291
-
292
- interface = gr.Interface(
293
- fn=gradio_interface,
294
- inputs=gr.Video(label="Upload Site Video"),
295
- outputs=[
296
- gr.Markdown(label="Detected Safety Violations"),
297
- gr.Textbox(label="Compliance Score"),
298
- gr.Markdown(label="Snapshots"),
299
- gr.Textbox(label="Salesforce Record ID"),
300
- gr.Textbox(label="Violation Details URL")
301
- ],
302
- title="Worksite Safety Violation Analyzer",
303
- description="Upload site videos to detect safety violations (No Helmet Violation, No Harness Violation, Unsafe Posture Violation). Non-violations are ignored.",
304
- allow_flagging="never"
305
- )
306
-
307
- if __name__ == "__main__":
308
- logger.info("Launching Safety Analyzer App...")
309
- interface.launch()
 
129
  logger.error(f"Error generating PDF: {e}")
130
  return "", "", None
131
 
132
+ def upload_pdf_to_salesforce(sf, pdf_file, report_id):
133
+ try:
134
+ if not pdf_file:
135
+ logger.error("No PDF file provided for upload")
136
+ return ""
137
+ encoded_pdf = base64.b64encode(pdf_file.getvalue()).decode('utf-8')
138
+ content_version_data = {
139
+ "Title": f"Safety_Violation_Report_{int(time.time())}",
140
+ "PathOnClient": f"safety_violation_{int(time.time())}.pdf",
141
+ "VersionData": encoded_pdf,
142
+ "FirstPublishLocationId": report_id
143
+ }
144
+ content_version = sf.ContentVersion.create(content_version_data)
145
+ result = sf.query(f"SELECT Id, ContentDocumentId FROM ContentVersion WHERE Id = '{content_version['id']}'")
146
+ if not result['records']:
147
+ logger.error("Failed to retrieve ContentVersion")
148
+ return ""
149
+ file_url = f"https://{sf.sf_instance}/sfc/servlet.shepherd/version/download/{content_version['id']}"
150
+ logger.info(f"PDF uploaded to Salesforce: {file_url}")
151
+ return file_url
152
+ except Exception as e:
153
+ logger.error(f"Error uploading PDF to Salesforce: {e}")
154
+ return ""
155
+
156
+ def push_report_to_salesforce(violations, score, pdf_path, pdf_file):
157
+ try:
158
+ sf = connect_to_salesforce()
159
+ violations_text = "\n".join(
160
+ f"{CONFIG['DISPLAY_NAMES'].get(v['violation'], v['violation'])} at {v['timestamp']:.2f}s (Confidence: {v['confidence']})"
161
+ for v in violations
162
+ ) or "No violations detected."
163
+ pdf_url = f"{CONFIG['PUBLIC_URL_BASE']}{os.path.basename(pdf_path)}" if pdf_path else ""
164
+
165
+ record_data = {
166
+ "Compliance_Score__c": score,
167
+ "Violations_Found__c": len(violations),
168
+ "Violations_Details__c": violations_text,
169
+ "Status__c": "Pending",
170
+ "PDF_Report_URL__c": pdf_url
171
+ }
172
+ logger.info(f"Creating Salesforce record with data: {record_data}")
173
+ try:
174
+ record = sf.Safety_Video_Report__c.create(record_data)
175
+ logger.info(f"Created Safety_Video_Report__c record: {record['id']}")
176
+ except Exception as e:
177
+ logger.error(f"Failed to create Safety_Video_Report__c: {e}")
178
+ record = sf.Account.create({"Name": f"Safety_Report_{int(time.time())}"})
179
+ logger.warning(f"Fell back to Account record: {record['id']}")
180
+ record_id = record["id"]
181
+
182
+ if pdf_file:
183
+ uploaded_url = upload_pdf_to_salesforce(sf, pdf_file, record_id)
184
+ if uploaded_url:
185
+ try:
186
+ sf.Safety_Video_Report__c.update(record_id, {"PDF_Report_URL__c": uploaded_url})
187
+ logger.info(f"Updated record {record_id} with PDF URL: {uploaded_url}")
188
+ except Exception as e:
189
+ logger.error(f"Failed to update Safety_Video_Report__c: {e}")
190
+ sf.Account.update(record_id, {"Description": uploaded_url})
191
+ logger.info(f"Updated Account record {record_id} with PDF URL")
192
+ pdf_url = uploaded_url
193
+
194
+ return record_id, pdf_url
195
+ except Exception as e:
196
+ logger.error(f"Salesforce record creation failed: {e}", exc_info=True)
197
+ return None, ""
198
+
199
  def calculate_safety_score(violations):
200
  penalties = {
201
  "no_helmet": 25,
 
309
  "violation_details_url": "",
310
  "message": f"Error processing video: {e}"
311
  }