drguilhermeapolinario commited on
Commit
32fae2e
·
verified ·
1 Parent(s): 1ae3216

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +26 -16
app.py CHANGED
@@ -132,31 +132,22 @@ def model(text, web_search):
132
  return "".join([response.token.text for response in stream if response.token.text != "</s>"])
133
 
134
 
135
- # Função assíncrona para obter as vozes
136
  async def get_voices():
137
- voices = await list_voices()
138
  return list(voices)
139
 
140
- # Executar a função assíncrona
141
  voices = asyncio.run(get_voices())
142
 
143
  # Filtrar as vozes em português do Brasil
144
  pt_br_voices = [voice for voice in voices if voice["Locale"] == "pt-BR"]
145
 
146
- # Imprimir as vozes disponíveis em português do Brasil
147
- for voice in pt_br_voices:
148
- print(f"Name: {voice['Name']}, Gender: {voice['Gender']}")
149
-
150
  # Escolher uma voz (por exemplo, a primeira da lista)
151
  chosen_voice = pt_br_voices[0]["Name"] if pt_br_voices else None
152
 
153
- if chosen_voice:
154
- print(f"Voz escolhida: {chosen_voice}")
155
- else:
156
- print("Nenhuma voz em português do Brasil encontrada.")
157
-
158
-
159
  async def respond(audio, web_search):
 
 
160
  user = transcribe(audio)
161
  reply = model(user, web_search)
162
  if chosen_voice:
@@ -168,12 +159,31 @@ async def respond(audio, web_search):
168
  await communicate.save(tmp_path)
169
  return tmp_path
170
 
 
 
 
171
  with gr.Blocks() as demo:
172
  with gr.Row():
173
  web_search = gr.Checkbox(label="Web Search", value=False)
174
- input = gr.Audio(label="User Input", sources="microphone", type="filepath")
175
- output = gr.Audio(label="AI", autoplay=True)
176
- gr.Interface(fn=respond, inputs=[input, web_search], outputs=[output], live=True)
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
177
 
178
  if __name__ == "__main__":
179
  demo.queue(max_size=200).launch()
 
132
  return "".join([response.token.text for response in stream if response.token.text != "</s>"])
133
 
134
 
 
135
  async def get_voices():
136
+ voices = await edge_tts.list_voices()
137
  return list(voices)
138
 
139
+ # Executar a função assíncrona para obter as vozes
140
  voices = asyncio.run(get_voices())
141
 
142
  # Filtrar as vozes em português do Brasil
143
  pt_br_voices = [voice for voice in voices if voice["Locale"] == "pt-BR"]
144
 
 
 
 
 
145
  # Escolher uma voz (por exemplo, a primeira da lista)
146
  chosen_voice = pt_br_voices[0]["Name"] if pt_br_voices else None
147
 
 
 
 
 
 
 
148
  async def respond(audio, web_search):
149
+ if audio is None:
150
+ return None
151
  user = transcribe(audio)
152
  reply = model(user, web_search)
153
  if chosen_voice:
 
159
  await communicate.save(tmp_path)
160
  return tmp_path
161
 
162
+ def transcribe_and_respond(audio, web_search):
163
+ return asyncio.run(respond(audio, web_search))
164
+
165
  with gr.Blocks() as demo:
166
  with gr.Row():
167
  web_search = gr.Checkbox(label="Web Search", value=False)
168
+ input_audio = gr.Audio(source="microphone", type="filepath", streaming=True)
169
+ output_audio = gr.Audio(label="AI Response", autoplay=True)
170
+
171
+ is_recording = gr.State(False)
172
+ last_interaction_time = gr.State(time.time())
173
+
174
+ def toggle_recording():
175
+ return not is_recording.value
176
+
177
+ def process_audio(audio, web_search, is_rec):
178
+ current_time = time.time()
179
+ if is_rec and (current_time - last_interaction_time.value > 2):
180
+ last_interaction_time.value = current_time
181
+ return transcribe_and_respond(audio, web_search), False
182
+ return None, is_rec
183
+
184
+ input_audio.stream(process_audio, inputs=[input_audio, web_search, is_recording], outputs=[output_audio, is_recording])
185
+
186
+ demo.load(toggle_recording, outputs=[is_recording])
187
 
188
  if __name__ == "__main__":
189
  demo.queue(max_size=200).launch()