katospiegel commited on
Commit
db096e5
1 Parent(s): 51cca5c

Improving the ouptut and scoring highlight

Browse files
Files changed (2) hide show
  1. app.py +11 -2
  2. helpers.py +46 -68
app.py CHANGED
@@ -8,6 +8,7 @@ from transcription import fast_transcription, speech_to_text, doWhisperX
8
  from whisperx.utils import get_writer
9
  from audio import normalizeAudio, separateVoiceInstrumental, mp3_to_wav, stereo_to_mono, cutaudio, compose_audio
10
  from helpers import guardar_en_archivo, guardar_dataframe_en_csv, generar_transcripcion
 
11
 
12
  import json
13
 
@@ -79,8 +80,15 @@ def transcribeWhisperX(audiofile, model, language, patience,
79
 
80
  ###########################################################################
81
 
82
- outputs = (audio_path, audio_normalized_path, vocal_path, novocal_path, lineas_txt_string, nombre_file_txt,
83
- str(file_path), guardar_dataframe_en_csv(diarize_segments), json.dumps(result_speakers))
 
 
 
 
 
 
 
84
 
85
  return outputs
86
 
@@ -127,6 +135,7 @@ transcribeII = gr.Interface(
127
  gr.Audio(type="filepath", label="vocal"),
128
  gr.Audio(type="filepath", label="no_vocal"),
129
  gr.TextArea(label="Transcripci贸n"),
 
130
  gr.File(label="Archivo TXT generado"),
131
  gr.File(label="Archivo SRT generado con turno de palabra"),
132
  gr.File(label="Archivo CSV generado con turno de palabra"),
 
8
  from whisperx.utils import get_writer
9
  from audio import normalizeAudio, separateVoiceInstrumental, mp3_to_wav, stereo_to_mono, cutaudio, compose_audio
10
  from helpers import guardar_en_archivo, guardar_dataframe_en_csv, generar_transcripcion
11
+ from helpers import crear_diccionario, generar_html_palabras
12
 
13
  import json
14
 
 
80
 
81
  ###########################################################################
82
 
83
+ # Creating the highlight
84
+
85
+ dout = crear_diccionario(json_data)
86
+ htmlout = generar_html_palabras(dout["word"], dout["score"])
87
+
88
+ ############################################################################
89
+
90
+ outputs = (audio_path, audio_normalized_path, vocal_path, novocal_path, lineas_txt_string, htmlout,
91
+ nombre_file_txt, str(file_path), guardar_dataframe_en_csv(diarize_segments), json.dumps(result_speakers))
92
 
93
  return outputs
94
 
 
135
  gr.Audio(type="filepath", label="vocal"),
136
  gr.Audio(type="filepath", label="no_vocal"),
137
  gr.TextArea(label="Transcripci贸n"),
138
+ gr.HTML(label="Scoring color mapping"),
139
  gr.File(label="Archivo TXT generado"),
140
  gr.File(label="Archivo SRT generado con turno de palabra"),
141
  gr.File(label="Archivo CSV generado con turno de palabra"),
helpers.py CHANGED
@@ -39,82 +39,33 @@ def dataframe_a_lista(df):
39
 
40
  return lista_strings
41
 
42
- # def generar_transcripcion(dataframe):
43
- # # Ordenamos el DataFrame por Timestamp
44
- # dataframe = dataframe.sort_values(by='Timestamp')
45
-
46
- # # Creamos un diccionario para almacenar los textos de cada speaker
47
- # textos_por_speaker = {}
48
-
49
- # # Iteramos sobre las filas del DataFrame
50
- # for index, row in dataframe.iterrows():
51
- # # Obtenemos los valores de la fila
52
- # timestamp = row['Timestamp']
53
- # speaker = row['speaker']
54
- # texto = row['text']
55
-
56
- # # Si el speaker no est谩 en el diccionario, lo agregamos
57
- # if speaker not in textos_por_speaker:
58
- # textos_por_speaker[speaker] = []
59
-
60
- # # Agregamos el texto al diccionario
61
- # textos_por_speaker[speaker].append(texto)
62
-
63
- # # Creamos una lista para almacenar las l铆neas de la transcripci贸n
64
- # lineas_transcripcion = []
65
-
66
- # # Iteramos sobre los speakers y sus textos
67
- # for speaker, textos in textos_por_speaker.items():
68
- # # Creamos un p谩rrafo con todos los textos del speaker
69
- # parrafo = ' '.join(textos)
70
-
71
- # # Agregamos la l铆nea de la transcripci贸n con el timestamp y el speaker
72
- # linea = f"{timestamp} - {speaker}: {parrafo}"
73
-
74
- # # Agregamos la l铆nea a la lista de la transcripci贸n
75
- # lineas_transcripcion.append(linea)
76
-
77
- # # Guardamos la transcripci贸n en un archivo de texto
78
- # nombre_archivo = guardar_en_archivo(lineas_transcripcion)
79
-
80
- # return lineas_transcripcion, nombre_archivo
81
-
82
  # def generar_transcripcion(diccionario):
83
  # # Obtenemos la lista de segmentos del diccionario
84
  # segmentos = diccionario['segments']
85
 
86
- # # Creamos un diccionario para almacenar los textos de cada speaker
87
- # textos_por_speaker = {}
88
-
89
- # # Iteramos sobre los segmentos del diccionario
90
- # for segmento in segmentos:
91
- # # Obtenemos los valores del segmento
92
- # speaker = segmento['speaker']
93
- # texto = segmento['text']
94
-
95
- # # Si el speaker no est谩 en el diccionario, lo agregamos
96
- # if speaker not in textos_por_speaker:
97
- # textos_por_speaker[speaker] = []
98
-
99
- # # Agregamos el texto al diccionario
100
- # textos_por_speaker[speaker].append(texto)
101
-
102
  # # Creamos una lista para almacenar las l铆neas de la transcripci贸n
103
  # lineas_transcripcion = []
104
 
105
- # # Iteramos sobre los speakers y sus textos
106
- # for speaker, textos in textos_por_speaker.items():
107
- # # Creamos un p谩rrafo con todos los textos del speaker
108
- # parrafo = ' '.join(textos)
 
 
109
 
110
- # # Agregamos el speaker al principio del p谩rrafo
111
- # parrafo_con_speaker = f"{speaker}: {parrafo}"
 
 
 
112
 
113
- # # Agregamos la l铆nea de la transcripci贸n
 
114
  # linea = parrafo_con_speaker
115
-
116
- # # Agregamos la l铆nea a la lista de la transcripci贸n
117
  # lineas_transcripcion.append(linea)
 
 
 
118
 
119
  # # Guardamos la transcripci贸n en un archivo de texto
120
  # nombre_archivo = guardar_en_archivo(lineas_transcripcion)
@@ -131,9 +82,10 @@ def generar_transcripcion(diccionario):
131
  # Iteramos sobre los segmentos del diccionario
132
  i = 0
133
  while i < len(segmentos):
134
- # Obtenemos el speaker y el texto del segmento actual
135
  speaker_actual = segmentos[i]['speaker']
136
  texto_actual = segmentos[i]['text']
 
137
 
138
  # Buscamos el siguiente segmento con un speaker diferente
139
  j = i + 1
@@ -142,7 +94,7 @@ def generar_transcripcion(diccionario):
142
  j += 1
143
 
144
  # Agregamos el texto del speaker actual a la transcripci贸n
145
- parrafo_con_speaker = f"{speaker_actual}: {texto_actual}"
146
  linea = parrafo_con_speaker
147
  lineas_transcripcion.append(linea)
148
 
@@ -181,4 +133,30 @@ def crear_diccionario(json_data):
181
  diccionario['word'].append(word)
182
  diccionario['score'].append(score)
183
 
184
- return diccionario
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
39
 
40
  return lista_strings
41
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
42
  # def generar_transcripcion(diccionario):
43
  # # Obtenemos la lista de segmentos del diccionario
44
  # segmentos = diccionario['segments']
45
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
46
  # # Creamos una lista para almacenar las l铆neas de la transcripci贸n
47
  # lineas_transcripcion = []
48
 
49
+ # # Iteramos sobre los segmentos del diccionario
50
+ # i = 0
51
+ # while i < len(segmentos):
52
+ # # Obtenemos el speaker y el texto del segmento actual
53
+ # speaker_actual = segmentos[i]['speaker']
54
+ # texto_actual = segmentos[i]['text']
55
 
56
+ # # Buscamos el siguiente segmento con un speaker diferente
57
+ # j = i + 1
58
+ # while j < len(segmentos) and segmentos[j]['speaker'] == speaker_actual:
59
+ # texto_actual += ' ' + segmentos[j]['text']
60
+ # j += 1
61
 
62
+ # # Agregamos el texto del speaker actual a la transcripci贸n
63
+ # parrafo_con_speaker = f"{speaker_actual}: {texto_actual}"
64
  # linea = parrafo_con_speaker
 
 
65
  # lineas_transcripcion.append(linea)
66
+
67
+ # # Actualizamos el 铆ndice para el siguiente speaker
68
+ # i = j
69
 
70
  # # Guardamos la transcripci贸n en un archivo de texto
71
  # nombre_archivo = guardar_en_archivo(lineas_transcripcion)
 
82
  # Iteramos sobre los segmentos del diccionario
83
  i = 0
84
  while i < len(segmentos):
85
+ # Obtenemos el speaker, el texto y el start time del segmento actual
86
  speaker_actual = segmentos[i]['speaker']
87
  texto_actual = segmentos[i]['text']
88
+ start_time = segmentos[i]['start']
89
 
90
  # Buscamos el siguiente segmento con un speaker diferente
91
  j = i + 1
 
94
  j += 1
95
 
96
  # Agregamos el texto del speaker actual a la transcripci贸n
97
+ parrafo_con_speaker = f"{speaker_actual} ({start_time}): {texto_actual}"
98
  linea = parrafo_con_speaker
99
  lineas_transcripcion.append(linea)
100
 
 
133
  diccionario['word'].append(word)
134
  diccionario['score'].append(score)
135
 
136
+ return diccionario
137
+
138
+ def generar_html_palabras(palabras, scores):
139
+ # Creamos una lista para almacenar los c贸digos HTML de cada palabra
140
+ html_palabras = []
141
+
142
+ # Iteramos sobre las palabras y sus scores
143
+ for palabra, score in zip(palabras, scores):
144
+ # Mapeamos el score a un color en rojo o verde
145
+ if score < 0.5:
146
+ color = f"rgb({int(255*score*2)}, {int(255*(1-score*2))}, 0)"
147
+ else:
148
+ color = f"rgb({int(255*(1-score)*2)}, {int(255*score*2)}, 0)"
149
+
150
+ # Creamos el c贸digo HTML para la palabra con el color asociado
151
+ html_palabra = f"<span style='color: {color}'>{palabra}</span>"
152
+
153
+ # Agregamos el c贸digo HTML a la lista
154
+ html_palabras.append(html_palabra)
155
+
156
+ # Unimos los c贸digos HTML de las palabras en una sola cadena
157
+ html_texto = ' '.join(html_palabras)
158
+
159
+ # Creamos el c贸digo HTML completo con el texto resaltado
160
+ html_completo = f"<p>{html_texto}</p>"
161
+
162
+ return html_completo