salomonsky commited on
Commit
61fd95d
verified
1 Parent(s): 9297239

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +23 -2
app.py CHANGED
@@ -7,13 +7,19 @@ import speech_recognition as sr
7
  from pydub import AudioSegment
8
  from audiorecorder import audiorecorder
9
 
 
 
10
  if "history" not in st.session_state:
11
  st.session_state.history = []
12
 
 
 
 
13
  def recognize_speech(audio_data, show_messages=True):
14
  recognizer = sr.Recognizer()
15
 
16
  try:
 
17
  audio = gTTS(audio_data.read(), lang='es')
18
  audio_recording = sr.AudioFile(io.BytesIO(audio_data.read()))
19
 
@@ -35,6 +41,7 @@ def recognize_speech(audio_data, show_messages=True):
35
  return audio_text
36
 
37
  def format_prompt(message, history):
 
38
  prompt = "<s>"
39
 
40
  for user_prompt, bot_response in history:
@@ -42,7 +49,7 @@ def format_prompt(message, history):
42
  prompt += f" {bot_response}</s> "
43
 
44
  prompt += f"[INST] {message} [/INST]"
45
- return prompt
46
 
47
  def generate(audio_text, history, temperature=None, max_new_tokens=256, top_p=0.95, repetition_penalty=1.0):
48
  client = InferenceClient("mistralai/Mixtral-8x7B-Instruct-v0.1")
@@ -76,10 +83,17 @@ def text_to_speech(text, speed=1.3):
76
  tts = gTTS(text=text, lang='es')
77
  audio_fp = io.BytesIO()
78
  tts.write_to_fp(audio_fp)
 
 
79
  audio = AudioSegment.from_file(audio_fp, format="mp3")
 
80
  modified_speed_audio = audio.speedup(playback_speed=speed)
 
81
  modified_audio_fp = io.BytesIO()
82
  modified_speed_audio.export(modified_audio_fp, format="mp3")
 
 
 
83
  return modified_audio_fp
84
 
85
  def play_audio(audio_file):
@@ -89,7 +103,14 @@ def play_audio(audio_file):
89
  def main():
90
  st.title("Chatbot de Voz a Voz")
91
  audio_text = ""
92
- audio_data = audiorecorder("Hablar 鈻讹笍", "Detener 馃洃")
 
 
 
 
 
 
 
93
 
94
  if not audio_data.empty():
95
  st.audio(audio_data.export().read(), format="audio/mp3")
 
7
  from pydub import AudioSegment
8
  from audiorecorder import audiorecorder
9
 
10
+ pre_prompt_text = "eres una IA conductual, tus respuestas ser谩n breves."
11
+
12
  if "history" not in st.session_state:
13
  st.session_state.history = []
14
 
15
+ if "pre_prompt_sent" not in st.session_state:
16
+ st.session_state.pre_prompt_sent = False
17
+
18
  def recognize_speech(audio_data, show_messages=True):
19
  recognizer = sr.Recognizer()
20
 
21
  try:
22
+ audio_data.seek(0)
23
  audio = gTTS(audio_data.read(), lang='es')
24
  audio_recording = sr.AudioFile(io.BytesIO(audio_data.read()))
25
 
 
41
  return audio_text
42
 
43
  def format_prompt(message, history):
44
+ preprompt = "Este es el inicio de la conversaci贸n. "
45
  prompt = "<s>"
46
 
47
  for user_prompt, bot_response in history:
 
49
  prompt += f" {bot_response}</s> "
50
 
51
  prompt += f"[INST] {message} [/INST]"
52
+ return preprompt + prompt
53
 
54
  def generate(audio_text, history, temperature=None, max_new_tokens=256, top_p=0.95, repetition_penalty=1.0):
55
  client = InferenceClient("mistralai/Mixtral-8x7B-Instruct-v0.1")
 
83
  tts = gTTS(text=text, lang='es')
84
  audio_fp = io.BytesIO()
85
  tts.write_to_fp(audio_fp)
86
+ audio_fp.seek(0)
87
+
88
  audio = AudioSegment.from_file(audio_fp, format="mp3")
89
+
90
  modified_speed_audio = audio.speedup(playback_speed=speed)
91
+
92
  modified_audio_fp = io.BytesIO()
93
  modified_speed_audio.export(modified_audio_fp, format="mp3")
94
+
95
+ modified_audio_fp.seek(0)
96
+
97
  return modified_audio_fp
98
 
99
  def play_audio(audio_file):
 
103
  def main():
104
  st.title("Chatbot de Voz a Voz")
105
  audio_text = ""
106
+
107
+ if not st.session_state.pre_prompt_sent:
108
+ st.session_state.pre_prompt_sent = True
109
+ st.session_state.history.append((pre_prompt_text, ""))
110
+ pre_prompt_audio_file = text_to_speech(pre_prompt_text)
111
+ play_audio(pre_prompt_audio_file)
112
+
113
+ audio_data = audiorecorder.record("Hablar 鈻讹笍", "Detener 馃洃")
114
 
115
  if not audio_data.empty():
116
  st.audio(audio_data.export().read(), format="audio/mp3")