eddiegulay commited on
Commit
1a7b3cd
1 Parent(s): 3aca3e9

Create inference.py

Browse files
Files changed (1) hide show
  1. inference.py +31 -0
inference.py ADDED
@@ -0,0 +1,31 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ from transformers import Wav2Vec2ForCTC, Wav2Vec2Processor
2
+ import torchaudio
3
+ import torch
4
+
5
+
6
+ repo_id =
7
+
8
+ model = Wav2Vec2ForCTC.from_pretrained(repo_id)
9
+ processor = Wav2Vec2Processor.from_pretrained(repo_id)
10
+
11
+
12
+
13
+ def transcribe(audio_path):
14
+ # Load the audio file
15
+ audio_input, sample_rate = torchaudio.load(audio_path)
16
+ target_sample_rate = 16000
17
+ audio_input = torchaudio.transforms.Resample(orig_freq=sample_rate, new_freq=target_sample_rate)(audio_input)
18
+
19
+ # Preprocess the audio data
20
+ input_dict = processor(audio_input[0], return_tensors="pt", padding=True, sampling_rate=16000)
21
+
22
+ # Perform inference and transcribe
23
+ logits = model(input_dict.input_values).logits
24
+ pred_ids = torch.argmax(logits, dim=-1)[0]
25
+ transcription = processor.decode(pred_ids)
26
+
27
+ return transcription
28
+
29
+
30
+ # transcribe sample audio
31
+ transcribe("download.wav")