File size: 1,679 Bytes
7fb9ab5
84d2d73
8026eab
 
7fb9ab5
8026eab
7fb9ab5
 
8026eab
 
 
 
 
 
 
 
 
7fb9ab5
 
8026eab
 
 
84d2d73
8026eab
3c11f4a
8026eab
3c11f4a
8026eab
7fb9ab5
8026eab
 
 
 
 
eb85cb3
8026eab
 
 
360fd06
37f8e3b
 
 
13a6bac
 
8026eab
84d2d73
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
from speechbox import PunctuationRestorer
import librosa
import subprocess
import gradio as gr

restorer = PunctuationRestorer.from_pretrained("openai/whisper-tiny.en")


def convert_to_wav(path):
    if path[-3:] != 'wav':
        new_path = '.'.join(path.split('.')[:-1]) + '.wav'
    try:
        subprocess.call(['ffmpeg', '-i', path, new_path, '-y'])
    except:  # noqa: E722
        return path, 'Error: Could not convert file to .wav'
    path = new_path
    return path, None


def restore(audio, original_transcript):
    path, error = convert_to_wav(audio)
    print(error)
    data, samplerate = librosa.load(path, sr=16_000)

    text, log_probs = restorer(data, original_transcript, samplerate, num_beams=1)

    return text, log_probs


gr.Interface(
    title='Punctuation Restorer',
    fn=restore,
    inputs=[
        gr.inputs.Audio(source="upload", type="filepath"),
        gr.inputs.Textbox(default="", label="normalized text")
    ],
    outputs=[
        gr.outputs.Textbox(label='Restored text'),
        gr.Number(label='Log probability')
    ],
    examples=[
        ["./common_voice_en_18301577.mp3", "do not cross the yellow light"],
        ["./sample1.flac", "going along slushy country roads and speaking to damp audiences in draughty school rooms day after day for a fortnight he'll have to put in an appearance at some place of worship on sunday morning and he can come to us immediately afterwards"],
        ["./sample2.flac", "before he had time to answer a much encumbered vera burst into the room with the question i say can i leave these here these were a small black pig and a lusty specimen of black red game cock"],
    ]
  ).launch()