Update app.py
Browse files
app.py
CHANGED
@@ -8,58 +8,21 @@ if not replicate_token:
|
|
8 |
raise ValueError("No se ha encontrado el token de API de Replicate.")
|
9 |
|
10 |
# Funci贸n para dividir el archivo de audio en segmentos de duraci贸n definida (en milisegundos)
|
11 |
-
def dividir_audio(audio_path, segment_duration_ms
|
12 |
audio = AudioSegment.from_file(audio_path)
|
13 |
audio_length = len(audio)
|
14 |
segments = []
|
15 |
|
16 |
-
# Divide el audio en fragmentos de la duraci贸n especificada
|
17 |
for i in range(0, audio_length, segment_duration_ms):
|
18 |
-
segment = audio[i:i + segment_duration_ms] #
|
19 |
segment_path = f"segment_{i // (60 * 1000)}.wav" # Nombre del archivo con el 铆ndice del minuto
|
20 |
segment.export(segment_path, format="wav") # Exporta el fragmento como un archivo WAV
|
21 |
|
22 |
-
# Verifica el tama帽o del archivo y asegura que no supere el l铆mite de 10MB
|
23 |
if os.path.getsize(segment_path) > 10 * 1024 * 1024: # 10 MB
|
24 |
-
print(f"Warning: Segment {segment_path} exceeds 10MB
|
25 |
-
return dividir_audio(audio_path, segment_duration_ms // 2) # Recursivamente reduce el tama帽o del segmento
|
26 |
|
27 |
segments.append(segment_path)
|
28 |
|
29 |
return segments
|
30 |
-
|
31 |
-
# Funci贸n para transcribir el audio
|
32 |
-
def transcribe_audio(audio_file):
|
33 |
-
# Cargar el archivo de audio completo
|
34 |
-
audio = AudioSegment.from_file(audio_file)
|
35 |
-
audio_duration_minutes = len(audio) / (1000 * 60) # Duraci贸n en minutos
|
36 |
-
|
37 |
-
# Si el audio dura m谩s de 5 minutos, dividirlo en segmentos de 5 minutos
|
38 |
-
if audio_duration_minutes > 5:
|
39 |
-
segments = dividir_audio(audio_file, segment_duration_ms=5 * 60 * 1000) # 5 minutos en milisegundos
|
40 |
-
else:
|
41 |
-
segments = [audio_file] # Si es menor de 5 minutos, no dividir
|
42 |
-
|
43 |
-
# Almacenar todas las transcripciones
|
44 |
-
all_transcriptions = []
|
45 |
-
|
46 |
-
# Procesar cada segmento individualmente
|
47 |
-
for segment_path in segments:
|
48 |
-
with open(segment_path, "rb") as audio:
|
49 |
-
output = replicate.run(
|
50 |
-
"vaibhavs10/incredibly-fast-whisper:3ab86df6c8f54c11309d4d1f930ac292bad43ace52d10c80d87eb258b3c9f79c",
|
51 |
-
input={
|
52 |
-
"task": "transcribe",
|
53 |
-
"audio": audio, # El archivo de audio cargado en Gradio
|
54 |
-
"language": "None", # Detecta autom谩ticamente el idioma
|
55 |
-
"timestamp": "chunk", # Incluye marcas de tiempo
|
56 |
-
"batch_size": 64,
|
57 |
-
"diarise_audio": False
|
58 |
-
}
|
59 |
-
)
|
60 |
-
# Almacenar la transcripci贸n del segmento
|
61 |
-
all_transcriptions.append(output['text'])
|
62 |
-
|
63 |
-
# Combina todas las transcripciones en una sola cadena
|
64 |
-
full_transcription = "\n".join(all_transcriptions)
|
65 |
-
return full_transcription # Devuelve la transcripci贸n completa
|
|
|
8 |
raise ValueError("No se ha encontrado el token de API de Replicate.")
|
9 |
|
10 |
# Funci贸n para dividir el archivo de audio en segmentos de duraci贸n definida (en milisegundos)
|
11 |
+
def dividir_audio(audio_path, segment_duration_ms):
|
12 |
audio = AudioSegment.from_file(audio_path)
|
13 |
audio_length = len(audio)
|
14 |
segments = []
|
15 |
|
16 |
+
# Divide el audio en fragmentos de la duraci贸n especificada (5 minutos en milisegundos)
|
17 |
for i in range(0, audio_length, segment_duration_ms):
|
18 |
+
segment = audio[i:i + segment_duration_ms] # Cada fragmento de hasta 5 minutos
|
19 |
segment_path = f"segment_{i // (60 * 1000)}.wav" # Nombre del archivo con el 铆ndice del minuto
|
20 |
segment.export(segment_path, format="wav") # Exporta el fragmento como un archivo WAV
|
21 |
|
22 |
+
# Verifica el tama帽o del archivo y asegura que no supere el l铆mite de 10MB, ajusta si es necesario
|
23 |
if os.path.getsize(segment_path) > 10 * 1024 * 1024: # 10 MB
|
24 |
+
print(f"Warning: Segment {segment_path} exceeds 10MB, consider reducing segment duration.")
|
|
|
25 |
|
26 |
segments.append(segment_path)
|
27 |
|
28 |
return segments
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|