File size: 10,054 Bytes
73b906e
 
 
 
 
 
f036671
90bf3ca
73b906e
8bc597c
db096e5
73b906e
412977d
1436ed9
412977d
73b906e
e4028fa
73b906e
82359e1
 
73b906e
e4028fa
 
73b906e
e4028fa
73b906e
e4028fa
 
 
 
 
 
73b906e
20bc888
 
da53eff
20bc888
 
73b906e
 
 
01f48d7
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
e575f73
 
 
01f48d7
f036671
98d0bf0
 
 
 
 
 
fdb47d9
f036671
98d0bf0
 
f036671
98d0bf0
f036671
98d0bf0
 
 
 
 
 
f036671
 
36d2951
f036671
 
 
 
 
 
 
ee9f03e
 
8bc597c
ee9f03e
 
89bb3ca
ee9f03e
 
89bb3ca
ee9f03e
 
 
36d2951
5f366ba
8bc597c
 
 
 
9471b7a
 
90bf3ca
ee9f03e
89bb3ca
db096e5
 
e3e356d
db096e5
 
 
 
98d0bf0
 
 
db096e5
98d0bf0
73b906e
8bc597c
73b906e
 
 
 
82359e1
73b906e
e4028fa
73b906e
 
 
 
 
01f48d7
 
 
ce185dc
73b906e
 
f036671
 
 
 
 
e4028fa
f036671
 
 
 
72284ae
f036671
3ad0b7a
124ba39
 
98d0bf0
 
 
 
 
 
 
 
f036671
 
 
 
 
8bc597c
db096e5
8bc597c
 
 
 
ac0af78
f036671
 
 
124ba39
73b906e
f036671
73b906e
 
6f39eb4
 
 
b1ed6da
98d0bf0
 
 
 
 
 
 
 
73b906e
 
 
 
 
e486aab
e50d677
 
 
 
 
3f36d8c
1436ed9
 
575823f
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
import torch

import gradio as gr
from transformers import pipeline
from transformers.pipelines.audio_utils import ffmpeg_read

from transcription import fast_transcription, speech_to_text, doWhisperX
from whisperx.utils import get_writer
from audio import normalizeAudio, separateVoiceInstrumental, mp3_to_wav, stereo_to_mono, cutaudio, compose_audio
from helpers import guardar_en_archivo, guardar_dataframe_en_csv, generar_transcripcion
from helpers import crear_diccionario, generar_html_palabras

import json
import os


def transcribe(audiofile, model, preprocesamiento):

    #if audiofile.type is not str:
    audio_path = audiofile.name

    if preprocesamiento == "Pre-procesamiento del audio":
        audio_normalized_path = normalizeAudio(audio_path, ".wav")

        novocal_path, vocal_path = separateVoiceInstrumental(audio_normalized_path)

        novocal_path = mp3_to_wav(novocal_path, "novocal")
        vocal_path = mp3_to_wav(vocal_path, "vocal")
    else:
        audio_normalized_path = audio_path
        novocal_path = audio_path
        vocal_path = audio_path

    result = fast_transcription(vocal_path, model, "es")

    out = [str(s["start"]) + " " + s["text"] for s in result["segments"]]

    #transcript = "\n".join(out)
    #Archivo
    nombre_archivo = guardar_en_archivo(out)

    ##########################################################################
    #SRT
    from pathlib import Path

    import datetime
    fecha_actual = datetime.datetime.now().strftime("%Y-%m-%d_%H-%M-%S")
    
    # Generar el nombre del archivo
    nombre_archivo_srt = f"transcription_{fecha_actual}.srt"

    file_path = Path(nombre_archivo_srt)
    writter_args = {"highlight_words": None, "max_line_count": None, "max_line_width": None}
    srt_writer = get_writer("srt", Path("."))
    result["language"] = "es"
    srt_writer(result, file_path, writter_args)
    ###########################################################################

    results_short = result
    results_short["segments"] = results_short["segments"][0:10]

    return audio_path, audio_normalized_path, vocal_path, novocal_path, nombre_archivo, nombre_archivo_srt, "\n".join(out), json.dumps(results_short)

def transcribeWhisperX(audiofile, model, language, preprocesamiento
                       #patience, initial_prompt, condition_on_previous_text, temperature,
                       #compression, logprob, no_speech_threshold
                       ):
    
    #if audiofile.type is not str:
    audio_path = audiofile.name

    if preprocesamiento == "Pre-procesamiento del audio":
        audio_normalized_path = normalizeAudio(audio_path, ".wav")

        novocal_path, vocal_path = separateVoiceInstrumental(audio_normalized_path)

        novocal_path = mp3_to_wav(novocal_path, "novocal")
        vocal_path = mp3_to_wav(vocal_path, "vocal")
    else:
        audio_normalized_path = audio_path
        novocal_path = audio_path
        vocal_path = audio_path

    #result = fast_transcription(vocal_path, model, "es")
    result_whisper, result_aligned, result_speakers, diarize_segments = doWhisperX(vocal_path, whisper_model=model, language=language)

    #out = [str(s["start"]) + " " + s["text"] for s in result["segments"]]

    #transcript = "\n".join(out)
    #Archivo
    #nombre_archivo = guardar_en_archivo(out)

    ##########################################################################
    from pathlib import Path

    import datetime
    fecha_actual = datetime.datetime.now().strftime("%Y-%m-%d_%H-%M-%S")
    
    # Generar el nombre del archivo
    nombre_archivo = f"transcription_{fecha_actual}.srt"

    file_path = Path(nombre_archivo)
    writter_args = {"highlight_words": None, "max_line_count": None, "max_line_width": None}
    srt_writer = get_writer("srt", Path("."))
    result_aligned["language"] = language
    srt_writer(result_aligned, file_path, writter_args)
    ###########################################################################

    # Creating the txt

    lineas_txt, nombre_file_txt = generar_transcripcion(result_speakers)
    lineas_txt_string = "\n".join(lineas_txt)

    ###########################################################################

    # Creating the highlight

    dout = crear_diccionario(result_speakers)
    htmlout = generar_html_palabras(dout["word"], dout["score"])

    ############################################################################

    results_short = result_speakers
    results_short["segments"] = results_short["segments"][0:10]

    outputs = (audio_path, audio_normalized_path, vocal_path, novocal_path, lineas_txt_string, htmlout,
               nombre_file_txt, str(file_path), guardar_dataframe_en_csv(diarize_segments), json.dumps(results_short))

    return outputs

transcribeI = gr.Interface(
    fn=transcribe,
    inputs=[
        gr.File(label="Upload Files"), #, file_count="multiple"
        gr.Radio(["base", "small", "medium", "large-v2"], label="Models", value="large-v2"),
        gr.Radio(["Audio Original","Pre-procesamiento del audio"], label="Mejora del audio", value="Pre-procesamiento del audio"),
    ],
    outputs=[gr.Audio(type="filepath", label="original"),
             gr.Audio(type="filepath", label="normalized"),
             gr.Audio(type="filepath", label="vocal"),
             gr.Audio(type="filepath", label="no_vocal"),
             gr.File(label="Archivo TXT generado"),
             gr.File(label="Archivo SRT generado"),
             gr.TextArea(label="Transcripci贸n. (Tiempo en segundos)"),
             gr.JSON(label="JSON Output")
        ],
    theme="huggingface",
    title="Transcripci贸n con Whisper",
    description=(
        "Esta p谩gina realiza una transcripci贸n de audio utilizando Whisper. Adem谩s a帽ade varias mejoras y utilidades: a) Preprocesamiento del audio y limpieza de ruido ambiental, b) Conversi贸n de los archivos de audio a un formato compatible con Whisper, c) C谩lculo de la marca temporal palabra por palabra, d) C谩lculo del nivel de seguridad de la transcripci贸n, e) Conversi贸n del resultado a .csv, .srt y ass.\n"
    ),
    allow_flagging="never",
    examples=[["Espana 04 - Video 01 - extracto 2 min.wav", "large-v2", "Pre-procesamiento del audio"]]

)

transcribeII = gr.Interface(
    fn=transcribeWhisperX,
    inputs=[
        gr.File(label="Upload Files"),
        gr.Radio(["base", "small", "medium", "large-v2"], label="Modelo", value="large-v2"),
        gr.Dropdown(["Cualquiera","es","en","fr","pt"], label="Lenguaje", value="Cualquiera"),
        gr.Radio(["Audio Original","Pre-procesamiento del audio"], label="Mejora del audio", value="Pre-procesamiento del audio"),
        #gr.Slider(minimum=0, maximum=1, label="Patience (Whisper parameter)", value=0.5, info="Optional patience value to use in beam decoding, as in https://arxiv.org/abs/2204.05424, the default (1.0) is equivalent to conventional beam search"),
        #gr.Textbox(label="Initial Prompt (Whisper parameter)", value=""),
        #gr.Textbox(label="Condition on previous text (Whisper parameter)", value=""),
        #gr.Slider(minimum=0, maximum=1, label="Temperature (Whisper parameter)", value=0.5, info="Temperature to use for sampling"),
        #gr.Slider(minimum=0, maximum=1, label="Compression Ratio Threshold (Whisper parameter)", value=0.5),
        #gr.Slider(minimum=0, maximum=1, label="Logprob Threshold (Whisper parameter)", value=0.5),
        #gr.Slider(minimum=0, maximum=1, label="No Speech Threshold (Whisper parameter)", value=0.5),
    ],
    outputs=[gr.Audio(type="filepath", label="original"),
             gr.Audio(type="filepath", label="normalized"),
             gr.Audio(type="filepath", label="vocal"),
             gr.Audio(type="filepath", label="no_vocal"),
             gr.TextArea(label="Transcripci贸n"),
             gr.HTML(label="Scoring color mapping"),
             gr.File(label="Archivo TXT generado"),
             gr.File(label="Archivo SRT generado con turno de palabra"),
             gr.File(label="Archivo CSV generado con turno de palabra"),
             gr.JSON(label="Resultados estructurados en JSON palabra por palabra"),
             #gr.JSON(label="JSON Output"),
             #gr.File(label="Archivo generado")
        ],
    theme="huggingface",
    title="Transcripci贸n con WhisperX",
    description=(
        "Esta p谩gina realiza una transcripci贸n de audio utilizando Whisper. Adem谩s a帽ade varias mejoras y utilidades: a) Preprocesamiento del audio y limpieza de ruido ambiental, b) Conversi贸n de los archivos de audio a un formato compatible con Whisper, c) C谩lculo de la marca temporal palabra por palabra, d) C谩lculo del nivel de seguridad de la transcripci贸n, e) Conversi贸n del resultado a .csv, .srt y ass.\n"
    ),
    allow_flagging="never",
    examples=[["Espana 04 - Video 01 - extracto 2 min.wav", 
               "large-v2",
               "Cualquiera",
               "Pre-procesamiento del audio"
               #0.5,
               #"",
               #"",
               #0.5,
               #0.5,
               #0.5,
               #0.5
               ]]

)

demo = gr.Blocks()
with demo:
    gr.Markdown("# Amanuensis. Transcripci贸n de audios basada en OpenAI Whisper.")
    gr.Markdown(""" ## Muestras
- Bajar muestra corta aqu铆: https://drive.google.com/file/d/1dP45fLKHoj8_MfUFF1H9uP11QEskdRf3/view?usp=share_link

- Bajar muestra larga aqu铆: https://drive.google.com/file/d/1Nd7Ho3qfsAo33fth4lKGHd7EzuYZ6ZTo/view?usp=share_link
""")
    gr.TabbedInterface([transcribeI, transcribeII], ["Transcripci贸n con Whisper", "Transcripci贸n y turno de palabra con WhisperX"])
    gr.Markdown("Interfaz desarrollada por Carlos Vivar Rios usando las tecnolog铆as de c贸digo abierto whisper, whiserX, y. Si quieres utilizar esta herramienta o una soluci贸n personalizada: carlosvivarrios@gmail.com")
    gr.Markdown("Echa un ojo a mis otros proyectos: carlosvivarrios.com")
demo.queue(concurrency_count=1).launch(enable_queue=True, auth=(os.environ['USER'], os.environ['PASSWORD']))