Spaces:
Build error
Build error
Update app.py
Browse files
app.py
CHANGED
|
@@ -1,80 +1,115 @@
|
|
|
|
|
|
|
|
|
|
|
| 1 |
import gradio as gr
|
| 2 |
-
import
|
| 3 |
-
from moviepy.editor import VideoFileClip
|
| 4 |
-
import speech_recognition as sr
|
| 5 |
import noisereduce as nr
|
| 6 |
-
import
|
| 7 |
-
|
| 8 |
-
|
| 9 |
-
|
| 10 |
-
|
| 11 |
-
|
| 12 |
-
|
| 13 |
-
|
| 14 |
-
|
| 15 |
-
|
| 16 |
-
|
| 17 |
-
|
| 18 |
-
|
| 19 |
-
|
| 20 |
-
|
| 21 |
-
|
| 22 |
-
|
| 23 |
-
|
| 24 |
-
|
| 25 |
-
|
| 26 |
-
|
| 27 |
-
|
| 28 |
-
|
| 29 |
-
|
| 30 |
-
|
| 31 |
-
|
| 32 |
-
|
| 33 |
-
|
| 34 |
-
|
| 35 |
-
|
| 36 |
-
|
| 37 |
-
|
| 38 |
-
|
| 39 |
-
|
| 40 |
-
|
| 41 |
-
|
| 42 |
-
|
| 43 |
-
|
| 44 |
-
|
| 45 |
-
|
| 46 |
-
|
| 47 |
-
|
| 48 |
-
|
| 49 |
-
|
| 50 |
-
|
| 51 |
-
|
| 52 |
-
|
| 53 |
-
|
| 54 |
-
|
| 55 |
-
|
| 56 |
-
|
| 57 |
-
|
| 58 |
-
|
| 59 |
-
|
| 60 |
-
|
| 61 |
-
|
| 62 |
-
|
| 63 |
-
|
| 64 |
-
|
| 65 |
-
|
| 66 |
-
|
| 67 |
-
|
| 68 |
-
|
| 69 |
-
|
| 70 |
-
#
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 71 |
iface = gr.Interface(
|
| 72 |
-
fn=
|
| 73 |
-
inputs=
|
| 74 |
-
outputs=
|
| 75 |
-
title="
|
| 76 |
-
description="
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 77 |
)
|
| 78 |
|
| 79 |
-
|
| 80 |
-
|
|
|
|
| 1 |
+
import sounddevice as sd
|
| 2 |
+
import numpy as np
|
| 3 |
+
import torchaudio
|
| 4 |
import gradio as gr
|
| 5 |
+
from scipy.io.wavfile import write
|
|
|
|
|
|
|
| 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 |
+
waveform, sample_rate = torchaudio.load(file)
|
| 35 |
+
|
| 36 |
+
# Reducir el ruido
|
| 37 |
+
reduced_noise = nr.reduce_noise(y=waveform.numpy()[0], sr=sample_rate)
|
| 38 |
+
|
| 39 |
+
# Convertir de nuevo a tensor
|
| 40 |
+
reduced_waveform = torch.tensor(reduced_noise).unsqueeze(0)
|
| 41 |
+
|
| 42 |
+
# Asegurarse de que el audio est谩 en la frecuencia de muestreo del modelo
|
| 43 |
+
if sample_rate != 16000:
|
| 44 |
+
resampler = torchaudio.transforms.Resample(orig_freq=sample_rate, new_freq=16000)
|
| 45 |
+
reduced_waveform = resampler(reduced_waveform)
|
| 46 |
+
|
| 47 |
+
# Realizar la transcripci贸n usando el modelo de ASR
|
| 48 |
+
with torch.no_grad():
|
| 49 |
+
logits = asr_model(reduced_waveform)
|
| 50 |
+
|
| 51 |
+
# Obtener las predicciones de las etiquetas
|
| 52 |
+
predicted_ids = torch.argmax(logits, dim=-1)
|
| 53 |
+
|
| 54 |
+
# Convertir IDs a texto
|
| 55 |
+
transcription = ''.join([labels[i] for i in predicted_ids[0].tolist() if i < len(labels)])
|
| 56 |
+
|
| 57 |
+
return transcription.strip()
|
| 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
|
| 65 |
+
end_time = 0
|
| 66 |
+
|
| 67 |
+
for i, word in enumerate(words):
|
| 68 |
+
start_time = end_time
|
| 69 |
+
end_time = start_time + 1 # Duraci贸n fija de 1 segundo por palabra (ajustar seg煤n necesidad)
|
| 70 |
+
|
| 71 |
+
# Formato SRT
|
| 72 |
+
srt_output.append(f"{i + 1}")
|
| 73 |
+
srt_output.append(f"{str(timedelta(seconds=start_time)).split('.')[0].replace(',', '.')},000 --> {str(timedelta(seconds=end_time)).split('.')[0].replace(',', '.')},000")
|
| 74 |
+
srt_output.append(f"{word}\n")
|
| 75 |
+
|
| 76 |
+
return ''.join(srt_output)
|
| 77 |
+
|
| 78 |
+
def process_audio():
|
| 79 |
+
# Grabar audio y luego transcribir
|
| 80 |
+
audio_file = record_audio()
|
| 81 |
+
transcription = transcribe_audio(audio_file)
|
| 82 |
+
srt_content = format_as_srt(transcription)
|
| 83 |
+
|
| 84 |
+
# Guardar el contenido SRT en un archivo
|
| 85 |
+
with open('subtitles.srt', 'w') as f:
|
| 86 |
+
f.write(srt_content)
|
| 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=None,
|
| 101 |
+
outputs=["text", "file"],
|
| 102 |
+
title="馃帳 Grabador de Audio y Transcriptor a Subt铆tulos SRT 馃摐",
|
| 103 |
+
description="馃憢 Bienvenido a nuestra aplicaci贸n de grabaci贸n y transcripci贸n de audio a subt铆tulos. Graba tu voz y obt茅n la transcripci贸n junto con un archivo SRT de subt铆tulos.",
|
| 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 |
+
|