File size: 2,407 Bytes
4f02add
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
9dc2477
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
import torch
import nemo.collections.asr as nemo_asr
import gc
import numpy as np
import torchaudio
import gradio as gr

pretrained_model_path="./stt_fa_fastconformer_hybrid_large_finetuned.nemo"

# Clear up memory
torch.cuda.empty_cache()
gc.collect()
model = nemo_asr.models.EncDecHybridRNNTCTCModel.restore_from(pretrained_model_path)
device = 'cuda' if torch.cuda.is_available() else 'cpu'
# device = 'cpu'  # You can transcribe even longer samples on the CPU, though it will take much longer !
model = model.to(device)
model.freeze()

def transcribe(stream, new_chunk):
    if new_chunk is None:
        return None, ""
    # 'audio' is a tuple: (sample_rate, data)
    sample_rate, data = new_chunk

    # Ensure the model is on the correct device
    device = 'cuda' if torch.cuda.is_available() else 'cpu'

    # Convert audio data to the expected format
    if isinstance(data, np.ndarray):
        audio_tensor = torch.tensor(data, dtype=torch.float32)
    else:
        raise ValueError("Audio data must be a numpy array")

    # Resample if sample rate is not 16000
    target_sample_rate = 16000
    if sample_rate != target_sample_rate:
        resampler = torchaudio.transforms.Resample(orig_freq=sample_rate, new_freq=target_sample_rate)
        audio_tensor = resampler(audio_tensor)

    if stream is not None:
        stream['audio'] = torch.cat([stream['audio'], audio_tensor], dim=-1)
    else:
        stream = {"text": ""}
        stream['audio'] = audio_tensor


    max_length = 5 * target_sample_rate  # 5 seconds
    new_text = ""

    # Process all chunks that fit max_length
    while stream['audio'].shape[-1] > max_length:
        # Extract first max_length samples
        audio_chunk = stream['audio'][..., :max_length]

        # Transcribe
        with torch.no_grad():
            transcript = model.transcribe(audio_chunk)  # Add batch dimension if needed

        # Update text (adjust based on model's output format)
        new_text += " " + transcript[0][0].strip()  # Example adjustment

        # Remove processed audio from buffer
        stream['audio'] = stream['audio'][..., max_length:]

    stream['text'] += new_text
    return stream, stream['text'].strip()


interface = gr.Interface(
    fn=transcribe,
    inputs=['state', gr.Audio(sources="microphone", streaming=True, type="numpy")],
    outputs=["state", "text"],
    live=True,
)

interface.launch()