Spaces:
Running
Running
Update app.py
Browse files
app.py
CHANGED
@@ -1,33 +1,32 @@
|
|
1 |
import os
|
2 |
import io
|
3 |
import gradio as gr
|
4 |
-
import requests
|
5 |
import subprocess
|
|
|
|
|
6 |
|
7 |
-
# Obtener la API Key
|
8 |
try:
|
9 |
-
API_KEY = os.environ["
|
10 |
except KeyError:
|
11 |
-
raise ValueError("La API Key de
|
12 |
|
13 |
-
#
|
14 |
-
|
15 |
-
|
16 |
-
|
17 |
-
HEADERS = {"Authorization": f"Bearer {API_KEY}"}
|
18 |
|
19 |
def convert_to_wav(input_file):
|
20 |
"""Convierte archivos de audio a formato WAV LINEAR16 si es necesario."""
|
21 |
output_file = input_file + ".wav"
|
22 |
command = [
|
23 |
"ffmpeg", "-y", "-i", input_file,
|
24 |
-
"-acodec", "pcm_s16le", "-ar", "
|
25 |
]
|
26 |
subprocess.run(command, stdout=subprocess.DEVNULL, stderr=subprocess.DEVNULL)
|
27 |
return output_file
|
28 |
|
29 |
def transcribe(audio_file=None):
|
30 |
-
"""Transcribe audio a texto usando
|
31 |
if audio_file is None:
|
32 |
return "No se ha seleccionado ningún archivo.", ""
|
33 |
|
@@ -35,22 +34,31 @@ def transcribe(audio_file=None):
|
|
35 |
if not audio_file.endswith(".wav"):
|
36 |
audio_file = convert_to_wav(audio_file)
|
37 |
|
38 |
-
#
|
39 |
-
|
40 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
41 |
|
42 |
-
#
|
43 |
-
|
44 |
-
|
45 |
-
|
46 |
-
if response.status_code != 200:
|
47 |
-
return "Error en la transcripción", f"Código: {response.status_code}, Detalles: {response.text}"
|
48 |
-
|
49 |
-
# Extraer la transcripción
|
50 |
-
result = response.json()
|
51 |
-
transcript = result.get("text", "No se pudo obtener la transcripción.")
|
52 |
|
53 |
-
return transcript,
|
54 |
|
55 |
# Configuración de la interfaz Gradio
|
56 |
output1 = gr.Textbox(label='Transcripción')
|
@@ -60,8 +68,8 @@ demo = gr.Interface(
|
|
60 |
fn=transcribe,
|
61 |
inputs=gr.Audio(sources=["microphone", "upload"], type="filepath", label="Subir o grabar audio"),
|
62 |
outputs=[output1, output2],
|
63 |
-
title='Demo Speech-to-Text con
|
64 |
-
description='<p>Grabar o subir un archivo de audio para convertir voz a texto usando
|
65 |
)
|
66 |
|
67 |
demo.launch()
|
|
|
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 |
try:
|
10 |
+
API_KEY = os.environ["GOOGLE_API_KEY"]
|
11 |
except KeyError:
|
12 |
+
raise ValueError("La API Key de Google no está disponible. Configúrala en los Secrets como 'GOOGLE_API_KEY'.")
|
13 |
|
14 |
+
# Configurar cliente de Google Speech-to-Text con API Key
|
15 |
+
client_options = ClientOptions(api_key=API_KEY)
|
16 |
+
client = speech.SpeechClient(client_options=client_options)
|
|
|
|
|
17 |
|
18 |
def convert_to_wav(input_file):
|
19 |
"""Convierte archivos de audio a formato WAV LINEAR16 si es necesario."""
|
20 |
output_file = input_file + ".wav"
|
21 |
command = [
|
22 |
"ffmpeg", "-y", "-i", input_file,
|
23 |
+
"-acodec", "pcm_s16le", "-ar", "44100", "-ac", "1", output_file
|
24 |
]
|
25 |
subprocess.run(command, stdout=subprocess.DEVNULL, stderr=subprocess.DEVNULL)
|
26 |
return output_file
|
27 |
|
28 |
def transcribe(audio_file=None):
|
29 |
+
"""Transcribe audio a texto usando Google Cloud Speech-to-Text."""
|
30 |
if audio_file is None:
|
31 |
return "No se ha seleccionado ningún archivo.", ""
|
32 |
|
|
|
34 |
if not audio_file.endswith(".wav"):
|
35 |
audio_file = convert_to_wav(audio_file)
|
36 |
|
37 |
+
# Configuración de la solicitud
|
38 |
+
config = speech.RecognitionConfig(
|
39 |
+
encoding=speech.RecognitionConfig.AudioEncoding.LINEAR16,
|
40 |
+
sample_rate_hertz=44100,
|
41 |
+
audio_channel_count=1,
|
42 |
+
language_code="es-AR",
|
43 |
+
)
|
44 |
+
|
45 |
+
# Cargar el audio en binario
|
46 |
+
with io.open(audio_file, "rb") as file:
|
47 |
+
content = file.read()
|
48 |
+
audio = speech.RecognitionAudio(content=content)
|
49 |
+
|
50 |
+
# Realiza la transcripción
|
51 |
+
response = client.recognize(config=config, audio=audio)
|
52 |
+
|
53 |
+
transcript = []
|
54 |
+
confidence = []
|
55 |
|
56 |
+
# Leer la respuesta de la API
|
57 |
+
for result in response.results:
|
58 |
+
confidence.append(str(result.alternatives[0].confidence))
|
59 |
+
transcript.append(result.alternatives[0].transcript)
|
|
|
|
|
|
|
|
|
|
|
|
|
60 |
|
61 |
+
return ' '.join(transcript), '\n'.join(confidence)
|
62 |
|
63 |
# Configuración de la interfaz Gradio
|
64 |
output1 = gr.Textbox(label='Transcripción')
|
|
|
68 |
fn=transcribe,
|
69 |
inputs=gr.Audio(sources=["microphone", "upload"], type="filepath", label="Subir o grabar audio"),
|
70 |
outputs=[output1, output2],
|
71 |
+
title='Demo Speech-to-Text con Google Cloud',
|
72 |
+
description='<p>Grabar o subir un archivo de audio para convertir voz a texto usando Google Cloud Speech-to-Text.</p>'
|
73 |
)
|
74 |
|
75 |
demo.launch()
|