Spaces:
Sleeping
Sleeping
Create asr.py
Browse files
asr.py
ADDED
@@ -0,0 +1,49 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
import librosa
|
2 |
+
import torch
|
3 |
+
import numpy as np
|
4 |
+
from transformers import Wav2Vec2ForCTC, AutoProcessor
|
5 |
+
|
6 |
+
ASR_SAMPLING_RATE = 16_000
|
7 |
+
MODEL_ID = "facebook/mms-1b-all"
|
8 |
+
|
9 |
+
# Load MMS Model
|
10 |
+
processor = AutoProcessor.from_pretrained(MODEL_ID)
|
11 |
+
model = Wav2Vec2ForCTC.from_pretrained(MODEL_ID)
|
12 |
+
model.eval()
|
13 |
+
|
14 |
+
def transcribe_auto(audio_data=None):
|
15 |
+
if not audio_data:
|
16 |
+
return "<<ERROR: Empty Audio Input>>"
|
17 |
+
|
18 |
+
# Process Microphone Input
|
19 |
+
if isinstance(audio_data, tuple):
|
20 |
+
sr, audio_samples = audio_data
|
21 |
+
audio_samples = (audio_samples / 32768.0).astype(np.float32)
|
22 |
+
if sr != ASR_SAMPLING_RATE:
|
23 |
+
audio_samples = librosa.resample(audio_samples, orig_sr=sr, target_sr=ASR_SAMPLING_RATE)
|
24 |
+
|
25 |
+
# Process File Upload Input
|
26 |
+
else:
|
27 |
+
if not isinstance(audio_data, str):
|
28 |
+
return "<<ERROR: Invalid Audio Input>>"
|
29 |
+
audio_samples = librosa.load(audio_data, sr=ASR_SAMPLING_RATE, mono=True)[0]
|
30 |
+
|
31 |
+
inputs = processor(audio_samples, sampling_rate=ASR_SAMPLING_RATE, return_tensors="pt")
|
32 |
+
|
33 |
+
# **Step 1: Detect Language**
|
34 |
+
with torch.no_grad():
|
35 |
+
lang_id = model.generate(**inputs, task="lang-id")
|
36 |
+
detected_lang = processor.tokenizer.batch_decode(lang_id, skip_special_tokens=True)[0]
|
37 |
+
|
38 |
+
# **Step 2: Load Detected Language Adapter**
|
39 |
+
processor.tokenizer.set_target_lang(detected_lang)
|
40 |
+
model.load_adapter(detected_lang)
|
41 |
+
|
42 |
+
# **Step 3: Transcribe Audio**
|
43 |
+
with torch.no_grad():
|
44 |
+
outputs = model(**inputs).logits
|
45 |
+
ids = torch.argmax(outputs, dim=-1)[0]
|
46 |
+
transcription = processor.decode(ids)
|
47 |
+
|
48 |
+
return f"Detected Language: {detected_lang}\n\nTranscription:\n{transcription}"
|
49 |
+
|