SpeechScore / app.py
alibabasglab's picture
Create app.py
c3e6538 verified
raw
history blame
3.12 kB
import torch
#from transformers import pipeline
#from transformers.pipelines.audio_utils import ffmpeg_read
from speechscore import SpeechScore
import gradio as gr
MODEL_NAME = "alibabasglab/speechscore"
BATCH_SIZE = 1
device = 0 if torch.cuda.is_available() else "cpu"
mySpeechScore = SpeechScore([
'SRMR'
])
# Copied from https://github.com/openai/whisper/blob/c09a7ae299c4c34c5839a76380ae407e7d785914/whisper/utils.py#L50
def format_timestamp(seconds: float, always_include_hours: bool = False, decimal_marker: str = "."):
if seconds is not None:
milliseconds = round(seconds * 1000.0)
hours = milliseconds // 3_600_000
milliseconds -= hours * 3_600_000
minutes = milliseconds // 60_000
milliseconds -= minutes * 60_000
seconds = milliseconds // 1_000
milliseconds -= seconds * 1_000
hours_marker = f"{hours:02d}:" if always_include_hours or hours > 0 else ""
return f"{hours_marker}{minutes:02d}:{seconds:02d}{decimal_marker}{milliseconds:03d}"
else:
# we have a malformed timestamp so just return it as is
return seconds
def score(file, task, return_timestamps):
scores = mySpeechScore(test_path=file, reference_path=None, window=None, score_rate=16000, return_mean=True)
return scores
demo = gr.Blocks()
mic_score = gr.Interface(
fn=score,
inputs=[
gr.Audio(sources=["microphone"],
waveform_options=gr.WaveformOptions(
waveform_color="#01C6FF",
waveform_progress_color="#0066B4",
skip_length=2,
show_controls=False,
),
),
gr.Radio(["absolute_score", "relative_score"], label="Task", default="absolute_score"),
gr.Checkbox(default=False, label="Return timestamps"),
],
outputs="text",
layout="horizontal",
theme="huggingface",
title="Score speech from microphone",
description=(
"Score audio inputs with the click of a button! Demo uses the"
" commonly used speech quality assessment methods for the audio files"
" of arbitrary length."
),
allow_flagging="never",
)
file_score = gr.Interface(
fn=score,
inputs=[
gr.Audio(sources=["upload"], optional=True, label="Audio file", type="filepath"),
gr.Radio(["absolute_score", "relative_score"], label="Task", default="absolute_score"),
gr.Checkbox(default=False, label="Return timestamps"),
],
outputs="text",
layout="horizontal",
theme="huggingface",
title="Score speech from a file",
description=(
"Score audio inputs with the click of a button! Demo uses the"
" commonly used speech quality assessment methods for the audio files"
" of arbitrary length."
),
examples=[
["./example.flac", "score", False],
["./example.flac", "score", True],
],
cache_examples=True,
allow_flagging="never",
)
with demo:
gr.TabbedInterface([mic_score, file_score], ["Score Microphone", "Score Audio File"])
demo.launch(enable_queue=True)