Spaces:
Build error
Build error
Update utils_hf.py
Browse files- utils_hf.py +60 -1
utils_hf.py
CHANGED
|
@@ -19,6 +19,7 @@ load_dotenv()
|
|
| 19 |
hf_token = os.environ.get("HF_DIARIZATION_TOKEN")
|
| 20 |
api_key = os.environ.get("OPENAI_API_KEY")
|
| 21 |
|
|
|
|
| 22 |
|
| 23 |
device = torch.device("cuda" if torch.cuda.is_available() else "cpu")
|
| 24 |
st.info(f"Usando dispositivo: {device}")
|
|
@@ -33,7 +34,7 @@ if torch.cuda.is_available():
|
|
| 33 |
|
| 34 |
sumamry_pipeline = pipeline(
|
| 35 |
"text-generation",
|
| 36 |
-
model="
|
| 37 |
device=device,
|
| 38 |
)
|
| 39 |
|
|
@@ -149,6 +150,20 @@ def transcribe_audio_whisper_lib(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')
|
|
@@ -227,3 +242,47 @@ class StreamlitProgressHook(ProgressHook):
|
|
| 227 |
progress_message = f"{step_name:<20} ━ Progress data unavailable"
|
| 228 |
self.progress_text.text(progress_message)
|
| 229 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 19 |
hf_token = os.environ.get("HF_DIARIZATION_TOKEN")
|
| 20 |
api_key = os.environ.get("OPENAI_API_KEY")
|
| 21 |
|
| 22 |
+
client = Client(api_key=api_key)
|
| 23 |
|
| 24 |
device = torch.device("cuda" if torch.cuda.is_available() else "cpu")
|
| 25 |
st.info(f"Usando dispositivo: {device}")
|
|
|
|
| 34 |
|
| 35 |
sumamry_pipeline = pipeline(
|
| 36 |
"text-generation",
|
| 37 |
+
model="gpt2",
|
| 38 |
device=device,
|
| 39 |
)
|
| 40 |
|
|
|
|
| 150 |
st.stop()
|
| 151 |
|
| 152 |
|
| 153 |
+
def transcribe_audio_openai(audio_path):
|
| 154 |
+
try:
|
| 155 |
+
with open(audio_path, "rb") as audio_file:
|
| 156 |
+
transcript = client.audio.transcriptions.create(
|
| 157 |
+
model="whisper-1",
|
| 158 |
+
file=audio_file,
|
| 159 |
+
response_format="json"
|
| 160 |
+
)
|
| 161 |
+
return transcript.text
|
| 162 |
+
except Exception as e:
|
| 163 |
+
st.error(f"Error al transcribir el audio: {e}")
|
| 164 |
+
st.stop()
|
| 165 |
+
|
| 166 |
+
|
| 167 |
def download_youtube_audio(url, progress_bar):
|
| 168 |
# No incluyas la extensión '.wav' en el 'outtmpl'
|
| 169 |
output_path = os.path.join('temp_audio')
|
|
|
|
| 242 |
progress_message = f"{step_name:<20} ━ Progress data unavailable"
|
| 243 |
self.progress_text.text(progress_message)
|
| 244 |
|
| 245 |
+
# def diarize_in_segments(audio_path, segment_duration=300):
|
| 246 |
+
# try:
|
| 247 |
+
# audio_duration = get_audio_duration(audio_path)
|
| 248 |
+
# diarization_segments = []
|
| 249 |
+
# diarization_text = ""
|
| 250 |
+
# speakers = set()
|
| 251 |
+
#
|
| 252 |
+
# # Procesar cada segmento por separado
|
| 253 |
+
# for start_time in range(0, int(audio_duration), segment_duration):
|
| 254 |
+
# end_time = min(start_time + segment_duration, audio_duration)
|
| 255 |
+
# st.info(f"Procesando segmento desde {start_time} hasta {end_time} segundos...")
|
| 256 |
+
#
|
| 257 |
+
# with StreamlitProgressHook() as hook:
|
| 258 |
+
# diarization = pyannote_pipeline({
|
| 259 |
+
# 'audio': audio_path,
|
| 260 |
+
# 'start': start_time,
|
| 261 |
+
# 'end': end_time
|
| 262 |
+
# }, hook=hook)
|
| 263 |
+
#
|
| 264 |
+
# for segment, _, speaker in diarization.itertracks(yield_label=True):
|
| 265 |
+
# diarization_segments.append((segment.start, segment.end, speaker))
|
| 266 |
+
# diarization_text += f"{segment.start:.2f} - {segment.end:.2f}: {speaker}\n"
|
| 267 |
+
# speakers.add(speaker)
|
| 268 |
+
#
|
| 269 |
+
# return diarization_segments, diarization_text, speakers
|
| 270 |
+
# except Exception as e:
|
| 271 |
+
# st.error(f"Error al realizar la diarización de un segmento: {e}")
|
| 272 |
+
# return None, None, 0
|
| 273 |
+
|
| 274 |
+
# def transcribe_audio_whisper_transformers(audio_path):
|
| 275 |
+
# try:
|
| 276 |
+
# # Leer el archivo de audio y convertirlo en un numpy ndarray
|
| 277 |
+
# audio_data, _ = sf.read(audio_path)
|
| 278 |
+
#
|
| 279 |
+
# # Convertir a un solo canal si es necesario
|
| 280 |
+
# if len(audio_data.shape) > 1:
|
| 281 |
+
# audio_data = np.mean(audio_data, axis=1)
|
| 282 |
+
#
|
| 283 |
+
# # Transcribir el audio usando el modelo Whisper
|
| 284 |
+
# transcript = whisper_pipeline(audio_data, return_timestamps=True)
|
| 285 |
+
# return transcript['text']
|
| 286 |
+
# except Exception as e:
|
| 287 |
+
# st.error(f"Error al transcribir el audio: {e}")
|
| 288 |
+
# st.stop()
|