Spaces:
Paused
Paused
Refactor function to include error handling and streamline audio processing flow in
Browse files- 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 |
-
|
| 62 |
-
|
| 63 |
-
|
| 64 |
-
|
| 65 |
-
|
| 66 |
-
|
| 67 |
-
|
| 68 |
-
|
| 69 |
-
|
| 70 |
-
|
| 71 |
-
|
| 72 |
-
|
| 73 |
-
|
| 74 |
-
|
| 75 |
-
|
| 76 |
-
|
| 77 |
-
|
| 78 |
-
|
| 79 |
-
|
| 80 |
-
|
| 81 |
-
|
| 82 |
-
|
| 83 |
-
|
| 84 |
-
|
| 85 |
-
|
| 86 |
-
|
|
|
|
|
|
|
| 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")
|