File size: 2,174 Bytes
07c5b0a
 
 
3f8984b
07c5b0a
 
 
e758b0c
 
 
 
07c5b0a
3f8984b
 
 
 
8d1a8e1
24e6612
3f8984b
24e6612
3f8984b
8d1a8e1
24e6612
8d1a8e1
 
3f8984b
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
3dd81aa
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
62
import librosa
import torch
import numpy as np
import langid  # Language detection library
from transformers import Wav2Vec2ForCTC, AutoProcessor

ASR_SAMPLING_RATE = 16_000
MODEL_ID = "facebook/mms-1b-all"
# openai/whisper-large-v3-turbo
#ASR_SAMPLING_RATE = 16_000
#MODEL_ID = "openai/whisper-large-v3-turbo"

# Load MMS Model
processor = AutoProcessor.from_pretrained(MODEL_ID)
model = Wav2Vec2ForCTC.from_pretrained(MODEL_ID)
model.eval()

def detect_language(text):
    """Detects language using langid (fast & lightweight)."""
    lang, _ = langid.classify(text)
    return lang if lang in ["en", "sw"] else "en"  # Default to English

def transcribe_auto(audio_data=None):
    if not audio_data:
        return "<<ERROR: Empty Audio Input>>"
    
    # Process Microphone Input
    if isinstance(audio_data, tuple):
        sr, audio_samples = audio_data
        audio_samples = (audio_samples / 32768.0).astype(np.float32)
        if sr != ASR_SAMPLING_RATE:
            audio_samples = librosa.resample(audio_samples, orig_sr=sr, target_sr=ASR_SAMPLING_RATE)
    
    # Process File Upload Input
    else:
        if not isinstance(audio_data, str):
            return "<<ERROR: Invalid Audio Input>>"
        audio_samples = librosa.load(audio_data, sr=ASR_SAMPLING_RATE, mono=True)[0]

    inputs = processor(audio_samples, sampling_rate=ASR_SAMPLING_RATE, return_tensors="pt")

    # **Step 1: Transcribe without Language Detection**
    with torch.no_grad():
        outputs = model(**inputs).logits
        ids = torch.argmax(outputs, dim=-1)[0]
        raw_transcription = processor.decode(ids)

    # **Step 2: Detect Language from Transcription**
    detected_lang = detect_language(raw_transcription)
    lang_code = "eng" if detected_lang == "en" else "swh"

    # **Step 3: Reload Model with Correct Adapter**
    processor.tokenizer.set_target_lang(lang_code)
    model.load_adapter(lang_code)

    # **Step 4: Transcribe Again with Correct Adapter**
    with torch.no_grad():
        outputs = model(**inputs).logits
        ids = torch.argmax(outputs, dim=-1)[0]
        final_transcription = processor.decode(ids)

    return f"{final_transcription}"