Spaces:
Build error
Build error
File size: 10,313 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 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 | {
"cells": [
{
"cell_type": "code",
"id": "6369bdabdf59b658",
"metadata": {
"ExecuteTime": {
"end_time": "2025-06-10T20:28:20.833977Z",
"start_time": "2025-06-10T20:28:14.932967Z"
}
},
"source": [
"from langchain.document_loaders import YoutubeLoader\n",
"from langchain_yt_dlp.youtube_loader import YoutubeLoaderDL\n",
"from globals import *\n",
"import torch\n",
"import torchaudio.transforms as T\n",
"import pydub\n",
"import numpy as np\n",
"from transformers import Wav2Vec2ForCTC, Wav2Vec2Processor"
],
"outputs": [],
"execution_count": 33
},
{
"metadata": {
"ExecuteTime": {
"end_time": "2025-06-10T20:05:04.414620Z",
"start_time": "2025-06-10T20:05:04.412354Z"
}
},
"cell_type": "code",
"source": "url = \"https://www.youtube.com/watch?v=1htKBjuUWec\"\n",
"id": "666e521f8ecf3f47",
"outputs": [],
"execution_count": 14
},
{
"metadata": {
"ExecuteTime": {
"end_time": "2025-06-10T20:26:57.491908Z",
"start_time": "2025-06-10T20:26:57.489481Z"
}
},
"cell_type": "code",
"source": [
"# Load transcript as LangChain Documents\n",
"# loader = YoutubeLoader.from_youtube_url(url, add_video_info=False)\n",
"# loader = YoutubeLoaderDL.from_youtube_url(\n",
"# url, add_video_info=True\n",
"# )\n",
"# docs = loader.load()\n",
"#\n",
"# # Print the transcript content\n",
"# for doc in docs:\n",
"# print(doc.page_content)\n",
"\n",
"# Optionally, save to a file\n",
"# with open(\"transcript.txt\", \"w\", encoding=\"utf-8\") as f:\n",
"# for doc in docs:\n",
"# f.write(doc.page_content)"
],
"id": "initial_id",
"outputs": [],
"execution_count": 30
},
{
"metadata": {
"ExecuteTime": {
"end_time": "2025-06-10T20:27:03.519450Z",
"start_time": "2025-06-10T20:27:03.517474Z"
}
},
"cell_type": "code",
"source": [
"# from pytube import YouTube\n",
"#\n",
"# yt = YouTube(url)\n",
"#\n",
"# # Download the audio stream (usually mp4)\n",
"# stream = yt.streams.filter(only_audio=True).first()\n",
"# stream.download(firstilename=f\"{yt.title}.mp3\")"
],
"id": "ec4885c3a15d9a2b",
"outputs": [],
"execution_count": 31
},
{
"metadata": {
"ExecuteTime": {
"end_time": "2025-06-10T20:04:18.601366Z",
"start_time": "2025-06-10T20:04:18.597488Z"
}
},
"cell_type": "code",
"source": [
"import ssl\n",
"import certifi\n",
"# Correct: assign a lambda (function) that returns a properly configured SSL context\n",
"ssl._create_default_https_context = lambda: ssl.create_default_context(cafile=certifi.where())"
],
"id": "167af702547c15e4",
"outputs": [],
"execution_count": 12
},
{
"metadata": {
"ExecuteTime": {
"end_time": "2025-06-10T20:07:53.667018Z",
"start_time": "2025-06-10T20:07:52.627871Z"
}
},
"cell_type": "code",
"source": [
"\n",
"from pytube import YouTube\n",
"\n",
"def download_video(url, output_path='.'):\n",
" try:\n",
" yt = YouTube(url)\n",
" print('here')\n",
" stream = yt.streams.get_highest_resolution()\n",
" print(f\"Downloading: {yt.title}\")\n",
" stream.download(output_path=output_path)\n",
" print(\"Download completed.\")\n",
" except Exception as e:\n",
" print(f\"Error: {e}\")\n",
"\n",
"# Example usage\n",
"download_video(url)"
],
"id": "289b9a4321ea487b",
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"here\n",
"Error: HTTP Error 400: Bad Request\n"
]
}
],
"execution_count": 23
},
{
"metadata": {
"ExecuteTime": {
"end_time": "2025-06-10T20:08:47.590897Z",
"start_time": "2025-06-10T20:08:44.115350Z"
}
},
"cell_type": "code",
"source": [
"import yt_dlp\n",
"\n",
"ydl_opts = {\n",
" 'format': 'best', # or 'bestvideo+bestaudio'\n",
" 'outtmpl': '%(title)s.%(ext)s', # save as video title\n",
"}\n",
"\n",
"with yt_dlp.YoutubeDL(ydl_opts) as ydl:\n",
" ydl.download([url])"
],
"id": "4eb045792318e67a",
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"[youtube] Extracting URL: https://www.youtube.com/watch?v=1htKBjuUWec\n",
"[youtube] 1htKBjuUWec: Downloading webpage\n",
"[youtube] 1htKBjuUWec: Downloading tv client config\n",
"[youtube] 1htKBjuUWec: Downloading tv player API JSON\n",
"[youtube] 1htKBjuUWec: Downloading ios player API JSON\n",
"[youtube] 1htKBjuUWec: Downloading m3u8 information\n",
"[info] 1htKBjuUWec: Downloading 1 format(s): 18\n",
"[download] Destination: Teal'c coffee first time.mp4\n",
"[download] 100% of 1.19MiB in 00:00:01 at 1.09MiB/s \n"
]
}
],
"execution_count": 24
},
{
"metadata": {
"ExecuteTime": {
"end_time": "2025-06-10T20:22:32.706482Z",
"start_time": "2025-06-10T20:22:29.843517Z"
}
},
"cell_type": "code",
"source": [
"import yt_dlp\n",
"\n",
"file_name = 'my_audio_file'\n",
"ydl_opts = {\n",
" 'format': 'bestaudio/best',\n",
" 'outtmpl': f'files/{file_name}.%(ext)s', # <-- set your custom filename here\n",
" 'postprocessors': [{\n",
" 'key': 'FFmpegExtractAudio',\n",
" 'preferredcodec': 'mp3',\n",
" 'preferredquality': '192',\n",
" }],\n",
"}\n",
"\n",
"with yt_dlp.YoutubeDL(ydl_opts) as ydl:\n",
" ydl.download([url])"
],
"id": "68b51ca78254d8f",
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"[youtube] Extracting URL: https://www.youtube.com/watch?v=1htKBjuUWec\n",
"[youtube] 1htKBjuUWec: Downloading webpage\n",
"[youtube] 1htKBjuUWec: Downloading tv client config\n",
"[youtube] 1htKBjuUWec: Downloading tv player API JSON\n",
"[youtube] 1htKBjuUWec: Downloading ios player API JSON\n",
"[youtube] 1htKBjuUWec: Downloading m3u8 information\n",
"[info] 1htKBjuUWec: Downloading 1 format(s): 251\n",
"[download] Destination: files/my_audio_file.webm\n",
"[download] 100% of 444.93KiB in 00:00:00 at 3.41MiB/s \n",
"[ExtractAudio] Destination: files/my_audio_file.mp3\n",
"Deleting original file files/my_audio_file.webm (pass -k to keep)\n"
]
}
],
"execution_count": 26
},
{
"metadata": {
"ExecuteTime": {
"end_time": "2025-06-10T20:35:23.558866Z",
"start_time": "2025-06-10T20:35:23.515883Z"
}
},
"cell_type": "code",
"source": [
"audio_model_dir = './models_for_proj/wav2vec2-base-960h'\n",
"model = Wav2Vec2ForCTC.from_pretrained(audio_model_dir)\n",
"processor = Wav2Vec2Processor.from_pretrained(audio_model_dir)\n",
"\n",
"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",
" # y = y.mean(axis=1)\n",
" y = y[:,1]\n",
" if normalized:\n",
" return a.frame_rate, np.float32(y) / 2**15\n",
" else:\n",
" return a.frame_rate, y\n",
"\n",
"def describe_audio_tool(file_name: str) -> str:\n",
" \"\"\"\n",
" This tool receives a file name of an audio, uploads the audio and returns a detailed description of the audio.\n",
" Inputs: file_name as str\n",
" Outputs: audio detailed description as str\n",
" \"\"\"\n",
" # --------------------------------------------------------------------------- #\n",
" file_dir = f'files/{file_name}'\n",
" print(f\"{file_dir=}\")\n",
" audio_input_sr, audio_input_np = read_mp3(file_dir)\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",
" 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",
" return transcription"
],
"id": "64f438af2b38765f",
"outputs": [],
"execution_count": 43
},
{
"metadata": {
"ExecuteTime": {
"end_time": "2025-06-10T20:35:27.235493Z",
"start_time": "2025-06-10T20:35:26.202459Z"
}
},
"cell_type": "code",
"source": "describe_audio_tool(file_name=f'{file_name}.mp3')",
"id": "b4a6ae10e1cbbcae",
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"file_dir='files/my_audio_file.mp3'\n"
]
},
{
"data": {
"text/plain": [
"\"ALIS COFFEE'S GRAY WO IS JUST THINKING YET HE TAT SOMESCHICKERY A CHIC TEK H IS NOT HOT EXTREMELY\""
]
},
"execution_count": 44,
"metadata": {},
"output_type": "execute_result"
}
],
"execution_count": 44
},
{
"metadata": {},
"cell_type": "code",
"outputs": [],
"execution_count": null,
"source": "",
"id": "ce9aaf764346b7e4"
}
],
"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
}
|