cesar commited on
Commit
636fe04
·
verified ·
1 Parent(s): cbcfe06

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +26 -8
app.py CHANGED
@@ -1,6 +1,7 @@
1
- import io
2
  import os
 
3
  import gradio as gr
 
4
  from google.cloud import speech
5
  from google.api_core.client_options import ClientOptions
6
 
@@ -11,11 +12,25 @@ API_KEY = os.getenv("GOOGLE_API_KEY")
11
  if not API_KEY:
12
  raise ValueError("La API Key de Google no está configurada. Configúrala en la variable de entorno GOOGLE_API_KEY.")
13
 
14
- def transcribe(file_name):
 
 
 
 
 
 
 
 
 
 
15
  """Transcribe audio a texto usando Google Cloud Speech-to-Text con API Key."""
16
- if file_name is None:
17
  return '', ''
18
 
 
 
 
 
19
  # Configurar el cliente de Speech-to-Text con API Key
20
  client_options = ClientOptions(api_key=API_KEY)
21
  client = speech.SpeechClient(client_options=client_options)
@@ -29,7 +44,7 @@ def transcribe(file_name):
29
  )
30
 
31
  # Cargar el audio en binario
32
- with io.open(file_name, "rb") as audio_file:
33
  content = audio_file.read()
34
  audio = speech.RecognitionAudio(content=content)
35
 
@@ -46,16 +61,19 @@ def transcribe(file_name):
46
 
47
  return ' '.join(transcript), '\n'.join(confidence)
48
 
49
- # Configuración de la interfaz Gradio
50
  output1 = gr.Textbox(label='Transcripción')
51
  output2 = gr.Textbox(label='Confianza')
52
 
53
  demo = gr.Interface(
54
  transcribe,
55
- gr.Audio(sources=["microphone"], type="filepath", label='Grabar audio aquí', streaming=False),
 
 
 
56
  [output1, output2],
57
- title='Demo Reconocimiento de voz',
58
- description='<p>Grabar audio para convertir voz a texto usando IA.</p>'
59
  )
60
 
61
  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
 
 
12
  if not API_KEY:
13
  raise ValueError("La API Key de Google no está configurada. Configúrala en la variable de entorno GOOGLE_API_KEY.")
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", "44100", "-ac", "1", output_file
21
+ ]
22
+ subprocess.run(command, stdout=subprocess.DEVNULL, stderr=subprocess.DEVNULL)
23
+ return output_file
24
+
25
+ def transcribe(file_path):
26
  """Transcribe audio a texto usando Google Cloud Speech-to-Text con API Key."""
27
+ if file_path is None:
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
  # Configurar el cliente de Speech-to-Text con API Key
35
  client_options = ClientOptions(api_key=API_KEY)
36
  client = speech.SpeechClient(client_options=client_options)
 
44
  )
45
 
46
  # Cargar el audio en binario
47
+ with io.open(file_path, "rb") as audio_file:
48
  content = audio_file.read()
49
  audio = speech.RecognitionAudio(content=content)
50
 
 
61
 
62
  return ' '.join(transcript), '\n'.join(confidence)
63
 
64
+ # Configuración de la interfaz Gradio con opciones de grabación y subida de archivos
65
  output1 = gr.Textbox(label='Transcripción')
66
  output2 = gr.Textbox(label='Confianza')
67
 
68
  demo = gr.Interface(
69
  transcribe,
70
+ [
71
+ gr.Audio(sources=["microphone"], type="filepath", label='Grabar audio aquí', streaming=False),
72
+ gr.File(label="Subir archivo de audio")
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()