Spaces:
Build error
Build error
Update utils_hf.py
Browse files- utils_hf.py +9 -54
utils_hf.py
CHANGED
|
@@ -14,7 +14,6 @@ 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")
|
|
@@ -33,12 +32,12 @@ if torch.cuda.is_available():
|
|
| 33 |
pyannote_pipeline.to(torch.device("cuda"))
|
| 34 |
|
| 35 |
sumamry_pipeline = pipeline(
|
| 36 |
-
"
|
| 37 |
-
model="
|
| 38 |
device=device,
|
| 39 |
)
|
| 40 |
|
| 41 |
-
whisper_model = whisper.load_model("
|
| 42 |
|
| 43 |
|
| 44 |
def transcribir_segmentos(diarization_segments, audio_path):
|
|
@@ -63,7 +62,7 @@ def transcribir_segmentos(diarization_segments, audio_path):
|
|
| 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)
|
| 67 |
if transcript:
|
| 68 |
diarization_text += f"{speaker}: {transcript}\n"
|
| 69 |
file.write(f"{speaker}: {transcript}\n")
|
|
@@ -93,7 +92,9 @@ def summarize_speaker_transcripts(speaker_transcripts):
|
|
| 93 |
if len(full_text) > max_tokens:
|
| 94 |
full_text = full_text[:max_tokens]
|
| 95 |
|
| 96 |
-
|
|
|
|
|
|
|
| 97 |
summaries[speaker] = summary
|
| 98 |
return summaries
|
| 99 |
|
|
@@ -124,8 +125,6 @@ def diarize_full_audio(audio_path):
|
|
| 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)
|
|
@@ -150,7 +149,6 @@ def transcribe_audio_whisper_lib(audio_path):
|
|
| 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')
|
|
@@ -198,6 +196,7 @@ def get_audio_duration(audio_path):
|
|
| 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)
|
|
@@ -214,6 +213,7 @@ def update_progress(d, progress_bar):
|
|
| 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)
|
|
@@ -227,48 +227,3 @@ class StreamlitProgressHook(ProgressHook):
|
|
| 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()
|
|
|
|
| 14 |
from openai import Client
|
| 15 |
from dotenv import load_dotenv
|
| 16 |
|
|
|
|
| 17 |
load_dotenv()
|
| 18 |
|
| 19 |
hf_token = os.environ.get("HF_DIARIZATION_TOKEN")
|
|
|
|
| 32 |
pyannote_pipeline.to(torch.device("cuda"))
|
| 33 |
|
| 34 |
sumamry_pipeline = pipeline(
|
| 35 |
+
"text-generation",
|
| 36 |
+
model="gpt-3.5-turbo",
|
| 37 |
device=device,
|
| 38 |
)
|
| 39 |
|
| 40 |
+
whisper_model = whisper.load_model("turbo")
|
| 41 |
|
| 42 |
|
| 43 |
def transcribir_segmentos(diarization_segments, audio_path):
|
|
|
|
| 62 |
segment_audio_path = f"segment_{start_time}_{end_time}.wav"
|
| 63 |
extract_audio_segment(audio_path, segment_audio_path, start_time, end_time)
|
| 64 |
|
| 65 |
+
transcript = transcribe_audio_whisper_lib(segment_audio_path) # Cambiar aca el trancript
|
| 66 |
if transcript:
|
| 67 |
diarization_text += f"{speaker}: {transcript}\n"
|
| 68 |
file.write(f"{speaker}: {transcript}\n")
|
|
|
|
| 92 |
if len(full_text) > max_tokens:
|
| 93 |
full_text = full_text[:max_tokens]
|
| 94 |
|
| 95 |
+
prompt = f"Por favor, resume el siguiente texto:\n\n{full_text}"
|
| 96 |
+
|
| 97 |
+
summary = sumamry_pipeline(prompt, max_length=150, min_length=30, do_sample=False)[0]['text']
|
| 98 |
summaries[speaker] = summary
|
| 99 |
return summaries
|
| 100 |
|
|
|
|
| 125 |
return None, None, 0
|
| 126 |
|
| 127 |
|
|
|
|
|
|
|
| 128 |
def extract_audio_segment(input_audio_path, output_audio_path, start_time, end_time):
|
| 129 |
try:
|
| 130 |
audio = AudioFileClip(input_audio_path)
|
|
|
|
| 149 |
st.stop()
|
| 150 |
|
| 151 |
|
|
|
|
| 152 |
def download_youtube_audio(url, progress_bar):
|
| 153 |
# No incluyas la extensión '.wav' en el 'outtmpl'
|
| 154 |
output_path = os.path.join('temp_audio')
|
|
|
|
| 196 |
st.error(f"Error al obtener la duración del audio: {e}")
|
| 197 |
return 0
|
| 198 |
|
| 199 |
+
|
| 200 |
def update_progress(d, progress_bar):
|
| 201 |
if d['status'] == 'downloading':
|
| 202 |
total_bytes = d.get('total_bytes', None)
|
|
|
|
| 213 |
elif d['status'] == 'postprocessing':
|
| 214 |
progress_bar.progress(0.95) # 95% durante el procesamiento de audio
|
| 215 |
|
| 216 |
+
|
| 217 |
class StreamlitProgressHook(ProgressHook):
|
| 218 |
def __init__(self, transient: bool = False):
|
| 219 |
super().__init__(transient)
|
|
|
|
| 227 |
progress_message = f"{step_name:<20} ━ Progress data unavailable"
|
| 228 |
self.progress_text.text(progress_message)
|
| 229 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|