Update app.py
Browse files
app.py
CHANGED
@@ -1,75 +1,76 @@
|
|
1 |
-
import torch
|
2 |
-
import nemo.collections.asr as nemo_asr
|
3 |
-
import gc
|
4 |
-
import numpy as np
|
5 |
-
import torchaudio
|
6 |
-
|
7 |
-
|
8 |
-
|
9 |
-
|
10 |
-
|
11 |
-
|
12 |
-
|
13 |
-
|
14 |
-
|
15 |
-
|
16 |
-
model.
|
17 |
-
|
18 |
-
|
19 |
-
|
20 |
-
|
21 |
-
|
22 |
-
sample_rate, data
|
23 |
-
|
24 |
-
|
25 |
-
|
26 |
-
|
27 |
-
|
28 |
-
|
29 |
-
|
30 |
-
|
31 |
-
|
32 |
-
|
33 |
-
|
34 |
-
|
35 |
-
|
36 |
-
|
37 |
-
|
38 |
-
|
39 |
-
|
40 |
-
|
41 |
-
|
42 |
-
|
43 |
-
stream
|
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 |
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 |
+
import gradio as gr
|
7 |
+
|
8 |
+
pretrained_model_path="./stt_fa_fastconformer_hybrid_large_finetuned.nemo"
|
9 |
+
|
10 |
+
# Clear up memory
|
11 |
+
torch.cuda.empty_cache()
|
12 |
+
gc.collect()
|
13 |
+
model = nemo_asr.models.EncDecHybridRNNTCTCModel.restore_from(pretrained_model_path)
|
14 |
+
device = 'cuda' if torch.cuda.is_available() else 'cpu'
|
15 |
+
# device = 'cpu' # You can transcribe even longer samples on the CPU, though it will take much longer !
|
16 |
+
model = model.to(device)
|
17 |
+
model.freeze()
|
18 |
+
|
19 |
+
def transcribe(stream, new_chunk):
|
20 |
+
if new_chunk is None:
|
21 |
+
return None, ""
|
22 |
+
# 'audio' is a tuple: (sample_rate, data)
|
23 |
+
sample_rate, data = new_chunk
|
24 |
+
|
25 |
+
# Ensure the model is on the correct device
|
26 |
+
device = 'cuda' if torch.cuda.is_available() else 'cpu'
|
27 |
+
|
28 |
+
# Convert audio data to the expected format
|
29 |
+
if isinstance(data, np.ndarray):
|
30 |
+
audio_tensor = torch.tensor(data, dtype=torch.float32)
|
31 |
+
else:
|
32 |
+
raise ValueError("Audio data must be a numpy array")
|
33 |
+
|
34 |
+
# Resample if sample rate is not 16000
|
35 |
+
target_sample_rate = 16000
|
36 |
+
if sample_rate != target_sample_rate:
|
37 |
+
resampler = torchaudio.transforms.Resample(orig_freq=sample_rate, new_freq=target_sample_rate)
|
38 |
+
audio_tensor = resampler(audio_tensor)
|
39 |
+
|
40 |
+
if stream is not None:
|
41 |
+
stream['audio'] = torch.cat([stream['audio'], audio_tensor], dim=-1)
|
42 |
+
else:
|
43 |
+
stream = {"text": ""}
|
44 |
+
stream['audio'] = audio_tensor
|
45 |
+
|
46 |
+
|
47 |
+
max_length = 5 * target_sample_rate # 5 seconds
|
48 |
+
new_text = ""
|
49 |
+
|
50 |
+
# Process all chunks that fit max_length
|
51 |
+
while stream['audio'].shape[-1] > max_length:
|
52 |
+
# Extract first max_length samples
|
53 |
+
audio_chunk = stream['audio'][..., :max_length]
|
54 |
+
|
55 |
+
# Transcribe
|
56 |
+
with torch.no_grad():
|
57 |
+
transcript = model.transcribe(audio_chunk) # Add batch dimension if needed
|
58 |
+
|
59 |
+
# Update text (adjust based on model's output format)
|
60 |
+
new_text += " " + transcript[0][0].strip() # Example adjustment
|
61 |
+
|
62 |
+
# Remove processed audio from buffer
|
63 |
+
stream['audio'] = stream['audio'][..., max_length:]
|
64 |
+
|
65 |
+
stream['text'] += new_text
|
66 |
+
return stream, stream['text'].strip()
|
67 |
+
|
68 |
+
|
69 |
+
interface = gr.Interface(
|
70 |
+
fn=transcribe,
|
71 |
+
inputs=['state', gr.Audio(sources="microphone", streaming=True, type="numpy")],
|
72 |
+
outputs=["state", "text"],
|
73 |
+
live=True,
|
74 |
+
)
|
75 |
+
|
76 |
interface.launch()
|