Spaces:
Running
on
T4
Running
on
T4
oceansweep
commited on
Commit
•
8d1d1bc
1
Parent(s):
9db5a77
Update App_Function_Libraries/Audio_Files.py
Browse files- App_Function_Libraries/Audio_Files.py +691 -628
App_Function_Libraries/Audio_Files.py
CHANGED
@@ -1,629 +1,692 @@
|
|
1 |
-
# Audio_Files.py
|
2 |
-
#########################################
|
3 |
-
# Audio Processing Library
|
4 |
-
# This library is used to download or load audio files from a local directory.
|
5 |
-
#
|
6 |
-
####
|
7 |
-
#
|
8 |
-
# Functions:
|
9 |
-
#
|
10 |
-
# download_audio_file(url, save_path)
|
11 |
-
# process_audio(
|
12 |
-
# process_audio_file(audio_url, audio_file, whisper_model="small.en", api_name=None, api_key=None)
|
13 |
-
#
|
14 |
-
#
|
15 |
-
#########################################
|
16 |
-
# Imports
|
17 |
-
import json
|
18 |
-
import logging
|
19 |
-
import
|
20 |
-
import
|
21 |
-
import tempfile
|
22 |
-
import uuid
|
23 |
-
from datetime import datetime
|
24 |
-
|
25 |
-
|
26 |
-
import
|
27 |
-
|
28 |
-
|
29 |
-
|
30 |
-
from App_Function_Libraries.
|
31 |
-
|
32 |
-
#
|
33 |
-
|
34 |
-
|
35 |
-
from App_Function_Libraries.
|
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 |
-
if
|
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 |
-
if
|
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 |
-
summary=summary
|
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 |
-
|
349 |
-
|
350 |
-
|
351 |
-
|
352 |
-
|
353 |
-
|
354 |
-
|
355 |
-
|
356 |
-
|
357 |
-
|
358 |
-
|
359 |
-
|
360 |
-
|
361 |
-
|
362 |
-
|
363 |
-
|
364 |
-
|
365 |
-
|
366 |
-
|
367 |
-
|
368 |
-
|
369 |
-
|
370 |
-
|
371 |
-
|
372 |
-
|
373 |
-
|
374 |
-
|
375 |
-
|
376 |
-
|
377 |
-
|
378 |
-
|
379 |
-
|
380 |
-
|
381 |
-
|
382 |
-
|
383 |
-
|
384 |
-
|
385 |
-
|
386 |
-
|
387 |
-
|
388 |
-
|
389 |
-
|
390 |
-
|
391 |
-
|
392 |
-
|
393 |
-
|
394 |
-
|
395 |
-
|
396 |
-
|
397 |
-
)
|
398 |
-
|
399 |
-
|
400 |
-
|
401 |
-
|
402 |
-
|
403 |
-
|
404 |
-
|
405 |
-
|
406 |
-
|
407 |
-
|
408 |
-
|
409 |
-
|
410 |
-
|
411 |
-
|
412 |
-
|
413 |
-
|
414 |
-
|
415 |
-
|
416 |
-
|
417 |
-
|
418 |
-
|
419 |
-
|
420 |
-
|
421 |
-
|
422 |
-
|
423 |
-
|
424 |
-
|
425 |
-
|
426 |
-
|
427 |
-
|
428 |
-
|
429 |
-
|
430 |
-
|
431 |
-
if
|
432 |
-
|
433 |
-
|
434 |
-
|
435 |
-
|
436 |
-
|
437 |
-
|
438 |
-
|
439 |
-
|
440 |
-
|
441 |
-
|
442 |
-
|
443 |
-
|
444 |
-
|
445 |
-
|
446 |
-
|
447 |
-
|
448 |
-
|
449 |
-
|
450 |
-
|
451 |
-
|
452 |
-
|
453 |
-
|
454 |
-
|
455 |
-
|
456 |
-
|
457 |
-
|
458 |
-
|
459 |
-
|
460 |
-
|
461 |
-
|
462 |
-
|
463 |
-
|
464 |
-
|
465 |
-
|
466 |
-
|
467 |
-
|
468 |
-
|
469 |
-
|
470 |
-
|
471 |
-
|
472 |
-
|
473 |
-
|
474 |
-
|
475 |
-
|
476 |
-
|
477 |
-
|
478 |
-
|
479 |
-
|
480 |
-
|
481 |
-
|
482 |
-
|
483 |
-
|
484 |
-
|
485 |
-
|
486 |
-
|
487 |
-
|
488 |
-
|
489 |
-
|
490 |
-
|
491 |
-
|
492 |
-
|
493 |
-
|
494 |
-
|
495 |
-
|
496 |
-
|
497 |
-
|
498 |
-
|
499 |
-
|
500 |
-
|
501 |
-
|
502 |
-
|
503 |
-
|
504 |
-
|
505 |
-
|
506 |
-
|
507 |
-
|
508 |
-
|
509 |
-
|
510 |
-
|
511 |
-
|
512 |
-
|
513 |
-
|
514 |
-
|
515 |
-
|
516 |
-
|
517 |
-
|
518 |
-
|
519 |
-
|
520 |
-
|
521 |
-
|
522 |
-
|
523 |
-
|
524 |
-
|
525 |
-
|
526 |
-
|
527 |
-
|
528 |
-
|
529 |
-
|
530 |
-
|
531 |
-
|
532 |
-
|
533 |
-
|
534 |
-
|
535 |
-
|
536 |
-
|
537 |
-
|
538 |
-
|
539 |
-
|
540 |
-
|
541 |
-
|
542 |
-
|
543 |
-
|
544 |
-
|
545 |
-
|
546 |
-
|
547 |
-
|
548 |
-
|
549 |
-
|
550 |
-
|
551 |
-
|
552 |
-
|
553 |
-
|
554 |
-
|
555 |
-
|
556 |
-
|
557 |
-
|
558 |
-
|
559 |
-
|
560 |
-
|
561 |
-
|
562 |
-
|
563 |
-
|
564 |
-
|
565 |
-
|
566 |
-
|
567 |
-
|
568 |
-
|
569 |
-
|
570 |
-
|
571 |
-
|
572 |
-
|
573 |
-
|
574 |
-
|
575 |
-
|
576 |
-
|
577 |
-
|
578 |
-
|
579 |
-
|
580 |
-
|
581 |
-
|
582 |
-
|
583 |
-
|
584 |
-
|
585 |
-
|
586 |
-
|
587 |
-
|
588 |
-
|
589 |
-
|
590 |
-
|
591 |
-
|
592 |
-
|
593 |
-
|
594 |
-
|
595 |
-
|
596 |
-
|
597 |
-
|
598 |
-
|
599 |
-
|
600 |
-
|
601 |
-
|
602 |
-
|
603 |
-
|
604 |
-
|
605 |
-
|
606 |
-
|
607 |
-
|
608 |
-
|
609 |
-
|
610 |
-
|
611 |
-
|
612 |
-
|
613 |
-
|
614 |
-
|
615 |
-
|
616 |
-
|
617 |
-
|
618 |
-
|
619 |
-
|
620 |
-
|
621 |
-
|
622 |
-
|
623 |
-
|
624 |
-
|
625 |
-
|
626 |
-
|
627 |
-
|
628 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
629 |
#######################################################################################################################
|
|
|
1 |
+
# Audio_Files.py
|
2 |
+
#########################################
|
3 |
+
# Audio Processing Library
|
4 |
+
# This library is used to download or load audio files from a local directory.
|
5 |
+
#
|
6 |
+
####
|
7 |
+
#
|
8 |
+
# Functions:
|
9 |
+
#
|
10 |
+
# download_audio_file(url, save_path)
|
11 |
+
# process_audio(
|
12 |
+
# process_audio_file(audio_url, audio_file, whisper_model="small.en", api_name=None, api_key=None)
|
13 |
+
#
|
14 |
+
#
|
15 |
+
#########################################
|
16 |
+
# Imports
|
17 |
+
import json
|
18 |
+
import logging
|
19 |
+
import os
|
20 |
+
import subprocess
|
21 |
+
import tempfile
|
22 |
+
import uuid
|
23 |
+
from datetime import datetime
|
24 |
+
from pathlib import Path
|
25 |
+
|
26 |
+
import requests
|
27 |
+
import yt_dlp
|
28 |
+
|
29 |
+
from App_Function_Libraries.Audio_Transcription_Lib import speech_to_text
|
30 |
+
from App_Function_Libraries.Chunk_Lib import improved_chunking_process
|
31 |
+
#
|
32 |
+
# Local Imports
|
33 |
+
from App_Function_Libraries.SQLite_DB import add_media_to_database, add_media_with_keywords, \
|
34 |
+
check_media_and_whisper_model
|
35 |
+
from App_Function_Libraries.Summarization_General_Lib import save_transcription_and_summary, perform_transcription, \
|
36 |
+
perform_summarization
|
37 |
+
from App_Function_Libraries.Utils import create_download_directory, save_segments_to_json, downloaded_files, \
|
38 |
+
sanitize_filename
|
39 |
+
from App_Function_Libraries.Video_DL_Ingestion_Lib import extract_metadata
|
40 |
+
|
41 |
+
#
|
42 |
+
#######################################################################################################################
|
43 |
+
# Function Definitions
|
44 |
+
#
|
45 |
+
|
46 |
+
MAX_FILE_SIZE = 500 * 1024 * 1024
|
47 |
+
|
48 |
+
|
49 |
+
def download_audio_file(url, current_whisper_model="", use_cookies=False, cookies=None):
|
50 |
+
try:
|
51 |
+
# Check if media already exists in the database and compare whisper models
|
52 |
+
should_download, reason = check_media_and_whisper_model(
|
53 |
+
url=url,
|
54 |
+
current_whisper_model=current_whisper_model
|
55 |
+
)
|
56 |
+
|
57 |
+
if not should_download:
|
58 |
+
logging.info(f"Skipping audio download: {reason}")
|
59 |
+
return None
|
60 |
+
|
61 |
+
logging.info(f"Proceeding with audio download: {reason}")
|
62 |
+
|
63 |
+
# Set up the request headers
|
64 |
+
headers = {}
|
65 |
+
if use_cookies and cookies:
|
66 |
+
try:
|
67 |
+
cookie_dict = json.loads(cookies)
|
68 |
+
headers['Cookie'] = '; '.join([f'{k}={v}' for k, v in cookie_dict.items()])
|
69 |
+
except json.JSONDecodeError:
|
70 |
+
logging.warning("Invalid cookie format. Proceeding without cookies.")
|
71 |
+
|
72 |
+
# Make the request
|
73 |
+
response = requests.get(url, headers=headers, stream=True)
|
74 |
+
# Raise an exception for bad status codes
|
75 |
+
response.raise_for_status()
|
76 |
+
|
77 |
+
# Get the file size
|
78 |
+
file_size = int(response.headers.get('content-length', 0))
|
79 |
+
if file_size > 500 * 1024 * 1024: # 500 MB limit
|
80 |
+
raise ValueError("File size exceeds the 500MB limit.")
|
81 |
+
|
82 |
+
# Generate a unique filename
|
83 |
+
file_name = f"audio_{uuid.uuid4().hex[:8]}.mp3"
|
84 |
+
save_path = os.path.join('downloads', file_name)
|
85 |
+
|
86 |
+
# Ensure the downloads directory exists
|
87 |
+
os.makedirs('downloads', exist_ok=True)
|
88 |
+
|
89 |
+
|
90 |
+
# Download the file
|
91 |
+
with open(save_path, 'wb') as f:
|
92 |
+
for chunk in response.iter_content(chunk_size=8192):
|
93 |
+
if chunk:
|
94 |
+
f.write(chunk)
|
95 |
+
|
96 |
+
logging.info(f"Audio file downloaded successfully: {save_path}")
|
97 |
+
return save_path
|
98 |
+
|
99 |
+
except requests.RequestException as e:
|
100 |
+
logging.error(f"Error downloading audio file: {str(e)}")
|
101 |
+
raise
|
102 |
+
except ValueError as e:
|
103 |
+
logging.error(str(e))
|
104 |
+
raise
|
105 |
+
except Exception as e:
|
106 |
+
logging.error(f"Unexpected error downloading audio file: {str(e)}")
|
107 |
+
raise
|
108 |
+
|
109 |
+
|
110 |
+
def process_audio(
|
111 |
+
audio_file_path,
|
112 |
+
num_speakers=2,
|
113 |
+
whisper_model="small.en",
|
114 |
+
custom_prompt_input=None,
|
115 |
+
offset=0,
|
116 |
+
api_name=None,
|
117 |
+
api_key=None,
|
118 |
+
vad_filter=False,
|
119 |
+
rolling_summarization=False,
|
120 |
+
detail_level=0.01,
|
121 |
+
keywords="default,no_keyword_set",
|
122 |
+
chunk_text_by_words=False,
|
123 |
+
max_words=0,
|
124 |
+
chunk_text_by_sentences=False,
|
125 |
+
max_sentences=0,
|
126 |
+
chunk_text_by_paragraphs=False,
|
127 |
+
max_paragraphs=0,
|
128 |
+
chunk_text_by_tokens=False,
|
129 |
+
max_tokens=0
|
130 |
+
):
|
131 |
+
try:
|
132 |
+
|
133 |
+
# Perform transcription
|
134 |
+
audio_file_path, segments = perform_transcription(audio_file_path, offset, whisper_model, vad_filter)
|
135 |
+
|
136 |
+
if audio_file_path is None or segments is None:
|
137 |
+
logging.error("Process_Audio: Transcription failed or segments not available.")
|
138 |
+
return "Process_Audio: Transcription failed.", None, None, None, None, None
|
139 |
+
|
140 |
+
logging.debug(f"Process_Audio: Transcription audio_file: {audio_file_path}")
|
141 |
+
logging.debug(f"Process_Audio: Transcription segments: {segments}")
|
142 |
+
|
143 |
+
transcription_text = {'audio_file': audio_file_path, 'transcription': segments}
|
144 |
+
logging.debug(f"Process_Audio: Transcription text: {transcription_text}")
|
145 |
+
|
146 |
+
# Save segments to JSON
|
147 |
+
segments_json_path = save_segments_to_json(segments)
|
148 |
+
|
149 |
+
# Perform summarization
|
150 |
+
summary_text = None
|
151 |
+
if api_name:
|
152 |
+
if rolling_summarization is not None:
|
153 |
+
pass
|
154 |
+
# FIXME rolling summarization
|
155 |
+
# summary_text = rolling_summarize_function(
|
156 |
+
# transcription_text,
|
157 |
+
# detail=detail_level,
|
158 |
+
# api_name=api_name,
|
159 |
+
# api_key=api_key,
|
160 |
+
# custom_prompt=custom_prompt_input,
|
161 |
+
# chunk_by_words=chunk_text_by_words,
|
162 |
+
# max_words=max_words,
|
163 |
+
# chunk_by_sentences=chunk_text_by_sentences,
|
164 |
+
# max_sentences=max_sentences,
|
165 |
+
# chunk_by_paragraphs=chunk_text_by_paragraphs,
|
166 |
+
# max_paragraphs=max_paragraphs,
|
167 |
+
# chunk_by_tokens=chunk_text_by_tokens,
|
168 |
+
# max_tokens=max_tokens
|
169 |
+
# )
|
170 |
+
else:
|
171 |
+
summary_text = perform_summarization(api_name, segments_json_path, custom_prompt_input, api_key)
|
172 |
+
|
173 |
+
if summary_text is None:
|
174 |
+
logging.error("Summary text is None. Check summarization function.")
|
175 |
+
summary_file_path = None
|
176 |
+
else:
|
177 |
+
summary_text = 'Summary not available'
|
178 |
+
summary_file_path = None
|
179 |
+
|
180 |
+
# Save transcription and summary
|
181 |
+
download_path = create_download_directory("Audio_Processing")
|
182 |
+
json_file_path, summary_file_path = save_transcription_and_summary(transcription_text, summary_text,
|
183 |
+
download_path)
|
184 |
+
|
185 |
+
# Update function call to add_media_to_database so that it properly applies the title, author and file type
|
186 |
+
# Add to database
|
187 |
+
add_media_to_database(None, {'title': 'Audio File', 'author': 'Unknown'}, segments, summary_text, keywords,
|
188 |
+
custom_prompt_input, whisper_model)
|
189 |
+
|
190 |
+
return transcription_text, summary_text, json_file_path, summary_file_path, None, None
|
191 |
+
|
192 |
+
except Exception as e:
|
193 |
+
logging.error(f"Error in process_audio: {str(e)}")
|
194 |
+
return str(e), None, None, None, None, None
|
195 |
+
|
196 |
+
|
197 |
+
def process_single_audio(audio_file_path, whisper_model, api_name, api_key, keep_original,custom_keywords, source,
|
198 |
+
custom_prompt_input, chunk_method, max_chunk_size, chunk_overlap, use_adaptive_chunking,
|
199 |
+
use_multi_level_chunking, chunk_language):
|
200 |
+
progress = []
|
201 |
+
transcription = ""
|
202 |
+
summary = ""
|
203 |
+
|
204 |
+
def update_progress(message):
|
205 |
+
progress.append(message)
|
206 |
+
return "\n".join(progress)
|
207 |
+
|
208 |
+
try:
|
209 |
+
# Check file size before processing
|
210 |
+
file_size = os.path.getsize(audio_file_path)
|
211 |
+
if file_size > MAX_FILE_SIZE:
|
212 |
+
update_progress(f"File size ({file_size / (1024 * 1024):.2f} MB) exceeds the maximum limit of {MAX_FILE_SIZE / (1024 * 1024):.2f} MB. Skipping this file.")
|
213 |
+
return "\n".join(progress), "", ""
|
214 |
+
|
215 |
+
# Perform transcription
|
216 |
+
update_progress("Starting transcription...")
|
217 |
+
segments = speech_to_text(audio_file_path, whisper_model=whisper_model)
|
218 |
+
transcription = " ".join([segment['Text'] for segment in segments])
|
219 |
+
update_progress("Audio transcribed successfully.")
|
220 |
+
|
221 |
+
# Perform summarization if API is provided
|
222 |
+
if api_name and api_key:
|
223 |
+
update_progress("Starting summarization...")
|
224 |
+
summary = perform_summarization(api_name, transcription, "Summarize the following audio transcript",
|
225 |
+
api_key)
|
226 |
+
update_progress("Audio summarized successfully.")
|
227 |
+
else:
|
228 |
+
summary = "No summary available"
|
229 |
+
|
230 |
+
# Prepare keywords
|
231 |
+
keywords = "audio,transcription"
|
232 |
+
if custom_keywords:
|
233 |
+
keywords += f",{custom_keywords}"
|
234 |
+
|
235 |
+
# Add to database
|
236 |
+
add_media_with_keywords(
|
237 |
+
url=source,
|
238 |
+
title=os.path.basename(audio_file_path),
|
239 |
+
media_type='audio',
|
240 |
+
content=transcription,
|
241 |
+
keywords=keywords,
|
242 |
+
prompt="Summarize the following audio transcript",
|
243 |
+
summary=summary,
|
244 |
+
transcription_model=whisper_model,
|
245 |
+
author="Unknown",
|
246 |
+
ingestion_date=None # This will use the current date
|
247 |
+
)
|
248 |
+
update_progress("Audio file added to database successfully.")
|
249 |
+
|
250 |
+
if not keep_original and source != "Uploaded File":
|
251 |
+
os.remove(audio_file_path)
|
252 |
+
update_progress(f"Temporary file {audio_file_path} removed.")
|
253 |
+
elif keep_original and source != "Uploaded File":
|
254 |
+
update_progress(f"Original audio file kept at: {audio_file_path}")
|
255 |
+
|
256 |
+
except Exception as e:
|
257 |
+
update_progress(f"Error processing {source}: {str(e)}")
|
258 |
+
transcription = f"Error: {str(e)}"
|
259 |
+
summary = "No summary due to error"
|
260 |
+
|
261 |
+
return "\n".join(progress), transcription, summary
|
262 |
+
|
263 |
+
|
264 |
+
def process_audio_files(audio_urls, audio_file, whisper_model, api_name, api_key, use_cookies, cookies, keep_original,
|
265 |
+
custom_keywords, custom_prompt_input, chunk_method, max_chunk_size, chunk_overlap,
|
266 |
+
use_adaptive_chunking, use_multi_level_chunking, chunk_language, diarize):
|
267 |
+
progress = []
|
268 |
+
temp_files = []
|
269 |
+
all_transcriptions = []
|
270 |
+
all_summaries = []
|
271 |
+
|
272 |
+
def update_progress(message):
|
273 |
+
progress.append(message)
|
274 |
+
return "\n".join(progress)
|
275 |
+
|
276 |
+
def cleanup_files():
|
277 |
+
for file in temp_files:
|
278 |
+
try:
|
279 |
+
if os.path.exists(file):
|
280 |
+
os.remove(file)
|
281 |
+
update_progress(f"Temporary file {file} removed.")
|
282 |
+
except Exception as e:
|
283 |
+
update_progress(f"Failed to remove temporary file {file}: {str(e)}")
|
284 |
+
|
285 |
+
def reencode_mp3(mp3_file_path):
|
286 |
+
try:
|
287 |
+
reencoded_mp3_path = mp3_file_path.replace(".mp3", "_reencoded.mp3")
|
288 |
+
subprocess.run([ffmpeg_cmd, '-i', mp3_file_path, '-codec:a', 'libmp3lame', reencoded_mp3_path], check=True)
|
289 |
+
update_progress(f"Re-encoded {mp3_file_path} to {reencoded_mp3_path}.")
|
290 |
+
return reencoded_mp3_path
|
291 |
+
except subprocess.CalledProcessError as e:
|
292 |
+
update_progress(f"Error re-encoding {mp3_file_path}: {str(e)}")
|
293 |
+
raise
|
294 |
+
|
295 |
+
def convert_mp3_to_wav(mp3_file_path):
|
296 |
+
try:
|
297 |
+
wav_file_path = mp3_file_path.replace(".mp3", ".wav")
|
298 |
+
subprocess.run([ffmpeg_cmd, '-i', mp3_file_path, wav_file_path], check=True)
|
299 |
+
update_progress(f"Converted {mp3_file_path} to {wav_file_path}.")
|
300 |
+
return wav_file_path
|
301 |
+
except subprocess.CalledProcessError as e:
|
302 |
+
update_progress(f"Error converting {mp3_file_path} to WAV: {str(e)}")
|
303 |
+
raise
|
304 |
+
|
305 |
+
try:
|
306 |
+
# Check and set the ffmpeg command
|
307 |
+
global ffmpeg_cmd
|
308 |
+
if os.name == "nt":
|
309 |
+
logging.debug("Running on Windows")
|
310 |
+
ffmpeg_cmd = os.path.join(os.getcwd(), "Bin", "ffmpeg.exe")
|
311 |
+
else:
|
312 |
+
ffmpeg_cmd = 'ffmpeg' # Assume 'ffmpeg' is in PATH for non-Windows systems
|
313 |
+
|
314 |
+
# Ensure ffmpeg is accessible
|
315 |
+
if not os.path.exists(ffmpeg_cmd) and os.name == "nt":
|
316 |
+
raise FileNotFoundError(f"ffmpeg executable not found at path: {ffmpeg_cmd}")
|
317 |
+
|
318 |
+
# Define chunk options early to avoid undefined errors
|
319 |
+
chunk_options = {
|
320 |
+
'method': chunk_method,
|
321 |
+
'max_size': max_chunk_size,
|
322 |
+
'overlap': chunk_overlap,
|
323 |
+
'adaptive': use_adaptive_chunking,
|
324 |
+
'multi_level': use_multi_level_chunking,
|
325 |
+
'language': chunk_language
|
326 |
+
}
|
327 |
+
|
328 |
+
# Process multiple URLs
|
329 |
+
urls = [url.strip() for url in audio_urls.split('\n') if url.strip()]
|
330 |
+
|
331 |
+
for i, url in enumerate(urls):
|
332 |
+
update_progress(f"Processing URL {i + 1}/{len(urls)}: {url}")
|
333 |
+
|
334 |
+
# Download and process audio file
|
335 |
+
audio_file_path = download_audio_file(url, use_cookies, cookies)
|
336 |
+
if not os.path.exists(audio_file_path):
|
337 |
+
update_progress(f"Downloaded file not found: {audio_file_path}")
|
338 |
+
continue
|
339 |
+
|
340 |
+
temp_files.append(audio_file_path)
|
341 |
+
update_progress("Audio file downloaded successfully.")
|
342 |
+
|
343 |
+
# Re-encode MP3 to fix potential issues
|
344 |
+
reencoded_mp3_path = reencode_mp3(audio_file_path)
|
345 |
+
if not os.path.exists(reencoded_mp3_path):
|
346 |
+
update_progress(f"Re-encoded file not found: {reencoded_mp3_path}")
|
347 |
+
continue
|
348 |
+
|
349 |
+
temp_files.append(reencoded_mp3_path)
|
350 |
+
|
351 |
+
# Convert re-encoded MP3 to WAV
|
352 |
+
wav_file_path = convert_mp3_to_wav(reencoded_mp3_path)
|
353 |
+
if not os.path.exists(wav_file_path):
|
354 |
+
update_progress(f"Converted WAV file not found: {wav_file_path}")
|
355 |
+
continue
|
356 |
+
|
357 |
+
temp_files.append(wav_file_path)
|
358 |
+
|
359 |
+
# Initialize transcription
|
360 |
+
transcription = ""
|
361 |
+
|
362 |
+
# Transcribe audio
|
363 |
+
if diarize:
|
364 |
+
segments = speech_to_text(wav_file_path, whisper_model=whisper_model, diarize=True)
|
365 |
+
else:
|
366 |
+
segments = speech_to_text(wav_file_path, whisper_model=whisper_model)
|
367 |
+
|
368 |
+
# Handle segments nested under 'segments' key
|
369 |
+
if isinstance(segments, dict) and 'segments' in segments:
|
370 |
+
segments = segments['segments']
|
371 |
+
|
372 |
+
if isinstance(segments, list):
|
373 |
+
transcription = " ".join([segment.get('Text', '') for segment in segments])
|
374 |
+
update_progress("Audio transcribed successfully.")
|
375 |
+
else:
|
376 |
+
update_progress("Unexpected segments format received from speech_to_text.")
|
377 |
+
logging.error(f"Unexpected segments format: {segments}")
|
378 |
+
continue
|
379 |
+
|
380 |
+
if not transcription.strip():
|
381 |
+
update_progress("Transcription is empty.")
|
382 |
+
else:
|
383 |
+
# Apply chunking
|
384 |
+
chunked_text = improved_chunking_process(transcription, chunk_options)
|
385 |
+
|
386 |
+
# Summarize
|
387 |
+
if api_name:
|
388 |
+
try:
|
389 |
+
summary = perform_summarization(api_name, chunked_text, custom_prompt_input, api_key)
|
390 |
+
update_progress("Audio summarized successfully.")
|
391 |
+
except Exception as e:
|
392 |
+
logging.error(f"Error during summarization: {str(e)}")
|
393 |
+
summary = "Summary generation failed"
|
394 |
+
else:
|
395 |
+
summary = "No summary available (API not provided)"
|
396 |
+
|
397 |
+
all_transcriptions.append(transcription)
|
398 |
+
all_summaries.append(summary)
|
399 |
+
|
400 |
+
# Add to database
|
401 |
+
add_media_with_keywords(
|
402 |
+
url=url,
|
403 |
+
title=os.path.basename(wav_file_path),
|
404 |
+
media_type='audio',
|
405 |
+
content=transcription,
|
406 |
+
keywords=custom_keywords,
|
407 |
+
prompt=custom_prompt_input,
|
408 |
+
summary=summary,
|
409 |
+
transcription_model=whisper_model,
|
410 |
+
author="Unknown",
|
411 |
+
ingestion_date=datetime.now().strftime('%Y-%m-%d')
|
412 |
+
)
|
413 |
+
update_progress("Audio file processed and added to database.")
|
414 |
+
|
415 |
+
# Process uploaded file if provided
|
416 |
+
if audio_file:
|
417 |
+
if os.path.getsize(audio_file.name) > MAX_FILE_SIZE:
|
418 |
+
update_progress(
|
419 |
+
f"Uploaded file size exceeds the maximum limit of {MAX_FILE_SIZE / (1024 * 1024):.2f}MB. Skipping this file.")
|
420 |
+
else:
|
421 |
+
# Re-encode MP3 to fix potential issues
|
422 |
+
reencoded_mp3_path = reencode_mp3(audio_file.name)
|
423 |
+
if not os.path.exists(reencoded_mp3_path):
|
424 |
+
update_progress(f"Re-encoded file not found: {reencoded_mp3_path}")
|
425 |
+
return update_progress("Processing failed: Re-encoded file not found"), "", ""
|
426 |
+
|
427 |
+
temp_files.append(reencoded_mp3_path)
|
428 |
+
|
429 |
+
# Convert re-encoded MP3 to WAV
|
430 |
+
wav_file_path = convert_mp3_to_wav(reencoded_mp3_path)
|
431 |
+
if not os.path.exists(wav_file_path):
|
432 |
+
update_progress(f"Converted WAV file not found: {wav_file_path}")
|
433 |
+
return update_progress("Processing failed: Converted WAV file not found"), "", ""
|
434 |
+
|
435 |
+
temp_files.append(wav_file_path)
|
436 |
+
|
437 |
+
# Initialize transcription
|
438 |
+
transcription = ""
|
439 |
+
|
440 |
+
if diarize:
|
441 |
+
segments = speech_to_text(wav_file_path, whisper_model=whisper_model, diarize=True)
|
442 |
+
else:
|
443 |
+
segments = speech_to_text(wav_file_path, whisper_model=whisper_model)
|
444 |
+
|
445 |
+
# Handle segments nested under 'segments' key
|
446 |
+
if isinstance(segments, dict) and 'segments' in segments:
|
447 |
+
segments = segments['segments']
|
448 |
+
|
449 |
+
if isinstance(segments, list):
|
450 |
+
transcription = " ".join([segment.get('Text', '') for segment in segments])
|
451 |
+
else:
|
452 |
+
update_progress("Unexpected segments format received from speech_to_text.")
|
453 |
+
logging.error(f"Unexpected segments format: {segments}")
|
454 |
+
|
455 |
+
chunked_text = improved_chunking_process(transcription, chunk_options)
|
456 |
+
|
457 |
+
if api_name and api_key:
|
458 |
+
try:
|
459 |
+
summary = perform_summarization(api_name, chunked_text, custom_prompt_input, api_key)
|
460 |
+
update_progress("Audio summarized successfully.")
|
461 |
+
except Exception as e:
|
462 |
+
logging.error(f"Error during summarization: {str(e)}")
|
463 |
+
summary = "Summary generation failed"
|
464 |
+
else:
|
465 |
+
summary = "No summary available (API not provided)"
|
466 |
+
|
467 |
+
all_transcriptions.append(transcription)
|
468 |
+
all_summaries.append(summary)
|
469 |
+
|
470 |
+
add_media_with_keywords(
|
471 |
+
url="Uploaded File",
|
472 |
+
title=os.path.basename(wav_file_path),
|
473 |
+
media_type='audio',
|
474 |
+
content=transcription,
|
475 |
+
keywords=custom_keywords,
|
476 |
+
prompt=custom_prompt_input,
|
477 |
+
summary=summary,
|
478 |
+
transcription_model=whisper_model,
|
479 |
+
author="Unknown",
|
480 |
+
ingestion_date=datetime.now().strftime('%Y-%m-%d')
|
481 |
+
)
|
482 |
+
update_progress("Uploaded file processed and added to database.")
|
483 |
+
|
484 |
+
# Final cleanup
|
485 |
+
if not keep_original:
|
486 |
+
cleanup_files()
|
487 |
+
|
488 |
+
final_progress = update_progress("All processing complete.")
|
489 |
+
final_transcriptions = "\n\n".join(all_transcriptions)
|
490 |
+
final_summaries = "\n\n".join(all_summaries)
|
491 |
+
|
492 |
+
return final_progress, final_transcriptions, final_summaries
|
493 |
+
|
494 |
+
except Exception as e:
|
495 |
+
logging.error(f"Error processing audio files: {str(e)}")
|
496 |
+
cleanup_files()
|
497 |
+
return update_progress(f"Processing failed: {str(e)}"), "", ""
|
498 |
+
|
499 |
+
|
500 |
+
def download_youtube_audio(url):
|
501 |
+
try:
|
502 |
+
# Determine ffmpeg path based on the operating system.
|
503 |
+
ffmpeg_path = './Bin/ffmpeg.exe' if os.name == 'nt' else 'ffmpeg'
|
504 |
+
|
505 |
+
# Create a temporary directory
|
506 |
+
with tempfile.TemporaryDirectory() as temp_dir:
|
507 |
+
# Extract information about the video
|
508 |
+
with yt_dlp.YoutubeDL({'quiet': True}) as ydl:
|
509 |
+
info_dict = ydl.extract_info(url, download=False)
|
510 |
+
sanitized_title = sanitize_filename(info_dict['title'])
|
511 |
+
|
512 |
+
# Setup the temporary filenames
|
513 |
+
temp_video_path = Path(temp_dir) / f"{sanitized_title}_temp.mp4"
|
514 |
+
temp_audio_path = Path(temp_dir) / f"{sanitized_title}.mp3"
|
515 |
+
|
516 |
+
# Initialize yt-dlp with options for downloading
|
517 |
+
ydl_opts = {
|
518 |
+
'format': 'bestaudio[ext=m4a]/best[height<=480]', # Prefer best audio, or video up to 480p
|
519 |
+
'ffmpeg_location': ffmpeg_path,
|
520 |
+
'outtmpl': str(temp_video_path),
|
521 |
+
'noplaylist': True,
|
522 |
+
'quiet': True
|
523 |
+
}
|
524 |
+
|
525 |
+
# Execute yt-dlp to download the video/audio
|
526 |
+
with yt_dlp.YoutubeDL(ydl_opts) as ydl:
|
527 |
+
ydl.download([url])
|
528 |
+
|
529 |
+
# Check if the file exists
|
530 |
+
if not temp_video_path.exists():
|
531 |
+
raise FileNotFoundError(f"Expected file was not found: {temp_video_path}")
|
532 |
+
|
533 |
+
# Use ffmpeg to extract audio
|
534 |
+
ffmpeg_command = [
|
535 |
+
ffmpeg_path,
|
536 |
+
'-i', str(temp_video_path),
|
537 |
+
'-vn', # No video
|
538 |
+
'-acodec', 'libmp3lame',
|
539 |
+
'-b:a', '192k',
|
540 |
+
str(temp_audio_path)
|
541 |
+
]
|
542 |
+
subprocess.run(ffmpeg_command, check=True, stdout=subprocess.DEVNULL, stderr=subprocess.DEVNULL)
|
543 |
+
|
544 |
+
# Check if the audio file was created
|
545 |
+
if not temp_audio_path.exists():
|
546 |
+
raise FileNotFoundError(f"Expected audio file was not found: {temp_audio_path}")
|
547 |
+
|
548 |
+
# Create a persistent directory for the download if it doesn't exist
|
549 |
+
persistent_dir = Path("downloads")
|
550 |
+
persistent_dir.mkdir(exist_ok=True)
|
551 |
+
|
552 |
+
# Move the file from the temporary directory to the persistent directory
|
553 |
+
persistent_file_path = persistent_dir / f"{sanitized_title}.mp3"
|
554 |
+
os.replace(str(temp_audio_path), str(persistent_file_path))
|
555 |
+
|
556 |
+
# Add the file to the list of downloaded files
|
557 |
+
downloaded_files.append(str(persistent_file_path))
|
558 |
+
|
559 |
+
return str(persistent_file_path), f"Audio downloaded successfully: {sanitized_title}.mp3"
|
560 |
+
except Exception as e:
|
561 |
+
return None, f"Error downloading audio: {str(e)}"
|
562 |
+
|
563 |
+
|
564 |
+
def process_podcast(url, title, author, keywords, custom_prompt, api_name, api_key, whisper_model,
|
565 |
+
keep_original=False, enable_diarization=False, use_cookies=False, cookies=None,
|
566 |
+
chunk_method=None, max_chunk_size=300, chunk_overlap=0, use_adaptive_chunking=False,
|
567 |
+
use_multi_level_chunking=False, chunk_language='english'):
|
568 |
+
progress = []
|
569 |
+
error_message = ""
|
570 |
+
temp_files = []
|
571 |
+
|
572 |
+
def update_progress(message):
|
573 |
+
progress.append(message)
|
574 |
+
return "\n".join(progress)
|
575 |
+
|
576 |
+
def cleanup_files():
|
577 |
+
if not keep_original:
|
578 |
+
for file in temp_files:
|
579 |
+
try:
|
580 |
+
if os.path.exists(file):
|
581 |
+
os.remove(file)
|
582 |
+
update_progress(f"Temporary file {file} removed.")
|
583 |
+
except Exception as e:
|
584 |
+
update_progress(f"Failed to remove temporary file {file}: {str(e)}")
|
585 |
+
|
586 |
+
try:
|
587 |
+
# Download podcast
|
588 |
+
audio_file = download_audio_file(url, use_cookies, cookies)
|
589 |
+
temp_files.append(audio_file)
|
590 |
+
update_progress("Podcast downloaded successfully.")
|
591 |
+
|
592 |
+
# Extract metadata
|
593 |
+
metadata = extract_metadata(url)
|
594 |
+
title = title or metadata.get('title', 'Unknown Podcast')
|
595 |
+
author = author or metadata.get('uploader', 'Unknown Author')
|
596 |
+
|
597 |
+
# Format metadata for storage
|
598 |
+
metadata_text = f"""
|
599 |
+
Metadata:
|
600 |
+
Title: {title}
|
601 |
+
Author: {author}
|
602 |
+
Series: {metadata.get('series', 'N/A')}
|
603 |
+
Episode: {metadata.get('episode', 'N/A')}
|
604 |
+
Season: {metadata.get('season', 'N/A')}
|
605 |
+
Upload Date: {metadata.get('upload_date', 'N/A')}
|
606 |
+
Duration: {metadata.get('duration', 'N/A')} seconds
|
607 |
+
Description: {metadata.get('description', 'N/A')}
|
608 |
+
"""
|
609 |
+
|
610 |
+
# Update keywords
|
611 |
+
new_keywords = []
|
612 |
+
if metadata.get('series'):
|
613 |
+
new_keywords.append(f"series:{metadata['series']}")
|
614 |
+
if metadata.get('episode'):
|
615 |
+
new_keywords.append(f"episode:{metadata['episode']}")
|
616 |
+
if metadata.get('season'):
|
617 |
+
new_keywords.append(f"season:{metadata['season']}")
|
618 |
+
|
619 |
+
keywords = f"{keywords},{','.join(new_keywords)}" if keywords else ','.join(new_keywords)
|
620 |
+
|
621 |
+
update_progress(f"Metadata extracted - Title: {title}, Author: {author}, Keywords: {keywords}")
|
622 |
+
|
623 |
+
# Transcribe the podcast
|
624 |
+
try:
|
625 |
+
if enable_diarization:
|
626 |
+
segments = speech_to_text(audio_file, whisper_model=whisper_model, diarize=True)
|
627 |
+
else:
|
628 |
+
segments = speech_to_text(audio_file, whisper_model=whisper_model)
|
629 |
+
transcription = " ".join([segment['Text'] for segment in segments])
|
630 |
+
update_progress("Podcast transcribed successfully.")
|
631 |
+
except Exception as e:
|
632 |
+
error_message = f"Transcription failed: {str(e)}"
|
633 |
+
raise
|
634 |
+
|
635 |
+
# Apply chunking
|
636 |
+
chunk_options = {
|
637 |
+
'method': chunk_method,
|
638 |
+
'max_size': max_chunk_size,
|
639 |
+
'overlap': chunk_overlap,
|
640 |
+
'adaptive': use_adaptive_chunking,
|
641 |
+
'multi_level': use_multi_level_chunking,
|
642 |
+
'language': chunk_language
|
643 |
+
}
|
644 |
+
chunked_text = improved_chunking_process(transcription, chunk_options)
|
645 |
+
|
646 |
+
# Combine metadata and transcription
|
647 |
+
full_content = metadata_text + "\n\nTranscription:\n" + transcription
|
648 |
+
|
649 |
+
# Summarize if API is provided
|
650 |
+
summary = None
|
651 |
+
if api_name and api_key:
|
652 |
+
try:
|
653 |
+
summary = perform_summarization(api_name, chunked_text, custom_prompt, api_key)
|
654 |
+
update_progress("Podcast summarized successfully.")
|
655 |
+
except Exception as e:
|
656 |
+
error_message = f"Summarization failed: {str(e)}"
|
657 |
+
raise
|
658 |
+
|
659 |
+
# Add to database
|
660 |
+
try:
|
661 |
+
add_media_with_keywords(
|
662 |
+
url=url,
|
663 |
+
title=title,
|
664 |
+
media_type='podcast',
|
665 |
+
content=full_content,
|
666 |
+
keywords=keywords,
|
667 |
+
prompt=custom_prompt,
|
668 |
+
summary=summary or "No summary available",
|
669 |
+
transcription_model=whisper_model,
|
670 |
+
author=author,
|
671 |
+
ingestion_date=datetime.now().strftime('%Y-%m-%d')
|
672 |
+
)
|
673 |
+
update_progress("Podcast added to database successfully.")
|
674 |
+
except Exception as e:
|
675 |
+
error_message = f"Error adding podcast to database: {str(e)}"
|
676 |
+
raise
|
677 |
+
|
678 |
+
# Cleanup
|
679 |
+
cleanup_files()
|
680 |
+
|
681 |
+
return (update_progress("Processing complete."), full_content, summary or "No summary generated.",
|
682 |
+
title, author, keywords, error_message)
|
683 |
+
|
684 |
+
except Exception as e:
|
685 |
+
logging.error(f"Error processing podcast: {str(e)}")
|
686 |
+
cleanup_files()
|
687 |
+
return update_progress(f"Processing failed: {str(e)}"), "", "", "", "", "", str(e)
|
688 |
+
|
689 |
+
|
690 |
+
#
|
691 |
+
#
|
692 |
#######################################################################################################################
|