Views
No views yet
| Audio | Whisper Base | Whisper-Hindi2Hinglish-Swift |
|---|---|---|
| وہاں بس دن میں کتنی بار چلتی ہے | vah bas din mein kitni baar chalti hai? | |
| سلمان کی ایمیت سے پراوہویت ہوتے ہیں اس کمپنی کے سیر بھاؤ جانے کیسے | salmaan ki image se prabhaavit hote hain is company ke share bhaav jaane kaise? | |
| تو لویا تو لویا | vah roya aur aur roya. | |
| حلمت نہ پیننے سے بھارت میں ہر گنٹے ہوتی ہے چار لوگوں کی موت | helmet na pahnne se bhaarat mein har gante hoti hai chaar logon ki maut. | |
| اوستہ مجھے چٹھیکہ جواب نہ دینے کے لیٹانٹہ | usne mujhe chithi ka javaab na dene ke lie daanta. | |
| پرانا شاہ دیواروں سے گیرا ہوا ہے | puraana shahar divaaron se ghera hua hai. |
| Dataset | Whisper Base | Whisper-Hindi2Hinglish-Swift |
|---|---|---|
| Common-Voice | 106.7936 | 38.6549 |
| FLEURS | 104.2783 | 35.0888 |
| Indic-Voices | 110.8399 | 65.2147 |
pip install --upgrade transformerspipeline
class to transcribe audios of arbitrary length:1import torch
2from transformers import AutoModelForSpeechSeq2Seq, AutoProcessor, pipeline
3from datasets import load_dataset
4
5# Set device (GPU if available, otherwise CPU) and precision
6device = "cuda:0" if torch.cuda.is_available() else "cpu"
7torch_dtype = torch.float16 if torch.cuda.is_available() else torch.float32
8
9# Specify the pre-trained model ID
10model_id = "Oriserve/Whisper-Hindi2Hinglish-Swift"
11
12# Load the speech-to-text model with specified configurations
13model = AutoModelForSpeechSeq2Seq.from_pretrained(
14 model_id,
15 torch_dtype=torch_dtype, # Use appropriate precision (float16 for GPU, float32 for CPU)
16 low_cpu_mem_usage=True, # Optimize memory usage during loading
17 use_safetensors=True # Use safetensors format for better security
18)
19model.to(device) # Move model to specified device
20
21# Load the processor for audio preprocessing and tokenization
22processor = AutoProcessor.from_pretrained(model_id)
23
24# Create speech recognition pipeline
25pipe = pipeline(
26 "automatic-speech-recognition",
27 model=model,
28 tokenizer=processor.tokenizer,
29 feature_extractor=processor.feature_extractor,
30 torch_dtype=torch_dtype,
31 device=device,
32 generate_kwargs={
33 "task": "transcribe", # Set task to transcription
34 "language": "en" # Specify English language
35 }
36)
37
38# Process audio file and print transcription
39sample = "sample.wav" # Input audio file path
40result = pipe(sample) # Run inference
41print(result["text"]) # Print transcribed textpip install -U openai-whisper tqdm1import torch
2from transformers import AutoModelForSpeechSeq2Seq
3import re
4from tqdm import tqdm
5from collections import OrderedDict
6import json
7
8# Load parameter name mapping from HF to OpenAI format
9with open('convert_hf2openai.json', 'r') as f:
10 reverse_translation = json.load(f)
11
12reverse_translation = OrderedDict(reverse_translation)
13
14def save_model(model, save_path):
15 def reverse_translate(current_param):
16 # Convert parameter names using regex patterns
17 for pattern, repl in reverse_translation.items():
18 if re.match(pattern, current_param):
19 return re.sub(pattern, repl, current_param)
20
21 # Extract model dimensions from config
22 config = model.config
23 model_dims = {
24 "n_mels": config.num_mel_bins, # Number of mel spectrogram bins
25 "n_vocab": config.vocab_size, # Vocabulary size
26 "n_audio_ctx": config.max_source_positions, # Max audio context length
27 "n_audio_state": config.d_model, # Audio encoder state dimension
28 "n_audio_head": config.encoder_attention_heads, # Audio encoder attention heads
29 "n_audio_layer": config.encoder_layers, # Number of audio encoder layers
30 "n_text_ctx": config.max_target_positions, # Max text context length
31 "n_text_state": config.d_model, # Text decoder state dimension
32 "n_text_head": config.decoder_attention_heads, # Text decoder attention heads
33 "n_text_layer": config.decoder_layers, # Number of text decoder layers
34 }
35
36 # Convert model state dict to Whisper format
37 original_model_state_dict = model.state_dict()
38 new_state_dict = {}
39
40 for key, value in tqdm(original_model_state_dict.items()):
41 key = key.replace("model.", "") # Remove 'model.' prefix
42 new_key = reverse_translate(key) # Convert parameter names
43 if new_key is not None:
44 new_state_dict[new_key] = value
45
46 # Create final model dictionary
47 pytorch_model = {"dims": model_dims, "model_state_dict": new_state_dict}
48
49 # Save converted model
50 torch.save(pytorch_model, save_path)
51
52# Load Hugging Face model
53model_id = "Oriserve/Whisper-Hindi2Hinglish-Swift"
54model = AutoModelForSpeechSeq2Seq.from_pretrained(
55 model_id,
56 low_cpu_mem_usage=True, # Optimize memory usage
57 use_safetensors=True # Use safetensors format
58)
59
60# Convert and save model
61model_save_path = "Whisper-Hindi2Hinglish-Swift.pt"
62save_model(model,model_save_path)1import whisper
2# Load converted model with Whisper and transcribe
3model = whisper.load_model("Whisper-Hindi2Hinglish-Swift.pt")
4result = model.transcribe("sample.wav")
5print(result["text"])