File size: 2,659 Bytes
666f810
 
ed9aac5
666f810
f42dcac
666f810
ed9aac5
47bfd84
2fa0634
 
 
 
 
 
 
 
 
 
 
 
 
 
ed9aac5
2fa0634
82d5b80
47bfd84
 
0af87d2
47bfd84
0af87d2
47bfd84
 
 
0af87d2
666f810
 
 
 
 
2fa0634
 
 
 
 
 
 
666f810
2fa0634
 
ed9aac5
 
47bfd84
ed9aac5
 
 
 
 
 
 
 
47bfd84
ed9aac5
1503067
2fa0634
 
 
 
 
 
 
 
 
 
 
 
738bf68
 
ed9aac5
 
 
 
 
da91c46
ed9aac5
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
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
import os
os.system("pip install git+https://github.com/openai/whisper.git")
import gradio as gr
import whisper

model = whisper.load_model("small")


language_id_lookup = {
            "English"   : "en",
            "German"    : "de",
            "Greek"     : "el",
            "Spanish"   : "es",
            "Finnish"   : "fi",
            "Russian"   : "ru",
            "Hungarian" : "hu",
            "Dutch"     : "nl",
            "French"    : "fr",
            'Polish'    : "pl",
            'Portuguese': "pt",
            'Italian'   : "it",
            }


def predict(audio, language, mic_audio=None):
    # audio = tuple (sample_rate, frames) or (sample_rate, (frames, channels))
    if mic_audio is not None:
        input_audio = mic_audio
    elif audio is not None:
        input_audio = audio
    else:
        return "(please provide audio)"

    audio = whisper.load_audio(input_audio)
    audio = whisper.pad_or_trim(audio)
    
    mel = whisper.log_mel_spectrogram(audio).to(model.device)
    options = whisper.DecodingOptions(fp16 = False)
    result = whisper.decode(model, mel, options)

    if(language == "Detect Language"):
        outLanguage, probs = model.detect_language(mel)
    else:
        outLanguage = language_id_lookup(language.split()[0])


    
    print(result.text + " " + outLanguage)
    return result.text, outLanguage



title = "Demo for Whisper -> Something -> XLS-R"

description = """
<b>How to use:</b> Upload an audio file or record using the microphone. The audio is converted to mono and resampled to 16 kHz before
being passed into the model. The output is the text transcription of the audio.
"""

gr.Interface(
    fn=predict,
    inputs=[
        gr.Audio(label="Upload Speech", source="upload", type="filepath"),
        gr.inputs.Dropdown(['English Text',
                            'German Text',
                            'Greek Text',
                            'Spanish Text',
                            'Finnish Text',
                            'Russian Text',
                            'Hungarian Text',
                            'Dutch Text',
                            'French Text',
                            'Polish Text',
                            'Portuguese Text',
                            'Italian Text',
                            'Detect Language'], type="value", default='English Text', label="Select the Language of the that you are speaking in."),
        gr.Audio(label="Record Speech", source="microphone", type="filepath"),
    ],
    outputs=[
        gr.Text(label="Transcription"),
    ],
    title=title,
    description=description,
).launch()