cesar commited on
Commit
e04c18e
·
verified ·
1 Parent(s): d2a852e

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +21 -21
app.py CHANGED
@@ -2,60 +2,60 @@ import io
2
  import os
3
  import gradio as gr
4
  from google.cloud import speech
 
5
 
 
 
6
 
7
- rutas = [os.getcwd(),"deploygpt-e9475e7c2c7c.json"]
8
- os.environ["GOOGLE_APPLICATION_CREDENTIALS"] = '/'.join(rutas)
9
-
10
 
11
  def transcribe(file_name):
12
- """Transcribe audio bytes to text using Google Cloud Speech to Text."""
13
  if file_name is None:
14
  return '', ''
15
 
16
- # Crea un cliente de Speech to Text
17
- client = speech.SpeechClient()
 
18
 
19
- # Configura la configuración de la solicitud
20
  config = speech.RecognitionConfig(
21
  encoding=speech.RecognitionConfig.AudioEncoding.LINEAR16,
22
  sample_rate_hertz=44100,
23
- #enable_automatic_punctuation=True,
24
  audio_channel_count=1,
25
  language_code="es-AR",
26
- #model='latest_short',
27
  )
28
 
29
- # Crea una solicitud de reconocimiento de audio
30
  with io.open(file_name, "rb") as audio_file:
31
  content = audio_file.read()
32
  audio = speech.RecognitionAudio(content=content)
33
 
34
  # Realiza la transcripción
35
- response = client.recognize(request={"config": config, "audio": audio})
36
 
37
  transcript = []
38
  confidence = []
39
- # Reads the response
 
40
  for result in response.results:
41
- print("Confidence: {}".format(result.alternatives[0].confidence))
42
  confidence.append(str(result.alternatives[0].confidence))
43
- print("Transcript: {}".format(result.alternatives[0].transcript))
44
  transcript.append(result.alternatives[0].transcript)
45
 
46
  return ' '.join(transcript), '\n'.join(confidence)
47
 
 
48
  output1 = gr.Textbox(label='Transcripción')
49
- output2 = gr.Textbox(label='Umbral')
 
50
  demo = gr.Interface(
51
  transcribe,
52
- gr.Audio(sources=["microphone"],
53
- type="filepath", # Crea un archivo temporal en formato wav
54
- label='Grabar audio aquí',
55
- streaming=False),
56
  [output1, output2],
57
- title='Demo uso de Speech-to-Text usando la Api de Google',
58
- description='<p>Grabar audio para convertir voz a texto.</p>'
59
  )
60
 
61
  demo.launch()
 
2
  import os
3
  import gradio as gr
4
  from google.cloud import speech
5
+ from google.api_core.client_options import ClientOptions
6
 
7
+ # Obtener la API Key desde las variables de entorno
8
+ API_KEY = os.getenv("GOOGLE_API_KEY")
9
 
10
+ # Verificar si la API Key está configurada
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)
22
 
23
+ # Configuración de la solicitud
24
  config = speech.RecognitionConfig(
25
  encoding=speech.RecognitionConfig.AudioEncoding.LINEAR16,
26
  sample_rate_hertz=44100,
 
27
  audio_channel_count=1,
28
  language_code="es-AR",
 
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
 
36
  # Realiza la transcripción
37
+ response = client.recognize(config=config, audio=audio)
38
 
39
  transcript = []
40
  confidence = []
41
+
42
+ # Lee la respuesta de la API
43
  for result in response.results:
 
44
  confidence.append(str(result.alternatives[0].confidence))
 
45
  transcript.append(result.alternatives[0].transcript)
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 Speech-to-Text con API Key de Google',
58
+ description='<p>Grabar audio para convertir voz a texto usando Google Cloud Speech-to-Text.</p>'
59
  )
60
 
61
  demo.launch()