hudaakram commited on
Commit
f241201
·
verified ·
1 Parent(s): d596bdf

Delete app.py

Browse files
Files changed (1) hide show
  1. app.py +0 -53
app.py DELETED
@@ -1,53 +0,0 @@
1
- import gradio as gr, numpy as np, librosa, tensorflow as tf, os, json
2
-
3
- SR, N_SAMPLES, N_MELS, HOP, NFFT = 16000, 16000, 64, 160, 400
4
- PAD_T = 100
5
- LABELS = ['yes','no','up','down','left','right','on','off','stop','go','unknown','silence']
6
-
7
- model = tf.keras.models.load_model("speech_command_cnn") # put model files in Space or load from Hub
8
-
9
- def load_and_feat(path):
10
- y, _ = librosa.load(path, sr=SR)
11
- if len(y) > N_SAMPLES: y = y[:N_SAMPLES]
12
- if len(y) < N_SAMPLES: y = np.pad(y, (0, N_SAMPLES-len(y)))
13
- S = librosa.feature.melspectrogram(y=y, sr=SR, n_fft=NFFT, hop_length=HOP, n_mels=N_MELS, fmax=8000)
14
- logS = librosa.power_to_db(S, ref=np.max)
15
- logS = (logS - logS.mean()) / (logS.std()+1e-9)
16
- f = logS[..., np.newaxis]
17
- T = f.shape[1]
18
- if T < PAD_T: f = np.pad(f, ((0,0),(0,PAD_T-T),(0,0)))
19
- else: f = f[:, :PAD_T, :]
20
- return f.astype(np.float32)
21
-
22
- INTENTS = {
23
- 'yes': 'confirm',
24
- 'no': 'cancel',
25
- 'stop': 'pause',
26
- 'go': 'start',
27
- 'on': 'turn_on',
28
- 'off': 'turn_off',
29
- 'up': 'volume_up',
30
- 'down': 'volume_down',
31
- 'left': 'move_left',
32
- 'right': 'move_right'
33
- }
34
-
35
- def predict(audio_path):
36
- f = load_and_feat(audio_path)
37
- p = model.predict(f[np.newaxis,...], verbose=0)[0]
38
- top_idx = np.argsort(-p)[:3]
39
- top = [(LABELS[i], float(p[i])) for i in top_idx]
40
- label = LABELS[int(np.argmax(p))]
41
- intent = INTENTS.get(label, 'none')
42
- return {k: float(v) for k,v in top}, intent
43
-
44
- demo = gr.Interface(
45
- fn=predict,
46
- inputs=gr.Audio(sources=["microphone","upload"], type="filepath"),
47
- outputs=[gr.Label(num_top_classes=3), gr.Textbox(label="Intent")],
48
- title="Voice Command → Intent",
49
- description="Speak a short command: yes/no/stop/go/up/down/left/right/on/off"
50
- )
51
-
52
- if __name__ == "__main__":
53
- demo.launch()