Update app.py
Browse files
app.py
CHANGED
|
@@ -8,33 +8,40 @@ import requests
|
|
| 8 |
from datetime import datetime
|
| 9 |
from transformers import pipeline
|
| 10 |
|
| 11 |
-
# ποΈ
|
| 12 |
classifier = pipeline(
|
| 13 |
"audio-classification",
|
| 14 |
model="padmalcom/wav2vec2-large-nonverbalvocalization-classification"
|
| 15 |
)
|
| 16 |
|
| 17 |
-
# ===
|
| 18 |
-
|
| 19 |
def convert_audio(input_path, output_path="input.wav"):
|
| 20 |
-
|
| 21 |
-
|
| 22 |
-
|
| 23 |
-
|
| 24 |
-
|
| 25 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
| 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")
|
| 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 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 53 |
def process_uploaded(audio_file, start_stop, high_thresh, med_thresh):
|
| 54 |
if start_stop != "Start":
|
| 55 |
return "System is stopped."
|
| 56 |
|
| 57 |
-
|
| 58 |
-
|
|
|
|
|
|
|
| 59 |
|
|
|
|
| 60 |
label = detection["label"]
|
| 61 |
score = detection["score"]
|
|
|
|
| 62 |
|
| 63 |
-
#
|
| 64 |
-
if label
|
| 65 |
level = "High-Risk"
|
| 66 |
-
elif label
|
| 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 |
-
#
|
| 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 |
-
#
|
| 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 |
-
|
| 100 |
-
- **Start/Stop**
|
| 101 |
-
- **
|
| 102 |
-
-
|
| 103 |
-
- All metadata and alerts are stored for dashboards and auditing.
|
| 104 |
""",
|
| 105 |
allow_flagging="never"
|
| 106 |
)
|
| 107 |
|
| 108 |
-
# Optional:
|
| 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
|
| 119 |
level = "High-Risk"
|
| 120 |
-
elif
|
| 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...
|
| 131 |
while True:
|
| 132 |
time.sleep(interval)
|
| 133 |
|
|
|
|
| 134 |
if __name__ == "__main__":
|
| 135 |
-
# Optional
|
| 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 |
|