Spaces:
Runtime error
Runtime error
Create app.py
Browse files
app.py
ADDED
|
@@ -0,0 +1,49 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import gradio as gr
|
| 2 |
+
import soundfile as sf
|
| 3 |
+
import numpy as np
|
| 4 |
+
import tempfile
|
| 5 |
+
import torchaudio
|
| 6 |
+
from transformers import AutoModel
|
| 7 |
+
|
| 8 |
+
# Load ASR Model
|
| 9 |
+
def load_model():
|
| 10 |
+
return AutoModel.from_pretrained("ai4bharat/indic-conformer-600m-multilingual", trust_remote_code=True)
|
| 11 |
+
|
| 12 |
+
model = load_model()
|
| 13 |
+
|
| 14 |
+
def process_audio(audio, language, decoding_method):
|
| 15 |
+
if isinstance(audio, tuple): # Recorded audio
|
| 16 |
+
sample_rate, data = audio
|
| 17 |
+
temp_wav = tempfile.NamedTemporaryFile(delete=False, suffix=".wav")
|
| 18 |
+
sf.write(temp_wav.name, data, sample_rate)
|
| 19 |
+
audio_path = temp_wav.name
|
| 20 |
+
else: # Uploaded file
|
| 21 |
+
audio_path = audio
|
| 22 |
+
|
| 23 |
+
# Load and resample audio
|
| 24 |
+
wav, sr = torchaudio.load(audio_path)
|
| 25 |
+
target_sample_rate = 16000
|
| 26 |
+
if sr != target_sample_rate:
|
| 27 |
+
resampler = torchaudio.transforms.Resample(orig_freq=sr, new_freq=target_sample_rate)
|
| 28 |
+
wav = resampler(wav)
|
| 29 |
+
|
| 30 |
+
# Perform ASR with selected decoding method
|
| 31 |
+
transcription = model(wav, language, decoding_method)
|
| 32 |
+
|
| 33 |
+
return transcription
|
| 34 |
+
|
| 35 |
+
iface = gr.Interface(
|
| 36 |
+
fn=process_audio,
|
| 37 |
+
inputs=[
|
| 38 |
+
gr.Audio(source="microphone", type="numpy"),
|
| 39 |
+
gr.Audio(source="upload"),
|
| 40 |
+
gr.Dropdown(["hi", "ta", "bn", "mr", "te", "gu", "kn", "ml", "pa", "ur"], label="Select Language"),
|
| 41 |
+
gr.Radio(["ctc", "rnnt"], label="Decoding Method")
|
| 42 |
+
],
|
| 43 |
+
outputs="text",
|
| 44 |
+
title="Multilingual ASR with Indic-Conformer",
|
| 45 |
+
description="Record or upload an audio file, select a language and decoding method, and transcribe it using the AI4Bharat Indic-Conformer model."
|
| 46 |
+
)
|
| 47 |
+
|
| 48 |
+
if __name__ == "__main__":
|
| 49 |
+
iface.launch()
|