Chandima Prabhath commited on
Commit
5e86bf8
·
1 Parent(s): e443eca

Refactor response_audio function for improved logging and error handling

Browse files
Files changed (1) hide show
  1. app.py +13 -10
app.py CHANGED
@@ -99,21 +99,24 @@ def response_text(message_id, chat_id, prompt):
99
  send_message(message_id, chat_id, "There was an error processing your request.")
100
 
101
  def response_audio(message_id, chat_id, prompt):
102
- logging.debug(f"Entering response_audio with prompt: {prompt}")
103
  try:
104
  result = generate_voice_reply(prompt, model="openai-audio", voice="coral", audio_dir=audio_dir)
105
- logging.debug(f"Result from generate_voice_reply: {result}")
106
- if result:
 
107
  audio_file_path, audio_data = result
108
- logging.debug(f"Audio file path generated: {audio_file_path}")
109
  send_result = send_audio(message_id, chat_id, audio_file_path)
110
- logging.debug(f"Result from send_audio: {send_result}")
111
- os.remove(audio_file_path) # Clean up the file after sending
112
- logging.debug(f"Removed audio file: {audio_file_path}")
 
113
  else:
114
- logging.debug("generate_voice_reply returned None, falling back to response_text")
 
115
  except Exception as e:
116
- logging.debug(f"Exception in response_audio: {e}")
117
  send_message(message_id, chat_id, "There was an error generating the audio. Please try again later.")
118
 
119
  def handle_image_generation(message_id, chat_id, prompt):
@@ -166,7 +169,7 @@ async def whatsapp_webhook(request: Request):
166
  send_message(message_id, chat_id, "Generating...")
167
  threading.Thread(target=handle_image_generation, args=(message_id, chat_id, prompt)).start()
168
  else:
169
- threading.Thread(target=response_text, args=(message_id, chat_id, body)).start()
170
  return {"success": True}
171
 
172
  def main():
 
99
  send_message(message_id, chat_id, "There was an error processing your request.")
100
 
101
  def response_audio(message_id, chat_id, prompt):
102
+ logging.debug("Entering response_audio with prompt: %s", prompt)
103
  try:
104
  result = generate_voice_reply(prompt, model="openai-audio", voice="coral", audio_dir=audio_dir)
105
+ logging.debug("Result from generate_voice_reply: %s", result)
106
+ # Check result and also ensure the audio_file_path is not None or empty
107
+ if result and result[0]:
108
  audio_file_path, audio_data = result
109
+ logging.debug("Audio file path generated: %s", audio_file_path)
110
  send_result = send_audio(message_id, chat_id, audio_file_path)
111
+ logging.debug("Result from send_audio: %s", send_result)
112
+ if os.path.exists(audio_file_path):
113
+ os.remove(audio_file_path) # Clean up the file after sending
114
+ logging.debug("Removed audio file: %s", audio_file_path)
115
  else:
116
+ logging.debug("generate_voice_reply returned None or empty audio file path, falling back to response_text")
117
+ response_text(message_id, chat_id, prompt)
118
  except Exception as e:
119
+ logging.debug("Exception in response_audio: %s", e)
120
  send_message(message_id, chat_id, "There was an error generating the audio. Please try again later.")
121
 
122
  def handle_image_generation(message_id, chat_id, prompt):
 
169
  send_message(message_id, chat_id, "Generating...")
170
  threading.Thread(target=handle_image_generation, args=(message_id, chat_id, prompt)).start()
171
  else:
172
+ threading.Thread(target=response_audio, args=(message_id, chat_id, body)).start()
173
  return {"success": True}
174
 
175
  def main():