PrashanthB461 commited on
Commit
d6d0b51
·
verified ·
1 Parent(s): d6b1114

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +25 -11
app.py CHANGED
@@ -48,7 +48,7 @@ except Exception as e:
48
  raise
49
 
50
  # ==========================
51
- # Salesforce connection (hardcoded creds for testing)
52
  # ==========================
53
  def connect_to_salesforce():
54
  sf = Salesforce(
@@ -66,15 +66,29 @@ def violations_to_text(violations):
66
  lines.append(line)
67
  return "\n".join(lines)
68
 
69
- def push_report_to_salesforce(score, violations, pdf_filename):
 
 
 
 
 
 
 
 
 
 
 
 
 
 
70
  sf = connect_to_salesforce()
71
 
72
  violations_count = len(violations)
73
  violations_details = violations_to_text(violations)
74
 
75
- # Updated public URL base with static/output path
76
- PUBLIC_PDF_BASE_URL = "https://huggingface.co/spaces/PrashanthB461/AI_Safety_Demo1/resolve/main/static/output/"
77
- pdf_url = f"{PUBLIC_PDF_BASE_URL}{os.path.basename(pdf_filename)}"
78
 
79
  try:
80
  record = sf.Safety_Video_Report__c.create({
@@ -82,7 +96,8 @@ def push_report_to_salesforce(score, violations, pdf_filename):
82
  "Violations_Found__c": violations_count,
83
  "Violations_Details__c": violations_details,
84
  "Status__c": "Pending",
85
- "PDF_Report_URL__c": pdf_url
 
86
  })
87
  print(f"✅ Salesforce record created with Id: {record.get('id')}")
88
  return record.get("id")
@@ -145,9 +160,7 @@ def generate_pdf_report(violations, snapshots, score):
145
 
146
  c.save()
147
  print(f"PDF generated at {pdf_path}")
148
-
149
  return pdf_path
150
-
151
  except Exception as e:
152
  print(f"❌ Error generating PDF: {e}")
153
  return ""
@@ -223,9 +236,10 @@ def process_video(video_data, frame_skip=5, max_frames=100):
223
 
224
  score = calculate_safety_score(violations)
225
  pdf_path = generate_pdf_report(violations, snapshots, score)
 
226
 
227
- if pdf_path:
228
- report_id = push_report_to_salesforce(score, violations, pdf_path)
229
  print(f"Salesforce record created with Id: {report_id}")
230
  else:
231
  report_id = None
@@ -291,4 +305,4 @@ interface = gr.Interface(
291
 
292
  if __name__ == "__main__":
293
  print("🚀 Launching Safety Analyzer App...")
294
- interface.launch()
 
48
  raise
49
 
50
  # ==========================
51
+ # Salesforce connection
52
  # ==========================
53
  def connect_to_salesforce():
54
  sf = Salesforce(
 
66
  lines.append(line)
67
  return "\n".join(lines)
68
 
69
+ def generate_violation_text_file(violations):
70
+ try:
71
+ text_filename = f"violations_{int(time.time())}.txt"
72
+ text_path = os.path.join(STATIC_OUTPUT_DIR, text_filename)
73
+ with open(text_path, "w") as f:
74
+ f.write("Worksite Safety Violation Details\n")
75
+ f.write("=" * 40 + "\n")
76
+ f.write(violations_to_text(violations))
77
+ print(f"Text file generated at {text_path}")
78
+ return text_path
79
+ except Exception as e:
80
+ print(f"❌ Error generating text file: {e}")
81
+ return ""
82
+
83
+ def push_report_to_salesforce(score, violations, pdf_filename, text_filename):
84
  sf = connect_to_salesforce()
85
 
86
  violations_count = len(violations)
87
  violations_details = violations_to_text(violations)
88
 
89
+ PUBLIC_BASE_URL = "https://huggingface.co/spaces/PrashanthB461/AI_Safety_Demo1/resolve/main/static/output/"
90
+ pdf_url = f"{PUBLIC_BASE_URL}{os.path.basename(pdf_filename)}"
91
+ text_url = f"{PUBLIC_BASE_URL}{os.path.basename(text_filename)}" if text_filename else ""
92
 
93
  try:
94
  record = sf.Safety_Video_Report__c.create({
 
96
  "Violations_Found__c": violations_count,
97
  "Violations_Details__c": violations_details,
98
  "Status__c": "Pending",
99
+ "PDF_Report_URL__c": pdf_url,
100
+ "Violation_Details_URL__c": text_url # New field for text file URL
101
  })
102
  print(f"✅ Salesforce record created with Id: {record.get('id')}")
103
  return record.get("id")
 
160
 
161
  c.save()
162
  print(f"PDF generated at {pdf_path}")
 
163
  return pdf_path
 
164
  except Exception as e:
165
  print(f"❌ Error generating PDF: {e}")
166
  return ""
 
236
 
237
  score = calculate_safety_score(violations)
238
  pdf_path = generate_pdf_report(violations, snapshots, score)
239
+ text_path = generate_violation_text_file(violations)
240
 
241
+ if pdf_path and text_path:
242
+ report_id = push_report_to_salesforce(score, violations, pdf_path, text_path)
243
  print(f"Salesforce record created with Id: {report_id}")
244
  else:
245
  report_id = None
 
305
 
306
  if __name__ == "__main__":
307
  print("🚀 Launching Safety Analyzer App...")
308
+ interface.launch()