kadsai commited on
Commit
5368aa1
·
verified ·
1 Parent(s): d340b15

fix output audio

Browse files
Files changed (1) hide show
  1. app.py +12 -15
app.py CHANGED
@@ -163,11 +163,11 @@ def respond(message, history, system_message, domain, model_name, max_tokens, te
163
  return [{"role": "assistant", "content": f"❌ Erreur : {e}"}]
164
 
165
 
166
- def chatbot_with_voice(input_audio, history=[],domain="corismoney",model_name="gpt-3.5-turbo",max_tokens=500,temperature=0.7,top_p=1.0):
167
  if input_audio is None:
168
  return history, "", None
169
 
170
- # Ouverture binaire du fichier audio uploadé
171
  with open(input_audio, "rb") as audio_file:
172
  transcription = client.audio.transcriptions.create(
173
  model="whisper-1",
@@ -175,7 +175,7 @@ def chatbot_with_voice(input_audio, history=[],domain="corismoney",model_name="g
175
  )
176
  user_message = transcription.text
177
 
178
- # Appel backend chatbot (la fonction respond doit retourner la nouvelle histoire)
179
  updated_history = respond(
180
  message=user_message,
181
  history=history,
@@ -189,21 +189,19 @@ def chatbot_with_voice(input_audio, history=[],domain="corismoney",model_name="g
189
 
190
  bot_response = updated_history[-1]['content']
191
 
192
- # Génération audio avec gTTS
193
  tts = gTTS(bot_response, lang='fr')
194
  audio_fp = io.BytesIO()
195
  tts.write_to_fp(audio_fp)
196
  audio_fp.seek(0)
197
- audio = AudioSegment.from_file(audio_fp, format="mp3")
198
 
199
- # Export wav dans BytesIO pour Gradio
200
- wav_io = io.BytesIO()
201
- audio.export(wav_io, format="wav")
202
- wav_io.seek(0)
 
203
 
204
- # Gradio audio accepte soit un tuple (BytesIO, filename), soit juste un filepath
205
- # On renvoie l'historique mis à jour, un input message vide, et le tuple audio
206
- return updated_history, "", (wav_io, "output.wav")
207
 
208
 
209
 
@@ -419,9 +417,8 @@ with gr.Blocks(theme=gr.themes.Soft(), css=".gradio-container {background-color:
419
  audio_input = gr.Audio(label="🎙️", type="filepath", interactive=True)
420
  send_button = gr.Button("🚀 Envoyer", variant="primary")
421
 
422
- response_audio = gr.Audio(label="🔊 Réponse audio", interactive=False)
423
- audio_input.change(fn=chatbot_with_voice,inputs=[audio_input, chatbot, domain, model_name, max_tokens, temperature, top_p],
424
- outputs=[chatbot, message_input, response_audio]
425
  )
426
 
427
 
 
163
  return [{"role": "assistant", "content": f"❌ Erreur : {e}"}]
164
 
165
 
166
+ def chatbot_with_voice(input_audio, history=[], domain="corismoney", model_name="gpt-3.5-turbo", max_tokens=500, temperature=0.7, top_p=1.0):
167
  if input_audio is None:
168
  return history, "", None
169
 
170
+ # Transcription avec Whisper
171
  with open(input_audio, "rb") as audio_file:
172
  transcription = client.audio.transcriptions.create(
173
  model="whisper-1",
 
175
  )
176
  user_message = transcription.text
177
 
178
+ # Réponse du chatbot
179
  updated_history = respond(
180
  message=user_message,
181
  history=history,
 
189
 
190
  bot_response = updated_history[-1]['content']
191
 
192
+ # Synthèse vocale avec gTTS
193
  tts = gTTS(bot_response, lang='fr')
194
  audio_fp = io.BytesIO()
195
  tts.write_to_fp(audio_fp)
196
  audio_fp.seek(0)
 
197
 
198
+ # Conversion MP3 -> WAV en numpy pour Gradio
199
+ audio = AudioSegment.from_file(audio_fp, format="mp3")
200
+ audio = audio.set_frame_rate(16000).set_channels(1) # mono, 16kHz
201
+ samples = np.array(audio.get_array_of_samples()).astype(np.float32) / (2**15)
202
+ sample_rate = 16000
203
 
204
+ return updated_history, "", (sample_rate, samples)
 
 
205
 
206
 
207
 
 
417
  audio_input = gr.Audio(label="🎙️", type="filepath", interactive=True)
418
  send_button = gr.Button("🚀 Envoyer", variant="primary")
419
 
420
+ response_audio = gr.Audio(label="🔊 Réponse audio",type="numpy", interactive=False)
421
+ audio_input.change(fn=chatbot_with_voice,inputs=[audio_input, chatbot, domain, model_name, max_tokens, temperature, top_p],outputs=[chatbot, message_input, response_audio]
 
422
  )
423
 
424