File size: 1,520 Bytes
e5af6ea
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
# Chatbot / Events/ Stop
# Use this card for chatbot interactions. Streaming can be stopped by the user.
# #chatbot #events #stop
# ---
from h2o_wave import main, app, Q, ui, data
import asyncio


async def stream_message(q):
    stream = ''
    q.page['example'].data += [stream, False]
    # Show the "Stop generating" button
    q.page['example'].generating = True
    for w in 'I am a fake chatbot. Sorry, I cannot help you.'.split():
        await asyncio.sleep(0.3)
        stream += w + ' '
        q.page['example'].data[-1] = [stream, False]
        await q.page.save()
    # Hide the "Stop generating" button
    q.page['example'].generating = False
    await q.page.save()


@app('/demo')
async def serve(q: Q):
    if not q.client.initialized:
        q.page['example'] = ui.chatbot_card(
            box='1 1 5 5',
            data=data(fields='content from_user', t='list'),
            name='chatbot',
            events=['stop']
        )
        q.client.initialized = True

    # Handle the stop event.
    if q.events.chatbot and q.events.chatbot.stop:
        # Cancel the streaming task.
        q.client.task.cancel()
        # Hide the "Stop generating" button.
        q.page['example'].generating = False
    # A new message arrived.
    elif q.args.chatbot:
        # Append user message.
        q.page['example'].data += [q.args.chatbot, True]
        # Run the streaming within cancelable asyncio task.
        q.client.task = asyncio.create_task(stream_message(q))

    await q.page.save()