Spaces:
Running
Running
File size: 59,717 Bytes
43cd37c c5b0bb7 43cd37c c5b0bb7 43cd37c c5b0bb7 43cd37c c5b0bb7 43cd37c c5b0bb7 43cd37c c5b0bb7 43cd37c c5b0bb7 43cd37c c5b0bb7 43cd37c c5b0bb7 43cd37c c5b0bb7 43cd37c c5b0bb7 43cd37c c5b0bb7 43cd37c c5b0bb7 43cd37c c5b0bb7 43cd37c c5b0bb7 43cd37c c5b0bb7 43cd37c c5b0bb7 43cd37c c5b0bb7 43cd37c c5b0bb7 43cd37c c5b0bb7 43cd37c c5b0bb7 43cd37c c5b0bb7 43cd37c c5b0bb7 43cd37c c5b0bb7 43cd37c c5b0bb7 43cd37c c5b0bb7 |
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 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 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 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 689 690 691 692 693 694 695 696 697 698 699 700 701 702 703 704 705 706 707 708 709 710 711 712 713 714 715 716 717 718 719 720 721 722 723 724 725 726 727 728 729 730 731 732 733 734 735 736 737 738 739 740 741 742 743 744 745 746 747 748 749 750 751 752 753 754 755 756 757 758 759 760 761 762 763 764 765 766 767 768 769 770 771 772 773 774 775 776 777 778 779 780 781 782 783 784 785 786 787 788 789 790 791 792 793 794 795 796 797 798 799 800 801 802 803 804 805 806 807 808 809 810 811 812 813 814 815 816 817 818 819 820 821 822 823 824 825 826 827 828 829 830 831 832 833 834 835 836 837 838 839 840 841 842 843 844 845 846 847 848 849 850 851 852 853 854 855 856 857 858 859 860 861 862 863 864 865 866 867 868 869 870 871 872 873 874 875 876 877 878 879 880 881 882 883 884 885 886 887 888 889 890 891 892 893 894 895 896 897 898 899 900 901 902 903 904 905 906 907 908 909 910 911 912 913 914 915 916 917 918 919 920 921 922 923 924 925 926 927 928 929 930 931 932 933 934 935 936 937 938 939 940 941 942 943 944 945 946 947 948 949 950 951 952 953 954 955 956 957 958 959 960 961 962 963 964 965 966 967 968 969 970 971 972 973 974 975 976 977 978 979 980 981 982 |
# Video_transcription_tab.py
# Description: This file contains the code for the video transcription tab in the Gradio UI.
#
# Imports
import json
import logging
import os
from datetime import datetime
#
# External Imports
import gradio as gr
import yt_dlp
from App_Function_Libraries.Chunk_Lib import improved_chunking_process
#
# Local Imports
from App_Function_Libraries.DB.DB_Manager import add_media_to_database, \
check_media_and_whisper_model, check_existing_media, update_media_content_with_version, list_prompts
from App_Function_Libraries.Gradio_UI.Gradio_Shared import whisper_models, update_user_prompt
from App_Function_Libraries.Gradio_UI.Gradio_Shared import error_handler
from App_Function_Libraries.Summarization.Summarization_General_Lib import perform_transcription, perform_summarization, \
save_transcription_and_summary
from App_Function_Libraries.Utils.Utils import convert_to_seconds, safe_read_file, format_transcription, \
create_download_directory, generate_unique_identifier, extract_text_from_segments, default_api_endpoint, \
global_api_endpoints, format_api_name
from App_Function_Libraries.Video_DL_Ingestion_Lib import parse_and_expand_urls, extract_metadata, download_video
from App_Function_Libraries.Benchmarks_Evaluations.ms_g_eval import run_geval
# Import metrics logging
from App_Function_Libraries.Metrics.metrics_logger import log_counter, log_histogram
#
#######################################################################################################################
#
# Functions:
def create_video_transcription_tab():
try:
default_value = None
if default_api_endpoint:
if default_api_endpoint in global_api_endpoints:
default_value = format_api_name(default_api_endpoint)
else:
logging.warning(f"Default API endpoint '{default_api_endpoint}' not found in global_api_endpoints")
except Exception as e:
logging.error(f"Error setting default API endpoint: {str(e)}")
default_value = None
with gr.TabItem("Video Transcription + Summarization", visible=True):
gr.Markdown("# Transcribe & Summarize Videos from URLs")
with gr.Row():
gr.Markdown("""Follow this project at [tldw - GitHub](https://github.com/rmusser01/tldw)""")
with gr.Row():
gr.Markdown(
"""If you're wondering what all this is, please see the 'Introduction/Help' tab up above for more detailed information and how to obtain an API Key.""")
with gr.Row():
with gr.Column():
url_input = gr.Textbox(label="URL(s) (Mandatory)",
placeholder="Enter video URLs here, one per line. Supports YouTube, Vimeo, other video sites and Youtube playlists.",
lines=5)
video_file_input = gr.File(label="Upload Video File (Optional)", file_types=["video/*"])
diarize_input = gr.Checkbox(label="Enable Speaker Diarization", value=False)
vad_checkbox = gr.Checkbox(label="Enable Voice-Audio-Detection(VAD)", value=True)
whisper_model_input = gr.Dropdown(choices=whisper_models, value="medium", label="Whisper Model")
with gr.Row():
custom_prompt_checkbox = gr.Checkbox(label="Use a Custom Prompt",
value=False,
visible=True)
preset_prompt_checkbox = gr.Checkbox(label="Use a pre-set Prompt",
value=False,
visible=True)
# Initialize state variables for pagination
current_page_state = gr.State(value=1)
total_pages_state = gr.State(value=1)
with gr.Row():
# Add pagination controls
preset_prompt = gr.Dropdown(label="Select Preset Prompt",
choices=[],
visible=False)
with gr.Row():
prev_page_button = gr.Button("Previous Page", visible=False)
page_display = gr.Markdown("Page 1 of X", visible=False)
next_page_button = gr.Button("Next Page", visible=False)
with gr.Row():
system_prompt_input = gr.Textbox(label="System Prompt",
value="""<s>You are a bulleted notes specialist. [INST]```When creating comprehensive bulleted notes, you should follow these guidelines: Use multiple headings based on the referenced topics, not categories like quotes or terms. Headings should be surrounded by bold formatting and not be listed as bullet points themselves. Leave no space between headings and their corresponding list items underneath. Important terms within the content should be emphasized by setting them in bold font. Any text that ends with a colon should also be bolded. Before submitting your response, review the instructions, and make any corrections necessary to adhered to the specified format. Do not reference these instructions within the notes.``` \nBased on the content between backticks create comprehensive bulleted notes.[/INST]
**Bulleted Note Creation Guidelines**
**Headings**:
- Based on referenced topics, not categories like quotes or terms
- Surrounded by **bold** formatting
- Not listed as bullet points
- No space between headings and list items underneath
**Emphasis**:
- **Important terms** set in bold font
- **Text ending in a colon**: also bolded
**Review**:
- Ensure adherence to specified format
- Do not reference these instructions in your response.</s>[INST] {{ .Prompt }} [/INST]
""",
lines=3,
visible=False,
interactive=True)
with gr.Row():
custom_prompt_input = gr.Textbox(label="Custom Prompt",
placeholder="Enter custom prompt here",
lines=3,
visible=False)
custom_prompt_checkbox.change(
fn=lambda x: (gr.update(visible=x, interactive=x), gr.update(visible=x, interactive=x)),
inputs=[custom_prompt_checkbox],
outputs=[custom_prompt_input, system_prompt_input]
)
def on_preset_prompt_checkbox_change(is_checked):
if is_checked:
prompts, total_pages, current_page = list_prompts(page=1, per_page=20)
page_display_text = f"Page {current_page} of {total_pages}"
return (
gr.update(visible=True, interactive=True, choices=prompts), # preset_prompt
gr.update(visible=True), # prev_page_button
gr.update(visible=True), # next_page_button
gr.update(value=page_display_text, visible=True), # page_display
current_page, # current_page_state
total_pages # total_pages_state
)
else:
return (
gr.update(visible=False, interactive=False), # preset_prompt
gr.update(visible=False), # prev_page_button
gr.update(visible=False), # next_page_button
gr.update(visible=False), # page_display
1, # current_page_state
1 # total_pages_state
)
preset_prompt_checkbox.change(
fn=on_preset_prompt_checkbox_change,
inputs=[preset_prompt_checkbox],
outputs=[preset_prompt, prev_page_button, next_page_button, page_display, current_page_state, total_pages_state]
)
def on_prev_page_click(current_page, total_pages):
new_page = max(current_page - 1, 1)
prompts, total_pages, current_page = list_prompts(page=new_page, per_page=20)
page_display_text = f"Page {current_page} of {total_pages}"
return gr.update(choices=prompts), gr.update(value=page_display_text), current_page
prev_page_button.click(
fn=on_prev_page_click,
inputs=[current_page_state, total_pages_state],
outputs=[preset_prompt, page_display, current_page_state]
)
def on_next_page_click(current_page, total_pages):
new_page = min(current_page + 1, total_pages)
prompts, total_pages, current_page = list_prompts(page=new_page, per_page=20)
page_display_text = f"Page {current_page} of {total_pages}"
return gr.update(choices=prompts), gr.update(value=page_display_text), current_page
next_page_button.click(
fn=on_next_page_click,
inputs=[current_page_state, total_pages_state],
outputs=[preset_prompt, page_display, current_page_state]
)
def update_prompts(preset_name):
prompts = update_user_prompt(preset_name)
return (
gr.update(value=prompts["user_prompt"], visible=True, interactive=True),
gr.update(value=prompts["system_prompt"], visible=True, interactive=True)
)
preset_prompt.change(
update_prompts,
inputs=preset_prompt,
outputs=[custom_prompt_input, system_prompt_input]
)
# Refactored API selection dropdown
api_name_input = gr.Dropdown(
choices=["None"] + [format_api_name(api) for api in global_api_endpoints],
value=default_value,
label="API for Summarization/Analysis (Optional)"
)
api_key_input = gr.Textbox(label="API Key (Optional - Set in Config.txt)", placeholder="Enter your API key here",
type="password")
keywords_input = gr.Textbox(label="Keywords", placeholder="Enter keywords here (comma-separated)",
value="default,no_keyword_set")
batch_size_input = gr.Slider(minimum=1, maximum=10, value=1, step=1,
label="Batch Size (Number of videos to process simultaneously)")
timestamp_option = gr.Checkbox(label="Include Timestamps", value=True)
keep_original_video = gr.Checkbox(label="Keep Original Video", value=False)
# First, create a checkbox to toggle the chunking options
chunking_options_checkbox = gr.Checkbox(label="Show Chunking Options", value=False)
summarize_recursively = gr.Checkbox(label="Enable Recursive Summarization", value=False)
use_cookies_input = gr.Checkbox(label="Use cookies for authenticated download", value=False)
use_time_input = gr.Checkbox(label="Use Start and End Time", value=False)
confab_checkbox = gr.Checkbox(label="Perform Confabulation Check of Summary", value=False)
overwrite_checkbox = gr.Checkbox(label="Overwrite Existing Media", value=False)
with gr.Row(visible=False) as time_input_box:
gr.Markdown("### Start and End time")
with gr.Column():
start_time_input = gr.Textbox(label="Start Time (Optional)",
placeholder="e.g., 1:30 or 90 (in seconds)")
end_time_input = gr.Textbox(label="End Time (Optional)",
placeholder="e.g., 5:45 or 345 (in seconds)")
use_time_input.change(
fn=lambda x: gr.update(visible=x),
inputs=[use_time_input],
outputs=[time_input_box]
)
cookies_input = gr.Textbox(
label="User Session Cookies",
placeholder="Paste your cookies here (JSON format)",
lines=3,
visible=False
)
use_cookies_input.change(
fn=lambda x: gr.update(visible=x),
inputs=[use_cookies_input],
outputs=[cookies_input]
)
# Then, create a Box to group the chunking options
with gr.Row(visible=False) as chunking_options_box:
gr.Markdown("### Chunking Options")
with gr.Column():
chunk_method = gr.Dropdown(choices=['words', 'sentences', 'paragraphs', 'tokens'],
label="Chunking Method")
max_chunk_size = gr.Slider(minimum=100, maximum=8000, value=400, step=1,
label="Max Chunk Size")
chunk_overlap = gr.Slider(minimum=0, maximum=5000, value=100, step=1, label="Chunk Overlap")
use_adaptive_chunking = gr.Checkbox(
label="Use Adaptive Chunking (Adjust chunking based on text complexity)")
use_multi_level_chunking = gr.Checkbox(label="Use Multi-level Chunking")
chunk_language = gr.Dropdown(choices=['english', 'french', 'german', 'spanish'],
label="Chunking Language")
# Add JavaScript to toggle the visibility of the chunking options box
chunking_options_checkbox.change(
fn=lambda x: gr.update(visible=x),
inputs=[chunking_options_checkbox],
outputs=[chunking_options_box]
)
process_button = gr.Button("Process Videos")
with gr.Column():
progress_output = gr.Textbox(label="Progress")
error_output = gr.Textbox(label="Errors", visible=False)
results_output = gr.HTML(label="Results")
confabulation_output = gr.Textbox(label="Confabulation Check Results", visible=False)
download_transcription = gr.File(label="Download All Transcriptions as JSON")
download_summary = gr.File(label="Download All Summaries as Text")
@error_handler
def process_videos_with_error_handling(inputs, start_time, end_time, diarize, vad_use, whisper_model,
custom_prompt_checkbox, custom_prompt, chunking_options_checkbox,
chunk_method, max_chunk_size, chunk_overlap, use_adaptive_chunking,
use_multi_level_chunking, chunk_language, api_name,
api_key, keywords, use_cookies, cookies, batch_size,
timestamp_option, keep_original_video, summarize_recursively, overwrite_existing=False,
progress: gr.Progress = gr.Progress()) -> tuple:
try:
# Start overall processing timer
proc_start_time = datetime.now()
logging.info("Entering process_videos_with_error_handling")
logging.info(f"Received inputs: {inputs}")
if not inputs:
raise ValueError("No inputs provided")
logging.debug("Input(s) is(are) valid")
# Ensure batch_size is an integer
try:
batch_size = int(batch_size)
except (ValueError, TypeError):
batch_size = 1 # Default to processing one video at a time if invalid
# Separate URLs and local files
urls = [input for input in inputs if
isinstance(input, str) and input.startswith(('http://', 'https://'))]
local_files = [input for input in inputs if
isinstance(input, str) and not input.startswith(('http://', 'https://'))]
# Parse and expand URLs if there are any
expanded_urls = parse_and_expand_urls(urls) if urls else []
valid_local_files = []
invalid_local_files = []
for file_path in local_files:
if os.path.exists(file_path):
valid_local_files.append(file_path)
else:
invalid_local_files.append(file_path)
error_message = f"Local file not found: {file_path}"
logging.error(error_message)
if invalid_local_files:
logging.warning(f"Found {len(invalid_local_files)} invalid local file paths")
# FIXME - Add more complete error handling for invalid local files
all_inputs = expanded_urls + valid_local_files
logging.info(f"Total valid inputs to process: {len(all_inputs)} "
f"({len(expanded_urls)} URLs, {len(valid_local_files)} local files)")
all_inputs = expanded_urls + local_files
logging.info(f"Total inputs to process: {len(all_inputs)}")
results = []
errors = []
results_html = ""
all_transcriptions = {}
all_summaries = ""
# Start timing
start_proc = datetime.now()
for i in range(0, len(all_inputs), batch_size):
batch = all_inputs[i:i + batch_size]
batch_results = []
for input_item in batch:
# Start individual video processing timer
video_start_time = datetime.now()
try:
start_seconds = convert_to_seconds(start_time)
end_seconds = convert_to_seconds(end_time) if end_time else None
logging.info(f"Attempting to extract metadata for {input_item}")
if input_item.startswith(('http://', 'https://')):
logging.info(f"Attempting to extract metadata for URL: {input_item}")
video_metadata = extract_metadata(input_item, use_cookies, cookies)
if not video_metadata:
raise ValueError(f"Failed to extract metadata for {input_item}")
else:
logging.info(f"Processing local file: {input_item}")
video_metadata = {"title": os.path.basename(input_item), "url": input_item}
chunk_options = {
'method': chunk_method,
'max_size': max_chunk_size,
'overlap': chunk_overlap,
'adaptive': use_adaptive_chunking,
'multi_level': use_multi_level_chunking,
'language': chunk_language
} if chunking_options_checkbox else None
if custom_prompt_checkbox:
custom_prompt = custom_prompt
else:
custom_prompt = ("""
<s>You are a bulleted notes specialist. [INST]```When creating comprehensive bulleted notes, you should follow these guidelines: Use multiple headings based on the referenced topics, not categories like quotes or terms. Headings should be surrounded by bold formatting and not be listed as bullet points themselves. Leave no space between headings and their corresponding list items underneath. Important terms within the content should be emphasized by setting them in bold font. Any text that ends with a colon should also be bolded. Before submitting your response, review the instructions, and make any corrections necessary to adhered to the specified format. Do not reference these instructions within the notes.``` \nBased on the content between backticks create comprehensive bulleted notes.[/INST]
**Bulleted Note Creation Guidelines**
**Headings**:
- Based on referenced topics, not categories like quotes or terms
- Surrounded by **bold** formatting
- Not listed as bullet points
- No space between headings and list items underneath
**Emphasis**:
- **Important terms** set in bold font
- **Text ending in a colon**: also bolded
**Review**:
- Ensure adherence to specified format
- Do not reference these instructions in your response.</s>[INST] {{ .Prompt }} [/INST]
""")
logging.debug("Gradio_Related.py: process_url_with_metadata being called")
# FIXME - Would assume this is where the multi-processing for recursive summarization would occur
result = process_url_with_metadata(
input_item, 2, whisper_model,
custom_prompt,
start_seconds, api_name, api_key,
vad_use, False, False, summarize_recursively, 0.01, None, keywords, None, diarize,
end_time=end_seconds,
include_timestamps=timestamp_option,
metadata=video_metadata,
use_chunking=chunking_options_checkbox,
chunk_options=chunk_options,
keep_original_video=keep_original_video,
current_whisper_model=whisper_model,
overwrite_existing=overwrite_existing
)
if result[0] is None:
error_message = "Processing failed without specific error"
batch_results.append(
(input_item, error_message, "Error", video_metadata, None, None))
errors.append(f"Error processing {input_item}: {error_message}")
# Log failure metric
log_counter(
metric_name="videos_failed_total",
labels={"whisper_model": whisper_model, "api_name": api_name},
value=1
)
else:
url, transcription, summary, json_file, summary_file, result_metadata = result
if transcription is None:
error_message = f"Processing failed for {input_item}: Transcription is None"
batch_results.append(
(input_item, error_message, "Error", result_metadata, None, None))
errors.append(error_message)
# Log failure metric
log_counter(
metric_name="videos_failed_total",
labels={"whisper_model": whisper_model, "api_name": api_name},
value=1
)
else:
batch_results.append(
(input_item, transcription, "Success", result_metadata, json_file,
summary_file))
# Log success metric
log_counter(
metric_name="videos_processed_total",
labels={"whisper_model": whisper_model, "api_name": api_name},
value=1
)
# Calculate processing time
video_end_time = datetime.now()
processing_time = (video_end_time - video_start_time).total_seconds()
log_histogram(
metric_name="video_processing_time_seconds",
value=processing_time,
labels={"whisper_model": whisper_model, "api_name": api_name}
)
# Log transcription and summary metrics
if transcription:
log_counter(
metric_name="transcriptions_generated_total",
labels={"whisper_model": whisper_model},
value=1
)
if summary:
log_counter(
metric_name="summaries_generated_total",
labels={"whisper_model": whisper_model},
value=1
)
except Exception as e:
# Log failure
log_counter(
metric_name="videos_failed_total",
labels={"whisper_model": whisper_model, "api_name": api_name},
value=1
)
error_message = f"Error processing {input_item}: {str(e)}"
logging.error(error_message, exc_info=True)
batch_results.append((input_item, error_message, "Error", {}, None, None))
errors.append(error_message)
results.extend(batch_results)
logging.debug(f"Processed {len(batch_results)} videos in batch")
if isinstance(progress, gr.Progress):
progress((i + len(batch)) / len(all_inputs),
f"Processed {i + len(batch)}/{len(all_inputs)} videos")
# Generate HTML for results
logging.debug(f"Generating HTML for {len(results)} results")
for url, transcription, status, metadata, json_file, summary_file in results:
if status == "Success":
title = metadata.get('title', 'Unknown Title')
# Check if transcription is a string (which it should be now)
if isinstance(transcription, str):
# Split the transcription into metadata and actual transcription
parts = transcription.split('\n\n', 1)
if len(parts) == 2:
metadata_text, transcription_text = parts
else:
metadata_text = "Metadata not found"
transcription_text = transcription
else:
metadata_text = "Metadata format error"
transcription_text = "Transcription format error"
summary = safe_read_file(summary_file) if summary_file else "No summary available"
# FIXME - Add to other functions that generate HTML
# Format the transcription
formatted_transcription = format_transcription(transcription_text)
# Format the summary
formatted_summary = format_transcription(summary)
results_html += f"""
<div class="result-box">
<gradio-accordion>
<gradio-accordion-item label="{title}">
<p><strong>URL:</strong> <a href="{url}" target="_blank">{url}</a></p>
<h4>Metadata:</h4>
<pre>{metadata_text}</pre>
<h4>Transcription:</h4>
<div class="transcription" style="white-space: pre-wrap; word-wrap: break-word;">
{formatted_transcription}
</div>
<h4>Summary:</h4>
<div class="summary">{formatted_summary}</div>
</gradio-accordion-item>
</gradio-accordion>
</div>
"""
logging.debug(f"Transcription for {url}: {transcription[:200]}...")
all_transcriptions[url] = transcription
all_summaries += f"Title: {title}\nURL: {url}\n\n{metadata_text}\n\nTranscription:\n{transcription_text}\n\nSummary:\n{summary}\n\n---\n\n"
else:
results_html += f"""
<div class="result-box error">
<h3>Error processing {url}</h3>
<p>{transcription}</p>
</div>
"""
# Save all transcriptions and summaries to files
logging.debug("Saving all transcriptions and summaries to files")
with open('all_transcriptions.json', 'w', encoding='utf-8') as f:
json.dump(all_transcriptions, f, indent=2, ensure_ascii=False)
with open('all_summaries.txt', 'w', encoding='utf-8') as f:
f.write(all_summaries)
error_summary = "\n".join(errors) if errors else "No errors occurred."
total_inputs = len(all_inputs)
# End overall processing timer
proc_end_time = datetime.now()
total_processing_time = (proc_end_time - proc_start_time).total_seconds()
log_histogram(
metric_name="total_processing_time_seconds",
value=total_processing_time,
labels={"whisper_model": whisper_model, "api_name": api_name}
)
return (
f"Processed {total_inputs} videos. {len(errors)} errors occurred.",
error_summary,
results_html,
'all_transcriptions.json',
'all_summaries.txt'
)
except Exception as e:
logging.error(f"Unexpected error in process_videos_with_error_handling: {str(e)}", exc_info=True)
# Log unexpected failure metric
log_counter(
metric_name="videos_failed_total",
labels={"whisper_model": whisper_model, "api_name": api_name},
value=1
)
return (
f"An unexpected error occurred: {str(e)}",
str(e),
"<div class='result-box error'><h3>Unexpected Error</h3><p>" + str(e) + "</p></div>",
None,
None
)
def process_videos_wrapper(url_input, video_file, start_time, end_time, diarize, vad_use, whisper_model,
custom_prompt_checkbox, custom_prompt, chunking_options_checkbox,
chunk_method, max_chunk_size, chunk_overlap, use_adaptive_chunking,
use_multi_level_chunking, chunk_language, summarize_recursively, api_name,
api_key, keywords, use_cookies, cookies, batch_size,
timestamp_option, keep_original_video, confab_checkbox, overwrite_existing=False):
global result
try:
logging.info("process_videos_wrapper(): process_videos_wrapper called")
# Define file paths
transcriptions_file = os.path.join('all_transcriptions.json')
summaries_file = os.path.join('all_summaries.txt')
# Delete existing files if they exist
for file_path in [transcriptions_file, summaries_file]:
try:
if os.path.exists(file_path):
os.remove(file_path)
logging.info(f"Deleted existing file: {file_path}")
except Exception as e:
logging.warning(f"Failed to delete file {file_path}: {str(e)}")
# Handle both URL input and file upload
inputs = []
if url_input:
inputs.extend([url.strip() for url in url_input.split('\n') if url.strip()])
if video_file is not None:
# Assuming video_file is a file object with a 'name' attribute
inputs.append(video_file.name)
if not inputs:
raise ValueError("No input provided. Please enter URLs or upload a video file.")
result = process_videos_with_error_handling(
inputs, start_time, end_time, diarize, vad_use, whisper_model,
custom_prompt_checkbox, custom_prompt, chunking_options_checkbox,
chunk_method, max_chunk_size, chunk_overlap, use_adaptive_chunking,
use_multi_level_chunking, chunk_language, api_name,
api_key, keywords, use_cookies, cookies, batch_size,
timestamp_option, keep_original_video, summarize_recursively, overwrite_existing
)
confabulation_result = None
if confab_checkbox:
logging.info("Confabulation check enabled")
# Assuming result[1] contains the transcript and result[2] contains the summary
confabulation_result = run_geval(result[1], result[2], api_key, api_name)
logging.info(f"Simplified G-Eval result: {confabulation_result}")
# Ensure that result is a tuple with 5 elements
if not isinstance(result, tuple) or len(result) != 5:
raise ValueError(
f"process_videos_wrapper(): Expected 5 outputs, but got {len(result) if isinstance(result, tuple) else 1}")
# Return the confabulation result along with other outputs
return (*result, confabulation_result)
except Exception as e:
logging.error(f"process_videos_wrapper(): Error in process_videos_wrapper: {str(e)}", exc_info=True)
# Return a tuple with 6 elements in case of any error (including None for simple_geval_result)
return (
f"process_videos_wrapper(): An error occurred: {str(e)}", # progress_output
str(e), # error_output
f"<div class='error'>Error: {str(e)}</div>", # results_output
None, # download_transcription
None, # download_summary
None # simple_geval_result
)
# FIXME - remove dead args for process_url_with_metadata
@error_handler
def process_url_with_metadata(input_item, num_speakers, whisper_model, custom_prompt, offset, api_name,
api_key, vad_filter, download_video_flag, download_audio,
rolling_summarization,
detail_level, question_box, keywords, local_file_path, diarize, end_time=None,
include_timestamps=True, metadata=None, use_chunking=False,
chunk_options=None, keep_original_video=False, current_whisper_model="Blank", overwrite_existing=False):
try:
logging.info(f"Starting process_url_metadata for URL: {input_item}")
# Create download path
download_path = create_download_directory("Video_Downloads")
logging.info(f"Download path created at: {download_path}")
# Initialize info_dict
info_dict = {}
# Handle URL or local file
if os.path.isfile(input_item):
video_file_path = input_item
unique_id = generate_unique_identifier(input_item)
# Extract basic info from local file
info_dict = {
'webpage_url': unique_id,
'title': os.path.basename(input_item),
'description': "Local file",
'channel_url': None,
'duration': None,
'channel': None,
'uploader': None,
'upload_date': None
}
else:
# Extract video information
with yt_dlp.YoutubeDL({'quiet': True}) as ydl:
try:
full_info = ydl.extract_info(input_item, download=False)
# Create a safe subset of info to log
safe_info = {
'title': full_info.get('title', 'No title'),
'duration': full_info.get('duration', 'Unknown duration'),
'upload_date': full_info.get('upload_date', 'Unknown upload date'),
'uploader': full_info.get('uploader', 'Unknown uploader'),
'view_count': full_info.get('view_count', 'Unknown view count')
}
logging.debug(f"Full info extracted for {input_item}: {safe_info}")
except Exception as e:
logging.error(f"Error extracting video info: {str(e)}")
return None, None, None, None, None, None
# Filter the required metadata
if full_info:
info_dict = {
'webpage_url': full_info.get('webpage_url', input_item),
'title': full_info.get('title'),
'description': full_info.get('description'),
'channel_url': full_info.get('channel_url'),
'duration': full_info.get('duration'),
'channel': full_info.get('channel'),
'uploader': full_info.get('uploader'),
'upload_date': full_info.get('upload_date')
}
logging.debug(f"Filtered info_dict: {info_dict}")
else:
logging.error("Failed to extract video information")
return None, None, None, None, None, None
# FIXME - MAKE SURE THIS WORKS WITH LOCAL FILES
# FIXME - Add a toggle to force processing even if media exists
# Check if media already exists in the database
logging.info("Checking if media already exists in the database...")
media_exists, reason = check_media_and_whisper_model(
title=info_dict.get('title'),
url=info_dict.get('webpage_url'),
current_whisper_model=current_whisper_model
)
if not media_exists:
logging.info(
f"process_url_with_metadata: Media does not exist in the database. Reason: {reason}")
else:
if "same whisper model" in reason:
logging.info(
f"process_url_with_metadata: Skipping download and processing as media exists and uses the same Whisper model. Reason: {reason}")
return input_item, None, None, None, None, info_dict
else:
logging.info(
f"process_url_with_metadata: Media found, but with a different Whisper model. Reason: {reason}")
# Download video/audio
logging.info("Downloading video/audio...")
video_file_path = download_video(input_item, download_path, full_info, download_video_flag,
current_whisper_model=current_whisper_model)
if video_file_path is None:
logging.info(
f"process_url_with_metadata: Download skipped for {input_item}. Media might already exist or be processed.")
return input_item, None, None, None, None, info_dict
# FIXME - add check for existing media with different whisper model for local files
# FIXME Check to make sure this works
media_exists, reason = check_media_and_whisper_model(
title=info_dict.get('title'),
url=info_dict.get('webpage_url'),
current_whisper_model=current_whisper_model
)
if not media_exists:
logging.info(
f"process_url_with_metadata: Media does not exist in the database. Reason: {reason}")
else:
if "same whisper model" in reason:
logging.info(
f"process_url_with_metadata: Skipping download and processing as media exists and uses the same Whisper model. Reason: {reason}")
return input_item, None, None, None, None, info_dict
else:
same_whisper_model = True
logging.info(
f"process_url_with_metadata: Media found, but with a different Whisper model. Reason: {reason}")
logging.info(f"process_url_with_metadata: Processing file: {video_file_path}")
# Perform transcription
logging.info("process_url_with_metadata: Starting transcription...")
logging.info(f"process_url_with_metadata: overwrite existing?: {overwrite_existing}")
audio_file_path, segments = perform_transcription(video_file_path, offset, whisper_model,
vad_filter, diarize, overwrite_existing)
if audio_file_path is None or segments is None:
logging.error("process_url_with_metadata: Transcription failed or segments not available.")
return None, None, None, None, None, None
logging.info(f"process_url_with_metadata: Transcription completed. Number of segments: {len(segments)}")
# Add metadata to segments
segments_with_metadata = {
"metadata": info_dict,
"segments": segments
}
# Save segments with metadata to JSON file
segments_json_path = os.path.splitext(audio_file_path)[0] + ".segments.json"
with open(segments_json_path, 'w') as f:
json.dump(segments_with_metadata, f, indent=2)
# Delete the .wav file after successful transcription
files_to_delete = [audio_file_path]
for file_path in files_to_delete:
if file_path and os.path.exists(file_path):
try:
os.remove(file_path)
logging.info(f"process_url_with_metadata: Successfully deleted file: {file_path}")
except Exception as e:
logging.warning(f"process_url_with_metadata: Failed to delete file {file_path}: {str(e)}")
# Delete the mp4 file after successful transcription if not keeping original audio
# Modify the file deletion logic to respect keep_original_video
if not keep_original_video:
files_to_delete = [audio_file_path, video_file_path]
for file_path in files_to_delete:
if file_path and os.path.exists(file_path):
try:
os.remove(file_path)
logging.info(f"process_url_with_metadata: Successfully deleted file: {file_path}")
except Exception as e:
logging.warning(f"process_url_with_metadata: Failed to delete file {file_path}: {str(e)}")
else:
logging.info(f"process_url_with_metadata: Keeping original video file: {video_file_path}")
logging.info(f"process_url_with_metadata: Keeping original audio file: {audio_file_path}")
# Process segments based on the timestamp option
if not include_timestamps:
segments = [{'Text': segment['Text']} for segment in segments]
logging.info(f"Segments processed for timestamp inclusion: {segments}")
# Extract text from segments
transcription_text = extract_text_from_segments(segments)
if transcription_text.startswith("Error:"):
logging.error(f"process_url_with_metadata: Failed to extract transcription: {transcription_text}")
return None, None, None, None, None, None
# Use transcription_text instead of segments for further processing
full_text_with_metadata = f"{json.dumps(info_dict, indent=2)}\n\n{transcription_text}"
logging.debug(f"process_url_with_metadata: Full text with metadata extracted: {full_text_with_metadata[:100]}...")
# Perform summarization if API is provided
summary_text = None
if api_name:
# API key resolution handled at base of function if none provided
api_key = api_key if api_key else None
logging.info(f"process_url_with_metadata: Starting summarization with {api_name}...")
# Perform Chunking if enabled
# FIXME - Setup a proper prompt for Recursive Summarization
if use_chunking:
logging.info("process_url_with_metadata: Chunking enabled. Starting chunking...")
chunked_texts = improved_chunking_process(full_text_with_metadata, chunk_options)
if chunked_texts is None:
logging.warning("Chunking failed, falling back to full text summarization")
summary_text = perform_summarization(api_name, full_text_with_metadata, custom_prompt,
api_key)
else:
logging.debug(
f"process_url_with_metadata: Chunking completed. Processing {len(chunked_texts)} chunks...")
summaries = []
if rolling_summarization:
# Perform recursive summarization on each chunk
for chunk in chunked_texts:
chunk_summary = perform_summarization(api_name, chunk['text'], custom_prompt,
api_key)
if chunk_summary:
summaries.append(
f"Chunk {chunk['metadata']['chunk_index']}/{chunk['metadata']['total_chunks']}: {chunk_summary}")
summary_text = "\n\n".join(summaries)
else:
logging.error("All chunk summarizations failed")
summary_text = None
for chunk in chunked_texts:
# Perform Non-recursive summarization on each chunk
chunk_summary = perform_summarization(api_name, chunk['text'], custom_prompt,
api_key)
if chunk_summary:
summaries.append(
f"Chunk {chunk['metadata']['chunk_index']}/{chunk['metadata']['total_chunks']}: {chunk_summary}")
if summaries:
summary_text = "\n\n".join(summaries)
logging.info(f"Successfully summarized {len(summaries)} chunks")
else:
logging.error("All chunk summarizations failed")
summary_text = None
else:
# Regular summarization without chunking
summary_text = perform_summarization(api_name, full_text_with_metadata, custom_prompt,
api_key) if api_name else None
if summary_text is None:
logging.error("Summarization failed.")
return None, None, None, None, None, None
logging.debug(f"process_url_with_metadata: Summarization completed: {summary_text[:100]}...")
# Save transcription and summary
logging.info("process_url_with_metadata: Saving transcription and summary...")
download_path = create_download_directory("Audio_Processing")
json_file_path, summary_file_path = save_transcription_and_summary(full_text_with_metadata,
summary_text,
download_path, info_dict)
logging.info(f"process_url_with_metadata: Transcription saved to: {json_file_path}")
logging.info(f"process_url_with_metadata: Summary saved to: {summary_file_path}")
# Prepare keywords for database
if isinstance(keywords, str):
keywords_list = [kw.strip() for kw in keywords.split(',') if kw.strip()]
elif isinstance(keywords, (list, tuple)):
keywords_list = keywords
else:
keywords_list = []
logging.info(f"process_url_with_metadata: Keywords prepared: {keywords_list}")
existing_media = check_existing_media(info_dict['webpage_url'])
if existing_media:
# Update existing media with new version
media_id = existing_media['id']
update_result = update_media_content_with_version(media_id, info_dict, full_text_with_metadata,
custom_prompt, summary_text, whisper_model)
logging.info(f"process_url_with_metadata: {update_result}")
else:
# Add new media to database
add_result = add_media_to_database(info_dict['webpage_url'], info_dict, full_text_with_metadata,
summary_text,
keywords_list, custom_prompt, whisper_model)
logging.info(f"process_url_with_metadata: {add_result}")
return info_dict[
'webpage_url'], full_text_with_metadata, summary_text, json_file_path, summary_file_path, info_dict
except Exception as e:
logging.error(f"Error in process_url_with_metadata: {str(e)}", exc_info=True)
return None, None, None, None, None, None
def toggle_confabulation_output(checkbox_value):
return gr.update(visible=checkbox_value)
confab_checkbox.change(
fn=toggle_confabulation_output,
inputs=[confab_checkbox],
outputs=[confabulation_output]
)
process_button.click(
fn=process_videos_wrapper,
inputs=[
url_input,
video_file_input,
start_time_input,
end_time_input,
diarize_input,
vad_checkbox,
whisper_model_input,
custom_prompt_checkbox,
custom_prompt_input,
chunking_options_checkbox,
chunk_method,
max_chunk_size,
chunk_overlap,
use_adaptive_chunking,
use_multi_level_chunking,
chunk_language,
summarize_recursively,
api_name_input,
api_key_input,
keywords_input,
use_cookies_input,
cookies_input,
batch_size_input,
timestamp_option,
keep_original_video,
confab_checkbox,
overwrite_checkbox
],
outputs=[progress_output, error_output, results_output, download_transcription, download_summary, confabulation_output]
)
#
# End of Video_transcription_tab.py
#######################################################################################################################
|