Update app.py
Browse files
app.py
CHANGED
@@ -2,6 +2,7 @@ import os
|
|
2 |
import gradio as gr
|
3 |
import replicate
|
4 |
from pydub import AudioSegment
|
|
|
5 |
|
6 |
# Aseg煤rate de que REPLICATE_API_TOKEN est茅 configurado en las variables de entorno
|
7 |
replicate_token = os.getenv("REPLICATE_API_TOKEN")
|
@@ -10,7 +11,7 @@ if not replicate_token:
|
|
10 |
raise ValueError("No se ha encontrado el token de API de Replicate.")
|
11 |
|
12 |
# Funci贸n para dividir el archivo de audio en segmentos de duraci贸n definida (en milisegundos)
|
13 |
-
def dividir_audio(audio_path, segment_duration_ms):
|
14 |
audio = AudioSegment.from_file(audio_path)
|
15 |
audio_length = len(audio)
|
16 |
segments = []
|
@@ -30,8 +31,25 @@ def dividir_audio(audio_path, segment_duration_ms):
|
|
30 |
|
31 |
return segments
|
32 |
|
33 |
-
# Funci贸n para
|
34 |
-
def
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
35 |
# Cargar el archivo de audio completo
|
36 |
audio = AudioSegment.from_file(audio_file)
|
37 |
audio_duration_minutes = len(audio) / (1000 * 60) # Duraci贸n en minutos
|
@@ -45,35 +63,44 @@ def transcribe_audio(audio_file):
|
|
45 |
# Almacenar todas las transcripciones
|
46 |
all_transcriptions = []
|
47 |
|
48 |
-
# Procesar cada segmento individualmente
|
49 |
-
for segment_path in segments:
|
50 |
with open(segment_path, "rb") as audio:
|
51 |
-
|
52 |
-
|
53 |
-
|
54 |
-
"
|
55 |
-
|
56 |
-
|
57 |
-
|
58 |
-
|
59 |
-
|
60 |
-
|
61 |
-
|
62 |
-
|
63 |
-
|
64 |
-
|
|
|
|
|
|
|
|
|
|
|
65 |
# Combina todas las transcripciones en una sola cadena
|
66 |
full_transcription = "\n".join(all_transcriptions)
|
67 |
-
|
|
|
|
|
|
|
68 |
|
69 |
# Crea la interfaz de Gradio
|
70 |
with gr.Blocks() as demo:
|
71 |
-
gr.Markdown("# Transcriptor de Audio a Texto")
|
72 |
audio_input = gr.Audio(type="filepath", label="Sube tu archivo de audio")
|
73 |
output_text = gr.Textbox(label="Transcripci贸n")
|
|
|
74 |
|
75 |
-
transcribe_button = gr.Button("Transcribir")
|
76 |
-
transcribe_button.click(fn=
|
77 |
|
78 |
# Ejecuta la aplicaci贸n
|
79 |
demo.launch()
|
|
|
2 |
import gradio as gr
|
3 |
import replicate
|
4 |
from pydub import AudioSegment
|
5 |
+
from fpdf import FPDF
|
6 |
|
7 |
# Aseg煤rate de que REPLICATE_API_TOKEN est茅 configurado en las variables de entorno
|
8 |
replicate_token = os.getenv("REPLICATE_API_TOKEN")
|
|
|
11 |
raise ValueError("No se ha encontrado el token de API de Replicate.")
|
12 |
|
13 |
# Funci贸n para dividir el archivo de audio en segmentos de duraci贸n definida (en milisegundos)
|
14 |
+
def dividir_audio(audio_path, segment_duration_ms=5*60*1000):
|
15 |
audio = AudioSegment.from_file(audio_path)
|
16 |
audio_length = len(audio)
|
17 |
segments = []
|
|
|
31 |
|
32 |
return segments
|
33 |
|
34 |
+
# Funci贸n para crear el PDF
|
35 |
+
def crear_pdf(transcripcion):
|
36 |
+
pdf = FPDF()
|
37 |
+
pdf.add_page()
|
38 |
+
|
39 |
+
pdf.set_font("Arial", size=12)
|
40 |
+
|
41 |
+
# Agregar la transcripci贸n al PDF l铆nea por l铆nea
|
42 |
+
for line in transcripcion.split("\n"):
|
43 |
+
pdf.multi_cell(0, 10, line)
|
44 |
+
|
45 |
+
# Guardar el archivo PDF en la carpeta actual
|
46 |
+
pdf_path = "/mnt/data/transcripcion_audio.pdf"
|
47 |
+
pdf.output(pdf_path)
|
48 |
+
|
49 |
+
return pdf_path
|
50 |
+
|
51 |
+
# Funci贸n para transcribir los segmentos de audio y generar el PDF
|
52 |
+
def transcribe_audio_y_pdf(audio_file, progress=gr.Progress()):
|
53 |
# Cargar el archivo de audio completo
|
54 |
audio = AudioSegment.from_file(audio_file)
|
55 |
audio_duration_minutes = len(audio) / (1000 * 60) # Duraci贸n en minutos
|
|
|
63 |
# Almacenar todas las transcripciones
|
64 |
all_transcriptions = []
|
65 |
|
66 |
+
# Procesar cada segmento individualmente y mostrar progresivamente
|
67 |
+
for index, segment_path in enumerate(segments):
|
68 |
with open(segment_path, "rb") as audio:
|
69 |
+
try:
|
70 |
+
progress(index / len(segments)) # Actualizar el progreso
|
71 |
+
output = replicate.run(
|
72 |
+
"vaibhavs10/incredibly-fast-whisper:3ab86df6c8f54c11309d4d1f930ac292bad43ace52d10c80d87eb258b3c9f79c",
|
73 |
+
input={
|
74 |
+
"task": "transcribe",
|
75 |
+
"audio": audio, # El archivo de audio cargado
|
76 |
+
"language": "None", # Detecta autom谩ticamente el idioma
|
77 |
+
"timestamp": "chunk", # Incluye marcas de tiempo
|
78 |
+
"batch_size": 64,
|
79 |
+
"diarise_audio": False
|
80 |
+
}
|
81 |
+
)
|
82 |
+
transcription = output['text']
|
83 |
+
all_transcriptions.append(f"Segment {index + 1}:\n{transcription}")
|
84 |
+
|
85 |
+
except Exception as e:
|
86 |
+
return f"Error transcribiendo el segmento {index + 1}: {e}"
|
87 |
+
|
88 |
# Combina todas las transcripciones en una sola cadena
|
89 |
full_transcription = "\n".join(all_transcriptions)
|
90 |
+
|
91 |
+
# Crear PDF y devolver la ruta del archivo
|
92 |
+
pdf_path = crear_pdf(full_transcription)
|
93 |
+
return full_transcription, pdf_path
|
94 |
|
95 |
# Crea la interfaz de Gradio
|
96 |
with gr.Blocks() as demo:
|
97 |
+
gr.Markdown("# Transcriptor de Audio a Texto (Genera PDF)")
|
98 |
audio_input = gr.Audio(type="filepath", label="Sube tu archivo de audio")
|
99 |
output_text = gr.Textbox(label="Transcripci贸n")
|
100 |
+
output_pdf = gr.File(label="Descarga el PDF")
|
101 |
|
102 |
+
transcribe_button = gr.Button("Transcribir y Crear PDF")
|
103 |
+
transcribe_button.click(fn=transcribe_audio_y_pdf, inputs=audio_input, outputs=[output_text, output_pdf])
|
104 |
|
105 |
# Ejecuta la aplicaci贸n
|
106 |
demo.launch()
|