| import gradio as gr |
| import whisper |
| import os |
|
|
| |
| MODEL_OPTIONS = ["tiny", "base", "small", "medium", "large"] |
|
|
| |
| model = whisper.load_model("base") |
|
|
| |
| IDIOMAS_DISPONIBLES = [ |
| ("Espa帽ol", "es"), |
| ("Ingl茅s", "en"), |
| ("Catal谩n", "ca"), |
| ("Franc茅s", "fr"), |
| ("Alem谩n", "de"), |
| ("Italiano", "it"), |
| ("Portugu茅s", "pt"), |
| ("Gallego", "gl"), |
| ("Euskera", "eu"), |
| ("Chino", "zh"), |
| ("Japon茅s", "ja"), |
| ] |
|
|
| def transcribir(audio_paths, model_name, idioma_nombre, traducir): |
| """ |
| Funci贸n que recibe uno o m谩s archivos de audio, ejecuta la transcripci贸n, |
| permite elegir el modelo de Whisper, especificar el idioma y traducir la transcripci贸n. |
| Guarda los resultados en archivos .txt y .srt, y devuelve enlaces de descarga. |
| """ |
|
|
| global model |
| if not model_name: |
| gr.Warning("鈿狅笍 Por favor, selecciona un modelo de Whisper antes de continuar.") |
| return "", "", "" |
|
|
| |
| idioma_dict = dict(IDIOMAS_DISPONIBLES) |
| idioma = idioma_dict.get(idioma_nombre, None) |
|
|
| if not idioma: |
| gr.Warning("鈿狅笍 Por favor, selecciona un idioma antes de continuar.") |
| return "", "", "" |
|
|
| |
| model = whisper.load_model(model_name) |
|
|
| archivos_resultantes = [] |
| |
| for audio_path in audio_paths: |
| |
| result = model.transcribe(audio_path, language=idioma) |
|
|
| texto_transcrito = result.get("text", "") |
|
|
| |
| if traducir: |
| result_traducido = model.transcribe(audio_path, task="translate", language="en") |
| texto_transcrito = result_traducido.get("text", "") |
|
|
| |
| base_name = os.path.splitext(os.path.basename(audio_path))[0] |
| txt_file = f"{base_name}.txt" |
| srt_file = f"{base_name}.srt" |
|
|
| |
| with open(txt_file, "w", encoding="utf-8") as f: |
| f.write(texto_transcrito) |
|
|
| |
| with open(srt_file, "w", encoding="utf-8") as f: |
| for i, segment in enumerate(result["segments"], start=1): |
| start = segment["start"] |
| end = segment["end"] |
| text = segment["text"] |
|
|
| f.write(f"{i}\n") |
| f.write(f"{start:.3f} --> {end:.3f}\n") |
| f.write(f"{text}\n\n") |
|
|
| archivos_resultantes.append((texto_transcrito, txt_file, srt_file)) |
|
|
| if len(archivos_resultantes) == 1: |
| return archivos_resultantes[0] |
| else: |
| transcripciones, txt_files, srt_files = zip(*archivos_resultantes) |
| return list(transcripciones), list(txt_files), list(srt_files) |
|
|
| |
| iface = gr.Interface( |
| fn=transcribir, |
| inputs=[ |
| gr.Files(type="filepath", label="Sube uno o m谩s archivos"), |
| gr.Dropdown(MODEL_OPTIONS, value=None, label="Selecciona el modelo de Whisper", interactive=True), |
| gr.Dropdown( |
| choices=[nombre for nombre, _ in IDIOMAS_DISPONIBLES], |
| value=None, |
| label="Selecciona el idioma del audio", |
| interactive=True |
| ), |
| gr.Checkbox(label="Traducir al ingl茅s", value=False) |
| ], |
| outputs=[ |
| gr.Textbox(label="Transcripci贸n"), |
| gr.File(label="Descargar .txt"), |
| gr.File(label="Descargar .srt") |
| ], |
| title="Transcriptor de Audio con Whisper", |
| description="Sube uno o m谩s archivos de audio y obt茅n la transcripci贸n con opciones avanzadas." |
| ) |
|
|
| iface.launch(share=True, pwa=True |
|
|
|
|
|
|
|
|