Spaces:
Sleeping
Sleeping
File size: 2,110 Bytes
88dc3ba 335cb84 eb4d23a 88dc3ba 6a6d2f9 5cb2a95 6a6d2f9 88dc3ba 95dcc38 88dc3ba 95dcc38 88dc3ba 7a0b87f |
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 |
import subprocess
subprocess.run(["python", "-m", "pip", "install", "--upgrade", "pip"])
subprocess.run(["pip", "install", "gradio", "--upgrade"])
subprocess.run(["pip", "install", "datasets"])
subprocess.run(["pip", "install", "transformers"])
subprocess.run(["pip", "install", "torch", "torchvision", "torchaudio", "-f", "https://download.pytorch.org/whl/torch_stable.html"])
import gradio as gr
from transformers import WhisperProcessor, WhisperForConditionalGeneration
# Load model and processor
processor = WhisperProcessor.from_pretrained("openai/whisper-large")
model = WhisperForConditionalGeneration.from_pretrained("openai/whisper-large")
forced_decoder_ids = processor.get_decoder_prompt_ids(language="italian", task="transcribe")
# Custom preprocessing function
def preprocess_audio(audio_data, sampling_rate=16_000):
# Ensure that the input data is a valid format for the model
# Convert the audio data to a numpy array with a correct shape
raw_speech = np.asarray(audio_data, dtype=np.float32)
# Pad or truncate the audio data to the required length
if len(raw_speech) > processor.feature_extractor.max_len:
raw_speech = raw_speech[:processor.feature_extractor.max_len]
else:
raw_speech = np.pad(raw_speech, (0, processor.feature_extractor.max_len - len(raw_speech)))
# Process the audio data using the Whisper processor
processed_data = processor(
raw_speech,
sampling_rate=sampling_rate,
return_tensors="pt",
padding=True,
truncation=True
)
return processed_data.input_features
# Function to perform ASR on audio data
def transcribe_audio(audio_data):
# Preprocess the audio data
input_features = preprocess_audio(audio_data)
# Generate token ids
predicted_ids = model.generate(input_features)
# Decode token ids to text
transcription = processor.batch_decode(predicted_ids, skip_special_tokens=True)
return transcription[0]
# Create Gradio interface
audio_input = gr.Audio()
gr.Interface(fn=transcribe_audio, inputs=audio_input, outputs="text").launch()
|