Spaces:
Runtime error

File size: 1,065 Bytes
43d0388
 
 
 
67be4d3
43d0388
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
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()