Dimsumcat commited on
Commit
5735916
·
verified ·
1 Parent(s): 369af86

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +89 -9
app.py CHANGED
@@ -9,15 +9,97 @@ import matplotlib.pyplot as plt
9
  model = tf.keras.models.load_model("model.h5")
10
 
11
  # Function to process audio, predict, and generate results
12
- def process_audio(audio_file, inhale_duration, exhale_duration):
13
  try:
14
- # Calculate total time to stop recording (inhale + exhale)
15
- total_duration = inhale_duration + exhale_duration
16
  # Load the audio file
17
  y, sr = librosa.load(audio_file, sr=16000)
18
 
19
- # Ensure the audio is only processed for the total duration
20
- y = y[:int(total_duration * sr)]
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
21
 
22
  # Detect segments (e.g., using energy or silence)
23
  intervals = librosa.effects.split(y, top_db=20)
@@ -79,17 +161,15 @@ with gr.Blocks() as demo:
79
  gr.Markdown("### Breathe Training Application")
80
  with gr.Row():
81
  audio_input = gr.Audio(type="filepath", label="Upload or Record Audio")
82
- inhale_duration_input = gr.Number(label="Inhale Duration (s)", value=5, interactive=True)
83
- exhale_duration_input = gr.Number(label="Exhale Duration (s)", value=5, interactive=True)
84
  result_output = gr.Textbox(label="Prediction Results (Table)")
85
  waveform_output = gr.Image(label="Waveform with Highlighted Segments")
86
  submit_button = gr.Button("Analyze")
87
 
88
  submit_button.click(
89
  fn=process_audio,
90
- inputs=[audio_input, inhale_duration_input, exhale_duration_input],
91
  outputs=[result_output, waveform_output]
92
  )
93
 
94
  # Run the Gradio app
95
- demo.launch()
 
9
  model = tf.keras.models.load_model("model.h5")
10
 
11
  # Function to process audio, predict, and generate results
12
+ def process_audio(audio_file):
13
  try:
 
 
14
  # Load the audio file
15
  y, sr = librosa.load(audio_file, sr=16000)
16
 
17
+ # Detect segments (e.g., using energy or silence)
18
+ intervals = librosa.effects.split(y, top_db=20)
19
+
20
+ results = []
21
+
22
+ plt.figure(figsize=(10, 4))
23
+ librosa.display.waveshow(y, sr=sr, alpha=0.5)
24
+
25
+ # Process each segment
26
+ for i, (start, end) in enumerate(intervals):
27
+ segment = y[start:end]
28
+ duration = (end - start) / sr
29
+
30
+ # Compute the amplitude (mean absolute value)
31
+ amplitude = np.mean(np.abs(segment))
32
+
33
+ # Extract MFCC features
34
+ mfcc = librosa.feature.mfcc(y=segment, sr=sr, n_mfcc=13)
35
+ mfcc = np.mean(mfcc, axis=1).reshape(1, -1)
36
+
37
+ # Predict inhale or exhale (override with amplitude logic)
38
+ prediction = model.predict(mfcc)
39
+ label_from_model = "Inhale" if np.argmax(prediction) == 0 else "Exhale"
40
+
41
+ # Assign label based on amplitude
42
+ label = "Inhale" if amplitude > 0.05 else "Exhale" # Threshold for exhale
43
+
44
+ # Append results
45
+ results.append({
46
+ "Segment": i + 1,
47
+ "Type": label,
48
+ "Duration (s)": round(duration, 2),
49
+ "Amplitude": round(amplitude, 4)
50
+ })
51
+
52
+ # Highlight segment on waveform with swapped colors
53
+ plt.axvspan(start / sr, end / sr, color='red' if label == "Inhale" else 'blue', alpha=0.3)
54
+
55
+ # Save the waveform with highlighted segments
56
+ plt.title("Audio Waveform with Inhale/Exhale Segments")
57
+ plt.xlabel("Time (s)")
58
+ plt.ylabel("Amplitude")
59
+ plt.savefig("waveform_highlighted.png")
60
+ plt.close()
61
+
62
+ # Format results as a table
63
+ result_table = "Segment\tType\tDuration (s)\tAmplitude\n" + "\n".join(
64
+ f"{row['Segment']}\t{row['Type']}\t{row['Duration (s)']}\t{row['Amplitude']}" for row in results
65
+ )
66
+
67
+ return result_table, "waveform_highlighted.png"
68
+
69
+ except Exception as e:
70
+ return f"Error: {str(e)}", None
71
+
72
+ # Define Gradio interface
73
+ with gr.Blocks() as demo:
74
+ gr.Markdown("### Breathe Training Application")
75
+ with gr.Row():
76
+ audio_input = gr.Audio(type="filepath", label="Upload or Record Audio")
77
+ result_output = gr.Textbox(label="Prediction Results (Table)")
78
+ waveform_output = gr.Image(label="Waveform with Highlighted Segments")
79
+ submit_button = gr.Button("Analyze")
80
+
81
+ submit_button.click(
82
+ fn=process_audio,
83
+ inputs=[audio_input],
84
+ outputs=[result_output, waveform_output]
85
+ )
86
+
87
+ # Run the Gradio app
88
+ demo.launch() make user can add period of breathe in and out as they want the sum of both selection will be the time that record auto stop mport gradio as gr
89
+ import tensorflow as tf
90
+ import librosa
91
+ import librosa.display
92
+ import numpy as np
93
+ import matplotlib.pyplot as plt
94
+
95
+ # Load the pre-trained model
96
+ model = tf.keras.models.load_model("model.h5")
97
+
98
+ # Function to process audio, predict, and generate results
99
+ def process_audio(audio_file):
100
+ try:
101
+ # Load the audio file
102
+ y, sr = librosa.load(audio_file, sr=16000)
103
 
104
  # Detect segments (e.g., using energy or silence)
105
  intervals = librosa.effects.split(y, top_db=20)
 
161
  gr.Markdown("### Breathe Training Application")
162
  with gr.Row():
163
  audio_input = gr.Audio(type="filepath", label="Upload or Record Audio")
 
 
164
  result_output = gr.Textbox(label="Prediction Results (Table)")
165
  waveform_output = gr.Image(label="Waveform with Highlighted Segments")
166
  submit_button = gr.Button("Analyze")
167
 
168
  submit_button.click(
169
  fn=process_audio,
170
+ inputs=[audio_input],
171
  outputs=[result_output, waveform_output]
172
  )
173
 
174
  # Run the Gradio app
175
+ demo.launch()