Komal133 commited on
Commit
4bf7be2
Β·
verified Β·
1 Parent(s): 5777b8f

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +50 -32
app.py CHANGED
@@ -8,33 +8,40 @@ import requests
8
  from datetime import datetime
9
  from transformers import pipeline
10
 
11
- # πŸŽ™οΈ 1) Load detection model
12
  classifier = pipeline(
13
  "audio-classification",
14
  model="padmalcom/wav2vec2-large-nonverbalvocalization-classification"
15
  )
16
 
17
- # === Helpers ===
18
-
19
  def convert_audio(input_path, output_path="input.wav"):
20
- cmd = [
21
- "ffmpeg", "-i", input_path,
22
- "-acodec", "pcm_s16le", "-ar", "16000", "-ac", "1",
23
- output_path, "-y"
24
- ]
25
- subprocess.run(cmd, check=True, stdout=subprocess.PIPE, stderr=subprocess.PIPE)
 
 
 
 
26
  return output_path
27
 
 
28
  def detect_scream(audio_path):
29
  audio, sr = librosa.load(audio_path, sr=16000)
30
  results = classifier(audio)
 
 
31
  if not results:
32
  return {"label": None, "score": 0.0}
33
  top = results[0]
34
  return {"label": top["label"].lower(), "score": float(top["score"]) * 100}
35
 
 
36
  def send_salesforce_alert(audio_meta, detection):
37
- SF_URL = os.getenv("SF_ALERT_URL") # e.g. Salesforce REST endpoint
38
  SF_TOKEN = os.getenv("SF_API_TOKEN")
39
  headers = {"Authorization": f"Bearer {SF_TOKEN}", "Content-Type": "application/json"}
40
  payload = {
@@ -44,26 +51,36 @@ def send_salesforce_alert(audio_meta, detection):
44
  "AlertLevel": audio_meta["alert_level"],
45
  "Timestamp": audio_meta["timestamp"],
46
  }
47
- # POST to Salesforce for alert, metadata and audit
48
- resp = requests.post(SF_URL, json=payload, headers=headers, timeout=5)
49
- resp.raise_for_status()
50
- return resp.json()
51
 
52
- # 🎯 2) Main processing function called by Gradio
 
 
 
 
 
 
 
 
 
 
53
  def process_uploaded(audio_file, start_stop, high_thresh, med_thresh):
54
  if start_stop != "Start":
55
  return "System is stopped."
56
 
57
- wav_path = convert_audio(audio_file)
58
- detection = detect_scream(wav_path)
 
 
59
 
 
60
  label = detection["label"]
61
  score = detection["score"]
 
62
 
63
- # 3) Determine alert level
64
- if label == "scream" and score >= high_thresh:
65
  level = "High-Risk"
66
- elif label == "scream" and score >= med_thresh:
67
  level = "Medium-Risk"
68
  else:
69
  level = "None"
@@ -74,7 +91,7 @@ def process_uploaded(audio_file, start_stop, high_thresh, med_thresh):
74
  "alert_level": level
75
  }
76
 
77
- # 4) Trigger alert if needed
78
  if level in ("High-Risk", "Medium-Risk"):
79
  try:
80
  sf_resp = send_salesforce_alert(audio_meta, detection)
@@ -84,7 +101,7 @@ def process_uploaded(audio_file, start_stop, high_thresh, med_thresh):
84
 
85
  return f"Detection: {label} ({score:.1f}%) β€” Alert: {level}"
86
 
87
- # 🎚️ 5) Setup Gradio Interface with config and control
88
  iface = gr.Interface(
89
  fn=process_uploaded,
90
  inputs=[
@@ -96,16 +113,15 @@ iface = gr.Interface(
96
  outputs="text",
97
  title="πŸ“’ Scream Detection & Salesforce Alerts",
98
  description="""
99
- - Upload an audio sample or stream via Raspberry Pi integration.
100
- - **Start/Stop** toggles detection.
101
- - **Threshold sliders** let you configure alert sensitivities.
102
- - On detection, alerts are logged and pushed to Salesforce.
103
- - All metadata and alerts are stored for dashboards and auditing.
104
  """,
105
  allow_flagging="never"
106
  )
107
 
108
- # Optional: thread for real-time, device-based listening (e.g., for Pi)
109
  def pi_listener(high_thresh=80, med_thresh=50, interval=1.0):
110
  import sounddevice as sd
111
  import numpy as np
@@ -115,9 +131,9 @@ def pi_listener(high_thresh=80, med_thresh=50, interval=1.0):
115
  detection = classifier(wav.astype(np.float32))
116
  lbl, sc = (detection[0]["label"].lower(), detection[0]["score"] * 100)
117
  level = "None"
118
- if lbl == "scream" and sc >= high_thresh:
119
  level = "High-Risk"
120
- elif lbl == "scream" and sc >= med_thresh:
121
  level = "Medium-Risk"
122
  if level != "None":
123
  timestamp = datetime.utcnow().isoformat() + "Z"
@@ -126,13 +142,15 @@ def pi_listener(high_thresh=80, med_thresh=50, interval=1.0):
126
  {"label": lbl, "score": sc}
127
  )
128
  print(f"[{timestamp}] {level} scream detected ({sc:.1f}%) – alert sent.")
 
129
  with sd.InputStream(channels=1, samplerate=16000, callback=callback):
130
- print("Raspberry Pi listener running... Press Ctrl+C to stop.")
131
  while True:
132
  time.sleep(interval)
133
 
 
134
  if __name__ == "__main__":
135
- # Optional: uncomment next lines to start Pi listener in background
136
  # pi_thread = threading.Thread(target=pi_listener, daemon=True)
137
  # pi_thread.start()
138
 
 
8
  from datetime import datetime
9
  from transformers import pipeline
10
 
11
+ # πŸŽ™οΈ Load Hugging Face model for non-verbal vocalization classification
12
  classifier = pipeline(
13
  "audio-classification",
14
  model="padmalcom/wav2vec2-large-nonverbalvocalization-classification"
15
  )
16
 
17
+ # === Audio Conversion ===
 
18
  def convert_audio(input_path, output_path="input.wav"):
19
+ try:
20
+ cmd = [
21
+ "ffmpeg", "-i", input_path,
22
+ "-acodec", "pcm_s16le", "-ar", "16000", "-ac", "1",
23
+ output_path, "-y"
24
+ ]
25
+ subprocess.run(cmd, check=True, stdout=subprocess.PIPE, stderr=subprocess.PIPE)
26
+ except subprocess.CalledProcessError as e:
27
+ print(f"[ERROR] FFmpeg failed: {e.stderr.decode()}")
28
+ raise
29
  return output_path
30
 
31
+ # === Scream Detection ===
32
  def detect_scream(audio_path):
33
  audio, sr = librosa.load(audio_path, sr=16000)
34
  results = classifier(audio)
35
+ print(f"[DEBUG] Raw classifier output: {results}") # For debugging label issues
36
+
37
  if not results:
38
  return {"label": None, "score": 0.0}
39
  top = results[0]
40
  return {"label": top["label"].lower(), "score": float(top["score"]) * 100}
41
 
42
+ # === Salesforce Alerting ===
43
  def send_salesforce_alert(audio_meta, detection):
44
+ SF_URL = os.getenv("SF_ALERT_URL")
45
  SF_TOKEN = os.getenv("SF_API_TOKEN")
46
  headers = {"Authorization": f"Bearer {SF_TOKEN}", "Content-Type": "application/json"}
47
  payload = {
 
51
  "AlertLevel": audio_meta["alert_level"],
52
  "Timestamp": audio_meta["timestamp"],
53
  }
 
 
 
 
54
 
55
+ try:
56
+ resp = requests.post(SF_URL, json=payload, headers=headers, timeout=5)
57
+ resp.raise_for_status()
58
+ return resp.json()
59
+ except requests.exceptions.RequestException as e:
60
+ print(f"[ERROR] Salesforce alert failed: {e}")
61
+ if resp is not None:
62
+ print(f"Response: {resp.status_code} - {resp.text}")
63
+ raise
64
+
65
+ # === Main Gradio Processing Function ===
66
  def process_uploaded(audio_file, start_stop, high_thresh, med_thresh):
67
  if start_stop != "Start":
68
  return "System is stopped."
69
 
70
+ try:
71
+ wav_path = convert_audio(audio_file)
72
+ except Exception as e:
73
+ return f"Audio conversion failed: {e}"
74
 
75
+ detection = detect_scream(wav_path)
76
  label = detection["label"]
77
  score = detection["score"]
78
+ print(f"[INFO] File: {audio_file}, Label: {label}, Score: {score:.2f}%")
79
 
80
+ # Determine risk level based on score
81
+ if label and "scream" in label and score >= high_thresh:
82
  level = "High-Risk"
83
+ elif label and "scream" in label and score >= med_thresh:
84
  level = "Medium-Risk"
85
  else:
86
  level = "None"
 
91
  "alert_level": level
92
  }
93
 
94
+ # Trigger alert if needed
95
  if level in ("High-Risk", "Medium-Risk"):
96
  try:
97
  sf_resp = send_salesforce_alert(audio_meta, detection)
 
101
 
102
  return f"Detection: {label} ({score:.1f}%) β€” Alert: {level}"
103
 
104
+ # === Gradio Interface ===
105
  iface = gr.Interface(
106
  fn=process_uploaded,
107
  inputs=[
 
113
  outputs="text",
114
  title="πŸ“’ Scream Detection & Salesforce Alerts",
115
  description="""
116
+ Upload an audio sample or stream from a device like Raspberry Pi.
117
+ - **Start/Stop** detection system.
118
+ - **Adjust thresholds** for sensitivity.
119
+ - Alerts are sent to Salesforce for audit, dashboard, and real-time response.
 
120
  """,
121
  allow_flagging="never"
122
  )
123
 
124
+ # === Optional: Real-Time Listener (e.g., for Raspberry Pi) ===
125
  def pi_listener(high_thresh=80, med_thresh=50, interval=1.0):
126
  import sounddevice as sd
127
  import numpy as np
 
131
  detection = classifier(wav.astype(np.float32))
132
  lbl, sc = (detection[0]["label"].lower(), detection[0]["score"] * 100)
133
  level = "None"
134
+ if "scream" in lbl and sc >= high_thresh:
135
  level = "High-Risk"
136
+ elif "scream" in lbl and sc >= med_thresh:
137
  level = "Medium-Risk"
138
  if level != "None":
139
  timestamp = datetime.utcnow().isoformat() + "Z"
 
142
  {"label": lbl, "score": sc}
143
  )
144
  print(f"[{timestamp}] {level} scream detected ({sc:.1f}%) – alert sent.")
145
+
146
  with sd.InputStream(channels=1, samplerate=16000, callback=callback):
147
+ print("🎧 Raspberry Pi real-time listener running...")
148
  while True:
149
  time.sleep(interval)
150
 
151
+ # === Launch Gradio App ===
152
  if __name__ == "__main__":
153
+ # Optional background listener thread (uncomment if needed)
154
  # pi_thread = threading.Thread(target=pi_listener, daemon=True)
155
  # pi_thread.start()
156