File size: 1,988 Bytes
9ece3ee
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
import gradio as gr
import torch
from transformers import WhisperProcessor, WhisperForConditionalGeneration
import soundfile as sf
import numpy as np
from scipy import signal

# Ensure the model runs on GPU if available
device = "cuda" if torch.cuda.is_available() else "cpu"
print(f"Running on device: {device}")

# Load the model and processor
print("Loading Whisper model for Macedonian transcription...")
processor = WhisperProcessor.from_pretrained("openai/whisper-large-v3")
model = WhisperForConditionalGeneration.from_pretrained("openai/whisper-large-v3").to(device)
print("✓ Model loaded successfully!")

def process_audio(audio_path):
    try:
        # Load and resample to 16kHz using scipy
        waveform, sr = sf.read(audio_path)
        if len(waveform.shape) > 1:  # Convert stereo to mono
            waveform = waveform.mean(axis=1)
        if sr != 16000:  # Resample if necessary
            num_samples = int(len(waveform) * 16000 / sr)
            waveform = signal.resample(waveform, num_samples)
        
        # Process the audio
        inputs = processor(waveform, sampling_rate=16000, return_tensors="pt").to(device)
        print("Transcribing...")
        predicted_ids = model.generate(**inputs, language="mk")
        transcription = processor.batch_decode(predicted_ids, skip_special_tokens=True)[0]
        return transcription
    except Exception as e:
        return f"Error during transcription: {str(e)}"

# Gradio interface
demo = gr.Interface(
    fn=process_audio,
    inputs=gr.Audio(sources=["microphone", "upload"], type="filepath"),
    outputs="text",
    title="Македонско препознавање на говор / Macedonian Speech Recognition",
    description="Качете аудио или користете микрофон за транскрипција на македонски говор / Upload audio or use microphone to transcribe Macedonian speech"
)

if __name__ == "__main__":
    demo.launch(share=True)