Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
|
@@ -18,18 +18,19 @@ asr = pipeline(
|
|
| 18 |
generate_kwargs={"language": "en", "task": "transcribe"}
|
| 19 |
)
|
| 20 |
|
| 21 |
-
def classify(
|
| 22 |
"""
|
| 23 |
-
|
| 24 |
returns: transcript (str), safety probabilities (dict), unsafe probability (str)
|
| 25 |
"""
|
| 26 |
-
|
| 27 |
-
|
| 28 |
-
|
| 29 |
-
|
| 30 |
-
|
| 31 |
-
|
| 32 |
-
|
|
|
|
| 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
|
| 42 |
-
audio_input
|
| 43 |
-
transcript_out
|
| 44 |
-
probs_out
|
| 45 |
-
|
| 46 |
|
| 47 |
iface = gr.Interface(
|
| 48 |
fn=classify,
|
| 49 |
inputs=audio_input,
|
| 50 |
-
outputs=[transcript_out, probs_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 |
)
|