Spaces:
Runtime error

gituser
'hi'
67be4d3
Raw History Blame Contribute Delete
1.07 kB
import gradio as gr
import numpy as np
import librosa
from keras.models import model_from_json, load_model
from Helper_Functions import load_model
target_sr = 22050
target_duration = 2 # in seconds
target_samples = target_sr * target_duration
model = load_model()
def transcribe(stream, new_chunk):
sr, y = new_chunk
y = y.astype(np.float32)
y_resampled = librosa.resample(y = y, orig_sr = sr, target_sr = target_sr)
if stream is not None:
stream = np.concatenate([stream, y_resampled])
else:
stream = y_resampled
# print(len(stream))
if len(stream) >= 44100:
# Take the first 2 seconds of audio
stream = stream[:target_samples]
prediction = model.predict(stream.reshape(1,-1))
output = "all okay"
if prediction > 0.5:
output = "GUNSHOT DETECTED"
return None, f"{output}"
return stream, ''
demo = gr.Interface(
transcribe,
["state", gr.Audio(sources=["microphone"], streaming=True)],
["state", "text"],
live=True,
)
demo.launch()