Pooya-Fallah commited on
Commit
583f220
·
verified ·
1 Parent(s): eedd095

Upload 2 files

Browse files
Files changed (2) hide show
  1. app.py +61 -0
  2. requirements.txt +12 -0
app.py ADDED
@@ -0,0 +1,61 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import torch
2
+ import nemo.collections.asr as nemo_asr
3
+ import gc
4
+ import numpy as np
5
+ import torchaudio
6
+
7
+ pretrained_model_path="./stt_fa_fastconformer_hybrid_large_finetuned.nemo"
8
+
9
+ # Clear up memory
10
+ torch.cuda.empty_cache()
11
+ gc.collect()
12
+ model = nemo_asr.models.EncDecHybridRNNTCTCModel.restore_from(pretrained_model_path)
13
+ device = 'cuda' if torch.cuda.is_available() else 'cpu'
14
+ # device = 'cpu' # You can transcribe even longer samples on the CPU, though it will take much longer !
15
+ model = model.to(device)
16
+ model.freeze()
17
+
18
+ def transcribe(audio):
19
+ # 'audio' is a tuple: (sample_rate, data)
20
+ sample_rate, data = audio
21
+
22
+ # Convert to mono if stereo
23
+ if data.ndim > 1:
24
+ data = data.mean(axis=1)
25
+
26
+ # Ensure the model is on the correct device
27
+ device = 'cuda' if torch.cuda.is_available() else 'cpu'
28
+
29
+ # Convert audio data to the expected format
30
+ if isinstance(data, np.ndarray):
31
+ audio_tensor = torch.tensor(data, dtype=torch.float32)
32
+ else:
33
+ raise ValueError("Audio data must be a numpy array")
34
+
35
+ # Resample if sample rate is not 16000
36
+ target_sample_rate = 16000
37
+ if sample_rate != target_sample_rate:
38
+ resampler = torchaudio.transforms.Resample(orig_freq=sample_rate, new_freq=target_sample_rate)
39
+ audio_tensor = resampler(audio_tensor)
40
+
41
+ # Trim audio if longer than 30 seconds
42
+ max_length = 30 * target_sample_rate # 30 seconds
43
+ if audio_tensor.shape[-1] > max_length:
44
+ audio_tensor = audio_tensor[..., :max_length]
45
+
46
+ # Transcribe
47
+ with torch.no_grad():
48
+ transcript = model.transcribe(audio_tensor)
49
+
50
+ return transcript[0][0] # Assuming single input
51
+
52
+ import gradio as gr
53
+
54
+ interface = gr.Interface(
55
+ fn=transcribe,
56
+ inputs=gr.Audio(sources=["upload", "microphone"]), # Allows both file upload and recording
57
+ outputs="text",
58
+ live=False # Set to True for real-time transcription
59
+ )
60
+
61
+ interface.launch()
requirements.txt ADDED
@@ -0,0 +1,12 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ # Python packages
2
+ wget
3
+ text-unidecode
4
+ matplotlib>=3.3.2
5
+ ffmpeg-python
6
+ gradio
7
+ numpy
8
+ torch
9
+ torchaudio
10
+
11
+ # Install NeMo from the Git repository (branch: main)
12
+ git+https://github.com/NVIDIA/NeMo.git@main#egg=nemo_toolkit[all]