File size: 6,039 Bytes
cd7aa15 fcd8965 cd7aa15 f0a5b40 cd7aa15 f0a5b40 cd7aa15 098a61e cd7aa15 1bb8243 cd7aa15 3a79217 098a61e 3a79217 cd7aa15 fcd8965 f0a5b40 3a79217 1bb8243 393feaa cd7aa15 7d7504d cd7aa15 d2d38cf cd7aa15 d2d38cf f0a5b40 cd7aa15 3a79217 7d7504d d2d38cf 7d7504d d2d38cf 3a79217 f0a5b40 3a79217 f0a5b40 cd7aa15 f0a5b40 3a79217 f0a5b40 cd7aa15 f0a5b40 cd7aa15 3a79217 f0a5b40 3a79217 f0a5b40 3a79217 f0a5b40 cd7aa15 f0a5b40 cd7aa15 f0a5b40 cd7aa15 f0a5b40 cd7aa15 f0a5b40 3a79217 7d7504d f0a5b40 cd7aa15 f0a5b40 7d7504d f0a5b40 cd7aa15 f0a5b40 cd7aa15 f0a5b40 cd7aa15 7d7504d f0a5b40 cd7aa15 f0a5b40 cd7aa15 |
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 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 |
import os
import tarfile
import torch
import torchaudio
import numpy as np
import streamlit as st
from transformers import (
AutoProcessor,
AutoModelForSpeechSeq2Seq,
TrainingArguments,
Trainer,
DataCollatorForSeq2Seq,
)
# ================================
# 1οΈβ£ Load Model & Processor
# ================================
MODEL_NAME = "AqeelShafy7/AudioSangraha-Audio_to_Text"
# Load ASR model and processor
processor = AutoProcessor.from_pretrained(MODEL_NAME)
model = AutoModelForSpeechSeq2Seq.from_pretrained(MODEL_NAME)
# Move model to GPU if available
device = "cuda" if torch.cuda.is_available() else "cpu"
model.to(device)
print(f"β
Model loaded on {device}")
# ================================
# 2οΈβ£ Load Dataset (Recursively from Extracted Path)
# ================================
DATASET_TAR_PATH = "dev-clean.tar.gz"
EXTRACT_PATH = "./librispeech_dev_clean"
# Extract dataset if not already extracted
if not os.path.exists(EXTRACT_PATH):
print("π Extracting dataset...")
with tarfile.open(DATASET_TAR_PATH, "r:gz") as tar:
tar.extractall(EXTRACT_PATH)
print("β
Extraction complete.")
else:
print("β
Dataset already extracted.")
# Base directory where audio files are stored
AUDIO_FOLDER = os.path.join(EXTRACT_PATH, "LibriSpeech", "dev-clean")
# Recursively find all `.flac` files inside the dataset directory
def find_audio_files(base_folder):
"""Recursively search for all .flac files in subdirectories."""
audio_files = []
for root, _, files in os.walk(base_folder):
for file in files:
if file.endswith(".flac"):
audio_files.append(os.path.join(root, file))
return audio_files
# Get all audio files
audio_files = find_audio_files(AUDIO_FOLDER)
if not audio_files:
raise FileNotFoundError(f"β No .flac files found in {AUDIO_FOLDER}. Check dataset structure!")
print(f"β
Found {len(audio_files)} audio files in dataset!")
# ================================
# 3οΈβ£ Preprocess Dataset (Fixed input_features)
# ================================
def load_and_process_audio(audio_path):
"""Loads and processes a single audio file into model format."""
waveform, sample_rate = torchaudio.load(audio_path)
# Resample to 16kHz
waveform = torchaudio.transforms.Resample(orig_freq=sample_rate, new_freq=16000)(waveform)
# Convert to model input format
input_features = processor(waveform.squeeze().numpy(), sampling_rate=16000, return_tensors="pt").input_features[0]
return input_features
# Manually create dataset structure
dataset = [{"input_features": load_and_process_audio(f), "labels": []} for f in audio_files[:100]]
# Split dataset into train and eval (Recommended Fix)
train_size = int(0.9 * len(dataset))
train_dataset = dataset[:train_size]
eval_dataset = dataset[train_size:]
print(f"β
Dataset Loaded! Training: {len(train_dataset)}, Evaluation: {len(eval_dataset)}")
# ================================
# 4οΈβ£ Training Arguments & Trainer
# ================================
training_args = TrainingArguments(
output_dir="./asr_model_finetuned",
eval_strategy="epoch", # Fix: Proper evaluation
save_strategy="epoch",
learning_rate=5e-5,
per_device_train_batch_size=8,
per_device_eval_batch_size=8,
num_train_epochs=3,
weight_decay=0.01,
logging_dir="./logs",
logging_steps=500,
save_total_limit=2,
push_to_hub=True,
)
# Data collator (for dynamic padding)
data_collator = DataCollatorForSeq2Seq(processor, model=model)
# Define Trainer (Fixed `processing_class` warning)
trainer = Trainer(
model=model,
args=training_args,
train_dataset=train_dataset,
eval_dataset=eval_dataset, # Fix: Providing eval_dataset
processing_class=processor, # Fix: Replacing deprecated `tokenizer`
data_collator=data_collator,
)
# ================================
# 5οΈβ£ Fine-Tuning Execution
# ================================
if st.button("Start Fine-Tuning"):
with st.spinner("Fine-tuning in progress... Please wait!"):
trainer.train()
st.success("β
Fine-Tuning Completed! Model updated.")
# ================================
# 6οΈβ£ Streamlit ASR Web App
# ================================
st.title("ποΈ Speech-to-Text ASR with Fine-Tuning πΆ")
# Upload audio file
audio_file = st.file_uploader("Upload an audio file", type=["wav", "mp3", "flac"])
if audio_file:
# Save uploaded file temporarily
audio_path = "temp_audio.wav"
with open(audio_path, "wb") as f:
f.write(audio_file.read())
# Load and process audio
waveform, sample_rate = torchaudio.load(audio_path)
waveform = torchaudio.transforms.Resample(orig_freq=sample_rate, new_freq=16000)(waveform)
# Convert audio to model input
input_features = processor(waveform.squeeze().numpy(), sampling_rate=16000, return_tensors="pt").input_features[0]
# Perform ASR inference
with torch.no_grad():
input_tensor = torch.tensor([input_features]).to(device)
logits = model(input_tensor).logits
predicted_ids = torch.argmax(logits, dim=-1)
transcription = processor.batch_decode(predicted_ids)[0]
# Display transcription
st.success("π Transcription:")
st.write(transcription)
# ================================
# 7οΈβ£ Fine-Tune Model with User Correction
# ================================
user_correction = st.text_area("π§ Correct the transcription (if needed):", transcription)
if st.button("Fine-Tune with Correction"):
if user_correction:
corrected_input = processor.tokenizer(user_correction).input_ids
# Dynamically add new example to dataset
dataset.append({"input_features": input_features, "labels": corrected_input})
# Perform quick re-training (1 epoch)
trainer.args.num_train_epochs = 1
trainer.train()
st.success("β
Model fine-tuned with new correction! Try another audio file.")
|