Dimsumcat commited on
Commit
8e6cfd5
·
verified ·
1 Parent(s): ec4cbbc

Create app.py

Browse files
Files changed (1) hide show
  1. app.py +50 -0
app.py ADDED
@@ -0,0 +1,50 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import gradio as gr
2
+ import tensorflow as tf
3
+ import librosa
4
+ import numpy as np
5
+ import matplotlib.pyplot as plt
6
+ import pandas as pd
7
+
8
+ # Load the pre-trained model
9
+ model = tf.keras.models.load_model("model.h5")
10
+
11
+ # Function to process audio and make predictions
12
+ def process_audio(audio_file):
13
+ # Load audio file
14
+ y, sr = librosa.load(audio_file, sr=16000)
15
+
16
+ # Feature extraction
17
+ mfcc = librosa.feature.mfcc(y=y, sr=sr, n_mfcc=13)
18
+ mfcc = np.mean(mfcc, axis=1).reshape(1, -1)
19
+
20
+ # Predict inhale/exhale using the model
21
+ prediction = model.predict(mfcc)
22
+
23
+ # For demonstration, return the prediction and a waveform plot
24
+ plt.figure(figsize=(10, 4))
25
+ librosa.display.waveshow(y, sr=sr)
26
+ plt.title("Audio Waveform")
27
+ plt.xlabel("Time (s)")
28
+ plt.ylabel("Amplitude")
29
+ plt.savefig("waveform.png")
30
+ plt.close()
31
+
32
+ return f"Prediction: {np.argmax(prediction)}", "waveform.png"
33
+
34
+ # Define Gradio interface
35
+ with gr.Blocks() as demo:
36
+ gr.Markdown("### Breathe Training Application")
37
+ with gr.Row():
38
+ audio_input = gr.Audio(label="Upload or Record Audio", type="filepath")
39
+ result_output = gr.Textbox(label="Prediction Result")
40
+ waveform_output = gr.Image(label="Waveform")
41
+ submit_button = gr.Button("Analyze")
42
+
43
+ submit_button.click(
44
+ fn=process_audio,
45
+ inputs=[audio_input],
46
+ outputs=[result_output, waveform_output]
47
+ )
48
+
49
+ # Run the Gradio app
50
+ demo.launch()