File size: 1,787 Bytes
36d155d
1378b33
acbc440
7d5800b
1378b33
36d155d
0c16d63
 
 
 
 
 
 
 
 
 
 
 
 
 
1378b33
 
36d155d
3a41822
1378b33
 
 
 
 
 
 
 
 
 
 
 
 
 
acbc440
 
1378b33
 
 
 
 
acbc440
 
2a0eff5
 
 
1378b33
acbc440
 
 
 
 
 
 
 
 
 
 
 
 
1378b33
acbc440
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
import gradio as gr
import torch
import json
import numpy as np
from transformers import AutoModelForSpeechSeq2Seq, AutoProcessor, pipeline

# This code was omitted for deployment reasons (model is too RAM-hungry)

# from speechbrain.inference.separation import SepformerSeparation as separator
# import torchaudio

# model = separator.from_hparams(source="speechbrain/sepformer-whamr16k", savedir='pretrained_models/sepformer-whamr16k')

# def separate_speech(path):
#     est_sources = model.separate_file(path=path)
#     output_path = "output.wav"
#     torchaudio.save(output_path, est_sources[:, :, 0].detach().cpu(), 16000)
#     return output_path


device = "cuda:0" if torch.cuda.is_available() else "cpu"
torch_dtype = torch.float16 if torch.cuda.is_available() else torch.float32

model_id = "openai/whisper-tiny"

model = AutoModelForSpeechSeq2Seq.from_pretrained(
    model_id, torch_dtype=torch_dtype, low_cpu_mem_usage=True, use_safetensors=True
)
model.to(device)

processor = AutoProcessor.from_pretrained(model_id)

pipe = pipeline(
    "automatic-speech-recognition",
    model=model,
    tokenizer=processor.tokenizer,
    feature_extractor=processor.feature_extractor,
    max_new_tokens=128,
    chunk_length_s=15,
    batch_size=1,
    return_timestamps=True,
    torch_dtype=torch_dtype,
    device=device,
)

def transcribe_speech(filepath):
    result = pipe(filepath)['chunks']
    for item in result:
        item['timestamp'] = list(item['timestamp'])
    return json.dumps(result)

demo = gr.Blocks()

file_transcribe = gr.Interface(
    fn=transcribe_speech,
    inputs=gr.Audio(sources="upload", type="filepath"),
    outputs="text",
)

with demo:
    gr.TabbedInterface(
        [file_transcribe],
        ["Song Lyrics"],
    )

demo.launch(debug=True)