JulsdL commited on
Commit
ab94a07
·
1 Parent(s): 33e9530

Refactor function to include error handling and streamline audio processing flow in

Browse files
Files changed (1) hide show
  1. smartquery/app.py +28 -26
smartquery/app.py CHANGED
@@ -58,32 +58,34 @@ async def on_audio_chunk(chunk: cl.AudioChunk):
58
 
59
  @cl.on_audio_end
60
  async def on_audio_end(elements: list[Audio]):
61
- audio_buffer: BytesIO = cl.user_session.get("audio_buffer")
62
- audio_buffer.seek(0)
63
- audio_file = audio_buffer.read()
64
- audio_mime_type: str = cl.user_session.get("audio_mime_type")
65
-
66
- input_audio_el = Audio(
67
- mime=audio_mime_type, content=audio_file, name=audio_buffer.name
68
- )
69
- await cl.Message(
70
- author="You",
71
- type="user_message",
72
- content="",
73
- elements=[input_audio_el, *elements]
74
- ).send()
75
-
76
- answer_message = await cl.Message(content="").send()
77
-
78
- whisper_input = (audio_buffer.name, audio_file, audio_mime_type)
79
- transcription = await speech_to_text(whisper_input)
80
-
81
- await process_message(transcription, answer_message, audio_mime_type)
82
-
83
- # Reset audio buffer and mime type
84
- cl.user_session.set("audio_buffer", None)
85
- cl.user_session.set("audio_mime_type", None)
86
- print("Audio buffer reset")
 
 
87
 
88
  async def process_message(content: str, answer_message=None, mime_type=None):
89
  agent = cl.user_session.get("agent")
 
58
 
59
  @cl.on_audio_end
60
  async def on_audio_end(elements: list[Audio]):
61
+ try:
62
+ audio_buffer: BytesIO = cl.user_session.get("audio_buffer")
63
+ audio_buffer.seek(0)
64
+ audio_file = audio_buffer.read()
65
+ audio_mime_type: str = cl.user_session.get("audio_mime_type")
66
+
67
+ input_audio_el = Audio(
68
+ mime=audio_mime_type, content=audio_file, name=audio_buffer.name
69
+ )
70
+ await cl.Message(
71
+ author="You",
72
+ type="user_message",
73
+ content="",
74
+ elements=[input_audio_el, *elements]
75
+ ).send()
76
+
77
+ whisper_input = (audio_buffer.name, audio_file, audio_mime_type)
78
+ transcription = await speech_to_text(whisper_input)
79
+
80
+ await process_message(transcription)
81
+ except Exception as e:
82
+ print(f"Error processing audio: {e}")
83
+ await cl.Message(content="Error processing audio. Please try again.").send()
84
+ finally:
85
+ # Reset audio buffer and mime type
86
+ cl.user_session.set("audio_buffer", None)
87
+ cl.user_session.set("audio_mime_type", None)
88
+ print("Audio buffer reset")
89
 
90
  async def process_message(content: str, answer_message=None, mime_type=None):
91
  agent = cl.user_session.get("agent")