salomonsky commited on
Commit
7ed9a89
verified
1 Parent(s): 6bf8da4

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +19 -15
app.py CHANGED
@@ -7,7 +7,7 @@ from audiorecorder import audiorecorder
7
  import speech_recognition as sr
8
  from pydub import AudioSegment
9
 
10
- pre_prompt_text = "You are a behavioral AI, your answers should be brief, stoic and humanistic."
11
 
12
  if "history" not in st.session_state:
13
  st.session_state.history = []
@@ -25,14 +25,14 @@ def recognize_speech(audio_data, show_messages=True):
25
  try:
26
  audio_text = recognizer.recognize_google(audio, language="es-ES")
27
  if show_messages:
28
- st.subheader("Recognized text:")
29
  st.write(audio_text)
30
- st.success("Completed.")
31
  except sr.UnknownValueError:
32
- st.warning("The audio could not be recognized. Did you try to record something?")
33
  audio_text = ""
34
  except sr.RequestError:
35
- st.error("Talk to me to get started!")
36
  audio_text = ""
37
 
38
  return audio_text
@@ -79,26 +79,30 @@ def generate(audio_text, history, temperature=None, max_new_tokens=512, top_p=0.
79
 
80
  def text_to_speech(text, speed=1.3):
81
  tts = gTTS(text=text, lang='es')
82
- tts.speed = speed
83
- audio_bytes = io.BytesIO()
84
- tts.write_to_fp(audio_bytes)
85
- audio_bytes.seek(0)
86
- return base64.b64encode(audio_bytes.read()).decode()
 
 
 
 
87
 
88
  def main():
89
- audio_data = audiorecorder("Push to Play", "Stop Recording...")
90
 
91
  if not audio_data.empty():
92
- st.audio(audio_data.export().read(), format="audio/mpeg;base64,{0}".format(base64.b64encode(audio_data.export().read()).decode()), autoplay=True)
93
- audio_data.export("audio.mp3", format="mp3")
94
- audio_text = recognize_speech("audio.mp3")
95
 
96
  if audio_text:
97
  output, audio_file = generate(audio_text, history=st.session_state.history)
98
 
99
  if audio_file is not None:
100
  st.markdown(
101
- f"""<audio controls="controls" src="data:audio/mpeg;base64,{base64.b64encode(audio_file.read()).decode()}"></audio>""",
102
  unsafe_allow_html=True)
103
 
104
  if __name__ == "__main__":
 
7
  import speech_recognition as sr
8
  from pydub import AudioSegment
9
 
10
+ pre_prompt_text = "Eres una IA conductual, tus respuestas deber谩n ser breves, est贸icas y humanistas."
11
 
12
  if "history" not in st.session_state:
13
  st.session_state.history = []
 
25
  try:
26
  audio_text = recognizer.recognize_google(audio, language="es-ES")
27
  if show_messages:
28
+ st.subheader("Texto Reconocido:")
29
  st.write(audio_text)
30
+ st.success("Reconocimiento de voz completado.")
31
  except sr.UnknownValueError:
32
+ st.warning("No se pudo reconocer el audio. 驴Intentaste grabar algo?")
33
  audio_text = ""
34
  except sr.RequestError:
35
+ st.error("Hablame para comenzar!")
36
  audio_text = ""
37
 
38
  return audio_text
 
79
 
80
  def text_to_speech(text, speed=1.3):
81
  tts = gTTS(text=text, lang='es')
82
+ audio_fp = io.BytesIO()
83
+ tts.write_to_fp(audio_fp)
84
+ audio_fp.seek(0)
85
+ audio = AudioSegment.from_file(audio_fp, format="mp3")
86
+ modified_speed_audio = audio.speedup(playback_speed=speed)
87
+ modified_audio_fp = io.BytesIO()
88
+ modified_speed_audio.export(modified_audio_fp, format="mp3")
89
+ modified_audio_fp.seek(0)
90
+ return modified_audio_fp
91
 
92
  def main():
93
+ audio_data = audiorecorder("Presiona para hablar", "Deteniendo la grabaci贸n...")
94
 
95
  if not audio_data.empty():
96
+ st.audio(audio_data.export().read(), format="audio/wav")
97
+ audio_data.export("audio.wav", format="wav")
98
+ audio_text = recognize_speech("audio.wav")
99
 
100
  if audio_text:
101
  output, audio_file = generate(audio_text, history=st.session_state.history)
102
 
103
  if audio_file is not None:
104
  st.markdown(
105
+ f"""<audio autoplay="autoplay" controls="controls" src="data:audio/mp3;base64,{base64.b64encode(audio_file.read()).decode()}" type="audio/mp3" id="audio_player"></audio>""",
106
  unsafe_allow_html=True)
107
 
108
  if __name__ == "__main__":