Spaces:
Build error
Build error
Update app.py
Browse files
app.py
CHANGED
|
@@ -1,37 +1,25 @@
|
|
| 1 |
-
import sounddevice as sd
|
| 2 |
import numpy as np
|
| 3 |
import torchaudio
|
| 4 |
import gradio as gr
|
| 5 |
-
from
|
| 6 |
import noisereduce as nr
|
| 7 |
import torch
|
| 8 |
from torchaudio.pipelines import WAV2VEC2_ASR_BASE_960H
|
| 9 |
from datetime import timedelta
|
| 10 |
|
| 11 |
-
# Configuraci贸n
|
| 12 |
-
duration = 5 # Duraci贸n de la grabaci贸n en segundos
|
| 13 |
-
sample_rate = 44100 # Frecuencia de muestreo
|
| 14 |
-
|
| 15 |
# Cargar el modelo de transcripci贸n de voz
|
| 16 |
bundle = WAV2VEC2_ASR_BASE_960H
|
| 17 |
asr_model = bundle.get_model()
|
| 18 |
labels = bundle.get_labels()
|
| 19 |
|
| 20 |
-
def record_audio():
|
| 21 |
-
# Grabar audio
|
| 22 |
-
print("Grabando...")
|
| 23 |
-
audio = sd.rec(int(duration * sample_rate), samplerate=sample_rate, channels=1, dtype='float64')
|
| 24 |
-
sd.wait() # Esperar a que termine la grabaci贸n
|
| 25 |
-
print("Grabaci贸n terminada.")
|
| 26 |
-
|
| 27 |
-
# Guardar como archivo WAV
|
| 28 |
-
write('recorded_audio.wav', sample_rate, audio)
|
| 29 |
-
|
| 30 |
-
return 'recorded_audio.wav'
|
| 31 |
-
|
| 32 |
def transcribe_audio(file):
|
| 33 |
-
# Cargar el audio grabado
|
| 34 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 35 |
|
| 36 |
# Reducir el ruido
|
| 37 |
reduced_noise = nr.reduce_noise(y=waveform.numpy()[0], sr=sample_rate)
|
|
@@ -58,7 +46,6 @@ def transcribe_audio(file):
|
|
| 58 |
|
| 59 |
def format_as_srt(transcription):
|
| 60 |
# Dividir la transcripci贸n en partes de ejemplo para los subt铆tulos
|
| 61 |
-
# Esto puede ajustarse seg煤n sea necesario para definir la duraci贸n de los subt铆tulos
|
| 62 |
words = transcription.split()
|
| 63 |
srt_output = []
|
| 64 |
start_time = 0
|
|
@@ -75,10 +62,9 @@ def format_as_srt(transcription):
|
|
| 75 |
|
| 76 |
return ''.join(srt_output)
|
| 77 |
|
| 78 |
-
def process_audio():
|
| 79 |
-
#
|
| 80 |
-
|
| 81 |
-
transcription = transcribe_audio(audio_file)
|
| 82 |
srt_content = format_as_srt(transcription)
|
| 83 |
|
| 84 |
# Guardar el contenido SRT en un archivo
|
|
@@ -87,29 +73,13 @@ def process_audio():
|
|
| 87 |
|
| 88 |
return transcription, 'subtitles.srt'
|
| 89 |
|
| 90 |
-
# Crear ejemplos para la interfaz
|
| 91 |
-
examples = [
|
| 92 |
-
["Graba un saludo", "Hola, 驴c贸mo est谩s?"],
|
| 93 |
-
["Graba una presentaci贸n", "Soy un apasionado de la programaci贸n."],
|
| 94 |
-
["Graba una explicaci贸n", "El reconocimiento de voz es fascinante."],
|
| 95 |
-
]
|
| 96 |
-
|
| 97 |
# Crear la interfaz Gradio
|
| 98 |
iface = gr.Interface(
|
| 99 |
fn=process_audio,
|
| 100 |
-
inputs=
|
| 101 |
outputs=["text", "file"],
|
| 102 |
title="馃帳 Grabador de Audio y Transcriptor a Subt铆tulos SRT 馃摐",
|
| 103 |
-
description="
|
| 104 |
-
examples=examples,
|
| 105 |
-
theme="default",
|
| 106 |
-
layout="vertical",
|
| 107 |
-
css="""
|
| 108 |
-
.title { color: #4A90E2; font-weight: bold; }
|
| 109 |
-
.description { font-size: 16px; color: #555; }
|
| 110 |
-
.footer { text-align: center; font-size: 12px; color: #777; }
|
| 111 |
-
""",
|
| 112 |
)
|
| 113 |
|
| 114 |
iface.launch()
|
| 115 |
-
|
|
|
|
|
|
|
| 1 |
import numpy as np
|
| 2 |
import torchaudio
|
| 3 |
import gradio as gr
|
| 4 |
+
from pydub import AudioSegment
|
| 5 |
import noisereduce as nr
|
| 6 |
import torch
|
| 7 |
from torchaudio.pipelines import WAV2VEC2_ASR_BASE_960H
|
| 8 |
from datetime import timedelta
|
| 9 |
|
|
|
|
|
|
|
|
|
|
|
|
|
| 10 |
# Cargar el modelo de transcripci贸n de voz
|
| 11 |
bundle = WAV2VEC2_ASR_BASE_960H
|
| 12 |
asr_model = bundle.get_model()
|
| 13 |
labels = bundle.get_labels()
|
| 14 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 15 |
def transcribe_audio(file):
|
| 16 |
+
# Cargar el audio grabado usando pydub
|
| 17 |
+
audio_segment = AudioSegment.from_file(file)
|
| 18 |
+
audio_segment = audio_segment.set_frame_rate(16000) # Cambiar la frecuencia de muestreo a 16 kHz
|
| 19 |
+
audio_segment.export("temp.wav", format="wav")
|
| 20 |
+
|
| 21 |
+
# Cargar el archivo WAV
|
| 22 |
+
waveform, sample_rate = torchaudio.load("temp.wav")
|
| 23 |
|
| 24 |
# Reducir el ruido
|
| 25 |
reduced_noise = nr.reduce_noise(y=waveform.numpy()[0], sr=sample_rate)
|
|
|
|
| 46 |
|
| 47 |
def format_as_srt(transcription):
|
| 48 |
# Dividir la transcripci贸n en partes de ejemplo para los subt铆tulos
|
|
|
|
| 49 |
words = transcription.split()
|
| 50 |
srt_output = []
|
| 51 |
start_time = 0
|
|
|
|
| 62 |
|
| 63 |
return ''.join(srt_output)
|
| 64 |
|
| 65 |
+
def process_audio(file):
|
| 66 |
+
# Transcribir el audio y generar subt铆tulos
|
| 67 |
+
transcription = transcribe_audio(file)
|
|
|
|
| 68 |
srt_content = format_as_srt(transcription)
|
| 69 |
|
| 70 |
# Guardar el contenido SRT en un archivo
|
|
|
|
| 73 |
|
| 74 |
return transcription, 'subtitles.srt'
|
| 75 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 76 |
# Crear la interfaz Gradio
|
| 77 |
iface = gr.Interface(
|
| 78 |
fn=process_audio,
|
| 79 |
+
inputs="audio",
|
| 80 |
outputs=["text", "file"],
|
| 81 |
title="馃帳 Grabador de Audio y Transcriptor a Subt铆tulos SRT 馃摐",
|
| 82 |
+
description="Graba tu voz y obt茅n la transcripci贸n junto con un archivo SRT de subt铆tulos.",
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 83 |
)
|
| 84 |
|
| 85 |
iface.launch()
|
|
|