MetiMiester commited on
Commit
c85978f
·
verified ·
1 Parent(s): 752ac49

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +16 -15
app.py CHANGED
@@ -18,18 +18,19 @@ asr = pipeline(
18
  generate_kwargs={"language": "en", "task": "transcribe"}
19
  )
20
 
21
- def classify(audio):
22
  """
23
- audio: Tuple(numpy_array, sampling_rate)
24
  returns: transcript (str), safety probabilities (dict), unsafe probability (str)
25
  """
26
- arr, sr = audio
27
- arr = arr.astype("float32")
28
- if arr.ndim > 1:
29
- arr = arr.mean(axis=1)
30
-
31
- # ASR transcription
32
- result = asr({"array": arr, "sampling_rate": sr})
 
33
  txt = result["text"].strip()
34
 
35
  # Safety classification
@@ -38,16 +39,16 @@ def classify(audio):
38
  unsafe_str = f"{proba:.2f}"
39
  return txt, label_probs, unsafe_str
40
 
41
- # Use the components API
42
- audio_input = gr.components.Audio(label="Upload or record audio", type="numpy")
43
- transcript_out = gr.components.Textbox(label="Transcript")
44
- probs_out = gr.components.Label(num_top_classes=2, label="Safety Probabilities")
45
- unsafe_prob_out = gr.components.Textbox(label="Unsafe Probability")
46
 
47
  iface = gr.Interface(
48
  fn=classify,
49
  inputs=audio_input,
50
- outputs=[transcript_out, probs_out, unsafe_prob_out],
51
  title="BubbleGuard Audio Safety Checker",
52
  description="Upload or record audio; get ASR transcript plus safe/unsafe probabilities."
53
  )
 
18
  generate_kwargs={"language": "en", "task": "transcribe"}
19
  )
20
 
21
+ def classify(audio_path):
22
  """
23
+ audio_path: str → path to uploaded/recorded file
24
  returns: transcript (str), safety probabilities (dict), unsafe probability (str)
25
  """
26
+ # Read file from disk
27
+ audio, sr = sf.read(audio_path, dtype="float32")
28
+ # If stereo, convert to mono
29
+ if audio.ndim > 1:
30
+ audio = audio.mean(axis=1)
31
+
32
+ # ASR
33
+ result = asr({"array": audio, "sampling_rate": sr})
34
  txt = result["text"].strip()
35
 
36
  # Safety classification
 
39
  unsafe_str = f"{proba:.2f}"
40
  return txt, label_probs, unsafe_str
41
 
42
+ # Use filepath-based Audio component
43
+ audio_input = gr.components.Audio(label="Upload or record audio", type="filepath")
44
+ transcript_out = gr.components.Textbox(label="Transcript")
45
+ probs_out = gr.components.Label(num_top_classes=2, label="Safety Probabilities")
46
+ unsafe_out = gr.components.Textbox(label="Unsafe Probability")
47
 
48
  iface = gr.Interface(
49
  fn=classify,
50
  inputs=audio_input,
51
+ outputs=[transcript_out, probs_out, unsafe_out],
52
  title="BubbleGuard Audio Safety Checker",
53
  description="Upload or record audio; get ASR transcript plus safe/unsafe probabilities."
54
  )