Create app.py
Browse files
app.py
ADDED
|
@@ -0,0 +1,41 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import gradio as gr
|
| 2 |
+
from transformers import Wav2Vec2ForCTC, Wav2Vec2Processor
|
| 3 |
+
import torch
|
| 4 |
+
import librosa
|
| 5 |
+
|
| 6 |
+
# Cargar el modelo y el procesador de Hugging Face
|
| 7 |
+
processor = Wav2Vec2Processor.from_pretrained("facebook/wav2vec2-large-960h")
|
| 8 |
+
model = Wav2Vec2ForCTC.from_pretrained("facebook/wav2vec2-large-960h")
|
| 9 |
+
|
| 10 |
+
def transcribe_audio(audio):
|
| 11 |
+
# Cargar el audio usando librosa
|
| 12 |
+
speech, rate = librosa.load(audio, sr=16000)
|
| 13 |
+
|
| 14 |
+
# Procesar el audio
|
| 15 |
+
input_values = processor(speech, return_tensors="pt", sampling_rate=rate).input_values
|
| 16 |
+
# Generar las predicciones (logits)
|
| 17 |
+
with torch.no_grad():
|
| 18 |
+
logits = model(input_values).logits
|
| 19 |
+
|
| 20 |
+
# Obtener las predicciones (tokens) y convertirlas en texto
|
| 21 |
+
predicted_ids = torch.argmax(logits, dim=-1)
|
| 22 |
+
transcription = processor.batch_decode(predicted_ids)[0]
|
| 23 |
+
|
| 24 |
+
# Guardar la transcripci贸n en un archivo de texto
|
| 25 |
+
with open("transcription.txt", "w") as file:
|
| 26 |
+
file.write(transcription)
|
| 27 |
+
|
| 28 |
+
return "transcription.txt"
|
| 29 |
+
|
| 30 |
+
# Configurar la interfaz de Gradio
|
| 31 |
+
iface = gr.Interface(
|
| 32 |
+
fn=transcribe_audio,
|
| 33 |
+
inputs=gr.Audio(source="upload", type="filepath"),
|
| 34 |
+
outputs=gr.File(file_path=True),
|
| 35 |
+
title="Audio Transcriber",
|
| 36 |
+
description="Sube un archivo de audio y obt茅n la transcripci贸n en un archivo de texto."
|
| 37 |
+
)
|
| 38 |
+
|
| 39 |
+
# Iniciar la interfaz
|
| 40 |
+
if __name__ == "__main__":
|
| 41 |
+
iface.launch()
|