Spaces:
Sleeping
Sleeping
| import gradio as gr | |
| from transformers import pipeline | |
| import numpy as np | |
| # βββ LOAD MODEL βββββββββββββββββββββββββββββββββββββββββββ | |
| classifier = pipeline( | |
| "audio-classification", | |
| model="MIT/ast-finetuned-audioset-10-10-0.4593" | |
| ) | |
| # βββ THREAT MAPPING βββββββββββββββββββββββββββββββββββββββ | |
| def map_label(label, score): | |
| label = label.lower() | |
| if "engine" in label: | |
| return f"πͺ Chainsaw detected ({round(score, 2)})" | |
| if "gunshot" in label or "bang" in label or "explosion" in label: | |
| return f"π« Gunshot detected ({round(score, 2)})" | |
| if "vehicle" in label or "car" in label or "truck" in label: | |
| return f"π Vehicle detected ({round(score, 2)})" | |
| if "speech" in label or "talk" in label or "shout" in label: | |
| return f"π€ Human detected ({round(score, 2)})" | |
| return f"πΏ Safe: {label} ({round(score, 2)})" | |
| # βββ PREDICTION βββββββββββββββββββββββββββββββββββββββββββ | |
| def predict(audio): | |
| if audio is None: | |
| return "π€ Waiting for audio..." | |
| sr, data = audio | |
| # Convert stereo β mono | |
| if len(data.shape) > 1: | |
| data = data.mean(axis=1) | |
| data = data.astype(np.float32) | |
| results = classifier({ | |
| "raw": data, | |
| "sampling_rate": sr | |
| }) | |
| top = results[0] | |
| # π₯ APPLY MAPPING HERE | |
| return map_label(top["label"], top["score"]) | |
| # βββ UI βββββββββββββββββββββββββββββββββββββββββββββββββββ | |
| with gr.Blocks() as demo: | |
| gr.Markdown("## π² Forest Guardian - Live Audio Detection") | |
| mic = gr.Audio( | |
| sources=["microphone"], | |
| streaming=True, # π₯ LIVE STREAMING | |
| type="numpy" | |
| ) | |
| output = gr.Textbox(label="Detection Result") | |
| mic.stream(fn=predict, inputs=mic, outputs=output) | |
| # βββ RUN ββββββββββββββββββββββββββββββββββββββββββββββββββ | |
| demo.launch() |