BridgeEight commited on
Commit
16a3b53
1 Parent(s): c43dcd1

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +53 -2
app.py CHANGED
@@ -10,6 +10,57 @@ InterFace.async_engine = AsyncEngine(
10
  backend_config=backend_config,
11
  tp=1)
12
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
13
  with gr.Blocks(css=CSS, theme=THEME) as demo:
14
  state_chatbot = gr.State([])
15
  state_session_id = gr.State(0)
@@ -51,12 +102,12 @@ with gr.Blocks(css=CSS, theme=THEME) as demo:
51
  cancel_btn.click(
52
  cancel_local_func,
53
  [state_chatbot, cancel_btn, reset_btn, state_session_id],
54
- [state_chatbot, cancel_btn, reset_btn],
55
  cancels=[send_event])
56
 
57
  reset_btn.click(reset_local_func,
58
  [instruction_txtbox, state_chatbot, state_session_id],
59
- [state_chatbot, chatbot, instruction_txtbox],
60
  cancels=[send_event])
61
 
62
  def init():
 
10
  backend_config=backend_config,
11
  tp=1)
12
 
13
+ async def reset_local_func(instruction_txtbox: gr.Textbox,
14
+ state_chatbot: Sequence, session_id: int):
15
+ """reset the session.
16
+
17
+ Args:
18
+ instruction_txtbox (str): user's prompt
19
+ state_chatbot (Sequence): the chatting history
20
+ session_id (int): the session id
21
+ """
22
+ state_chatbot = []
23
+ # end the session
24
+ with InterFace.lock:
25
+ InterFace.global_session_id += 1
26
+ session_id = InterFace.global_session_id
27
+ return (state_chatbot, state_chatbot, gr.Textbox.update(value=''), session_id)
28
+
29
+ async def cancel_local_func(state_chatbot: Sequence, cancel_btn: gr.Button,
30
+ reset_btn: gr.Button, session_id: int):
31
+ """stop the session.
32
+
33
+ Args:
34
+ instruction_txtbox (str): user's prompt
35
+ state_chatbot (Sequence): the chatting history
36
+ cancel_btn (gr.Button): the cancel button
37
+ reset_btn (gr.Button): the reset button
38
+ session_id (int): the session id
39
+ """
40
+ yield (state_chatbot, disable_btn, disable_btn)
41
+ InterFace.async_engine.stop_session(session_id)
42
+ # pytorch backend does not support resume chat history now
43
+ if InterFace.async_engine.backend == 'pytorch':
44
+ yield (state_chatbot, disable_btn, enable_btn)
45
+ else:
46
+ with InterFace.lock:
47
+ InterFace.global_session_id += 1
48
+ session_id = InterFace.global_session_id
49
+ messages = []
50
+ for qa in state_chatbot:
51
+ messages.append(dict(role='user', content=qa[0]))
52
+ if qa[1] is not None:
53
+ messages.append(dict(role='assistant', content=qa[1]))
54
+ gen_config = GenerationConfig(max_new_tokens=0)
55
+ async for out in InterFace.async_engine.generate(messages,
56
+ session_id,
57
+ gen_config=gen_config,
58
+ stream_response=True,
59
+ sequence_start=True,
60
+ sequence_end=False):
61
+ pass
62
+ yield (state_chatbot, disable_btn, enable_btn, session_id)
63
+
64
  with gr.Blocks(css=CSS, theme=THEME) as demo:
65
  state_chatbot = gr.State([])
66
  state_session_id = gr.State(0)
 
102
  cancel_btn.click(
103
  cancel_local_func,
104
  [state_chatbot, cancel_btn, reset_btn, state_session_id],
105
+ [state_chatbot, cancel_btn, reset_btn, state_session_id],
106
  cancels=[send_event])
107
 
108
  reset_btn.click(reset_local_func,
109
  [instruction_txtbox, state_chatbot, state_session_id],
110
+ [state_chatbot, chatbot, instruction_txtbox, state_session_id],
111
  cancels=[send_event])
112
 
113
  def init():