Spaces:
Running
Running
Create app.py
Browse files
app.py
ADDED
|
@@ -0,0 +1,98 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import torch
|
| 2 |
+
#from transformers import pipeline
|
| 3 |
+
#from transformers.pipelines.audio_utils import ffmpeg_read
|
| 4 |
+
from speechscore import SpeechScore
|
| 5 |
+
import gradio as gr
|
| 6 |
+
|
| 7 |
+
MODEL_NAME = "alibabasglab/speechscore"
|
| 8 |
+
BATCH_SIZE = 1
|
| 9 |
+
|
| 10 |
+
device = 0 if torch.cuda.is_available() else "cpu"
|
| 11 |
+
|
| 12 |
+
mySpeechScore = SpeechScore([
|
| 13 |
+
'SRMR'
|
| 14 |
+
])
|
| 15 |
+
|
| 16 |
+
|
| 17 |
+
# Copied from https://github.com/openai/whisper/blob/c09a7ae299c4c34c5839a76380ae407e7d785914/whisper/utils.py#L50
|
| 18 |
+
def format_timestamp(seconds: float, always_include_hours: bool = False, decimal_marker: str = "."):
|
| 19 |
+
if seconds is not None:
|
| 20 |
+
milliseconds = round(seconds * 1000.0)
|
| 21 |
+
|
| 22 |
+
hours = milliseconds // 3_600_000
|
| 23 |
+
milliseconds -= hours * 3_600_000
|
| 24 |
+
|
| 25 |
+
minutes = milliseconds // 60_000
|
| 26 |
+
milliseconds -= minutes * 60_000
|
| 27 |
+
|
| 28 |
+
seconds = milliseconds // 1_000
|
| 29 |
+
milliseconds -= seconds * 1_000
|
| 30 |
+
|
| 31 |
+
hours_marker = f"{hours:02d}:" if always_include_hours or hours > 0 else ""
|
| 32 |
+
return f"{hours_marker}{minutes:02d}:{seconds:02d}{decimal_marker}{milliseconds:03d}"
|
| 33 |
+
else:
|
| 34 |
+
# we have a malformed timestamp so just return it as is
|
| 35 |
+
return seconds
|
| 36 |
+
|
| 37 |
+
|
| 38 |
+
def score(file, task, return_timestamps):
|
| 39 |
+
scores = mySpeechScore(test_path=file, reference_path=None, window=None, score_rate=16000, return_mean=True)
|
| 40 |
+
return scores
|
| 41 |
+
|
| 42 |
+
|
| 43 |
+
demo = gr.Blocks()
|
| 44 |
+
|
| 45 |
+
mic_score = gr.Interface(
|
| 46 |
+
fn=score,
|
| 47 |
+
inputs=[
|
| 48 |
+
gr.Audio(sources=["microphone"],
|
| 49 |
+
waveform_options=gr.WaveformOptions(
|
| 50 |
+
waveform_color="#01C6FF",
|
| 51 |
+
waveform_progress_color="#0066B4",
|
| 52 |
+
skip_length=2,
|
| 53 |
+
show_controls=False,
|
| 54 |
+
),
|
| 55 |
+
),
|
| 56 |
+
gr.Radio(["absolute_score", "relative_score"], label="Task", default="absolute_score"),
|
| 57 |
+
gr.Checkbox(default=False, label="Return timestamps"),
|
| 58 |
+
],
|
| 59 |
+
outputs="text",
|
| 60 |
+
layout="horizontal",
|
| 61 |
+
theme="huggingface",
|
| 62 |
+
title="Score speech from microphone",
|
| 63 |
+
description=(
|
| 64 |
+
"Score audio inputs with the click of a button! Demo uses the"
|
| 65 |
+
" commonly used speech quality assessment methods for the audio files"
|
| 66 |
+
" of arbitrary length."
|
| 67 |
+
),
|
| 68 |
+
allow_flagging="never",
|
| 69 |
+
)
|
| 70 |
+
|
| 71 |
+
file_score = gr.Interface(
|
| 72 |
+
fn=score,
|
| 73 |
+
inputs=[
|
| 74 |
+
gr.Audio(sources=["upload"], optional=True, label="Audio file", type="filepath"),
|
| 75 |
+
gr.Radio(["absolute_score", "relative_score"], label="Task", default="absolute_score"),
|
| 76 |
+
gr.Checkbox(default=False, label="Return timestamps"),
|
| 77 |
+
],
|
| 78 |
+
outputs="text",
|
| 79 |
+
layout="horizontal",
|
| 80 |
+
theme="huggingface",
|
| 81 |
+
title="Score speech from a file",
|
| 82 |
+
description=(
|
| 83 |
+
"Score audio inputs with the click of a button! Demo uses the"
|
| 84 |
+
" commonly used speech quality assessment methods for the audio files"
|
| 85 |
+
" of arbitrary length."
|
| 86 |
+
),
|
| 87 |
+
examples=[
|
| 88 |
+
["./example.flac", "score", False],
|
| 89 |
+
["./example.flac", "score", True],
|
| 90 |
+
],
|
| 91 |
+
cache_examples=True,
|
| 92 |
+
allow_flagging="never",
|
| 93 |
+
)
|
| 94 |
+
|
| 95 |
+
with demo:
|
| 96 |
+
gr.TabbedInterface([mic_score, file_score], ["Score Microphone", "Score Audio File"])
|
| 97 |
+
|
| 98 |
+
demo.launch(enable_queue=True)
|