Update app.py
Browse files
app.py
CHANGED
|
@@ -1,53 +1,79 @@
|
|
| 1 |
-
|
| 2 |
-
import
|
| 3 |
import soundfile as sf
|
|
|
|
|
|
|
| 4 |
from datetime import datetime
|
| 5 |
-
import requests
|
| 6 |
|
| 7 |
-
# Initialize
|
| 8 |
classifier = pipeline(
|
| 9 |
"audio-classification",
|
| 10 |
model="padmalcom/wav2vec2-large-nonverbalvocalization-classification",
|
| 11 |
)
|
| 12 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 13 |
def detect_scream(audio_path: str):
|
|
|
|
|
|
|
|
|
|
|
|
|
| 14 |
audio, sr = sf.read(audio_path)
|
| 15 |
-
# Resample
|
| 16 |
if sr != classifier.feature_extractor.sampling_rate:
|
| 17 |
-
import librosa
|
| 18 |
audio = librosa.resample(audio, orig_sr=sr, target_sr=classifier.feature_extractor.sampling_rate)
|
|
|
|
| 19 |
results = classifier(audio)
|
| 20 |
top = results[0]
|
| 21 |
label = top["label"]
|
| 22 |
-
score = float(top["score"]) * 100
|
| 23 |
-
|
| 24 |
if label.lower() == "scream" and score > 80:
|
| 25 |
alert = "High-Risk"
|
| 26 |
elif label.lower() == "scream" and score > 50:
|
| 27 |
alert = "Medium-Risk"
|
| 28 |
else:
|
| 29 |
alert = "None"
|
|
|
|
| 30 |
return label, score, alert
|
| 31 |
|
| 32 |
-
def log_to_salesforce(
|
| 33 |
-
|
| 34 |
-
|
| 35 |
-
|
| 36 |
-
|
| 37 |
-
|
| 38 |
-
"Alert_Level__c": alert,
|
| 39 |
-
"Timestamp__c": datetime.utcnow().isoformat(),
|
| 40 |
-
# add User__c if available
|
| 41 |
-
})
|
| 42 |
-
# trigger Salesforce alert automation (email/SMS/in‑app)
|
| 43 |
|
| 44 |
def main():
|
| 45 |
-
|
| 46 |
-
|
| 47 |
-
|
| 48 |
-
|
| 49 |
-
|
| 50 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 51 |
|
| 52 |
if __name__ == "__main__":
|
| 53 |
main()
|
|
|
|
| 1 |
+
import os
|
| 2 |
+
import subprocess
|
| 3 |
import soundfile as sf
|
| 4 |
+
import librosa
|
| 5 |
+
from transformers import pipeline
|
| 6 |
from datetime import datetime
|
|
|
|
| 7 |
|
| 8 |
+
# Initialize Hugging Face pipeline
|
| 9 |
classifier = pipeline(
|
| 10 |
"audio-classification",
|
| 11 |
model="padmalcom/wav2vec2-large-nonverbalvocalization-classification",
|
| 12 |
)
|
| 13 |
|
| 14 |
+
def convert_audio(input_path, output_path="input.wav"):
|
| 15 |
+
"""Convert audio to 16kHz mono WAV using ffmpeg."""
|
| 16 |
+
if not os.path.isfile(input_path):
|
| 17 |
+
raise FileNotFoundError(f"File not found: {input_path}")
|
| 18 |
+
|
| 19 |
+
cmd = [
|
| 20 |
+
"ffmpeg", "-i", input_path,
|
| 21 |
+
"-acodec", "pcm_s16le",
|
| 22 |
+
"-ar", "16000",
|
| 23 |
+
"-ac", "1",
|
| 24 |
+
output_path,
|
| 25 |
+
"-y"
|
| 26 |
+
]
|
| 27 |
+
|
| 28 |
+
try:
|
| 29 |
+
subprocess.run(cmd, check=True, stdout=subprocess.PIPE, stderr=subprocess.PIPE)
|
| 30 |
+
return output_path
|
| 31 |
+
except subprocess.CalledProcessError as e:
|
| 32 |
+
raise RuntimeError("FFmpeg conversion failed: " + e.stderr.decode())
|
| 33 |
+
|
| 34 |
def detect_scream(audio_path: str):
|
| 35 |
+
"""Run scream detection on a WAV file."""
|
| 36 |
+
if not os.path.isfile(audio_path):
|
| 37 |
+
raise FileNotFoundError(f"Audio file not found: {audio_path}")
|
| 38 |
+
|
| 39 |
audio, sr = sf.read(audio_path)
|
| 40 |
+
# Resample if needed
|
| 41 |
if sr != classifier.feature_extractor.sampling_rate:
|
|
|
|
| 42 |
audio = librosa.resample(audio, orig_sr=sr, target_sr=classifier.feature_extractor.sampling_rate)
|
| 43 |
+
|
| 44 |
results = classifier(audio)
|
| 45 |
top = results[0]
|
| 46 |
label = top["label"]
|
| 47 |
+
score = float(top["score"]) * 100
|
| 48 |
+
|
| 49 |
if label.lower() == "scream" and score > 80:
|
| 50 |
alert = "High-Risk"
|
| 51 |
elif label.lower() == "scream" and score > 50:
|
| 52 |
alert = "Medium-Risk"
|
| 53 |
else:
|
| 54 |
alert = "None"
|
| 55 |
+
|
| 56 |
return label, score, alert
|
| 57 |
|
| 58 |
+
def log_to_salesforce(audio_url, label, score, alert):
|
| 59 |
+
"""Placeholder for Salesforce integration."""
|
| 60 |
+
print("Logging to Salesforce...")
|
| 61 |
+
print(f"Audio URL: {audio_url}")
|
| 62 |
+
print(f"Result: {label}, Score: {score:.1f}%, Alert Level: {alert}")
|
| 63 |
+
# Integration with Salesforce via simple-salesforce or REST API goes here
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 64 |
|
| 65 |
def main():
|
| 66 |
+
# Input file path (any audio format)
|
| 67 |
+
raw_input_path = "my_audio.mp3" # Change to your input file
|
| 68 |
+
audio_url = "https://yourstorage.com/path/to/audio" # Simulated URL
|
| 69 |
+
|
| 70 |
+
try:
|
| 71 |
+
wav_path = convert_audio(raw_input_path)
|
| 72 |
+
label, score, alert = detect_scream(wav_path)
|
| 73 |
+
print(f"Detected: {label} ({score:.1f}%) — Alert Level: {alert}")
|
| 74 |
+
log_to_salesforce(audio_url, label, score, alert)
|
| 75 |
+
except Exception as e:
|
| 76 |
+
print("Error:", str(e))
|
| 77 |
|
| 78 |
if __name__ == "__main__":
|
| 79 |
main()
|