Views
No views yet
1import torch
2import torchaudio
3import gradio as gr
4import numpy as np
5from transformers import pipeline, AutoProcessor, AutoModelForCTC
6
7device = torch.device("cuda" if torch.cuda.is_available() else "cpu")
8
9# Load the model and processor
10MODEL_NAME = "<fill this to your model>"
11processor = AutoProcessor.from_pretrained(MODEL_NAME)
12model = AutoModelForCTC.from_pretrained(MODEL_NAME)
13
14# Move model to GPU
15model.to(device)
16
17# Create the pipeline with the model and processor
18transcriber = pipeline("automatic-speech-recognition", model=model, tokenizer=processor.tokenizer, feature_extractor=processor.feature_extractor, device=device)
19
20def transcribe(audio):
21 sr, y = audio
22 y = y.astype(np.float32)
23 y /= np.max(np.abs(y))
24
25 return transcriber({"sampling_rate": sr, "raw": y})["text"]
26
27demo = gr.Interface(
28 transcribe,
29 gr.Audio(sources=["upload"]),
30 "text",
31)
32
33demo.launch(share=True)1import torch
2import torchaudio
3import gradio as gr
4import numpy as np
5from transformers import pipeline, AutoProcessor, AutoModelForCTC
6
7device = torch.device("cuda" if torch.cuda.is_available() else "cpu")
8
9# Load the model and processor
10MODEL_NAME = "<fill this to actual model>"
11processor = AutoProcessor.from_pretrained(MODEL_NAME)
12model = AutoModelForCTC.from_pretrained(MODEL_NAME)
13
14# Move model to GPU
15model.to(device)
16
17# Load audio file
18AUDIO_PATH = "<replace 'path_to_audio_file.wav' with the actual path to your audio file>"
19audio_input, sample_rate = torchaudio.load(AUDIO_PATH)
20
21# Ensure the audio is mono (1 channel)
22if audio_input.shape[0] > 1:
23 audio_input = torch.mean(audio_input, dim=0, keepdim=True)
24
25# Resample audio if necessary
26if sample_rate != 16000:
27 resampler = torchaudio.transforms.Resample(orig_freq=sample_rate, new_freq=16000)
28 audio_input = resampler(audio_input)
29
30# Process the audio input
31input_values = processor(audio_input.squeeze(), sampling_rate=16000, return_tensors="pt").input_values
32
33# Move input values to GPU
34input_values = input_values.to(device)
35
36# Perform inference
37with torch.no_grad():
38 logits = model(input_values).logits
39
40# Decode the logits to text
41predicted_ids = torch.argmax(logits, dim=-1)
42transcription = processor.batch_decode(predicted_ids)[0]
43
44print("Transcription:", transcription)| Training Loss | Epoch | Step | Validation Loss | Wer |
|---|---|---|---|---|
| 0.6796 | 2.8329 | 2000 | 0.5100 | 0.5010 |
| 0.4236 | 5.6657 | 4000 | 0.3792 | 0.3598 |
| 0.318 | 8.4986 | 6000 | 0.3244 | 0.2846 |
| 0.2444 | 11.3314 | 8000 | 0.3026 | 0.2674 |
| 0.1916 | 14.1643 | 10000 | 0.2682 | 0.2364 |
| 0.1588 | 16.9972 | 12000 | 0.2762 | 0.2398 |
| 0.1338 | 19.8300 | 14000 | 0.2623 | 0.2116 |
| 0.1201 | 22.6629 | 16000 | 0.2672 | 0.2081 |
| 0.1005 | 25.4958 | 18000 | 0.2596 | 0.1978 |
| 0.0921 | 28.3286 | 20000 | 0.2595 | 0.1881 |
| 0.0853 | 31.1615 | 22000 | 0.2671 | 0.1730 |
| 0.0761 | 33.9943 | 24000 | 0.2588 | 0.1744 |
| 0.0689 | 36.8272 | 26000 | 0.2490 | 0.1668 |
| 0.0646 | 39.6601 | 28000 | 0.2630 | 0.1633 |
| 0.0615 | 42.4929 | 30000 | 0.2677 | 0.1688 |
| 0.0563 | 45.3258 | 32000 | 0.2627 | 0.1585 |
| 0.0524 | 48.1586 | 34000 | 0.2497 | 0.1468 |
| 0.0511 | 50.9915 | 36000 | 0.2520 | 0.1516 |
| 0.0486 | 53.8244 | 38000 | 0.2418 | 0.1544 |
| 0.0415 | 56.6572 | 40000 | 0.2571 | 0.1489 |
| 0.0409 | 59.4901 | 42000 | 0.2687 | 0.1502 |
| 0.0361 | 62.3229 | 44000 | 0.2542 | 0.1371 |
| 0.0346 | 65.1558 | 46000 | 0.2504 | 0.1344 |
| 0.0312 | 67.9887 | 48000 | 0.2603 | 0.1337 |
| 0.0307 | 70.8215 | 50000 | 0.2641 | 0.1254 |
| 0.0305 | 73.6544 | 52000 | 0.2675 | 0.1289 |
| 0.0265 | 76.4873 | 54000 | 0.2625 | 0.1261 |
| 0.0271 | 79.3201 | 56000 | 0.2573 | 0.1268 |
| 0.0257 | 82.1530 | 58000 | 0.2571 | 0.1241 |
| 0.0247 | 84.9858 | 60000 | 0.2555 | 0.1296 |