Dimsumcat commited on
Commit
7d0b2db
·
verified ·
1 Parent(s): 681bb83

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +9 -23
app.py CHANGED
@@ -1,11 +1,10 @@
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 time
7
- import io
8
- from PIL import Image
9
 
10
  # Load the pre-trained model (optional for this logic, depending on use case)
11
  model = tf.keras.models.load_model("model.h5")
@@ -100,35 +99,23 @@ def process_audio(audio_file, inhale_duration, exhale_duration):
100
  countdown_message = f"Starting recording with total duration of {total_duration} seconds.\n"
101
  countdown_message += f"Starting Inhale for {inhale_duration} seconds...\n"
102
 
103
- # Balloon size animation (use Gradio's JS for animation)
104
- balloon_animation = []
105
-
106
  for t in range(inhale_duration, 0, -1):
107
  time.sleep(1)
108
  countdown_message += f"Inhale: {t}s remaining...\n"
109
- balloon_animation.append(f"scaleBalloon({t}, 'inhale')") # Animation call
110
-
111
  countdown_message += f"Switching to Exhale for {exhale_duration} seconds...\n"
112
-
113
  for t in range(exhale_duration, 0, -1):
114
  time.sleep(1)
115
  countdown_message += f"Exhale: {t}s remaining...\n"
116
- balloon_animation.append(f"scaleBalloon({t}, 'exhale')") # Animation call
117
-
118
  countdown_message += "Recording Finished!"
119
 
120
- # Balloon image to return as the animated image
121
- balloon_image = create_balloon_image()
122
-
123
- return result_table, "waveform_highlighted.png", countdown_message, balloon_image, balloon_animation
124
 
125
- # Create a balloon image to display
126
- def create_balloon_image():
127
- # Create a balloon using PIL or load a pre-existing image and resize it
128
- balloon = Image.new('RGBA', (200, 200), (255, 255, 255, 0))
129
- # Drawing a simple circle for the balloon
130
- balloon = balloon.resize((200, 200)) # Initial size
131
- return balloon
132
 
133
  # Define Gradio interface
134
  with gr.Blocks() as demo:
@@ -144,14 +131,13 @@ with gr.Blocks() as demo:
144
  result_output = gr.Textbox(label="Prediction Results (Table)")
145
  waveform_output = gr.Image(label="Waveform with Highlighted Segments")
146
  countdown_output = gr.Textbox(label="Countdown Timer")
147
- balloon_output = gr.Image(label="Balloon Animation")
148
 
149
  submit_button = gr.Button("Start Record")
150
 
151
  submit_button.click(
152
  fn=process_audio,
153
  inputs=[audio_input, inhale_duration, exhale_duration],
154
- outputs=[result_output, waveform_output, countdown_output, balloon_output]
155
  )
156
 
157
  # Run the Gradio app
 
1
  import gradio as gr
2
  import tensorflow as tf
3
  import librosa
4
+ import librosa.display
5
  import numpy as np
6
  import matplotlib.pyplot as plt
7
  import time
 
 
8
 
9
  # Load the pre-trained model (optional for this logic, depending on use case)
10
  model = tf.keras.models.load_model("model.h5")
 
99
  countdown_message = f"Starting recording with total duration of {total_duration} seconds.\n"
100
  countdown_message += f"Starting Inhale for {inhale_duration} seconds...\n"
101
 
 
 
 
102
  for t in range(inhale_duration, 0, -1):
103
  time.sleep(1)
104
  countdown_message += f"Inhale: {t}s remaining...\n"
105
+
 
106
  countdown_message += f"Switching to Exhale for {exhale_duration} seconds...\n"
107
+
108
  for t in range(exhale_duration, 0, -1):
109
  time.sleep(1)
110
  countdown_message += f"Exhale: {t}s remaining...\n"
111
+
 
112
  countdown_message += "Recording Finished!"
113
 
114
+ return result_table, "waveform_highlighted.png", countdown_message
115
+
116
+ except Exception as e:
117
+ return f"Error: {str(e)}", None, None
118
 
 
 
 
 
 
 
 
119
 
120
  # Define Gradio interface
121
  with gr.Blocks() as demo:
 
131
  result_output = gr.Textbox(label="Prediction Results (Table)")
132
  waveform_output = gr.Image(label="Waveform with Highlighted Segments")
133
  countdown_output = gr.Textbox(label="Countdown Timer")
 
134
 
135
  submit_button = gr.Button("Start Record")
136
 
137
  submit_button.click(
138
  fn=process_audio,
139
  inputs=[audio_input, inhale_duration, exhale_duration],
140
+ outputs=[result_output, waveform_output, countdown_output]
141
  )
142
 
143
  # Run the Gradio app