File size: 1,647 Bytes
61a93bc
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
import gradio as gr
import soundfile as sf
import numpy as np
import tempfile
import torchaudio
from transformers import AutoModel

# Load ASR Model
def load_model():
    return AutoModel.from_pretrained("ai4bharat/indic-conformer-600m-multilingual", trust_remote_code=True)

model = load_model()

def process_audio(audio, language, decoding_method):
    if isinstance(audio, tuple):  # Recorded audio
        sample_rate, data = audio
        temp_wav = tempfile.NamedTemporaryFile(delete=False, suffix=".wav")
        sf.write(temp_wav.name, data, sample_rate)
        audio_path = temp_wav.name
    else:  # Uploaded file
        audio_path = audio
    
    # Load and resample audio
    wav, sr = torchaudio.load(audio_path)
    target_sample_rate = 16000
    if sr != target_sample_rate:
        resampler = torchaudio.transforms.Resample(orig_freq=sr, new_freq=target_sample_rate)
        wav = resampler(wav)
    
    # Perform ASR with selected decoding method
    transcription = model(wav, language, decoding_method)
    
    return transcription

iface = gr.Interface(
    fn=process_audio,
    inputs=[
        gr.Audio(source="microphone", type="numpy"),
        gr.Audio(source="upload"),
        gr.Dropdown(["hi", "ta", "bn", "mr", "te", "gu", "kn", "ml", "pa", "ur"], label="Select Language"),
        gr.Radio(["ctc", "rnnt"], label="Decoding Method")
    ],
    outputs="text",
    title="Multilingual ASR with Indic-Conformer",
    description="Record or upload an audio file, select a language and decoding method, and transcribe it using the AI4Bharat Indic-Conformer model."
)

if __name__ == "__main__":
    iface.launch()