File size: 1,915 Bytes
b949dde
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
583f220
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
import torch
import nemo.collections.asr as nemo_asr
import gc
import numpy as np
import torchaudio

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(audio):
    # 'audio' is a tuple: (sample_rate, data)
    sample_rate, data = audio

    # Convert to mono if stereo
    if data.ndim > 1:
        data = data.mean(axis=1)

    # 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)

    # Trim audio if longer than 600 seconds
    max_length = 600 * target_sample_rate  # 600 seconds
    if audio_tensor.shape[-1] > max_length:
        audio_tensor = audio_tensor[..., :max_length]

    # Transcribe
    with torch.no_grad():
        transcript = model.transcribe(audio_tensor)

    return transcript[0][0]  # Assuming single input

import gradio as gr

interface = gr.Interface(
    fn=transcribe,
    inputs=gr.Audio(sources=["upload", "microphone"]),  # Allows both file upload and recording
    outputs="text",
    live=False  # Set to True for real-time transcription
)

interface.launch()