File size: 1,527 Bytes
670f5ce
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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 torch
import torchaudio
import numpy as np
from transformers import Wav2Vec2BertProcessor, Wav2Vec2BertForCTC

# Load model and processor
repo_id = "hriteshMaikap/marathi-asr-model"
processor = Wav2Vec2BertProcessor.from_pretrained(repo_id)
model = Wav2Vec2BertForCTC.from_pretrained(repo_id)
device = "cuda" if torch.cuda.is_available() else "cpu"
model = model.to(device)

def transcribe(audio):
    # Process audio
    waveform, sample_rate = torchaudio.load(audio)
    
    # Resample if needed
    if sample_rate != 16000:
        resampler = torchaudio.transforms.Resample(sample_rate, 16000)
        waveform = resampler(waveform)
    
    # Convert to mono if needed
    if waveform.shape[0] > 1:
        waveform = torch.mean(waveform, dim=0, keepdim=True)
    
    # Convert to numpy
    speech_array = waveform.squeeze().numpy()
    
    # Process and run inference
    with torch.no_grad():
        inputs = processor(speech_array, sampling_rate=16000, return_tensors="pt").to(device)
        logits = model(inputs.input_features).logits
        predicted_ids = torch.argmax(logits, dim=-1)
    
    # Decode the predicted IDs
    transcription = processor.decode(predicted_ids[0])
    
    return transcription

# Create Gradio interface
iface = gr.Interface(
    fn=transcribe,
    inputs=gr.Audio(source="microphone", type="filepath"),
    outputs="text",
    title="Marathi Speech Recognition",
    description="Record your voice in Marathi and get a transcription."
)

iface.launch()