Views
No views yet
!pip install librosa torch torchaudio transformers1import os
2import requests
3import librosa
4import torch
5import numpy as np
6from transformers import WhisperTokenizer, WhisperProcessor, WhisperFeatureExtractor, WhisperForConditionalGeneration
7
8# Define model and device
9model_path_ = "sha1779/BengaliRegionalASR"
10device = torch.device('cuda' if torch.cuda.is_available() else 'cpu')
11feature_extractor = WhisperFeatureExtractor.from_pretrained(model_path_)
12tokenizer = WhisperTokenizer.from_pretrained(model_path_)
13processor = WhisperProcessor.from_pretrained(model_path_)
14model = WhisperForConditionalGeneration.from_pretrained(model_path_).to(device)
15model.config.forced_decoder_ids = processor.get_decoder_prompt_ids(language="bengali", task="transcribe")
16
17# MP3 URL
18mp3_url = "https://huggingface.co/sha1779/BengaliRegionalASR/resolve/main/Mp3/valid_barishal%20(1).wav"
19local_audio_path = "temp_audio.wav"
20
21# Download the MP3 file
22print("Downloading audio file...")
23response = requests.get(mp3_url)
24if response.status_code == 200:
25 with open(local_audio_path, 'wb') as f:
26 f.write(response.content)
27 print("Download complete.")
28else:
29 raise Exception(f"Failed to download file. HTTP status code: {response.status_code}")
30
31# Load and preprocess the audio
32try:
33 print("Processing audio file...")
34 speech_array, sampling_rate = librosa.load(local_audio_path, sr=16000)
35 speech_array = librosa.resample(np.asarray(speech_array), orig_sr=sampling_rate, target_sr=16000)
36 input_features = feature_extractor(speech_array, sampling_rate=16000, return_tensors="pt").input_features
37
38 # Generate transcription
39 print("Generating transcription...")
40 predicted_ids = model.generate(inputs=input_features.to(device))[0]
41 transcription = processor.decode(predicted_ids, skip_special_tokens=True)
42
43 # Print the transcription
44 print("Transcription:", transcription)
45
46finally:
47 # Clean up: delete the temporary audio file
48 if os.path.exists(local_audio_path):
49 os.remove(local_audio_path)
50 print("Temporary audio file deleted.")
511import os
2import requests
3import librosa
4import torch
5import numpy as np
6from transformers import WhisperTokenizer, WhisperProcessor, WhisperFeatureExtractor, WhisperForConditionalGeneration
7
8# Define model and device
9model_path_ = "sha1779/BengaliRegionalASR"
10device = torch.device('cuda' if torch.cuda.is_available() else 'cpu')
11feature_extractor = WhisperFeatureExtractor.from_pretrained(model_path_)
12tokenizer = WhisperTokenizer.from_pretrained(model_path_)
13processor = WhisperProcessor.from_pretrained(model_path_)
14model = WhisperForConditionalGeneration.from_pretrained(model_path_).to(device)
15model.config.forced_decoder_ids = processor.get_decoder_prompt_ids(language="bengali", task="transcribe")
16
17# Remote MP3 file URL
18mp3_url = "https://huggingface.co/sha1779/BengaliRegionalASR/resolve/main/Mp3/valid_barishal%20(1).wav"
19local_audio_path = "temp_audio.wav"
20
21# Download the MP3 file
22response = requests.get(mp3_url)
23if response.status_code == 200:
24 with open(local_audio_path, 'wb') as f:
25 f.write(response.content)
26else:
27 raise Exception(f"Failed to download file. HTTP status code: {response.status_code}")
28
29# Load audio
30speech_array, sampling_rate = librosa.load(local_audio_path, sr=16000)
31
32# Define chunk parameters
33chunk_duration = 30 # seconds
34overlap = 5 # seconds
35chunk_size = int(chunk_duration * sampling_rate)
36overlap_size = int(overlap * sampling_rate)
37
38# Split audio into chunks
39chunks = [
40 speech_array[start : start + chunk_size]
41 for start in range(0, len(speech_array), chunk_size - overlap_size)
42]
43
44# Process and transcribe each chunk
45transcriptions = []
46for i, chunk in enumerate(chunks):
47 # Resample and extract features
48 chunk = librosa.resample(np.asarray(chunk), orig_sr=sampling_rate, target_sr=16000)
49 input_features = feature_extractor(chunk, sampling_rate=16000, return_tensors="pt").input_features
50
51 # Generate transcription
52 predicted_ids = model.generate(inputs=input_features.to(device))[0]
53 transcription = processor.decode(predicted_ids, skip_special_tokens=True)
54 transcriptions.append(transcription)
55
56# Combine and print the transcriptions
57print(" ".join(transcriptions))
58
59# Clean up temporary file
60os.remove(local_audio_path)
61