File size: 1,900 Bytes
2859a48
ec21c18
 
68195d5
 
 
 
2859a48
 
413f61e
2859a48
68195d5
ec21c18
 
 
68195d5
ec21c18
2859a48
d501976
68195d5
 
514fc2c
68195d5
 
54c9615
2071c0b
 
 
 
 
 
 
 
 
 
981a951
ec21c18
 
 
 
 
 
 
 
 
 
 
 
 
68195d5
 
 
 
 
 
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
import librosa
from transformers import AutoProcessor, Wav2Vec2ForCTC
import torch
import logging

# Set up logging
logging.basicConfig(level=logging.DEBUG)

ASR_SAMPLING_RATE = 16_000
MODEL_ID = "facebook/mms-1b-all"

try:
    processor = AutoProcessor.from_pretrained(MODEL_ID)
    model = Wav2Vec2ForCTC.from_pretrained(MODEL_ID)
    logging.info("ASR model and processor loaded successfully.")
except Exception as e:
    logging.error(f"Error loading ASR model or processor: {e}")

def transcribe(audio):
    try:
        if audio is None:
            logging.error("No audio file provided")
            return "ERROR: You have to either use the microphone or upload an audio file"
        
        logging.info(f"Loading audio file: {audio}")

        # Try loading the audio file with librosa
        try:
            audio_samples, _ = librosa.load(audio, sr=ASR_SAMPLING_RATE, mono=True)
        except FileNotFoundError:
            logging.error("Audio file not found")
            return "ERROR: Audio file not found"
        except Exception as e:
            logging.error(f"Error loading audio file with librosa: {e}")
            return f"ERROR: Unable to load audio file - {e}"
        
        # Set the language for the processor to Faroese
        lang_code = "fao"
        processor.tokenizer.set_target_lang(lang_code)
        model.load_adapter(lang_code)

        # Process the audio with the processor
        inputs = processor(audio_samples, sampling_rate=ASR_SAMPLING_RATE, return_tensors="pt")

        with torch.no_grad():
            outputs = model(**inputs).logits

        ids = torch.argmax(outputs, dim=-1)[0]
        transcription = processor.decode(ids)
        
        logging.info("Transcription completed successfully.")
        return transcription
    except Exception as e:
        logging.error(f"Error during transcription: {e}")
        return "ERROR"