Spaces:
Runtime error
Runtime error
Update app.py
Browse files
app.py
CHANGED
@@ -1,61 +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
|
42 |
-
max_length =
|
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()
|
|
|
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 600 seconds
|
42 |
+
max_length = 600 * target_sample_rate # 600 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()
|