File size: 1,600 Bytes
fe8fc6e
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
012dff0
 
 
 
 
 
 
 
 
 
 
fe8fc6e
 
 
012dff0
fe8fc6e
 
 
 
 
 
012dff0
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

# Load Whisper model and processor from Hugging Face
processor = WhisperProcessor.from_pretrained("openai/whisper-base")
model = WhisperForConditionalGeneration.from_pretrained("openai/whisper-base").to("cuda" if torch.cuda.is_available() else "cpu")

def transcribe(audio):
    try:
        # Load audio
        audio_input = processor(audio, sampling_rate=16000, return_tensors="pt")
        
        # Move to appropriate device
        audio_input = audio_input.input_features.to(model.device)

        # Generate transcription
        predicted_ids = model.generate(audio_input)
        transcription = processor.decode(predicted_ids[0], skip_special_tokens=True)
        
        return transcription
    except Exception as e:
        return f"Error: {str(e)}"

def convert_to_wav(audio_file_path):
    try:
        wav_file_path = os.path.splitext(audio_file_path)[0] + '.wav'
        audio = AudioSegment.from_file(audio_file_path)
        audio.export(wav_file_path, format='wav')
        logging.info(f'Converted {audio_file_path} to {wav_file_path}')
        return wav_file_path
    except Exception as e:
        logging.error(f'Error converting file to WAV: {e}')
        raise

# Create a Gradio interface
iface = gr.Interface(
    fn=transcribe,
    inputs=gr.Audio(type="filepath"),
    outputs="text",
    title="Whisper Transcription",
    description="Upload an audio file and get the transcription using Whisper model."
)

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