rjzevallos commited on
Commit
bcc3253
·
1 Parent(s): 921076a

Fix(app): init ASR model in background; add error handling; remove share=True

Browse files
Files changed (1) hide show
  1. app.py +20 -4
app.py CHANGED
@@ -1,4 +1,6 @@
1
  import time
 
 
2
 
3
  import gradio as gr
4
  import librosa
@@ -10,7 +12,7 @@ AUDIO_SECONDS_THRESHOLD = 2
10
  #pipe = pipeline("audio-classification", model="MIT/ast-finetuned-audioset-10-10-0.4593")
11
  prediction = [{"score": 1, "label": "recording..."}]
12
 
13
- from server_wrapper import process_chunk_from_bytes
14
 
15
 
16
 
@@ -27,7 +29,13 @@ def streaming_recording_fn(stream, new_chunk):
27
  y = librosa.resample(y, orig_sr=sr, target_sr=TARGET_SAMPLE_RATE)
28
  if stream is not None:
29
  if (stream.shape[-1] / TARGET_SAMPLE_RATE) >= AUDIO_SECONDS_THRESHOLD:
30
- prediction = process_chunk_from_bytes((stream * 32768).astype(np.int16).tobytes())
 
 
 
 
 
 
31
  file_name = f'./audio/{time.strftime("%Y%m%d_%H%M%S", time.localtime())}.wav'
32
  # # sf.write(file_name, stream, TARGET_SAMPLE_RATE)
33
  print(f"SAVE AUDIO: {file_name}")
@@ -58,5 +66,13 @@ with gr.Blocks() as demo:
58
  ["Streaming"])
59
 
60
  if __name__ == "__main__":
61
-
62
- demo.launch(share=True)
 
 
 
 
 
 
 
 
 
1
  import time
2
+ import threading
3
+ import os
4
 
5
  import gradio as gr
6
  import librosa
 
12
  #pipe = pipeline("audio-classification", model="MIT/ast-finetuned-audioset-10-10-0.4593")
13
  prediction = [{"score": 1, "label": "recording..."}]
14
 
15
+ from server_wrapper import process_chunk_from_bytes, init_model
16
 
17
 
18
 
 
29
  y = librosa.resample(y, orig_sr=sr, target_sr=TARGET_SAMPLE_RATE)
30
  if stream is not None:
31
  if (stream.shape[-1] / TARGET_SAMPLE_RATE) >= AUDIO_SECONDS_THRESHOLD:
32
+ try:
33
+ prediction = process_chunk_from_bytes((stream * 32768).astype(np.int16).tobytes())
34
+ except Exception as e:
35
+ print(f"process_chunk_from_bytes failed: {e}")
36
+ prediction = [{"score": 0.0, "label": "error"}]
37
+
38
+ os.makedirs('./audio', exist_ok=True)
39
  file_name = f'./audio/{time.strftime("%Y%m%d_%H%M%S", time.localtime())}.wav'
40
  # # sf.write(file_name, stream, TARGET_SAMPLE_RATE)
41
  print(f"SAVE AUDIO: {file_name}")
 
66
  ["Streaming"])
67
 
68
  if __name__ == "__main__":
69
+ # Initialize the ASR model in a background thread to avoid blocking the UI startup.
70
+ try:
71
+ t = threading.Thread(target=init_model, daemon=True)
72
+ t.start()
73
+ print("Background model initialization started")
74
+ except Exception as e:
75
+ print(f"Failed to start background model init: {e}")
76
+
77
+ # Do not use `share=True` inside Spaces; launch normally.
78
+ demo.launch()