Spaces:
Build error
Build error
Create app.py
Browse files
app.py
ADDED
|
@@ -0,0 +1,128 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import os
|
| 2 |
+
import pyannote.audio
|
| 3 |
+
import streamlit as st
|
| 4 |
+
from transformers import pipeline
|
| 5 |
+
from dotenv import load_dotenv
|
| 6 |
+
from libs.utils import extract_audio_segment, transcribe_audio, download_youtube_audio, get_audio_duration, StreamlitProgressHook
|
| 7 |
+
|
| 8 |
+
load_dotenv()
|
| 9 |
+
|
| 10 |
+
token = os.environ.get("HF_DIARIZATION_TOKEN")
|
| 11 |
+
|
| 12 |
+
|
| 13 |
+
summarizer = pipeline("summarization", model="facebook/bart-large-cnn")
|
| 14 |
+
|
| 15 |
+
|
| 16 |
+
st.title("Transcripci贸n y Diarizaci贸n de YouTube")
|
| 17 |
+
|
| 18 |
+
url = st.text_input("URL de YouTube:")
|
| 19 |
+
if st.button("Transcribir y Diarizar"):
|
| 20 |
+
if url:
|
| 21 |
+
progress_bar = st.progress(0)
|
| 22 |
+
with st.spinner("Descargando ..."):
|
| 23 |
+
try:
|
| 24 |
+
audio_path = download_youtube_audio(url, progress_bar)
|
| 25 |
+
if os.path.exists(audio_path):
|
| 26 |
+
st.success("Video descargado con 茅xito.")
|
| 27 |
+
else:
|
| 28 |
+
st.error(f"Error al descargar el video de YouTube:")
|
| 29 |
+
st.stop()
|
| 30 |
+
except Exception as e:
|
| 31 |
+
st.error(f"Error al procesar el video de YouTube: {e}")
|
| 32 |
+
st.stop()
|
| 33 |
+
|
| 34 |
+
with st.spinner("Realizando diarizaci贸n..."):
|
| 35 |
+
try:
|
| 36 |
+
pipeline = pyannote.audio.Pipeline.from_pretrained("pyannote/speaker-diarization-3.1",
|
| 37 |
+
use_auth_token=token)
|
| 38 |
+
|
| 39 |
+
with StreamlitProgressHook() as hook:
|
| 40 |
+
diarization = pipeline(audio_path, hook=hook)
|
| 41 |
+
|
| 42 |
+
diarization_segments = []
|
| 43 |
+
diarization_text = ""
|
| 44 |
+
speakers = set()
|
| 45 |
+
for segment, _, speaker in diarization.itertracks(yield_label=True):
|
| 46 |
+
diarization_segments.append((segment.start, segment.end, speaker))
|
| 47 |
+
diarization_text += f"{segment.start:.2f} - {segment.end:.2f}: Speaker {speaker}\n"
|
| 48 |
+
speakers.add(speaker)
|
| 49 |
+
|
| 50 |
+
num_speakers = len(speakers)
|
| 51 |
+
|
| 52 |
+
path = 'diarization.txt'
|
| 53 |
+
if os.path.exists(path):
|
| 54 |
+
os.remove(path)
|
| 55 |
+
st.success("Borrado archivo diarization.txt anterior")
|
| 56 |
+
|
| 57 |
+
with open("diarization.txt", "w") as file:
|
| 58 |
+
file.write(diarization_text)
|
| 59 |
+
|
| 60 |
+
st.text_area("Diarizaci贸n", diarization_text, height=200)
|
| 61 |
+
st.success(
|
| 62 |
+
f"Diarizaci贸n completada y guardada en diarization.txt. N煤mero de hablantes detectados: {num_speakers}")
|
| 63 |
+
except Exception as e:
|
| 64 |
+
st.error(f"Error al realizar la diarizaci贸n: {e}")
|
| 65 |
+
st.stop()
|
| 66 |
+
|
| 67 |
+
with st.spinner("Transcribiendo segmentos de audio..."):
|
| 68 |
+
try:
|
| 69 |
+
diarization_text = ""
|
| 70 |
+
audio_duration = get_audio_duration(audio_path)
|
| 71 |
+
|
| 72 |
+
path = 'diarization_transcription.txt'
|
| 73 |
+
if os.path.exists(path):
|
| 74 |
+
os.remove(path)
|
| 75 |
+
st.success("Borrado archivo diarization_transcription.txt anterior")
|
| 76 |
+
|
| 77 |
+
# Crear barra de progreso
|
| 78 |
+
progress_bar = st.progress(0)
|
| 79 |
+
total_segments = len(diarization_segments)
|
| 80 |
+
|
| 81 |
+
with open("diarization_transcription.txt", "w", encoding="utf-8") as file:
|
| 82 |
+
for i, (start_time, end_time, speaker) in enumerate(diarization_segments):
|
| 83 |
+
if start_time < 0 or end_time > audio_duration:
|
| 84 |
+
st.error(f"Segmento fuera de los l铆mites del audio: {start_time} - {end_time}")
|
| 85 |
+
continue
|
| 86 |
+
if end_time - start_time < 0.5:
|
| 87 |
+
continue
|
| 88 |
+
|
| 89 |
+
segment_audio_path = f"segment_{start_time}_{end_time}.wav"
|
| 90 |
+
extract_audio_segment(audio_path, segment_audio_path, start_time, end_time)
|
| 91 |
+
transcript = transcribe_audio(segment_audio_path)
|
| 92 |
+
if transcript:
|
| 93 |
+
diarization_text += f"{speaker}: {transcript}\n"
|
| 94 |
+
file.write(f"{speaker}: {transcript}\n")
|
| 95 |
+
os.remove(segment_audio_path)
|
| 96 |
+
|
| 97 |
+
progress = (i + 1) / total_segments
|
| 98 |
+
progress_bar.progress(progress)
|
| 99 |
+
|
| 100 |
+
st.success("Transcripci贸n completada.")
|
| 101 |
+
st.text_area("Diarizaci贸n y Transcripci贸n:", diarization_text)
|
| 102 |
+
st.success("Diarizaci贸n y transcripci贸n guardadas en diarization_transcription.txt")
|
| 103 |
+
except Exception as e:
|
| 104 |
+
st.error(f"Error al transcribir los segmentos de audio: {e}")
|
| 105 |
+
|
| 106 |
+
with st.spinner("Resumiendo hablantes..."):
|
| 107 |
+
try:
|
| 108 |
+
speaker_transcripts = {speaker: [] for speaker in speakers}
|
| 109 |
+
for line in diarization_text.split('\n'):
|
| 110 |
+
if line:
|
| 111 |
+
speaker = line.split(':')[0].split()[-1]
|
| 112 |
+
transcript = line.split(': ')[1]
|
| 113 |
+
speaker_transcripts[speaker].append(transcript)
|
| 114 |
+
|
| 115 |
+
for speaker, transcripts in speaker_transcripts.items():
|
| 116 |
+
full_text = ' '.join(transcripts)
|
| 117 |
+
max_tokens = 1024 # Ajusta seg煤n el modelo y tus necesidades
|
| 118 |
+
if len(full_text) > max_tokens:
|
| 119 |
+
full_text = full_text[:max_tokens]
|
| 120 |
+
|
| 121 |
+
# Usar el modelo de Hugging Face para resumir
|
| 122 |
+
summary = summarizer(full_text, max_length=150, do_sample=False)[0]['summary_text']
|
| 123 |
+
|
| 124 |
+
st.text_area(f"Resumen del Hablante {speaker}", summary, height=200)
|
| 125 |
+
except Exception as e:
|
| 126 |
+
st.error(f"Error al resumir por hablante: {e}")
|
| 127 |
+
else:
|
| 128 |
+
st.warning("Por favor, ingresa una URL de YouTube.")
|