Spaces:
Build error
Build error
Update utils_hf.py
Browse files- utils_hf.py +273 -286
utils_hf.py
CHANGED
|
@@ -1,287 +1,274 @@
|
|
| 1 |
-
# import numpy as np
|
| 2 |
-
# import soundfile as sf
|
| 3 |
-
# import torchaudio
|
| 4 |
-
|
| 5 |
-
import os
|
| 6 |
-
import yt_dlp
|
| 7 |
-
import streamlit as st
|
| 8 |
-
import torch
|
| 9 |
-
import whisper
|
| 10 |
-
from pyannote.audio import Pipeline
|
| 11 |
-
from pyannote.audio.pipelines.utils.hook import ProgressHook
|
| 12 |
-
from moviepy.editor import AudioFileClip
|
| 13 |
-
from transformers import pipeline
|
| 14 |
-
from openai import Client
|
| 15 |
-
from dotenv import load_dotenv
|
| 16 |
-
|
| 17 |
-
|
| 18 |
-
load_dotenv()
|
| 19 |
-
|
| 20 |
-
hf_token = os.environ.get("HF_DIARIZATION_TOKEN")
|
| 21 |
-
api_key = os.environ.get("OPENAI_API_KEY")
|
| 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 |
-
segment_audio_path
|
| 65 |
-
|
| 66 |
-
|
| 67 |
-
|
| 68 |
-
|
| 69 |
-
|
| 70 |
-
|
| 71 |
-
|
| 72 |
-
|
| 73 |
-
progress
|
| 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 |
-
audio_subclip
|
| 134 |
-
|
| 135 |
-
|
| 136 |
-
|
| 137 |
-
|
| 138 |
-
|
| 139 |
-
|
| 140 |
-
|
| 141 |
-
|
| 142 |
-
|
| 143 |
-
|
| 144 |
-
|
| 145 |
-
|
| 146 |
-
|
| 147 |
-
|
| 148 |
-
|
| 149 |
-
|
| 150 |
-
st.
|
| 151 |
-
|
| 152 |
-
|
| 153 |
-
|
| 154 |
-
def
|
| 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 |
-
|
| 219 |
-
|
| 220 |
-
|
| 221 |
-
|
| 222 |
-
|
| 223 |
-
|
| 224 |
-
|
| 225 |
-
|
| 226 |
-
|
| 227 |
-
|
| 228 |
-
|
| 229 |
-
|
| 230 |
-
|
| 231 |
-
|
| 232 |
-
|
| 233 |
-
|
| 234 |
-
|
| 235 |
-
|
| 236 |
-
|
| 237 |
-
|
| 238 |
-
|
| 239 |
-
|
| 240 |
-
|
| 241 |
-
|
| 242 |
-
|
| 243 |
-
|
| 244 |
-
#
|
| 245 |
-
#
|
| 246 |
-
#
|
| 247 |
-
#
|
| 248 |
-
#
|
| 249 |
-
#
|
| 250 |
-
#
|
| 251 |
-
#
|
| 252 |
-
#
|
| 253 |
-
#
|
| 254 |
-
#
|
| 255 |
-
#
|
| 256 |
-
#
|
| 257 |
-
#
|
| 258 |
-
#
|
| 259 |
-
|
| 260 |
-
#
|
| 261 |
-
#
|
| 262 |
-
#
|
| 263 |
-
#
|
| 264 |
-
#
|
| 265 |
-
#
|
| 266 |
-
#
|
| 267 |
-
#
|
| 268 |
-
#
|
| 269 |
-
#
|
| 270 |
-
#
|
| 271 |
-
# return
|
| 272 |
-
|
| 273 |
-
#
|
| 274 |
-
# try:
|
| 275 |
-
# # Leer el archivo de audio y convertirlo en un numpy ndarray
|
| 276 |
-
# audio_data, _ = sf.read(audio_path)
|
| 277 |
-
#
|
| 278 |
-
# # Convertir a un solo canal si es necesario
|
| 279 |
-
# if len(audio_data.shape) > 1:
|
| 280 |
-
# audio_data = np.mean(audio_data, axis=1)
|
| 281 |
-
#
|
| 282 |
-
# # Transcribir el audio usando el modelo Whisper
|
| 283 |
-
# transcript = whisper_pipeline(audio_data, return_timestamps=True)
|
| 284 |
-
# return transcript['text']
|
| 285 |
-
# except Exception as e:
|
| 286 |
-
# st.error(f"Error al transcribir el audio: {e}")
|
| 287 |
# st.stop()
|
|
|
|
| 1 |
+
# import numpy as np
|
| 2 |
+
# import soundfile as sf
|
| 3 |
+
# import torchaudio
|
| 4 |
+
|
| 5 |
+
import os
|
| 6 |
+
import yt_dlp
|
| 7 |
+
import streamlit as st
|
| 8 |
+
import torch
|
| 9 |
+
import whisper
|
| 10 |
+
from pyannote.audio import Pipeline
|
| 11 |
+
from pyannote.audio.pipelines.utils.hook import ProgressHook
|
| 12 |
+
from moviepy.editor import AudioFileClip
|
| 13 |
+
from transformers import pipeline
|
| 14 |
+
from openai import Client
|
| 15 |
+
from dotenv import load_dotenv
|
| 16 |
+
|
| 17 |
+
|
| 18 |
+
load_dotenv()
|
| 19 |
+
|
| 20 |
+
hf_token = os.environ.get("HF_DIARIZATION_TOKEN")
|
| 21 |
+
api_key = os.environ.get("OPENAI_API_KEY")
|
| 22 |
+
|
| 23 |
+
|
| 24 |
+
device = torch.device("cuda" if torch.cuda.is_available() else "cpu")
|
| 25 |
+
st.info(f"Usando dispositivo: {device}")
|
| 26 |
+
|
| 27 |
+
pyannote_pipeline = Pipeline.from_pretrained(
|
| 28 |
+
"pyannote/speaker-diarization-3.1",
|
| 29 |
+
use_auth_token=hf_token,
|
| 30 |
+
)
|
| 31 |
+
|
| 32 |
+
if torch.cuda.is_available():
|
| 33 |
+
pyannote_pipeline.to(torch.device("cuda"))
|
| 34 |
+
|
| 35 |
+
sumamry_pipeline = pipeline(
|
| 36 |
+
"summarization",
|
| 37 |
+
model="t5-base",
|
| 38 |
+
device=device,
|
| 39 |
+
)
|
| 40 |
+
|
| 41 |
+
whisper_model = whisper.load_model("base")
|
| 42 |
+
|
| 43 |
+
|
| 44 |
+
def transcribir_segmentos(diarization_segments, audio_path):
|
| 45 |
+
diarization_text = ""
|
| 46 |
+
audio_duration = get_audio_duration(audio_path)
|
| 47 |
+
path = 'diarization_transcription.txt'
|
| 48 |
+
if os.path.exists(path):
|
| 49 |
+
os.remove(path)
|
| 50 |
+
st.success("Borrado archivo diarization_transcription.txt anterior")
|
| 51 |
+
|
| 52 |
+
progress_bar = st.progress(0)
|
| 53 |
+
total_segments = len(diarization_segments)
|
| 54 |
+
|
| 55 |
+
with open("diarization_transcription.txt", "w", encoding="utf-8") as file:
|
| 56 |
+
for i, (start_time, end_time, speaker) in enumerate(diarization_segments):
|
| 57 |
+
if start_time < 0 or end_time > audio_duration:
|
| 58 |
+
st.error(f"Segmento fuera de los límites del audio: {start_time} - {end_time}")
|
| 59 |
+
continue
|
| 60 |
+
if end_time - start_time < 0.5:
|
| 61 |
+
continue
|
| 62 |
+
|
| 63 |
+
segment_audio_path = f"segment_{start_time}_{end_time}.wav"
|
| 64 |
+
extract_audio_segment(audio_path, segment_audio_path, start_time, end_time)
|
| 65 |
+
|
| 66 |
+
transcript = transcribe_audio_whisper_lib(segment_audio_path) # Cambiar aca el trancript
|
| 67 |
+
if transcript:
|
| 68 |
+
diarization_text += f"{speaker}: {transcript}\n"
|
| 69 |
+
file.write(f"{speaker}: {transcript}\n")
|
| 70 |
+
os.remove(segment_audio_path)
|
| 71 |
+
|
| 72 |
+
progress = (i + 1) / total_segments
|
| 73 |
+
progress_bar.progress(progress)
|
| 74 |
+
|
| 75 |
+
return diarization_text
|
| 76 |
+
|
| 77 |
+
|
| 78 |
+
def process_transcripts(diarization_text, speakers):
|
| 79 |
+
speaker_transcripts = {speaker: [] for speaker in speakers}
|
| 80 |
+
for line in diarization_text.split('\n'):
|
| 81 |
+
if line:
|
| 82 |
+
speaker = line.split(':')[0].split()[-1]
|
| 83 |
+
transcript = line.split(': ')[1]
|
| 84 |
+
speaker_transcripts[speaker].append(transcript)
|
| 85 |
+
return speaker_transcripts
|
| 86 |
+
|
| 87 |
+
|
| 88 |
+
def summarize_speaker_transcripts(speaker_transcripts):
|
| 89 |
+
summaries = {}
|
| 90 |
+
for speaker, transcripts in speaker_transcripts.items():
|
| 91 |
+
full_text = ' '.join(transcripts)
|
| 92 |
+
max_tokens = 1024 # Ajusta según el modelo y tus necesidades
|
| 93 |
+
if len(full_text) > max_tokens:
|
| 94 |
+
full_text = full_text[:max_tokens]
|
| 95 |
+
|
| 96 |
+
summary = sumamry_pipeline(full_text, max_length=150, min_length=30, do_sample=False)[0]['summary_text']
|
| 97 |
+
summaries[speaker] = summary
|
| 98 |
+
return summaries
|
| 99 |
+
|
| 100 |
+
|
| 101 |
+
def diarize_full_audio(audio_path):
|
| 102 |
+
try:
|
| 103 |
+
|
| 104 |
+
audio_duration = get_audio_duration(audio_path)
|
| 105 |
+
diarization_segments = []
|
| 106 |
+
diarization_text = ""
|
| 107 |
+
speakers = set()
|
| 108 |
+
|
| 109 |
+
st.info(f"Procesando el audio completo de {audio_duration} segundos...")
|
| 110 |
+
|
| 111 |
+
with StreamlitProgressHook() as hook:
|
| 112 |
+
diarization = pyannote_pipeline({
|
| 113 |
+
'audio': audio_path,
|
| 114 |
+
}, hook=hook)
|
| 115 |
+
|
| 116 |
+
for segment, _, speaker in diarization.itertracks(yield_label=True):
|
| 117 |
+
diarization_segments.append((segment.start, segment.end, speaker))
|
| 118 |
+
diarization_text += f"{segment.start:.2f} - {segment.end:.2f}: {speaker}\n"
|
| 119 |
+
speakers.add(speaker)
|
| 120 |
+
|
| 121 |
+
return diarization_segments, diarization_text, speakers
|
| 122 |
+
except Exception as e:
|
| 123 |
+
st.error(f"Error al realizar la diarización del audio entero: {e}")
|
| 124 |
+
return None, None, 0
|
| 125 |
+
|
| 126 |
+
|
| 127 |
+
|
| 128 |
+
|
| 129 |
+
def extract_audio_segment(input_audio_path, output_audio_path, start_time, end_time):
|
| 130 |
+
try:
|
| 131 |
+
audio = AudioFileClip(input_audio_path)
|
| 132 |
+
audio_subclip = audio.subclip(start_time, end_time)
|
| 133 |
+
audio_subclip.write_audiofile(output_audio_path)
|
| 134 |
+
except Exception as e:
|
| 135 |
+
st.error(f"Error al extraer el segmento de audio: {e}")
|
| 136 |
+
finally:
|
| 137 |
+
if 'audio' in locals():
|
| 138 |
+
audio.close()
|
| 139 |
+
if 'audio_subclip' in locals():
|
| 140 |
+
audio_subclip.close()
|
| 141 |
+
|
| 142 |
+
|
| 143 |
+
def transcribe_audio_whisper_lib(audio_path):
|
| 144 |
+
try:
|
| 145 |
+
with open(audio_path, "rb") as audio_file:
|
| 146 |
+
transcript = whisper_model.transcribe(audio_path)
|
| 147 |
+
return transcript["text"]
|
| 148 |
+
except Exception as e:
|
| 149 |
+
st.error(f"Error al transcribir el audio: {e}")
|
| 150 |
+
st.stop()
|
| 151 |
+
|
| 152 |
+
|
| 153 |
+
|
| 154 |
+
def download_youtube_audio(url, progress_bar):
|
| 155 |
+
# No incluyas la extensión '.wav' en el 'outtmpl'
|
| 156 |
+
output_path = os.path.join('temp_audio')
|
| 157 |
+
if os.path.exists(output_path + '.wav'):
|
| 158 |
+
os.remove(output_path + '.wav')
|
| 159 |
+
st.success("Borrado archivo temp_audio.wav anterior")
|
| 160 |
+
|
| 161 |
+
ydl_opts = {
|
| 162 |
+
'format': 'bestaudio/best',
|
| 163 |
+
'postprocessors': [{
|
| 164 |
+
'key': 'FFmpegExtractAudio',
|
| 165 |
+
'preferredcodec': 'wav',
|
| 166 |
+
'preferredquality': '192',
|
| 167 |
+
}],
|
| 168 |
+
'outtmpl': output_path,
|
| 169 |
+
'progress_hooks': [lambda d: update_progress(d, progress_bar)], # Usa la función de progreso
|
| 170 |
+
'noplaylist': True,
|
| 171 |
+
'user-agent': 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) '
|
| 172 |
+
'Chrome/58.0.3029.110 Safari/537.3',
|
| 173 |
+
'cookiefile': 'cookies.txt', # Aquí incluyes el archivo de cookies
|
| 174 |
+
}
|
| 175 |
+
|
| 176 |
+
try:
|
| 177 |
+
with yt_dlp.YoutubeDL(ydl_opts) as ydl:
|
| 178 |
+
ydl.download([url])
|
| 179 |
+
progress_bar.progress(1.0) # Asegúrate de que la barra de progreso llegue al 100% cuando termine
|
| 180 |
+
|
| 181 |
+
# Verifica si el archivo existe con la extensión .wav
|
| 182 |
+
final_output_path = output_path + '.wav'
|
| 183 |
+
st.write("Checking if file exists:", os.path.exists(final_output_path))
|
| 184 |
+
return final_output_path if os.path.exists(final_output_path) else None
|
| 185 |
+
except yt_dlp.utils.DownloadError as e:
|
| 186 |
+
st.error(f"Ocurrió un error al descargar el audio: {e}")
|
| 187 |
+
return None
|
| 188 |
+
except Exception as e:
|
| 189 |
+
st.error(f"Ocurrió un error: {e}")
|
| 190 |
+
return None
|
| 191 |
+
|
| 192 |
+
|
| 193 |
+
def get_audio_duration(audio_path):
|
| 194 |
+
try:
|
| 195 |
+
audio = AudioFileClip(audio_path)
|
| 196 |
+
return audio.duration
|
| 197 |
+
except Exception as e:
|
| 198 |
+
st.error(f"Error al obtener la duración del audio: {e}")
|
| 199 |
+
return 0
|
| 200 |
+
|
| 201 |
+
def update_progress(d, progress_bar):
|
| 202 |
+
if d['status'] == 'downloading':
|
| 203 |
+
total_bytes = d.get('total_bytes', None)
|
| 204 |
+
downloaded_bytes = d.get('downloaded_bytes', 0)
|
| 205 |
+
|
| 206 |
+
if total_bytes:
|
| 207 |
+
progress = downloaded_bytes / total_bytes
|
| 208 |
+
progress_bar.progress(progress) # Actualiza la barra de progreso con el porcentaje descargado
|
| 209 |
+
else:
|
| 210 |
+
progress_bar.progress(0.1) # Valor predeterminado si no se conoce el tamaño total
|
| 211 |
+
|
| 212 |
+
elif d['status'] == 'finished':
|
| 213 |
+
progress_bar.progress(0.9) # 90% cuando la descarga termina
|
| 214 |
+
elif d['status'] == 'postprocessing':
|
| 215 |
+
progress_bar.progress(0.95) # 95% durante el procesamiento de audio
|
| 216 |
+
|
| 217 |
+
class StreamlitProgressHook(ProgressHook):
|
| 218 |
+
def __init__(self, transient: bool = False):
|
| 219 |
+
super().__init__(transient)
|
| 220 |
+
self.progress_text = st.empty()
|
| 221 |
+
|
| 222 |
+
def __call__(self, step_name, step_artifact, file=None, total=None, completed=None):
|
| 223 |
+
super().__call__(step_name, step_artifact, file, total, completed)
|
| 224 |
+
if total is not None and completed is not None:
|
| 225 |
+
progress_message = f"{step_name:<20} ━ {'━' * int(30 * (completed / total))} {completed / total:.0%}"
|
| 226 |
+
else:
|
| 227 |
+
progress_message = f"{step_name:<20} ━ Progress data unavailable"
|
| 228 |
+
self.progress_text.text(progress_message)
|
| 229 |
+
|
| 230 |
+
|
| 231 |
+
# def diarize_in_segments(audio_path, segment_duration=300):
|
| 232 |
+
# try:
|
| 233 |
+
# audio_duration = get_audio_duration(audio_path)
|
| 234 |
+
# diarization_segments = []
|
| 235 |
+
# diarization_text = ""
|
| 236 |
+
# speakers = set()
|
| 237 |
+
#
|
| 238 |
+
# # Procesar cada segmento por separado
|
| 239 |
+
# for start_time in range(0, int(audio_duration), segment_duration):
|
| 240 |
+
# end_time = min(start_time + segment_duration, audio_duration)
|
| 241 |
+
# st.info(f"Procesando segmento desde {start_time} hasta {end_time} segundos...")
|
| 242 |
+
#
|
| 243 |
+
# with StreamlitProgressHook() as hook:
|
| 244 |
+
# diarization = pyannote_pipeline({
|
| 245 |
+
# 'audio': audio_path,
|
| 246 |
+
# 'start': start_time,
|
| 247 |
+
# 'end': end_time
|
| 248 |
+
# }, hook=hook)
|
| 249 |
+
#
|
| 250 |
+
# for segment, _, speaker in diarization.itertracks(yield_label=True):
|
| 251 |
+
# diarization_segments.append((segment.start, segment.end, speaker))
|
| 252 |
+
# diarization_text += f"{segment.start:.2f} - {segment.end:.2f}: {speaker}\n"
|
| 253 |
+
# speakers.add(speaker)
|
| 254 |
+
#
|
| 255 |
+
# return diarization_segments, diarization_text, speakers
|
| 256 |
+
# except Exception as e:
|
| 257 |
+
# st.error(f"Error al realizar la diarización de un segmento: {e}")
|
| 258 |
+
# return None, None, 0
|
| 259 |
+
|
| 260 |
+
# def transcribe_audio_whisper_transformers(audio_path):
|
| 261 |
+
# try:
|
| 262 |
+
# # Leer el archivo de audio y convertirlo en un numpy ndarray
|
| 263 |
+
# audio_data, _ = sf.read(audio_path)
|
| 264 |
+
#
|
| 265 |
+
# # Convertir a un solo canal si es necesario
|
| 266 |
+
# if len(audio_data.shape) > 1:
|
| 267 |
+
# audio_data = np.mean(audio_data, axis=1)
|
| 268 |
+
#
|
| 269 |
+
# # Transcribir el audio usando el modelo Whisper
|
| 270 |
+
# transcript = whisper_pipeline(audio_data, return_timestamps=True)
|
| 271 |
+
# return transcript['text']
|
| 272 |
+
# except Exception as e:
|
| 273 |
+
# st.error(f"Error al transcribir el audio: {e}")
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 274 |
# st.stop()
|