Update app.py
Browse files
app.py
CHANGED
|
@@ -8,9 +8,9 @@ import requests
|
|
| 8 |
from datetime import datetime
|
| 9 |
from transformers import pipeline
|
| 10 |
|
| 11 |
-
# Load detection model
|
| 12 |
try:
|
| 13 |
-
print("[INFO] Loading Hugging Face
|
| 14 |
classifier = pipeline(
|
| 15 |
"audio-classification",
|
| 16 |
model="padmalcom/wav2vec2-large-nonverbalvocalization-classification"
|
|
@@ -19,7 +19,7 @@ except Exception as e:
|
|
| 19 |
print(f"[ERROR] Failed to load model: {e}")
|
| 20 |
classifier = None
|
| 21 |
|
| 22 |
-
#
|
| 23 |
def convert_audio(input_path, output_path="input.wav"):
|
| 24 |
try:
|
| 25 |
cmd = [
|
|
@@ -28,32 +28,35 @@ def convert_audio(input_path, output_path="input.wav"):
|
|
| 28 |
output_path, "-y"
|
| 29 |
]
|
| 30 |
subprocess.run(cmd, check=True, stdout=subprocess.PIPE, stderr=subprocess.PIPE)
|
| 31 |
-
print(f"[
|
| 32 |
return output_path
|
| 33 |
except subprocess.CalledProcessError as e:
|
| 34 |
-
print(f"[ERROR]
|
| 35 |
raise RuntimeError("Audio conversion failed.")
|
| 36 |
|
| 37 |
-
#
|
| 38 |
def detect_scream(audio_path):
|
| 39 |
-
|
| 40 |
-
|
| 41 |
-
|
| 42 |
-
|
| 43 |
-
|
| 44 |
-
|
| 45 |
-
|
| 46 |
-
|
| 47 |
-
|
| 48 |
-
|
| 49 |
-
|
| 50 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
| 51 |
def send_salesforce_alert(audio_meta, detection):
|
| 52 |
SF_URL = os.getenv("SF_ALERT_URL")
|
| 53 |
SF_TOKEN = os.getenv("SF_API_TOKEN")
|
| 54 |
if not SF_URL or not SF_TOKEN:
|
| 55 |
-
|
| 56 |
-
raise RuntimeError("Missing Salesforce URL or token.")
|
| 57 |
|
| 58 |
headers = {
|
| 59 |
"Authorization": f"Bearer {SF_TOKEN}",
|
|
@@ -64,20 +67,16 @@ def send_salesforce_alert(audio_meta, detection):
|
|
| 64 |
"DetectedLabel": detection["label"],
|
| 65 |
"Score": detection["score"],
|
| 66 |
"AlertLevel": audio_meta["alert_level"],
|
| 67 |
-
"Timestamp": audio_meta["timestamp"]
|
| 68 |
}
|
| 69 |
|
| 70 |
-
|
| 71 |
-
|
| 72 |
-
|
| 73 |
-
|
| 74 |
-
|
| 75 |
-
|
| 76 |
-
|
| 77 |
-
raise RuntimeError("Salesforce alert failed.")
|
| 78 |
-
|
| 79 |
-
# Main Gradio handler
|
| 80 |
-
def process_uploaded(audio_file, start_stop, high_thresh, med_thresh, test_mode=False):
|
| 81 |
if start_stop != "Start":
|
| 82 |
return "π System is stopped."
|
| 83 |
|
|
@@ -86,16 +85,11 @@ def process_uploaded(audio_file, start_stop, high_thresh, med_thresh, test_mode=
|
|
| 86 |
except Exception as e:
|
| 87 |
return f"β Audio conversion error: {e}"
|
| 88 |
|
| 89 |
-
|
| 90 |
-
detection = {"label": "screaming", "score": 85.0}
|
| 91 |
-
print("[TEST MODE] Simulating scream detection with 85.0%")
|
| 92 |
-
else:
|
| 93 |
-
detection = detect_scream(wav_path)
|
| 94 |
-
|
| 95 |
label = detection["label"]
|
| 96 |
score = detection["score"]
|
| 97 |
|
| 98 |
-
# Determine
|
| 99 |
if label and "scream" in label and score >= high_thresh:
|
| 100 |
level = "High-Risk"
|
| 101 |
elif label and "scream" in label and score >= med_thresh:
|
|
@@ -109,36 +103,35 @@ def process_uploaded(audio_file, start_stop, high_thresh, med_thresh, test_mode=
|
|
| 109 |
"alert_level": level
|
| 110 |
}
|
| 111 |
|
|
|
|
| 112 |
if level in ("High-Risk", "Medium-Risk"):
|
| 113 |
try:
|
| 114 |
sf_resp = send_salesforce_alert(audio_meta, detection)
|
| 115 |
-
return f"β
Detection: {label} ({score:.1f}%) β {level} β Alert
|
| 116 |
except Exception as e:
|
| 117 |
return f"β οΈ Detection: {label} ({score:.1f}%) β {level} β ERROR: {e}"
|
| 118 |
|
| 119 |
return f"π’ Detection: {label} ({score:.1f}%) β Alert Level: {level}"
|
| 120 |
|
| 121 |
-
# Gradio
|
| 122 |
iface = gr.Interface(
|
| 123 |
fn=process_uploaded,
|
| 124 |
inputs=[
|
| 125 |
gr.Audio(type="filepath", label="Upload Audio"),
|
| 126 |
gr.Radio(["Start", "Stop"], label="System State", value="Start"),
|
| 127 |
gr.Slider(0, 100, value=80, step=1, label="High-Risk Threshold (%)"),
|
| 128 |
-
gr.Slider(0, 100, value=50, step=1, label="Medium-Risk Threshold (%)")
|
| 129 |
-
gr.Checkbox(label="Test Mode (Simulate Detection)", value=False)
|
| 130 |
],
|
| 131 |
outputs="text",
|
| 132 |
title="π’ Scream Detection & Salesforce Alerts",
|
| 133 |
description="""
|
| 134 |
-
|
| 135 |
-
|
| 136 |
-
π‘ Alerts are sent to Salesforce when scream intensity is high.
|
| 137 |
""",
|
| 138 |
allow_flagging="never"
|
| 139 |
)
|
| 140 |
|
| 141 |
-
# Optional
|
| 142 |
def pi_listener(high_thresh=80, med_thresh=50, interval=1.0):
|
| 143 |
import sounddevice as sd
|
| 144 |
import numpy as np
|
|
@@ -161,12 +154,14 @@ def pi_listener(high_thresh=80, med_thresh=50, interval=1.0):
|
|
| 161 |
print(f"[{timestamp}] {level} scream detected ({sc:.1f}%) β alert sent.")
|
| 162 |
|
| 163 |
with sd.InputStream(channels=1, samplerate=16000, callback=callback):
|
| 164 |
-
print("π
|
| 165 |
while True:
|
| 166 |
time.sleep(interval)
|
| 167 |
|
|
|
|
| 168 |
if __name__ == "__main__":
|
| 169 |
-
#
|
| 170 |
# pi_thread = threading.Thread(target=pi_listener, daemon=True)
|
| 171 |
# pi_thread.start()
|
|
|
|
| 172 |
iface.launch(server_name="0.0.0.0", server_port=int(os.getenv("PORT", 7860)))
|
|
|
|
| 8 |
from datetime import datetime
|
| 9 |
from transformers import pipeline
|
| 10 |
|
| 11 |
+
# ποΈ Load detection model
|
| 12 |
try:
|
| 13 |
+
print("[INFO] Loading Hugging Face model...")
|
| 14 |
classifier = pipeline(
|
| 15 |
"audio-classification",
|
| 16 |
model="padmalcom/wav2vec2-large-nonverbalvocalization-classification"
|
|
|
|
| 19 |
print(f"[ERROR] Failed to load model: {e}")
|
| 20 |
classifier = None
|
| 21 |
|
| 22 |
+
# === Audio Conversion ===
|
| 23 |
def convert_audio(input_path, output_path="input.wav"):
|
| 24 |
try:
|
| 25 |
cmd = [
|
|
|
|
| 28 |
output_path, "-y"
|
| 29 |
]
|
| 30 |
subprocess.run(cmd, check=True, stdout=subprocess.PIPE, stderr=subprocess.PIPE)
|
| 31 |
+
print(f"[DEBUG] Audio converted to WAV: {output_path}")
|
| 32 |
return output_path
|
| 33 |
except subprocess.CalledProcessError as e:
|
| 34 |
+
print(f"[ERROR] ffmpeg conversion failed: {e.stderr.decode()}")
|
| 35 |
raise RuntimeError("Audio conversion failed.")
|
| 36 |
|
| 37 |
+
# === Scream Detection ===
|
| 38 |
def detect_scream(audio_path):
|
| 39 |
+
try:
|
| 40 |
+
audio, sr = librosa.load(audio_path, sr=16000)
|
| 41 |
+
print(f"[DEBUG] Loaded audio: {len(audio)} samples at {sr} Hz")
|
| 42 |
+
if len(audio) == 0:
|
| 43 |
+
return {"label": "none", "score": 0.0}
|
| 44 |
+
results = classifier(audio)
|
| 45 |
+
print(f"[DEBUG] Model output: {results}")
|
| 46 |
+
if not results:
|
| 47 |
+
return {"label": "none", "score": 0.0}
|
| 48 |
+
top = results[0]
|
| 49 |
+
return {"label": top["label"].lower(), "score": float(top["score"]) * 100}
|
| 50 |
+
except Exception as e:
|
| 51 |
+
print(f"[ERROR] Detection failed: {e}")
|
| 52 |
+
return {"label": "error", "score": 0.0}
|
| 53 |
+
|
| 54 |
+
# === Send Alert to Salesforce ===
|
| 55 |
def send_salesforce_alert(audio_meta, detection):
|
| 56 |
SF_URL = os.getenv("SF_ALERT_URL")
|
| 57 |
SF_TOKEN = os.getenv("SF_API_TOKEN")
|
| 58 |
if not SF_URL or not SF_TOKEN:
|
| 59 |
+
raise RuntimeError("Salesforce config missing.")
|
|
|
|
| 60 |
|
| 61 |
headers = {
|
| 62 |
"Authorization": f"Bearer {SF_TOKEN}",
|
|
|
|
| 67 |
"DetectedLabel": detection["label"],
|
| 68 |
"Score": detection["score"],
|
| 69 |
"AlertLevel": audio_meta["alert_level"],
|
| 70 |
+
"Timestamp": audio_meta["timestamp"],
|
| 71 |
}
|
| 72 |
|
| 73 |
+
print(f"[DEBUG] Sending payload to Salesforce: {payload}")
|
| 74 |
+
resp = requests.post(SF_URL, json=payload, headers=headers, timeout=5)
|
| 75 |
+
resp.raise_for_status()
|
| 76 |
+
return resp.json()
|
| 77 |
+
|
| 78 |
+
# === Main Gradio Function ===
|
| 79 |
+
def process_uploaded(audio_file, start_stop, high_thresh, med_thresh):
|
|
|
|
|
|
|
|
|
|
|
|
|
| 80 |
if start_stop != "Start":
|
| 81 |
return "π System is stopped."
|
| 82 |
|
|
|
|
| 85 |
except Exception as e:
|
| 86 |
return f"β Audio conversion error: {e}"
|
| 87 |
|
| 88 |
+
detection = detect_scream(wav_path)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 89 |
label = detection["label"]
|
| 90 |
score = detection["score"]
|
| 91 |
|
| 92 |
+
# Determine risk level
|
| 93 |
if label and "scream" in label and score >= high_thresh:
|
| 94 |
level = "High-Risk"
|
| 95 |
elif label and "scream" in label and score >= med_thresh:
|
|
|
|
| 103 |
"alert_level": level
|
| 104 |
}
|
| 105 |
|
| 106 |
+
# Send to Salesforce if needed
|
| 107 |
if level in ("High-Risk", "Medium-Risk"):
|
| 108 |
try:
|
| 109 |
sf_resp = send_salesforce_alert(audio_meta, detection)
|
| 110 |
+
return f"β
Detection: {label} ({score:.1f}%) β {level} β Alert sent (ID: {sf_resp.get('id', 'N/A')})"
|
| 111 |
except Exception as e:
|
| 112 |
return f"β οΈ Detection: {label} ({score:.1f}%) β {level} β ERROR: {e}"
|
| 113 |
|
| 114 |
return f"π’ Detection: {label} ({score:.1f}%) β Alert Level: {level}"
|
| 115 |
|
| 116 |
+
# === Gradio UI ===
|
| 117 |
iface = gr.Interface(
|
| 118 |
fn=process_uploaded,
|
| 119 |
inputs=[
|
| 120 |
gr.Audio(type="filepath", label="Upload Audio"),
|
| 121 |
gr.Radio(["Start", "Stop"], label="System State", value="Start"),
|
| 122 |
gr.Slider(0, 100, value=80, step=1, label="High-Risk Threshold (%)"),
|
| 123 |
+
gr.Slider(0, 100, value=50, step=1, label="Medium-Risk Threshold (%)")
|
|
|
|
| 124 |
],
|
| 125 |
outputs="text",
|
| 126 |
title="π’ Scream Detection & Salesforce Alerts",
|
| 127 |
description="""
|
| 128 |
+
π§ Upload or record audio. System classifies screams and triggers alerts.
|
| 129 |
+
β οΈ Alerts are sent to Salesforce for High/Medium-Risk detections.
|
|
|
|
| 130 |
""",
|
| 131 |
allow_flagging="never"
|
| 132 |
)
|
| 133 |
|
| 134 |
+
# === Optional Real-Time Listener ===
|
| 135 |
def pi_listener(high_thresh=80, med_thresh=50, interval=1.0):
|
| 136 |
import sounddevice as sd
|
| 137 |
import numpy as np
|
|
|
|
| 154 |
print(f"[{timestamp}] {level} scream detected ({sc:.1f}%) β alert sent.")
|
| 155 |
|
| 156 |
with sd.InputStream(channels=1, samplerate=16000, callback=callback):
|
| 157 |
+
print("π Real-time detection started...")
|
| 158 |
while True:
|
| 159 |
time.sleep(interval)
|
| 160 |
|
| 161 |
+
# === App Entry ===
|
| 162 |
if __name__ == "__main__":
|
| 163 |
+
# Optional: enable real-time listener
|
| 164 |
# pi_thread = threading.Thread(target=pi_listener, daemon=True)
|
| 165 |
# pi_thread.start()
|
| 166 |
+
|
| 167 |
iface.launch(server_name="0.0.0.0", server_port=int(os.getenv("PORT", 7860)))
|