Spaces:
Running
on
Zero
Running
on
Zero
adding app.py and supporting files
Browse filesSigned-off-by: monica-sekoyan <[email protected]>
- .gitattributes +3 -0
- README.md +5 -5
- app.py +409 -0
- data/example-yt_saTD1u8PorI.mp3 +3 -0
- packages.txt +2 -0
- requirements.txt +2 -0
- supported_languages.py +27 -0
.gitattributes
CHANGED
|
@@ -33,3 +33,6 @@ saved_model/**/* filter=lfs diff=lfs merge=lfs -text
|
|
| 33 |
*.zip filter=lfs diff=lfs merge=lfs -text
|
| 34 |
*.zst filter=lfs diff=lfs merge=lfs -text
|
| 35 |
*tfevents* filter=lfs diff=lfs merge=lfs -text
|
|
|
|
|
|
|
|
|
|
|
|
| 33 |
*.zip filter=lfs diff=lfs merge=lfs -text
|
| 34 |
*.zst filter=lfs diff=lfs merge=lfs -text
|
| 35 |
*tfevents* filter=lfs diff=lfs merge=lfs -text
|
| 36 |
+
*.mp3 filter=lfs diff=lfs merge=lfs -text
|
| 37 |
+
*.wav filter=lfs diff=lfs merge=lfs -text
|
| 38 |
+
*.nemo filter=lfs diff=lfs merge=lfs -text
|
README.md
CHANGED
|
@@ -1,14 +1,14 @@
|
|
| 1 |
---
|
| 2 |
title: Canary 1b V2
|
| 3 |
-
emoji:
|
| 4 |
-
colorFrom:
|
| 5 |
-
colorTo:
|
| 6 |
sdk: gradio
|
| 7 |
sdk_version: 5.42.0
|
| 8 |
app_file: app.py
|
| 9 |
pinned: false
|
| 10 |
license: cc-by-4.0
|
| 11 |
-
short_description:
|
| 12 |
---
|
| 13 |
|
| 14 |
-
Check out the configuration reference at https://huggingface.co/docs/hub/spaces-config-reference
|
|
|
|
| 1 |
---
|
| 2 |
title: Canary 1b V2
|
| 3 |
+
emoji: π€πͺπΊ
|
| 4 |
+
colorFrom: orange
|
| 5 |
+
colorTo: red
|
| 6 |
sdk: gradio
|
| 7 |
sdk_version: 5.42.0
|
| 8 |
app_file: app.py
|
| 9 |
pinned: false
|
| 10 |
license: cc-by-4.0
|
| 11 |
+
short_description: Transcribe and Translate in 25 European Languages
|
| 12 |
---
|
| 13 |
|
| 14 |
+
Check out the configuration reference at https://huggingface.co/docs/hub/spaces-config-reference
|
app.py
ADDED
|
@@ -0,0 +1,409 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
from nemo.collections.asr.models import ASRModel
|
| 2 |
+
import torch
|
| 3 |
+
import gradio as gr
|
| 4 |
+
import spaces
|
| 5 |
+
import gc
|
| 6 |
+
import shutil
|
| 7 |
+
from pathlib import Path
|
| 8 |
+
from pydub import AudioSegment
|
| 9 |
+
import numpy as np
|
| 10 |
+
import os
|
| 11 |
+
import gradio.themes as gr_themes
|
| 12 |
+
import csv
|
| 13 |
+
from supported_languages import SUPPORTED_LANGS_MAP
|
| 14 |
+
|
| 15 |
+
device = "cuda" if torch.cuda.is_available() else "cpu"
|
| 16 |
+
MODEL_NAME="nvidia/canary-1b-v2"
|
| 17 |
+
|
| 18 |
+
model = ASRModel.from_pretrained(model_name=MODEL_NAME)
|
| 19 |
+
model.eval()
|
| 20 |
+
|
| 21 |
+
AVAILABLE_SRC_LANGS = list(SUPPORTED_LANGS_MAP.keys())
|
| 22 |
+
DEFAULT_SRC_LANG = "English"
|
| 23 |
+
AVAILABLE_TGT_LANGS = list(SUPPORTED_LANGS_MAP.keys())
|
| 24 |
+
DEFAULT_TGT_LANG = "English"
|
| 25 |
+
|
| 26 |
+
|
| 27 |
+
def start_session(request: gr.Request):
|
| 28 |
+
session_hash = request.session_hash
|
| 29 |
+
session_dir = Path(f'/tmp/{session_hash}')
|
| 30 |
+
session_dir.mkdir(parents=True, exist_ok=True)
|
| 31 |
+
|
| 32 |
+
print(f"Session with hash {session_hash} started.")
|
| 33 |
+
return session_dir.as_posix()
|
| 34 |
+
|
| 35 |
+
def end_session(request: gr.Request):
|
| 36 |
+
session_hash = request.session_hash
|
| 37 |
+
session_dir = Path(f'/tmp/{session_hash}')
|
| 38 |
+
|
| 39 |
+
if session_dir.exists():
|
| 40 |
+
shutil.rmtree(session_dir)
|
| 41 |
+
|
| 42 |
+
print(f"Session with hash {session_hash} ended.")
|
| 43 |
+
|
| 44 |
+
|
| 45 |
+
def update_src_lang_dropdown(selected_value):
|
| 46 |
+
if selected_value == DEFAULT_SRC_LANG:
|
| 47 |
+
tgt_langs = AVAILABLE_TGT_LANGS
|
| 48 |
+
default_tgt_lang = DEFAULT_TGT_LANG
|
| 49 |
+
else:
|
| 50 |
+
tgt_langs = [DEFAULT_TGT_LANG, selected_value]
|
| 51 |
+
default_tgt_lang = selected_value
|
| 52 |
+
return gr.Dropdown(choices=tgt_langs, value=default_tgt_lang, interactive=True)
|
| 53 |
+
|
| 54 |
+
def update_button_intstruction(src_lang, tgt_lang):
|
| 55 |
+
if src_lang == tgt_lang:
|
| 56 |
+
instruction = "Transcribe"
|
| 57 |
+
else:
|
| 58 |
+
instruction = "Translate"
|
| 59 |
+
return (gr.Button(f"{instruction} Uploaded File", variant="primary"), gr.Button(f"{instruction} Microphone Input", variant="primary"))
|
| 60 |
+
|
| 61 |
+
|
| 62 |
+
def get_audio_segment(audio_path, start_second, end_second):
|
| 63 |
+
if not audio_path or not Path(audio_path).exists():
|
| 64 |
+
print(f"Warning: Audio path '{audio_path}' not found or invalid for clipping.")
|
| 65 |
+
return None
|
| 66 |
+
try:
|
| 67 |
+
start_ms = int(start_second * 1000)
|
| 68 |
+
end_ms = int(end_second * 1000)
|
| 69 |
+
|
| 70 |
+
start_ms = max(0, start_ms)
|
| 71 |
+
if end_ms <= start_ms:
|
| 72 |
+
print(f"Warning: End time ({end_second}s) is not after start time ({start_second}s). Adjusting end time.")
|
| 73 |
+
end_ms = start_ms + 100
|
| 74 |
+
|
| 75 |
+
audio = AudioSegment.from_file(audio_path)
|
| 76 |
+
clipped_audio = audio[start_ms:end_ms]
|
| 77 |
+
|
| 78 |
+
samples = np.array(clipped_audio.get_array_of_samples())
|
| 79 |
+
if clipped_audio.channels == 2:
|
| 80 |
+
samples = samples.reshape((-1, 2)).mean(axis=1).astype(samples.dtype)
|
| 81 |
+
|
| 82 |
+
frame_rate = clipped_audio.frame_rate
|
| 83 |
+
if frame_rate <= 0:
|
| 84 |
+
print(f"Warning: Invalid frame rate ({frame_rate}) detected for clipped audio.")
|
| 85 |
+
frame_rate = audio.frame_rate
|
| 86 |
+
|
| 87 |
+
if samples.size == 0:
|
| 88 |
+
print(f"Warning: Clipped audio resulted in empty samples array ({start_second}s to {end_second}s).")
|
| 89 |
+
return None
|
| 90 |
+
|
| 91 |
+
return (frame_rate, samples)
|
| 92 |
+
except FileNotFoundError:
|
| 93 |
+
print(f"Error: Audio file not found at path: {audio_path}")
|
| 94 |
+
return None
|
| 95 |
+
except Exception as e:
|
| 96 |
+
print(f"Error clipping audio {audio_path} from {start_second}s to {end_second}s: {e}")
|
| 97 |
+
return None
|
| 98 |
+
|
| 99 |
+
@spaces.GPU
|
| 100 |
+
def get_transcripts_and_raw_times(audio_path, session_dir, source_lang, target_lang):
|
| 101 |
+
if not audio_path:
|
| 102 |
+
gr.Error("No audio file path provided for transcription.", duration=None)
|
| 103 |
+
# Return an update to hide the button
|
| 104 |
+
return [], [], None, gr.DownloadButton(visible=False)
|
| 105 |
+
|
| 106 |
+
vis_data = [["N/A", "N/A", "Processing failed"]]
|
| 107 |
+
raw_times_data = [[0.0, 0.0]]
|
| 108 |
+
processed_audio_path = None
|
| 109 |
+
csv_file_path = None
|
| 110 |
+
original_path_name = Path(audio_path).name
|
| 111 |
+
audio_name = Path(audio_path).stem
|
| 112 |
+
|
| 113 |
+
try:
|
| 114 |
+
try:
|
| 115 |
+
gr.Info(f"Loading audio: {original_path_name}", duration=2)
|
| 116 |
+
audio = AudioSegment.from_file(audio_path)
|
| 117 |
+
print('Audio loaded successfully')
|
| 118 |
+
except Exception as load_e:
|
| 119 |
+
gr.Error(f"Failed to load audio file {original_path_name}: {load_e}", duration=None)
|
| 120 |
+
# Return an update to hide the button
|
| 121 |
+
return [["Error", "Error", "Load failed"]], [[0.0, 0.0]], audio_path, gr.DownloadButton(visible=False)
|
| 122 |
+
|
| 123 |
+
resampled = False
|
| 124 |
+
mono = False
|
| 125 |
+
|
| 126 |
+
target_sr = 16000
|
| 127 |
+
if audio.frame_rate != target_sr:
|
| 128 |
+
try:
|
| 129 |
+
audio = audio.set_frame_rate(target_sr)
|
| 130 |
+
resampled = True
|
| 131 |
+
except Exception as resample_e:
|
| 132 |
+
gr.Error(f"Failed to resample audio: {resample_e}", duration=None)
|
| 133 |
+
# Return an update to hide the button
|
| 134 |
+
return [["Error", "Error", "Resample failed"]], [[0.0, 0.0]], audio_path, gr.DownloadButton(visible=False)
|
| 135 |
+
|
| 136 |
+
if audio.channels == 2:
|
| 137 |
+
try:
|
| 138 |
+
audio = audio.set_channels(1)
|
| 139 |
+
mono = True
|
| 140 |
+
except Exception as mono_e:
|
| 141 |
+
gr.Error(f"Failed to convert audio to mono: {mono_e}", duration=None)
|
| 142 |
+
# Return an update to hide the button
|
| 143 |
+
return [["Error", "Error", "Mono conversion failed"]], [[0.0, 0.0]], audio_path, gr.DownloadButton(visible=False)
|
| 144 |
+
elif audio.channels > 2:
|
| 145 |
+
gr.Error(f"Audio has {audio.channels} channels. Only mono (1) or stereo (2) supported.", duration=None)
|
| 146 |
+
# Return an update to hide the button
|
| 147 |
+
return [["Error", "Error", f"{audio.channels}-channel audio not supported"]], [[0.0, 0.0]], audio_path, gr.DownloadButton(visible=False)
|
| 148 |
+
|
| 149 |
+
if resampled or mono:
|
| 150 |
+
try:
|
| 151 |
+
processed_audio_path = Path(session_dir, f"{audio_name}_resampled.wav")
|
| 152 |
+
audio.export(processed_audio_path, format="wav")
|
| 153 |
+
transcribe_path = processed_audio_path.as_posix()
|
| 154 |
+
info_path_name = f"{original_path_name} (processed)"
|
| 155 |
+
except Exception as export_e:
|
| 156 |
+
gr.Error(f"Failed to export processed audio: {export_e}", duration=None)
|
| 157 |
+
if processed_audio_path and os.path.exists(processed_audio_path):
|
| 158 |
+
os.remove(processed_audio_path)
|
| 159 |
+
# Return an update to hide the button
|
| 160 |
+
return [["Error", "Error", "Export failed"]], [[0.0, 0.0]], audio_path, gr.DownloadButton(visible=False)
|
| 161 |
+
else:
|
| 162 |
+
transcribe_path = audio_path
|
| 163 |
+
info_path_name = original_path_name
|
| 164 |
+
|
| 165 |
+
try:
|
| 166 |
+
model.to(device)
|
| 167 |
+
gr.Info(f"Transcribing {info_path_name} on {device}...", duration=2)
|
| 168 |
+
output = model.transcribe([transcribe_path], timestamps=True, source_lang=SUPPORTED_LANGS_MAP[source_lang], target_lang=SUPPORTED_LANGS_MAP[target_lang])
|
| 169 |
+
|
| 170 |
+
if not output or not isinstance(output, list) or not output[0] or not hasattr(output[0], 'timestamp') or not output[0].timestamp or 'segment' not in output[0].timestamp:
|
| 171 |
+
gr.Error("Transcription failed or produced unexpected output format.", duration=None)
|
| 172 |
+
# Return an update to hide the button
|
| 173 |
+
return [["Error", "Error", "Transcription Format Issue"]], [[0.0, 0.0]], audio_path, gr.DownloadButton(visible=False)
|
| 174 |
+
|
| 175 |
+
segment_timestamps = output[0].timestamp['segment']
|
| 176 |
+
csv_headers = ["Start (s)", "End (s)", "Segment"]
|
| 177 |
+
vis_data = [[f"{ts['start']:.2f}", f"{ts['end']:.2f}", ts['segment']] for ts in segment_timestamps]
|
| 178 |
+
raw_times_data = [[ts['start'], ts['end']] for ts in segment_timestamps]
|
| 179 |
+
|
| 180 |
+
# Default button update (hidden) in case CSV writing fails
|
| 181 |
+
button_update = gr.DownloadButton(visible=False)
|
| 182 |
+
try:
|
| 183 |
+
csv_file_path = Path(session_dir, f"transcription_{audio_name}.csv")
|
| 184 |
+
writer = csv.writer(open(csv_file_path, 'w'))
|
| 185 |
+
writer.writerow(csv_headers)
|
| 186 |
+
writer.writerows(vis_data)
|
| 187 |
+
print(f"CSV transcript saved to temporary file: {csv_file_path}")
|
| 188 |
+
# If CSV is saved, create update to show button with path
|
| 189 |
+
button_update = gr.DownloadButton(value=csv_file_path, visible=True)
|
| 190 |
+
except Exception as csv_e:
|
| 191 |
+
gr.Error(f"Failed to create transcript CSV file: {csv_e}", duration=None)
|
| 192 |
+
print(f"Error writing CSV: {csv_e}")
|
| 193 |
+
# csv_file_path remains None, button_update remains hidden
|
| 194 |
+
|
| 195 |
+
gr.Info("Transcription complete.", duration=2)
|
| 196 |
+
# Return the data and the button update dictionary
|
| 197 |
+
return vis_data, raw_times_data, audio_path, button_update
|
| 198 |
+
|
| 199 |
+
except torch.cuda.OutOfMemoryError as e:
|
| 200 |
+
error_msg = 'CUDA out of memory. Please try a shorter audio or reduce GPU load.'
|
| 201 |
+
print(f"CUDA OutOfMemoryError: {e}")
|
| 202 |
+
gr.Error(error_msg, duration=None)
|
| 203 |
+
# Return an update to hide the button
|
| 204 |
+
return [["OOM", "OOM", error_msg]], [[0.0, 0.0]], audio_path, gr.DownloadButton(visible=False)
|
| 205 |
+
|
| 206 |
+
except FileNotFoundError:
|
| 207 |
+
error_msg = f"Audio file for transcription not found: {Path(transcribe_path).name}."
|
| 208 |
+
print(f"Error: Transcribe audio file not found at path: {transcribe_path}")
|
| 209 |
+
gr.Error(error_msg, duration=None)
|
| 210 |
+
# Return an update to hide the button
|
| 211 |
+
return [["Error", "Error", "File not found for transcription"]], [[0.0, 0.0]], audio_path, gr.DownloadButton(visible=False)
|
| 212 |
+
|
| 213 |
+
except Exception as e:
|
| 214 |
+
error_msg = f"Transcription failed: {e}"
|
| 215 |
+
print(f"Error during transcription processing: {e}")
|
| 216 |
+
gr.Error(error_msg, duration=None)
|
| 217 |
+
vis_data = [["Error", "Error", error_msg]]
|
| 218 |
+
raw_times_data = [[0.0, 0.0]]
|
| 219 |
+
# Return an update to hide the button
|
| 220 |
+
return vis_data, raw_times_data, audio_path, gr.DownloadButton(visible=False)
|
| 221 |
+
finally:
|
| 222 |
+
try:
|
| 223 |
+
if 'model' in locals() and hasattr(model, 'cpu'):
|
| 224 |
+
if device == 'cuda':
|
| 225 |
+
model.cpu()
|
| 226 |
+
gc.collect()
|
| 227 |
+
if device == 'cuda':
|
| 228 |
+
torch.cuda.empty_cache()
|
| 229 |
+
except Exception as cleanup_e:
|
| 230 |
+
print(f"Error during model cleanup: {cleanup_e}")
|
| 231 |
+
gr.Warning(f"Issue during model cleanup: {cleanup_e}", duration=5)
|
| 232 |
+
|
| 233 |
+
finally:
|
| 234 |
+
if processed_audio_path and os.path.exists(processed_audio_path):
|
| 235 |
+
try:
|
| 236 |
+
os.remove(processed_audio_path)
|
| 237 |
+
print(f"Temporary audio file {processed_audio_path} removed.")
|
| 238 |
+
except Exception as e:
|
| 239 |
+
print(f"Error removing temporary audio file {processed_audio_path}: {e}")
|
| 240 |
+
|
| 241 |
+
def play_segment(evt: gr.SelectData, raw_ts_list, current_audio_path):
|
| 242 |
+
if not isinstance(raw_ts_list, list):
|
| 243 |
+
print(f"Warning: raw_ts_list is not a list ({type(raw_ts_list)}). Cannot play segment.")
|
| 244 |
+
return gr.Audio(value=None, label="Selected Segment")
|
| 245 |
+
|
| 246 |
+
if not current_audio_path:
|
| 247 |
+
print("No audio path available to play segment from.")
|
| 248 |
+
return gr.Audio(value=None, label="Selected Segment")
|
| 249 |
+
|
| 250 |
+
selected_index = evt.index[0]
|
| 251 |
+
|
| 252 |
+
if selected_index < 0 or selected_index >= len(raw_ts_list):
|
| 253 |
+
print(f"Invalid index {selected_index} selected for list of length {len(raw_ts_list)}.")
|
| 254 |
+
return gr.Audio(value=None, label="Selected Segment")
|
| 255 |
+
|
| 256 |
+
if not isinstance(raw_ts_list[selected_index], (list, tuple)) or len(raw_ts_list[selected_index]) != 2:
|
| 257 |
+
print(f"Warning: Data at index {selected_index} is not in the expected format [start, end].")
|
| 258 |
+
return gr.Audio(value=None, label="Selected Segment")
|
| 259 |
+
|
| 260 |
+
start_time_s, end_time_s = raw_ts_list[selected_index]
|
| 261 |
+
|
| 262 |
+
print(f"Attempting to play segment: {current_audio_path} from {start_time_s:.2f}s to {end_time_s:.2f}s")
|
| 263 |
+
|
| 264 |
+
segment_data = get_audio_segment(current_audio_path, start_time_s, end_time_s)
|
| 265 |
+
|
| 266 |
+
if segment_data:
|
| 267 |
+
print("Segment data retrieved successfully.")
|
| 268 |
+
return gr.Audio(value=segment_data, autoplay=True, label=f"Segment: {start_time_s:.2f}s - {end_time_s:.2f}s", interactive=False)
|
| 269 |
+
else:
|
| 270 |
+
print("Failed to get audio segment data.")
|
| 271 |
+
return gr.Audio(value=None, label="Selected Segment")
|
| 272 |
+
|
| 273 |
+
article = (
|
| 274 |
+
"<p style='font-size: 1.1em;'>"
|
| 275 |
+
"This demo showcases <code><a href='https://huggingface.co/nvidia/canary-1b-v2'>canary-1b-v2</a></code>, a 1-billion-parameter model built for high-quality speech transcription and translation across 25 European languages."
|
| 276 |
+
"</p>"
|
| 277 |
+
"<p><strong style='color: #ffb300; font-size: 1.2em;'>Key Features:</strong></p>"
|
| 278 |
+
"<ul style='font-size: 1.1em;'>"
|
| 279 |
+
" <li>Support for <strong>25 European languages</strong></li>"
|
| 280 |
+
" <li>Automatic <strong>punctuation and capitalization</strong></li>"
|
| 281 |
+
" <li>Accurate <strong>word-level and segment-level timestamps</strong></li>"
|
| 282 |
+
" <li><strong>Segment-level timestamps</strong> for translated outputs</li>"
|
| 283 |
+
"</ul>"
|
| 284 |
+
"<p style='font-size: 1.1em;'>"
|
| 285 |
+
"This model is <strong>available for commercial and non-commercial use</strong>."
|
| 286 |
+
"</p>"
|
| 287 |
+
"<p style='text-align: center;'>"
|
| 288 |
+
"<a href='https://huggingface.co/nvidia/canary-1b-v2' target='_blank'>ποΈ Learn more about the Model</a> | "
|
| 289 |
+
"<a href='https://github.com/NVIDIA/NeMo' target='_blank'>π§βπ» NeMo Repository</a>"
|
| 290 |
+
"</p>"
|
| 291 |
+
)
|
| 292 |
+
|
| 293 |
+
examples = [
|
| 294 |
+
["data/example-yt_saTD1u8PorI.mp3"],
|
| 295 |
+
]
|
| 296 |
+
|
| 297 |
+
# Define an NVIDIA-inspired theme
|
| 298 |
+
nvidia_theme = gr_themes.Default(
|
| 299 |
+
primary_hue=gr_themes.Color(
|
| 300 |
+
c50="#FFF9E6", # Lightest yellow
|
| 301 |
+
c100="#FFF2CC",
|
| 302 |
+
c200="#FFEB99",
|
| 303 |
+
c300="#FFE066",
|
| 304 |
+
c400="#FFD633",
|
| 305 |
+
c500="#FFCC00", # Canary Yellow
|
| 306 |
+
c600="#E6B800",
|
| 307 |
+
c700="#CC9900",
|
| 308 |
+
c800="#B38600",
|
| 309 |
+
c900="#996600", # Orange-brown
|
| 310 |
+
c950="#805500"
|
| 311 |
+
),
|
| 312 |
+
neutral_hue="gray", # Use gray for neutral elements
|
| 313 |
+
font=[gr_themes.GoogleFont("Inter"), "ui-sans-serif", "system-ui", "sans-serif"],
|
| 314 |
+
).set()
|
| 315 |
+
|
| 316 |
+
# Apply the custom theme
|
| 317 |
+
with gr.Blocks(theme=nvidia_theme) as demo:
|
| 318 |
+
model_display_name = MODEL_NAME.split('/')[-1] if '/' in MODEL_NAME else MODEL_NAME
|
| 319 |
+
gr.Markdown(f"<h1 style='text-align: center; margin: 0 auto;'>π€ Transcribe and Translate with {model_display_name}</h1>")
|
| 320 |
+
gr.Markdown(f"<h2 style='text-align: center; margin: 0 auto;'>in 25 European Languages</h2>")
|
| 321 |
+
gr.HTML(article)
|
| 322 |
+
|
| 323 |
+
current_audio_path_state = gr.State(None)
|
| 324 |
+
raw_timestamps_list_state = gr.State([])
|
| 325 |
+
|
| 326 |
+
session_dir = gr.State()
|
| 327 |
+
demo.load(start_session, outputs=[session_dir])
|
| 328 |
+
|
| 329 |
+
with gr.Row():
|
| 330 |
+
source_lang_dropdown = gr.Dropdown(
|
| 331 |
+
choices=AVAILABLE_SRC_LANGS,
|
| 332 |
+
value=DEFAULT_SRC_LANG,
|
| 333 |
+
label="Select Source Language (Audio)",
|
| 334 |
+
interactive=True)
|
| 335 |
+
|
| 336 |
+
target_lang_dropdown = gr.Dropdown(
|
| 337 |
+
choices=AVAILABLE_TGT_LANGS,
|
| 338 |
+
value=DEFAULT_TGT_LANG,
|
| 339 |
+
label="Select Target Language (Output Text)",
|
| 340 |
+
interactive=True)
|
| 341 |
+
|
| 342 |
+
with gr.Tabs():
|
| 343 |
+
with gr.TabItem("Audio File"):
|
| 344 |
+
file_input = gr.Audio(sources=["upload"], type="filepath", label="Upload Audio File")
|
| 345 |
+
gr.Examples(examples=examples, inputs=[file_input], label="Example Audio Files (Click to Load)")
|
| 346 |
+
file_transcribe_btn = gr.Button("Transcribe Uploaded File", variant="primary")
|
| 347 |
+
|
| 348 |
+
with gr.TabItem("Microphone"):
|
| 349 |
+
mic_input = gr.Audio(sources=["microphone"], type="filepath", label="Record Audio")
|
| 350 |
+
mic_transcribe_btn = gr.Button("Transcribe Microphone Input", variant="primary")
|
| 351 |
+
|
| 352 |
+
gr.Markdown("---")
|
| 353 |
+
gr.Markdown(f"""
|
| 354 |
+
<div style="text-align: center; font-size: 1.2em; font-weight: bold; padding: 10px; margin: 10px 0; border-top: 0px solid #ccc; border-bottom: 1px solid #ccc;">
|
| 355 |
+
Ready to dive in? Just <span style="color:orange">click</span> on the text to jump to the part you need!
|
| 356 |
+
</div>
|
| 357 |
+
""")
|
| 358 |
+
|
| 359 |
+
# Define the DownloadButton *before* the DataFrame
|
| 360 |
+
download_btn = gr.DownloadButton(label="Download Transcript (CSV)", visible=False)
|
| 361 |
+
|
| 362 |
+
vis_timestamps_df = gr.DataFrame(
|
| 363 |
+
headers=["Start (s)", "End (s)", "Segment"],
|
| 364 |
+
datatype=["number", "number", "str"],
|
| 365 |
+
wrap=True,
|
| 366 |
+
label="Transcription Segments"
|
| 367 |
+
)
|
| 368 |
+
|
| 369 |
+
# selected_segment_player was defined after download_btn previously, keep it after df for layout
|
| 370 |
+
selected_segment_player = gr.Audio(label="Selected Segment", interactive=False)
|
| 371 |
+
|
| 372 |
+
source_lang_dropdown.select(
|
| 373 |
+
fn=update_src_lang_dropdown,
|
| 374 |
+
inputs=[source_lang_dropdown],
|
| 375 |
+
outputs=[target_lang_dropdown]
|
| 376 |
+
)
|
| 377 |
+
|
| 378 |
+
target_lang_dropdown.select(
|
| 379 |
+
fn=update_button_intstruction,
|
| 380 |
+
inputs=[source_lang_dropdown, target_lang_dropdown],
|
| 381 |
+
outputs=[file_transcribe_btn, mic_transcribe_btn]
|
| 382 |
+
)
|
| 383 |
+
|
| 384 |
+
mic_transcribe_btn.click(
|
| 385 |
+
fn=get_transcripts_and_raw_times,
|
| 386 |
+
inputs=[mic_input, session_dir, source_lang_dropdown, target_lang_dropdown],
|
| 387 |
+
outputs=[vis_timestamps_df, raw_timestamps_list_state, current_audio_path_state, download_btn],
|
| 388 |
+
api_name="transcribe_mic"
|
| 389 |
+
)
|
| 390 |
+
|
| 391 |
+
file_transcribe_btn.click(
|
| 392 |
+
fn=get_transcripts_and_raw_times,
|
| 393 |
+
inputs=[file_input, session_dir, source_lang_dropdown, target_lang_dropdown],
|
| 394 |
+
outputs=[vis_timestamps_df, raw_timestamps_list_state, current_audio_path_state, download_btn],
|
| 395 |
+
api_name="transcribe_file"
|
| 396 |
+
)
|
| 397 |
+
|
| 398 |
+
vis_timestamps_df.select(
|
| 399 |
+
fn=play_segment,
|
| 400 |
+
inputs=[raw_timestamps_list_state, current_audio_path_state],
|
| 401 |
+
outputs=[selected_segment_player],
|
| 402 |
+
)
|
| 403 |
+
|
| 404 |
+
demo.unload(end_session)
|
| 405 |
+
|
| 406 |
+
if __name__ == "__main__":
|
| 407 |
+
print("Launching Gradio Demo...")
|
| 408 |
+
demo.queue()
|
| 409 |
+
demo.launch()
|
data/example-yt_saTD1u8PorI.mp3
ADDED
|
@@ -0,0 +1,3 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
version https://git-lfs.github.com/spec/v1
|
| 2 |
+
oid sha256:3cb340c3b868eb3695cdb06683decbff217331c2459a69394be8d3ad3b53bdf0
|
| 3 |
+
size 2493472
|
packages.txt
ADDED
|
@@ -0,0 +1,2 @@
|
|
|
|
|
|
|
|
|
|
| 1 |
+
ffmpeg
|
| 2 |
+
libsndfile1
|
requirements.txt
ADDED
|
@@ -0,0 +1,2 @@
|
|
|
|
|
|
|
|
|
|
| 1 |
+
Cython
|
| 2 |
+
git+https://github.com/NVIDIA/NeMo.git@msekoyan/canary2_timestamps#egg=nemo_toolkit[asr]
|
supported_languages.py
ADDED
|
@@ -0,0 +1,27 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
SUPPORTED_LANGS_MAP = {
|
| 2 |
+
"Bulgarian": "bg",
|
| 3 |
+
"Croatian": "hr",
|
| 4 |
+
"Czech": "cs",
|
| 5 |
+
"Danish": "da",
|
| 6 |
+
"Dutch": "nl",
|
| 7 |
+
"English": "en",
|
| 8 |
+
"Estonian": "et",
|
| 9 |
+
"Finnish": "fi",
|
| 10 |
+
"French": "fr",
|
| 11 |
+
"German": "de",
|
| 12 |
+
"Greek": "el",
|
| 13 |
+
"Hungarian": "hu",
|
| 14 |
+
"Italian": "it",
|
| 15 |
+
"Latvian": "lv",
|
| 16 |
+
"Lithuanian": "lt",
|
| 17 |
+
"Maltese": "mt",
|
| 18 |
+
"Polish": "pl",
|
| 19 |
+
"Portuguese": "pt",
|
| 20 |
+
"Romanian": "ro",
|
| 21 |
+
"Slovak": "sk",
|
| 22 |
+
"Slovenian": "sl",
|
| 23 |
+
"Spanish": "es",
|
| 24 |
+
"Swedish": "sv",
|
| 25 |
+
"Russian": "ru",
|
| 26 |
+
"Ukrainian": "uk"
|
| 27 |
+
}
|