Create inference.py
Browse files- inference.py +31 -0
inference.py
ADDED
@@ -0,0 +1,31 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
import torch
|
2 |
+
import librosa
|
3 |
+
from transformers import AutoModelForCTC, Wav2Vec2Processor
|
4 |
+
|
5 |
+
# Load the model and processor
|
6 |
+
model = AutoModelForCTC.from_pretrained("aoxo/wav2vec2-base-mal")
|
7 |
+
processor = Wav2Vec2Processor.from_pretrained("aoxo/wav2vec2-base-mal")
|
8 |
+
|
9 |
+
# Function to transcribe audio
|
10 |
+
def transcribe_audio(audio_path):
|
11 |
+
# Load the audio file
|
12 |
+
# Resample to 16kHz if needed
|
13 |
+
waveform, _ = librosa.load(audio_path, sr=16000)
|
14 |
+
|
15 |
+
# Process the audio
|
16 |
+
inputs = processor(waveform, sampling_rate=16000, return_tensors="pt")
|
17 |
+
|
18 |
+
# Perform inference
|
19 |
+
with torch.no_grad():
|
20 |
+
logits = model(inputs.input_values).logits
|
21 |
+
|
22 |
+
# Decode the prediction
|
23 |
+
predicted_ids = torch.argmax(logits, dim=-1)
|
24 |
+
transcription = processor.batch_decode(predicted_ids)[0]
|
25 |
+
|
26 |
+
return transcription
|
27 |
+
|
28 |
+
# Example usage
|
29 |
+
audio_path = "path/to/your/audio/file.wav"
|
30 |
+
transcription = transcribe_audio(audio_path)
|
31 |
+
print("Transcription:", transcription)
|