File size: 1,848 Bytes
0d1ca3b
 
 
 
 
 
6732531
0d1ca3b
 
 
 
 
 
 
5c47605
0d1ca3b
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
5f17baf
0d1ca3b
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
d072b4f
0d1ca3b
 
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
# import gradio as gr
# import os
# gr.load("models/vrclc/Whisper-medium-Malayalam", examples = [
#     [os.path.join(os.path.abspath(''),"./sample1.wav")]    
# ]).launch()

import gradio as gr
import torch
import soundfile as sf
from transformers import pipeline

device = "cuda:0" if torch.cuda.is_available() else "cpu"
pipe = pipeline(
    "automatic-speech-recognition",
    model="vrclc/Whisper-small-Malayalam",
    chunk_length_s=10,
    device=device,
)

def transcribe(audio):
    """Transcribes Malayalam speech from an audio file."""
    try:
        if audio is None:
            return "Please record or upload an audio file."

        print(f"[DEBUG] Received audio: {audio}")

        # Handle filepath case from Gradio
        audio_path = audio if isinstance(audio, str) else audio.get("name", None)
        if audio_path is None:
            return "Could not read audio file."

        print(f"[DEBUG] Reading audio file: {audio_path}")
        audio_data, sample_rate = sf.read(audio_path)

        print(f"[DEBUG] Audio sample rate: {sample_rate}, shape: {audio_data.shape}")

        transcription = pipe(
            {"array": audio_data, "sampling_rate": sample_rate},
            chunk_length_s=10,
            batch_size=8,
        )["text"]

        print(f"[DEBUG] Transcription: {transcription}")
        return transcription

    except Exception as e:
        import traceback
        print("[ERROR] Exception during transcription:")
        traceback.print_exc()
        return f"Error: {str(e)}"

iface = gr.Interface(
    fn=transcribe,
    inputs=gr.Audio(sources=["microphone", "upload"], type="filepath"),
    outputs="text",
    title="Malayalam Speech Recognition",
    description="Record or upload Malayalam speech and submit to get the transcribed text.",
    examples=[["sample1.wav"]],
)
iface.launch()