File size: 1,546 Bytes
f345224
d2753e9
 
d222613
d2753e9
 
 
 
d222613
d2753e9
 
 
 
 
f345224
d2753e9
 
 
 
 
 
 
 
 
 
 
 
 
 
f345224
 
 
 
9e71ecb
f345224
 
dd6327f
f345224
d222613
f345224
d2753e9
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
import gradio as gr
from transformers import WhisperProcessor, WhisperForConditionalGeneration, pipeline
import torch

# Load Whisper model and processor from Hugging Face
model_name = "openai/whisper-large-v3"
processor = WhisperProcessor.from_pretrained(model_name)
model = WhisperForConditionalGeneration.from_pretrained(model_name)

# Ensure the model is using the correct device (GPU or CPU)
device = torch.device("cuda" if torch.cuda.is_available() else "cpu")
model.to(device)

# Function to handle transcription with language set to English by default
def transcribe(audio):
    # Load audio
    input_features = processor(audio, sampling_rate=16000, return_tensors="pt").input_features.to(device)

    # Generate transcription with attention_mask and correct input_features
    attention_mask = torch.ones(input_features.shape, dtype=torch.long, device=device)
    generated_ids = model.generate(
        input_features=input_features,
        attention_mask=attention_mask,
        language="en"  # Force translation to English
    )
    
    # Decode transcription
    transcription = processor.batch_decode(generated_ids, skip_special_tokens=True)[0]
    return transcription

# Create a Gradio Interface
interface = gr.Interface(
    fn=transcribe, 
    inputs=gr.Audio(sources="upload", type="filepath"), 
    outputs="text",
    title="Whisper Speech-to-Text API",
    description="Upload an audio file and get a transcription using OpenAI's Whisper model from Hugging Face."
)

# Launch the interface as an API
interface.launch()