Spaces:
Running
Running
Update app.py
Browse files
app.py
CHANGED
@@ -1,79 +1,70 @@
|
|
1 |
import os
|
2 |
import io
|
3 |
import gradio as gr
|
|
|
4 |
import subprocess
|
5 |
-
from google.cloud import speech
|
6 |
-
from google.api_core.client_options import ClientOptions
|
7 |
|
8 |
-
# Obtener la API Key desde las variables de entorno
|
9 |
-
API_KEY = os.getenv("
|
10 |
|
11 |
# Verificar si la API Key está configurada
|
12 |
if not API_KEY:
|
13 |
-
raise ValueError("La API Key de
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
14 |
|
15 |
def convert_to_wav(input_file):
|
16 |
"""Convierte archivos de audio a formato WAV LINEAR16 si es necesario."""
|
17 |
output_file = input_file + ".wav"
|
18 |
command = [
|
19 |
"ffmpeg", "-y", "-i", input_file,
|
20 |
-
"-acodec", "pcm_s16le", "-ar", "
|
21 |
]
|
22 |
subprocess.run(command, stdout=subprocess.DEVNULL, stderr=subprocess.DEVNULL)
|
23 |
return output_file
|
24 |
|
25 |
-
def transcribe(
|
26 |
-
"""Transcribe audio a texto usando
|
27 |
-
if
|
28 |
-
return
|
29 |
-
|
30 |
-
# Convertir a formato WAV si es necesario
|
31 |
-
if not file_path.endswith(".wav"):
|
32 |
-
file_path = convert_to_wav(file_path)
|
33 |
|
34 |
-
#
|
35 |
-
|
36 |
-
|
37 |
|
38 |
-
#
|
39 |
-
|
40 |
-
|
41 |
-
sample_rate_hertz=44100,
|
42 |
-
audio_channel_count=1,
|
43 |
-
language_code="es-AR",
|
44 |
-
)
|
45 |
|
46 |
-
#
|
47 |
-
|
48 |
-
content = audio_file.read()
|
49 |
-
audio = speech.RecognitionAudio(content=content)
|
50 |
|
51 |
-
#
|
52 |
-
response
|
|
|
53 |
|
54 |
-
|
55 |
-
|
56 |
-
|
57 |
-
# Lee la respuesta de la API
|
58 |
-
for result in response.results:
|
59 |
-
confidence.append(str(result.alternatives[0].confidence))
|
60 |
-
transcript.append(result.alternatives[0].transcript)
|
61 |
|
62 |
-
return
|
63 |
|
64 |
-
# Configuración de la interfaz Gradio
|
65 |
output1 = gr.Textbox(label='Transcripción')
|
66 |
output2 = gr.Textbox(label='Confianza')
|
67 |
|
68 |
demo = gr.Interface(
|
69 |
-
transcribe,
|
70 |
-
[
|
71 |
-
|
72 |
-
|
73 |
-
|
74 |
-
[output1, output2],
|
75 |
-
title='Demo Reconocimiento de Voz con Google',
|
76 |
-
description='<p>Grabar o subir un archivo de audio para convertir voz a texto usando Google Cloud Speech-to-Text.</p>'
|
77 |
)
|
78 |
|
79 |
demo.launch()
|
|
|
|
1 |
import os
|
2 |
import io
|
3 |
import gradio as gr
|
4 |
+
import requests
|
5 |
import subprocess
|
|
|
|
|
6 |
|
7 |
+
# Obtener la API Key de Hugging Face desde las variables de entorno
|
8 |
+
API_KEY = os.getenv("HUGGINGFACE_API_KEY")
|
9 |
|
10 |
# Verificar si la API Key está configurada
|
11 |
if not API_KEY:
|
12 |
+
raise ValueError("La API Key de Hugging Face no está configurada. Configúrala en la variable de entorno HUGGINGFACE_API_KEY.")
|
13 |
+
|
14 |
+
# URL del modelo de transcripción en Hugging Face
|
15 |
+
HF_MODEL = "facebook/wav2vec2-large-xlsr-53-spanish" # Cambiar si se usa otro modelo
|
16 |
+
API_URL = f"https://api-inference.huggingface.co/models/{HF_MODEL}"
|
17 |
+
|
18 |
+
# Headers para la solicitud
|
19 |
+
HEADERS = {"Authorization": f"Bearer {API_KEY}"}
|
20 |
|
21 |
def convert_to_wav(input_file):
|
22 |
"""Convierte archivos de audio a formato WAV LINEAR16 si es necesario."""
|
23 |
output_file = input_file + ".wav"
|
24 |
command = [
|
25 |
"ffmpeg", "-y", "-i", input_file,
|
26 |
+
"-acodec", "pcm_s16le", "-ar", "16000", "-ac", "1", output_file
|
27 |
]
|
28 |
subprocess.run(command, stdout=subprocess.DEVNULL, stderr=subprocess.DEVNULL)
|
29 |
return output_file
|
30 |
|
31 |
+
def transcribe(audio_file=None):
|
32 |
+
"""Transcribe audio a texto usando Hugging Face."""
|
33 |
+
if audio_file is None:
|
34 |
+
return "No se ha seleccionado ningún archivo.", ""
|
|
|
|
|
|
|
|
|
35 |
|
36 |
+
# Convertir a WAV si es necesario
|
37 |
+
if not audio_file.endswith(".wav"):
|
38 |
+
audio_file = convert_to_wav(audio_file)
|
39 |
|
40 |
+
# Cargar el archivo de audio
|
41 |
+
with open(audio_file, "rb") as file:
|
42 |
+
audio_bytes = file.read()
|
|
|
|
|
|
|
|
|
43 |
|
44 |
+
# Enviar el audio a la API de Hugging Face
|
45 |
+
response = requests.post(API_URL, headers=HEADERS, data=audio_bytes)
|
|
|
|
|
46 |
|
47 |
+
# Manejo de errores
|
48 |
+
if response.status_code != 200:
|
49 |
+
return "Error en la transcripción", f"Código: {response.status_code}, Detalles: {response.text}"
|
50 |
|
51 |
+
# Extraer la transcripción
|
52 |
+
result = response.json()
|
53 |
+
transcript = result.get("text", "No se pudo obtener la transcripción.")
|
|
|
|
|
|
|
|
|
54 |
|
55 |
+
return transcript, "Confianza no disponible en esta API"
|
56 |
|
57 |
+
# Configuración de la interfaz Gradio
|
58 |
output1 = gr.Textbox(label='Transcripción')
|
59 |
output2 = gr.Textbox(label='Confianza')
|
60 |
|
61 |
demo = gr.Interface(
|
62 |
+
fn=transcribe,
|
63 |
+
inputs=gr.Audio(sources=["microphone", "upload"], type="filepath", label="Subir o grabar audio"),
|
64 |
+
outputs=[output1, output2],
|
65 |
+
title='Demo Speech-to-Text con Hugging Face',
|
66 |
+
description='<p>Grabar o subir un archivo de audio para convertir voz a texto usando Hugging Face.</p>'
|
|
|
|
|
|
|
67 |
)
|
68 |
|
69 |
demo.launch()
|
70 |
+
|