Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
|
@@ -1,5 +1,5 @@
|
|
| 1 |
# app.py
|
| 2 |
-
# Whisper Transcriber — Gradio 3.x compatible complete file
|
| 3 |
# Requirements: gradio (3.x), pydub, pyzipper, python-docx, ffmpeg, whisper or faster-whisper
|
| 4 |
|
| 5 |
import os
|
|
@@ -59,8 +59,12 @@ FFMPEG_CANDIDATES = [
|
|
| 59 |
MODEL_CACHE = {}
|
| 60 |
EXTRACT_MAP = {} # friendly_name -> path
|
| 61 |
LAST_EXTRACT_DIR = None # path to last extraction folder (for download)
|
|
|
|
| 62 |
DEFAULT_ZIP_PASS = "dietcoke1"
|
| 63 |
|
|
|
|
|
|
|
|
|
|
| 64 |
CPU_COUNT = max(1, multiprocessing.cpu_count())
|
| 65 |
MAX_WORKERS = min(4, CPU_COUNT) # tune for your environment
|
| 66 |
|
|
@@ -470,9 +474,10 @@ def extract_zip_and_map(zip_path, zip_password=None):
|
|
| 470 |
and set LAST_EXTRACT_DIR to the extraction folder for download.
|
| 471 |
Returns (friendly_list, logs_str)
|
| 472 |
"""
|
| 473 |
-
global EXTRACT_MAP, LAST_EXTRACT_DIR
|
| 474 |
EXTRACT_MAP = {}
|
| 475 |
LAST_EXTRACT_DIR = None
|
|
|
|
| 476 |
run_id = uuid4().hex
|
| 477 |
temp_extract_dir = os.path.join(tempfile.gettempdir(), f"extracted_audio_{run_id}")
|
| 478 |
logs = []
|
|
@@ -525,6 +530,7 @@ def extract_zip_and_map(zip_path, zip_password=None):
|
|
| 525 |
return [], "\n".join(logs)
|
| 526 |
friendly = sorted(EXTRACT_MAP.keys())
|
| 527 |
LAST_EXTRACT_DIR = temp_extract_dir
|
|
|
|
| 528 |
return friendly, "\n".join(logs)
|
| 529 |
except Exception as e:
|
| 530 |
traceback.print_exc()
|
|
@@ -556,6 +562,76 @@ def download_extracted_folder():
|
|
| 556 |
except Exception as e:
|
| 557 |
return None, f"Failed to create ZIP: {e}"
|
| 558 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 559 |
# ---------- Batch transcription generator (streaming) ----------
|
| 560 |
def batch_transcribe_parallel_generator(
|
| 561 |
friendly_selected,
|
|
@@ -570,6 +646,8 @@ def batch_transcribe_parallel_generator(
|
|
| 570 |
refine_threshold=-1.0,
|
| 571 |
zip_password=None,
|
| 572 |
):
|
|
|
|
|
|
|
| 573 |
logs = []
|
| 574 |
transcripts = []
|
| 575 |
per_file_paths = []
|
|
@@ -626,6 +704,9 @@ def batch_transcribe_parallel_generator(
|
|
| 626 |
pct = int(5 + (completed / total) * 90)
|
| 627 |
yield "\n\n".join(logs), "\n\n".join(transcripts), None, pct
|
| 628 |
|
|
|
|
|
|
|
|
|
|
| 629 |
combined = "\n\n".join(transcripts)
|
| 630 |
out_doc = None
|
| 631 |
if merge_flag:
|
|
@@ -856,6 +937,7 @@ body { background: var(--bg); color: var(--text); font-family: Inter, system-ui,
|
|
| 856 |
.card { background:var(--card); border-radius:10px; padding:12px; box-shadow: 0 6px 20px rgba(16,24,40,0.04); }
|
| 857 |
.transcript-area { white-space:pre-wrap; font-family: ui-monospace, SFMono-Regular, Menlo, Monaco, "Roboto Mono", monospace; background: var(--transcript-bg); color: var(--transcript-color); padding:12px; border-radius:8px; min-height:200px; }
|
| 858 |
.small-note { color:var(--muted); font-size:12px;}
|
|
|
|
| 859 |
"""
|
| 860 |
|
| 861 |
with gr.Blocks(title="Whisper Transcriber — Parallel + Memory", css=CSS) as demo:
|
|
@@ -891,7 +973,8 @@ with gr.Blocks(title="Whisper Transcriber — Parallel + Memory", css=CSS) as de
|
|
| 891 |
trans_single_btn = gr.Button("Transcribe")
|
| 892 |
with gr.Column(scale=1):
|
| 893 |
single_trans_out = gr.Textbox(label="Transcript", lines=14, interactive=False)
|
| 894 |
-
|
|
|
|
| 895 |
|
| 896 |
def _do_single(audio, model_name, device_name, mem_on, srt_on):
|
| 897 |
if not audio:
|
|
@@ -918,15 +1001,28 @@ with gr.Blocks(title="Whisper Transcriber — Parallel + Memory", css=CSS) as de
|
|
| 918 |
batch_files = gr.File(label="Upload audio files", file_count="multiple", type="filepath")
|
| 919 |
batch_zip = gr.File(label="Or upload ZIP (optional)", file_count="single", type="filepath")
|
| 920 |
batch_zip_pass = gr.Textbox(label="ZIP password (if any)", value=DEFAULT_ZIP_PASS)
|
|
|
|
| 921 |
batch_preview_btn = gr.Button("Extract & List ZIP files")
|
| 922 |
-
batch_preview_out = gr.Textbox(label="ZIP members (preview)", lines=
|
| 923 |
batch_select = gr.CheckboxGroup(choices=[], label="Select extracted files to include", interactive=True)
|
| 924 |
-
#
|
|
|
|
|
|
|
|
|
|
| 925 |
batch_download_extracted_btn = gr.Button("Download extracted folder")
|
| 926 |
batch_extracted_zip = gr.File(label="Downloaded extracted ZIP")
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 927 |
batch_model = gr.Dropdown(choices=available_choices, value=default_choice, label="Model")
|
| 928 |
batch_device = gr.Dropdown(choices=["auto", "cpu", "cuda"], value="auto", label="Device")
|
| 929 |
-
batch_merge = gr.Checkbox(label="Merge transcripts into DOCX", value=True)
|
| 930 |
batch_mem = gr.Checkbox(label="Enable memory corrections", value=False)
|
| 931 |
batch_srt = gr.Checkbox(label="Generate SRTs", value=False)
|
| 932 |
batch_use_two_pass = gr.Checkbox(label="Use two-pass refinement", value=False)
|
|
@@ -934,37 +1030,70 @@ with gr.Blocks(title="Whisper Transcriber — Parallel + Memory", css=CSS) as de
|
|
| 934 |
batch_refine_thresh = gr.Number(value=-1.0, label="Refine threshold", precision=2)
|
| 935 |
batch_run_btn = gr.Button("Start Batch (parallel)")
|
| 936 |
with gr.Column(scale=1):
|
| 937 |
-
batch_logs_out = gr.Textbox(label="Logs", lines=12, interactive=False)
|
| 938 |
batch_combined_out = gr.Textbox(label="Combined transcripts", lines=12, interactive=False)
|
| 939 |
batch_progress = gr.Slider(minimum=0, maximum=100, value=0, step=1, label="Progress (%)", interactive=False)
|
| 940 |
batch_zip_download = gr.File(label="Download per-file transcripts ZIP")
|
| 941 |
batch_doc_download = gr.File(label="Download merged DOCX (if created)")
|
|
|
|
|
|
|
| 942 |
|
| 943 |
def _preview_zip_and_populate(zip_file, password):
|
| 944 |
"""
|
| 945 |
-
Extract the zip, populate EXTRACT_MAP and return updated CheckboxGroup choices +
|
| 946 |
"""
|
| 947 |
if not zip_file:
|
| 948 |
return gr.update(choices=[]), "No ZIP provided."
|
| 949 |
path = zip_file.name if hasattr(zip_file, "name") else str(zip_file)
|
| 950 |
friendly, logs = extract_zip_and_map(path, password)
|
| 951 |
if friendly:
|
| 952 |
-
|
| 953 |
-
return gr.update(choices=friendly), "\n".join(logs.splitlines())
|
| 954 |
return gr.update(choices=[]), logs
|
| 955 |
|
| 956 |
-
# wire preview/extract button to update the batch_select choices and preview textbox
|
| 957 |
batch_preview_btn.click(fn=_preview_zip_and_populate, inputs=[batch_zip, batch_zip_pass], outputs=[batch_select, batch_preview_out])
|
| 958 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 959 |
def _download_extracted_wrapper():
|
| 960 |
zip_path, msg = download_extracted_folder()
|
| 961 |
if zip_path:
|
| 962 |
return zip_path
|
| 963 |
-
# gr.File expects path or None; if failed, return None so nothing is downloadable
|
| 964 |
return None
|
| 965 |
|
| 966 |
batch_download_extracted_btn.click(fn=_download_extracted_wrapper, inputs=[], outputs=[batch_extracted_zip])
|
| 967 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 968 |
# wrapper generator — Gradio expects the function itself to be a generator that yields streaming tuples
|
| 969 |
def _start_batch(friendly_selected, uploaded_files, zip_file, zip_pass, model_name, device_name, merge_flag, mem_flag, srt_flag, use_two_pass, fast_model, refine_thresh):
|
| 970 |
up = uploaded_files
|
|
@@ -998,10 +1127,12 @@ with gr.Blocks(title="Whisper Transcriber — Parallel + Memory", css=CSS) as de
|
|
| 998 |
with gr.Column(scale=1):
|
| 999 |
mem_upload = gr.File(label="Upload memory files or ZIP (multiple)", file_count="multiple", type="filepath")
|
| 1000 |
mem_preview_zip_btn = gr.Button("Preview ZIP members (for selected ZIPs)")
|
| 1001 |
-
mem_zip_preview_out = gr.Textbox(label="ZIP members (preview)", lines=
|
| 1002 |
mem_zip_select = gr.CheckboxGroup(choices=[], label="Select ZIP members to import", interactive=True)
|
|
|
|
|
|
|
| 1003 |
mem_import_btn = gr.Button("Import selected files / uploaded files")
|
| 1004 |
-
mem_status = gr.Textbox(label="Import status", lines=
|
| 1005 |
mem_textbox = gr.Textbox(label="Add single word/phrase", placeholder="Type word or phrase")
|
| 1006 |
mem_add_btn = gr.Button("Add to memory")
|
| 1007 |
mem_clear_btn = gr.Button("Clear memory")
|
|
@@ -1012,6 +1143,8 @@ with gr.Blocks(title="Whisper Transcriber — Parallel + Memory", css=CSS) as de
|
|
| 1012 |
"- Supported encodings: utf-8, utf-16, latin-1, fallback.\n"
|
| 1013 |
"- JSON format: {\"words\":{\"word\":count}, \"phrases\":{\"phrase\":count}}"
|
| 1014 |
)
|
|
|
|
|
|
|
| 1015 |
|
| 1016 |
def _preview_many_zip(uploaded):
|
| 1017 |
if not uploaded:
|
|
@@ -1024,11 +1157,22 @@ with gr.Blocks(title="Whisper Transcriber — Parallel + Memory", css=CSS) as de
|
|
| 1024 |
members, log = preview_zip_members_for_memory(str(f))
|
| 1025 |
members_total.extend(members)
|
| 1026 |
if members_total:
|
|
|
|
| 1027 |
return "\n".join(members_total)
|
| 1028 |
return "No ZIPs found or no previewable members."
|
| 1029 |
|
| 1030 |
mem_preview_zip_btn.click(fn=_preview_many_zip, inputs=[mem_upload], outputs=[mem_zip_preview_out])
|
| 1031 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1032 |
def _import_mem(uploaded_files, selected_members):
|
| 1033 |
try:
|
| 1034 |
status = import_memory_files_multiple(uploaded_files, zip_members_to_import=selected_members)
|
|
|
|
| 1 |
# app.py
|
| 2 |
+
# Whisper Transcriber — Gradio 3.x compatible complete file with "merge last batch transcripts" feature
|
| 3 |
# Requirements: gradio (3.x), pydub, pyzipper, python-docx, ffmpeg, whisper or faster-whisper
|
| 4 |
|
| 5 |
import os
|
|
|
|
| 59 |
MODEL_CACHE = {}
|
| 60 |
EXTRACT_MAP = {} # friendly_name -> path
|
| 61 |
LAST_EXTRACT_DIR = None # path to last extraction folder (for download)
|
| 62 |
+
LAST_EXTRACT_LIST = [] # friendly names for last extraction (for select all)
|
| 63 |
DEFAULT_ZIP_PASS = "dietcoke1"
|
| 64 |
|
| 65 |
+
# NEW: last batch transcripts (set by batch generator). Each item: (friendly_name, txt_path, srt_path)
|
| 66 |
+
LAST_BATCH_TRANSCRIPTS = []
|
| 67 |
+
|
| 68 |
CPU_COUNT = max(1, multiprocessing.cpu_count())
|
| 69 |
MAX_WORKERS = min(4, CPU_COUNT) # tune for your environment
|
| 70 |
|
|
|
|
| 474 |
and set LAST_EXTRACT_DIR to the extraction folder for download.
|
| 475 |
Returns (friendly_list, logs_str)
|
| 476 |
"""
|
| 477 |
+
global EXTRACT_MAP, LAST_EXTRACT_DIR, LAST_EXTRACT_LIST
|
| 478 |
EXTRACT_MAP = {}
|
| 479 |
LAST_EXTRACT_DIR = None
|
| 480 |
+
LAST_EXTRACT_LIST = []
|
| 481 |
run_id = uuid4().hex
|
| 482 |
temp_extract_dir = os.path.join(tempfile.gettempdir(), f"extracted_audio_{run_id}")
|
| 483 |
logs = []
|
|
|
|
| 530 |
return [], "\n".join(logs)
|
| 531 |
friendly = sorted(EXTRACT_MAP.keys())
|
| 532 |
LAST_EXTRACT_DIR = temp_extract_dir
|
| 533 |
+
LAST_EXTRACT_LIST = friendly[:]
|
| 534 |
return friendly, "\n".join(logs)
|
| 535 |
except Exception as e:
|
| 536 |
traceback.print_exc()
|
|
|
|
| 562 |
except Exception as e:
|
| 563 |
return None, f"Failed to create ZIP: {e}"
|
| 564 |
|
| 565 |
+
# ---------- Merge uploaded text files into single Word file ----------
|
| 566 |
+
def merge_text_files_to_docx(uploaded_text_files):
|
| 567 |
+
"""
|
| 568 |
+
Accepts a list of uploaded text file paths (or single path), merges them in order into one .docx and returns path.
|
| 569 |
+
"""
|
| 570 |
+
if not uploaded_text_files:
|
| 571 |
+
return None, "No files provided."
|
| 572 |
+
if isinstance(uploaded_text_files, (str, os.PathLike)):
|
| 573 |
+
uploaded_text_files = [str(uploaded_text_files)]
|
| 574 |
+
elif isinstance(uploaded_text_files, dict) and uploaded_text_files.get("name"):
|
| 575 |
+
uploaded_text_files = [uploaded_text_files["name"]]
|
| 576 |
+
elif isinstance(uploaded_text_files, (list, tuple)):
|
| 577 |
+
normalized = []
|
| 578 |
+
for f in uploaded_text_files:
|
| 579 |
+
if isinstance(f, (str, os.PathLike)):
|
| 580 |
+
normalized.append(str(f))
|
| 581 |
+
elif isinstance(f, dict) and f.get("name"):
|
| 582 |
+
normalized.append(f["name"])
|
| 583 |
+
elif hasattr(f, "name"):
|
| 584 |
+
normalized.append(f.name)
|
| 585 |
+
uploaded_text_files = normalized
|
| 586 |
+
|
| 587 |
+
combined = []
|
| 588 |
+
for p in uploaded_text_files:
|
| 589 |
+
if not os.path.exists(p):
|
| 590 |
+
continue
|
| 591 |
+
try:
|
| 592 |
+
with open(p, "r", encoding="utf-8") as fh:
|
| 593 |
+
txt = fh.read()
|
| 594 |
+
except Exception:
|
| 595 |
+
with open(p, "r", encoding="latin-1", errors="replace") as fh:
|
| 596 |
+
txt = fh.read()
|
| 597 |
+
header = f"\n\n--- {os.path.basename(p)} ---\n\n"
|
| 598 |
+
combined.append(header + txt)
|
| 599 |
+
if not combined:
|
| 600 |
+
return None, "No readable text files."
|
| 601 |
+
merged_text = "\n".join(combined)
|
| 602 |
+
out_path = save_as_word(merged_text)
|
| 603 |
+
return out_path, "Merged"
|
| 604 |
+
|
| 605 |
+
# ---------- NEW: merge last batch transcripts ----------
|
| 606 |
+
def merge_last_batch_transcripts():
|
| 607 |
+
"""
|
| 608 |
+
Merge txt transcripts created by the last batch run (LAST_BATCH_TRANSCRIPTS) into a single .docx.
|
| 609 |
+
Returns (path_or_None, message)
|
| 610 |
+
"""
|
| 611 |
+
global LAST_BATCH_TRANSCRIPTS
|
| 612 |
+
if not LAST_BATCH_TRANSCRIPTS:
|
| 613 |
+
return None, "No last-batch transcripts available."
|
| 614 |
+
combined = []
|
| 615 |
+
for fname, txtp, srtp in LAST_BATCH_TRANSCRIPTS:
|
| 616 |
+
if not txtp or not os.path.exists(txtp):
|
| 617 |
+
continue
|
| 618 |
+
try:
|
| 619 |
+
with open(txtp, "r", encoding="utf-8", errors="replace") as fh:
|
| 620 |
+
txt = fh.read()
|
| 621 |
+
except Exception:
|
| 622 |
+
try:
|
| 623 |
+
with open(txtp, "r", encoding="latin-1", errors="replace") as fh:
|
| 624 |
+
txt = fh.read()
|
| 625 |
+
except Exception:
|
| 626 |
+
txt = ""
|
| 627 |
+
header = f"\n\n--- {fname} ---\n\n"
|
| 628 |
+
combined.append(header + txt)
|
| 629 |
+
if not combined:
|
| 630 |
+
return None, "No readable last-batch transcript files found."
|
| 631 |
+
merged_text = "\n".join(combined)
|
| 632 |
+
out_path = save_as_word(merged_text)
|
| 633 |
+
return out_path, f"Merged {len(combined)} files."
|
| 634 |
+
|
| 635 |
# ---------- Batch transcription generator (streaming) ----------
|
| 636 |
def batch_transcribe_parallel_generator(
|
| 637 |
friendly_selected,
|
|
|
|
| 646 |
refine_threshold=-1.0,
|
| 647 |
zip_password=None,
|
| 648 |
):
|
| 649 |
+
global LAST_BATCH_TRANSCRIPTS
|
| 650 |
+
LAST_BATCH_TRANSCRIPTS = [] # reset at start
|
| 651 |
logs = []
|
| 652 |
transcripts = []
|
| 653 |
per_file_paths = []
|
|
|
|
| 704 |
pct = int(5 + (completed / total) * 90)
|
| 705 |
yield "\n\n".join(logs), "\n\n".join(transcripts), None, pct
|
| 706 |
|
| 707 |
+
# Save per-file transcript list into global for later merging/downloading
|
| 708 |
+
LAST_BATCH_TRANSCRIPTS = per_file_paths[:]
|
| 709 |
+
|
| 710 |
combined = "\n\n".join(transcripts)
|
| 711 |
out_doc = None
|
| 712 |
if merge_flag:
|
|
|
|
| 937 |
.card { background:var(--card); border-radius:10px; padding:12px; box-shadow: 0 6px 20px rgba(16,24,40,0.04); }
|
| 938 |
.transcript-area { white-space:pre-wrap; font-family: ui-monospace, SFMono-Regular, Menlo, Monaco, "Roboto Mono", monospace; background: var(--transcript-bg); color: var(--transcript-color); padding:12px; border-radius:8px; min-height:200px; }
|
| 939 |
.small-note { color:var(--muted); font-size:12px;}
|
| 940 |
+
.btn-row { display:flex; gap:8px; margin-top:8px; }
|
| 941 |
"""
|
| 942 |
|
| 943 |
with gr.Blocks(title="Whisper Transcriber — Parallel + Memory", css=CSS) as demo:
|
|
|
|
| 973 |
trans_single_btn = gr.Button("Transcribe")
|
| 974 |
with gr.Column(scale=1):
|
| 975 |
single_trans_out = gr.Textbox(label="Transcript", lines=14, interactive=False)
|
| 976 |
+
# LOGS at bottom
|
| 977 |
+
single_logs = gr.Textbox(label="Logs", lines=6, interactive=False)
|
| 978 |
|
| 979 |
def _do_single(audio, model_name, device_name, mem_on, srt_on):
|
| 980 |
if not audio:
|
|
|
|
| 1001 |
batch_files = gr.File(label="Upload audio files", file_count="multiple", type="filepath")
|
| 1002 |
batch_zip = gr.File(label="Or upload ZIP (optional)", file_count="single", type="filepath")
|
| 1003 |
batch_zip_pass = gr.Textbox(label="ZIP password (if any)", value=DEFAULT_ZIP_PASS)
|
| 1004 |
+
# Extract and populate list
|
| 1005 |
batch_preview_btn = gr.Button("Extract & List ZIP files")
|
| 1006 |
+
batch_preview_out = gr.Textbox(label="ZIP members (preview)", lines=4, interactive=False)
|
| 1007 |
batch_select = gr.CheckboxGroup(choices=[], label="Select extracted files to include", interactive=True)
|
| 1008 |
+
# select-all / clear buttons
|
| 1009 |
+
batch_select_all_btn = gr.Button("Select All extracted")
|
| 1010 |
+
batch_clear_select_btn = gr.Button("Clear selection")
|
| 1011 |
+
# Download extracted and merge uploaded texts
|
| 1012 |
batch_download_extracted_btn = gr.Button("Download extracted folder")
|
| 1013 |
batch_extracted_zip = gr.File(label="Downloaded extracted ZIP")
|
| 1014 |
+
gr.Markdown("### Merge text files")
|
| 1015 |
+
merge_text_files_input = gr.File(label="Upload text files to merge (.txt/.srt/.json)", file_count="multiple", type="filepath")
|
| 1016 |
+
merge_text_btn = gr.Button("Merge uploaded text files -> DOCX")
|
| 1017 |
+
merge_text_out = gr.File(label="Merged DOCX download")
|
| 1018 |
+
# NEW: Merge last batch transcripts
|
| 1019 |
+
merge_last_batch_btn = gr.Button("Merge Last Batch Transcripts")
|
| 1020 |
+
merge_last_batch_status = gr.Textbox(label="Last-batch merge status", lines=3, interactive=False)
|
| 1021 |
+
merge_last_batch_download = gr.File(label="Merged last-batch DOCX")
|
| 1022 |
+
# Transcription parameters
|
| 1023 |
batch_model = gr.Dropdown(choices=available_choices, value=default_choice, label="Model")
|
| 1024 |
batch_device = gr.Dropdown(choices=["auto", "cpu", "cuda"], value="auto", label="Device")
|
| 1025 |
+
batch_merge = gr.Checkbox(label="Merge transcripts into DOCX after run", value=True)
|
| 1026 |
batch_mem = gr.Checkbox(label="Enable memory corrections", value=False)
|
| 1027 |
batch_srt = gr.Checkbox(label="Generate SRTs", value=False)
|
| 1028 |
batch_use_two_pass = gr.Checkbox(label="Use two-pass refinement", value=False)
|
|
|
|
| 1030 |
batch_refine_thresh = gr.Number(value=-1.0, label="Refine threshold", precision=2)
|
| 1031 |
batch_run_btn = gr.Button("Start Batch (parallel)")
|
| 1032 |
with gr.Column(scale=1):
|
|
|
|
| 1033 |
batch_combined_out = gr.Textbox(label="Combined transcripts", lines=12, interactive=False)
|
| 1034 |
batch_progress = gr.Slider(minimum=0, maximum=100, value=0, step=1, label="Progress (%)", interactive=False)
|
| 1035 |
batch_zip_download = gr.File(label="Download per-file transcripts ZIP")
|
| 1036 |
batch_doc_download = gr.File(label="Download merged DOCX (if created)")
|
| 1037 |
+
# Logs at bottom
|
| 1038 |
+
batch_logs_out = gr.Textbox(label="Logs", lines=8, interactive=False)
|
| 1039 |
|
| 1040 |
def _preview_zip_and_populate(zip_file, password):
|
| 1041 |
"""
|
| 1042 |
+
Extract the zip, populate EXTRACT_MAP and return updated CheckboxGroup choices + preview text.
|
| 1043 |
"""
|
| 1044 |
if not zip_file:
|
| 1045 |
return gr.update(choices=[]), "No ZIP provided."
|
| 1046 |
path = zip_file.name if hasattr(zip_file, "name") else str(zip_file)
|
| 1047 |
friendly, logs = extract_zip_and_map(path, password)
|
| 1048 |
if friendly:
|
| 1049 |
+
return gr.update(choices=friendly), "\n".join(friendly)
|
|
|
|
| 1050 |
return gr.update(choices=[]), logs
|
| 1051 |
|
|
|
|
| 1052 |
batch_preview_btn.click(fn=_preview_zip_and_populate, inputs=[batch_zip, batch_zip_pass], outputs=[batch_select, batch_preview_out])
|
| 1053 |
|
| 1054 |
+
def _select_all_batch():
|
| 1055 |
+
# uses LAST_EXTRACT_LIST set by extract
|
| 1056 |
+
global LAST_EXTRACT_LIST
|
| 1057 |
+
if LAST_EXTRACT_LIST:
|
| 1058 |
+
return gr.update(value=LAST_EXTRACT_LIST)
|
| 1059 |
+
return gr.update(value=[])
|
| 1060 |
+
|
| 1061 |
+
batch_select_all_btn.click(fn=_select_all_batch, inputs=[], outputs=[batch_select])
|
| 1062 |
+
|
| 1063 |
+
def _clear_batch_select():
|
| 1064 |
+
return gr.update(value=[])
|
| 1065 |
+
|
| 1066 |
+
batch_clear_select_btn.click(fn=_clear_batch_select, inputs=[], outputs=[batch_select])
|
| 1067 |
+
|
| 1068 |
def _download_extracted_wrapper():
|
| 1069 |
zip_path, msg = download_extracted_folder()
|
| 1070 |
if zip_path:
|
| 1071 |
return zip_path
|
|
|
|
| 1072 |
return None
|
| 1073 |
|
| 1074 |
batch_download_extracted_btn.click(fn=_download_extracted_wrapper, inputs=[], outputs=[batch_extracted_zip])
|
| 1075 |
|
| 1076 |
+
def _merge_texts(uploaded_texts):
|
| 1077 |
+
if not uploaded_texts:
|
| 1078 |
+
return None, "No files provided."
|
| 1079 |
+
out_path, msg = merge_text_files_to_docx(uploaded_texts)
|
| 1080 |
+
if out_path:
|
| 1081 |
+
return out_path
|
| 1082 |
+
return None, msg
|
| 1083 |
+
|
| 1084 |
+
merge_text_btn.click(fn=_merge_texts, inputs=[merge_text_files_input], outputs=[merge_text_out])
|
| 1085 |
+
|
| 1086 |
+
def _merge_last_batch_action():
|
| 1087 |
+
"""
|
| 1088 |
+
Merge last batch transcripts (global LAST_BATCH_TRANSCRIPTS) into docx and return file path.
|
| 1089 |
+
"""
|
| 1090 |
+
path, msg = merge_last_batch_transcripts()
|
| 1091 |
+
if path:
|
| 1092 |
+
return path, msg
|
| 1093 |
+
return None, msg
|
| 1094 |
+
|
| 1095 |
+
merge_last_batch_btn.click(fn=_merge_last_batch_action, inputs=[], outputs=[merge_last_batch_download, merge_last_batch_status])
|
| 1096 |
+
|
| 1097 |
# wrapper generator — Gradio expects the function itself to be a generator that yields streaming tuples
|
| 1098 |
def _start_batch(friendly_selected, uploaded_files, zip_file, zip_pass, model_name, device_name, merge_flag, mem_flag, srt_flag, use_two_pass, fast_model, refine_thresh):
|
| 1099 |
up = uploaded_files
|
|
|
|
| 1127 |
with gr.Column(scale=1):
|
| 1128 |
mem_upload = gr.File(label="Upload memory files or ZIP (multiple)", file_count="multiple", type="filepath")
|
| 1129 |
mem_preview_zip_btn = gr.Button("Preview ZIP members (for selected ZIPs)")
|
| 1130 |
+
mem_zip_preview_out = gr.Textbox(label="ZIP members (preview)", lines=4, interactive=False)
|
| 1131 |
mem_zip_select = gr.CheckboxGroup(choices=[], label="Select ZIP members to import", interactive=True)
|
| 1132 |
+
mem_select_all_btn = gr.Button("Select All members")
|
| 1133 |
+
mem_clear_select_btn = gr.Button("Clear selection")
|
| 1134 |
mem_import_btn = gr.Button("Import selected files / uploaded files")
|
| 1135 |
+
mem_status = gr.Textbox(label="Import status", lines=6, interactive=False)
|
| 1136 |
mem_textbox = gr.Textbox(label="Add single word/phrase", placeholder="Type word or phrase")
|
| 1137 |
mem_add_btn = gr.Button("Add to memory")
|
| 1138 |
mem_clear_btn = gr.Button("Clear memory")
|
|
|
|
| 1143 |
"- Supported encodings: utf-8, utf-16, latin-1, fallback.\n"
|
| 1144 |
"- JSON format: {\"words\":{\"word\":count}, \"phrases\":{\"phrase\":count}}"
|
| 1145 |
)
|
| 1146 |
+
# Logs at bottom
|
| 1147 |
+
mem_logs = gr.Textbox(label="Logs", lines=6, interactive=False)
|
| 1148 |
|
| 1149 |
def _preview_many_zip(uploaded):
|
| 1150 |
if not uploaded:
|
|
|
|
| 1157 |
members, log = preview_zip_members_for_memory(str(f))
|
| 1158 |
members_total.extend(members)
|
| 1159 |
if members_total:
|
| 1160 |
+
# set mem_zip_select choices via update
|
| 1161 |
return "\n".join(members_total)
|
| 1162 |
return "No ZIPs found or no previewable members."
|
| 1163 |
|
| 1164 |
mem_preview_zip_btn.click(fn=_preview_many_zip, inputs=[mem_upload], outputs=[mem_zip_preview_out])
|
| 1165 |
|
| 1166 |
+
def _select_all_mem():
|
| 1167 |
+
# try to use preview box content (not ideal) — but we stored last extract list globally as LAST_EXTRACT_LIST
|
| 1168 |
+
global LAST_EXTRACT_LIST
|
| 1169 |
+
if LAST_EXTRACT_LIST:
|
| 1170 |
+
return gr.update(value=LAST_EXTRACT_LIST)
|
| 1171 |
+
return gr.update(value=[])
|
| 1172 |
+
|
| 1173 |
+
mem_select_all_btn.click(fn=_select_all_mem, inputs=[], outputs=[mem_zip_select])
|
| 1174 |
+
mem_clear_select_btn.click(fn=_clear_batch_select, inputs=[], outputs=[mem_zip_select])
|
| 1175 |
+
|
| 1176 |
def _import_mem(uploaded_files, selected_members):
|
| 1177 |
try:
|
| 1178 |
status = import_memory_files_multiple(uploaded_files, zip_members_to_import=selected_members)
|