Spaces:
Build error
Build error
File size: 4,708 Bytes
45b200f |
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 |
{
"cells": [
{
"metadata": {},
"cell_type": "markdown",
"source": "Audio",
"id": "8b8c1a352260e82a"
},
{
"cell_type": "code",
"id": "initial_id",
"metadata": {
"collapsed": true,
"ExecuteTime": {
"end_time": "2025-06-10T09:38:10.760409Z",
"start_time": "2025-06-10T09:38:10.617508Z"
}
},
"source": [
"from transformers import Wav2Vec2ForCTC, Wav2Vec2Processor\n",
"import torch\n",
"import torchaudio.transforms as T\n",
"import pydub\n",
"import numpy as np"
],
"outputs": [],
"execution_count": 5
},
{
"metadata": {
"ExecuteTime": {
"end_time": "2025-06-10T09:43:53.684713Z",
"start_time": "2025-06-10T09:43:53.681866Z"
}
},
"cell_type": "code",
"source": [
"# CONSTANTS\n",
"audio_model_dir = './models_for_proj/wav2vec2-base-960h'\n",
"\n",
"# audio_dir = 'files/1f975693-876d-457b-a649-393859e79bf3.mp3'\n",
"audio_dir = 'files/99c9cc74-fdc8-46c6-8f8d-3ce2d3bfeea3.mp3'"
],
"id": "3ee50d096b2c9d44",
"outputs": [],
"execution_count": 19
},
{
"metadata": {
"ExecuteTime": {
"end_time": "2025-06-10T09:43:54.053411Z",
"start_time": "2025-06-10T09:43:54.006676Z"
}
},
"cell_type": "code",
"source": [
"\n",
"model = Wav2Vec2ForCTC.from_pretrained(audio_model_dir)\n",
"processor = Wav2Vec2Processor.from_pretrained(audio_model_dir)"
],
"id": "b51a485af7b9cf14",
"outputs": [],
"execution_count": 20
},
{
"metadata": {
"ExecuteTime": {
"end_time": "2025-06-10T09:43:54.603559Z",
"start_time": "2025-06-10T09:43:54.414677Z"
}
},
"cell_type": "code",
"source": [
"def read_mp3(f, normalized=False):\n",
" \"\"\"Read MP3 file to numpy array.\"\"\"\n",
" a = pydub.AudioSegment.from_mp3(f)\n",
" y = np.array(a.get_array_of_samples())\n",
" if a.channels == 2:\n",
" y = y.reshape((-1, 2))\n",
" if normalized:\n",
" return a.frame_rate, np.float32(y) / 2**15\n",
" else:\n",
" return a.frame_rate, y\n",
"\n",
"# Usage\n",
"audio_input_sr, audio_input_np = read_mp3(audio_dir)"
],
"id": "ac7e2b43ace4d232",
"outputs": [],
"execution_count": 21
},
{
"metadata": {
"ExecuteTime": {
"end_time": "2025-06-10T09:43:56.920665Z",
"start_time": "2025-06-10T09:43:56.244101Z"
}
},
"cell_type": "code",
"source": [
"# --------------------------------------------------------------------------- #\n",
"# audio_input_sr, audio_input_np = audio_input\n",
"audio_input_t = torch.tensor(audio_input_np, dtype=torch.float32)\n",
"target_sr = 16000\n",
"resampler = T.Resample(audio_input_sr, target_sr, dtype=audio_input_t.dtype)\n",
"resampled_audio_input_t: torch.Tensor = resampler(audio_input_t)\n",
"resampled_audio_input_np = resampled_audio_input_t.numpy()\n",
"# --------------------------------------------------------------------------- #\n",
"# result = asr_pipe_default(resampled_audio_input_np)\n",
"inputs = processor(resampled_audio_input_np, sampling_rate=16000, return_tensors=\"pt\", padding=True)\n",
"# Inference\n",
"with torch.no_grad():\n",
" logits = model(**inputs).logits\n",
"# Decode\n",
"predicted_ids = torch.argmax(logits, dim=-1)\n",
"transcription = processor.decode(predicted_ids[0])\n",
"# print(\"Transcription:\", transcription)\n",
"transcription"
],
"id": "2a4738e9d038985",
"outputs": [
{
"data": {
"text/plain": [
"'IN A SAUCEPAN COMBINE RIPE STRAWBERRIES GRANULATED SUGAR FRESHLY SQUEEZED LEMON JUICE AND CORNSTARCH COOK THE MIXTURE OF A MEDIUM HEAT STIRRING CONSTANTLY UNTIL IT THICKENS TO A SMOOTH CONSISTENCY REMOVE FROM HEAT AND STIR IN A DASH OF PURE VANILLA EXTRACT ALLOW THE STRAWBERRY PIE FEELING TO COOL BEFORE USING IT AS A DELICIOUS AND FRUITY FILLING FOR YOUR PIE CRUST'"
]
},
"execution_count": 22,
"metadata": {},
"output_type": "execute_result"
}
],
"execution_count": 22
},
{
"metadata": {},
"cell_type": "code",
"outputs": [],
"execution_count": null,
"source": "",
"id": "f159c2955f140600"
}
],
"metadata": {
"kernelspec": {
"display_name": "Python 3",
"language": "python",
"name": "python3"
},
"language_info": {
"codemirror_mode": {
"name": "ipython",
"version": 2
},
"file_extension": ".py",
"mimetype": "text/x-python",
"name": "python",
"nbconvert_exporter": "python",
"pygments_lexer": "ipython2",
"version": "2.7.6"
}
},
"nbformat": 4,
"nbformat_minor": 5
}
|