Spaces:
Sleeping
Sleeping
Update chainlit.py
Browse files- chainlit.py +5 -11
chainlit.py
CHANGED
|
@@ -106,29 +106,23 @@ async def on_chat_start():
|
|
| 106 |
@cl.on_message
|
| 107 |
async def main(message: cl.Message):
|
| 108 |
"""Main message handler with concurrent animation and API call"""
|
| 109 |
-
|
| 110 |
-
|
|
|
|
| 111 |
await msg.send()
|
| 112 |
|
| 113 |
-
# Create concurrent tasks
|
| 114 |
animation_task = asyncio.create_task(run_typing_animation(msg))
|
| 115 |
api_task = asyncio.create_task(ChatService.respond_to_chat(message.content))
|
| 116 |
|
| 117 |
try:
|
| 118 |
-
# Wait for API response (this will complete first usually)
|
| 119 |
response = await api_task
|
| 120 |
-
|
| 121 |
-
# Cancel animation task since we have the response
|
| 122 |
animation_task.cancel()
|
| 123 |
-
|
| 124 |
-
# Wait a bit for graceful animation cancellation
|
| 125 |
try:
|
| 126 |
await asyncio.wait_for(animation_task, timeout=0.1)
|
| 127 |
except (asyncio.CancelledError, asyncio.TimeoutError):
|
| 128 |
pass
|
| 129 |
-
|
| 130 |
except Exception as e:
|
| 131 |
-
# If API fails, cancel animation and show error
|
| 132 |
animation_task.cancel()
|
| 133 |
try:
|
| 134 |
await asyncio.wait_for(animation_task, timeout=0.1)
|
|
@@ -136,6 +130,6 @@ async def main(message: cl.Message):
|
|
| 136 |
pass
|
| 137 |
response = f"Error: {e}"
|
| 138 |
|
| 139 |
-
# Update
|
| 140 |
msg.content = response
|
| 141 |
await msg.update()
|
|
|
|
| 106 |
@cl.on_message
|
| 107 |
async def main(message: cl.Message):
|
| 108 |
"""Main message handler with concurrent animation and API call"""
|
| 109 |
+
|
| 110 |
+
# Create a placeholder message for typing animation
|
| 111 |
+
msg = cl.Message(content="⠋ Analyzing sentiment...", author="assistant")
|
| 112 |
await msg.send()
|
| 113 |
|
| 114 |
+
# Create concurrent tasks
|
| 115 |
animation_task = asyncio.create_task(run_typing_animation(msg))
|
| 116 |
api_task = asyncio.create_task(ChatService.respond_to_chat(message.content))
|
| 117 |
|
| 118 |
try:
|
|
|
|
| 119 |
response = await api_task
|
|
|
|
|
|
|
| 120 |
animation_task.cancel()
|
|
|
|
|
|
|
| 121 |
try:
|
| 122 |
await asyncio.wait_for(animation_task, timeout=0.1)
|
| 123 |
except (asyncio.CancelledError, asyncio.TimeoutError):
|
| 124 |
pass
|
|
|
|
| 125 |
except Exception as e:
|
|
|
|
| 126 |
animation_task.cancel()
|
| 127 |
try:
|
| 128 |
await asyncio.wait_for(animation_task, timeout=0.1)
|
|
|
|
| 130 |
pass
|
| 131 |
response = f"Error: {e}"
|
| 132 |
|
| 133 |
+
# Update final message
|
| 134 |
msg.content = response
|
| 135 |
await msg.update()
|