File size: 1,930 Bytes
44f2969
7df6e8c
44f2969
e473647
44f2969
 
 
 
 
e473647
 
 
 
44f2969
 
 
 
9963f1b
44f2969
9963f1b
44f2969
 
9963f1b
44f2969
 
 
7df6e8c
 
 
 
 
 
 
 
 
 
e473647
7df6e8c
e473647
7df6e8c
e473647
 
 
 
 
 
44f2969
 
e473647
44f2969
9963f1b
44f2969
 
 
 
 
 
 
 
 
 
 
 
 
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
import gradio as gr
import soundfile as sf
import torch
from transformers import Wav2Vec2ForCTC, Wav2Vec2Processor, pipeline

MODEL_NAME = "mikr/w2v-bert-2.0-czech-colab-cv16"
lang = "cs"

device = 0 if torch.cuda.is_available() else "cpu"

model = Wav2Vec2ForCTC.from_pretrained(MODEL_NAME).to(device)
processor = Wav2Vec2Processor.from_pretrained(MODEL_NAME)

pipe = pipeline(
    model=MODEL_NAME,
)

def transcribe(file_upload):
    warn_output = ""
    if (file_upload is None):
        return "ERROR: You have to either use the microphone or upload an audio file"

    file = file_upload
    text = pipe(file)["text"]
    return warn_output + text


def readwav(a_f):
    wav, sr = sf.read(a_f, dtype=np.float32)
    if len(wav.shape) == 2:
        wav = wav.mean(1)
    if sr != 16000:
        wlen = int(wav.shape[0] / sr * 16000)
        wav = signal.resample(wav, wlen)
    return wav

def transcribe2(file_upload):
    wav = readwav(file_upload)
    with torch.inference_mode():
        input_values = processor(wav, sampling_rate=16000).input_values[0]
        input_values = torch.tensor(input_values, device=device).unsqueeze(0)
        logits = model(input_values).logits
        pred_ids = torch.argmax(logits, dim=-1)
        xcp = processor.batch_decode(pred_ids)
        return xcp[0]


iface = gr.Interface(
    fn=transcribe2,
    inputs=[
        gr.File(type="binary", label="Upload Audio File"),  # Audio file upload
    ],
    outputs="text",
    theme="huggingface",
    title="Wav2Vec2-Bert demo - transcribe Czech Audio",
    description=(
        "Transcribe long-form microphone or audio inputs with the click of a button! Demo uses the fine-tuned"
        f" checkpoint [{MODEL_NAME}](https://huggingface.co/{MODEL_NAME}) from Whisper Fine Tuning Sprint Event 2022 "
        "and 🤗 Transformers to transcribe audio files of arbitrary length."
    ),
    allow_flagging="never",
)

iface.launch()