Limour commited on
Commit
cf65ac6
1 Parent(s): 89cf6c5

Upload 6 files

Browse files
mods/btn_com.py CHANGED
@@ -4,6 +4,18 @@ def init(cfg):
4
  gr = cfg['gr']
5
  lock = cfg['session_lock']
6
 
 
 
 
 
 
 
 
 
 
 
 
 
7
  # ========== 流式输出函数 ==========
8
  def btn_com(_n_keep, _n_discard,
9
  _temperature, _repeat_penalty, _frequency_penalty,
@@ -35,30 +47,36 @@ def init(cfg):
35
  mirostat_tau=_mirostat_tau,
36
  mirostat_eta=_mirostat_eta,
37
  ):
 
38
  if token in chat_template.eos or token == chat_template.nlnl:
39
  t_bot.extend(completion_tokens)
40
  print('token in eos', token)
41
  break
 
42
  completion_tokens.append(token)
43
  all_text = model.str_detokenize(completion_tokens)
44
  if not all_text:
45
  continue
46
  t_bot.extend(completion_tokens)
 
47
  history += all_text
48
  yield history
49
- if token in chat_template.onenl:
50
- # ========== 移除末尾的换行符 ==========
51
- if t_bot[-2] in chat_template.onenl:
52
- model.venv_pop_token()
53
- break
54
- if t_bot[-2] in chat_template.onerl and t_bot[-3] in chat_template.onenl:
55
- model.venv_pop_token()
56
- break
57
- if history[-2:] == '\n\n': # 各种 'x\n\n' 的token,比如'。\n\n'
58
- print('t_bot[-4:]', t_bot[-4:], repr(model.str_detokenize(t_bot[-4:])),
59
- repr(model.str_detokenize(t_bot[-1:])))
 
 
60
  break
61
- if len(t_bot) > _max_tokens:
 
62
  break
63
  completion_tokens = []
64
  # ========== 查看末尾的换行符 ==========
@@ -75,15 +93,22 @@ def init(cfg):
75
  def _inner():
76
  with lock:
77
  if cfg['session_active'] != finish:
78
- raise RuntimeError
79
  cfg['session_active'] = not cfg['session_active']
80
- return tmp, tmp, tmp, tmp
 
81
 
82
  return _inner
83
 
84
  btn_start_or_finish_outputs = [cfg['btn_submit'], cfg['btn_vo'],
85
  cfg['btn_suggest'], cfg['btn_retry']]
86
 
 
 
 
 
 
 
87
  cfg['btn_start'] = {
88
  'fn': btn_start_or_finish(False),
89
  'outputs': btn_start_or_finish_outputs
@@ -94,6 +119,9 @@ def init(cfg):
94
  'outputs': btn_start_or_finish_outputs
95
  }
96
 
 
 
 
97
  cfg['setting'] = [cfg[x] for x in ('setting_n_keep', 'setting_n_discard',
98
  'setting_temperature', 'setting_repeat_penalty', 'setting_frequency_penalty',
99
  'setting_presence_penalty', 'setting_repeat_last_n', 'setting_top_k',
 
4
  gr = cfg['gr']
5
  lock = cfg['session_lock']
6
 
7
+ with gr.Row():
8
+ cfg['btn_vo'] = gr.Button("旁白")
9
+ cfg['btn_rag'] = gr.Button("RAG")
10
+ cfg['btn_retry'] = gr.Button("Retry")
11
+ cfg['btn_stop'] = gr.Button("Stop")
12
+ cfg['btn_reset'] = gr.Button("Reset")
13
+ cfg['btn_debug'] = gr.Button("Debug")
14
+ cfg['btn_submit'] = gr.Button("Submit")
15
+ cfg['btn_suggest'] = gr.Button("建议")
16
+
17
+ cfg['btn_stop_status'] = True
18
+
19
  # ========== 流式输出函数 ==========
20
  def btn_com(_n_keep, _n_discard,
21
  _temperature, _repeat_penalty, _frequency_penalty,
 
47
  mirostat_tau=_mirostat_tau,
48
  mirostat_eta=_mirostat_eta,
49
  ):
50
+ # ========== eos or nlnl 说明eos了 ==========
51
  if token in chat_template.eos or token == chat_template.nlnl:
52
  t_bot.extend(completion_tokens)
53
  print('token in eos', token)
54
  break
55
+ # ========== 避免不完整的utf-8编码 ==========
56
  completion_tokens.append(token)
57
  all_text = model.str_detokenize(completion_tokens)
58
  if not all_text:
59
  continue
60
  t_bot.extend(completion_tokens)
61
+ # ========== 流式输出 ==========
62
  history += all_text
63
  yield history
64
+ # ========== \n role \n 结构说明eos了 ==========
65
+ tmp = chat_template.eos_in_role(history, t_bot)
66
+ if tmp:
67
+ tmp -= 1 # 最后一个token并未进入kv_cache
68
+ if tmp:
69
+ model.venv_pop_token(tmp)
70
+ break
71
+ # ========== \n\n 结构说明eos了 ==========
72
+ tmp = chat_template.eos_in_nlnl(history, t_bot)
73
+ if tmp:
74
+ tmp -= 1 # 最后一个token并未进入kv_cache
75
+ if tmp:
76
+ model.venv_pop_token(tmp)
77
  break
78
+ # ========== 过长 or 按下了stop按钮 ==========
79
+ if len(t_bot) > _max_tokens or cfg['btn_stop_status']:
80
  break
81
  completion_tokens = []
82
  # ========== 查看末尾的换行符 ==========
 
93
  def _inner():
94
  with lock:
95
  if cfg['session_active'] != finish:
96
+ raise RuntimeError('任务中断!请稍等或Reset,如已Reset,请忽略。')
97
  cfg['session_active'] = not cfg['session_active']
98
+ cfg['btn_stop_status'] = finish
99
+ return tmp, tmp, tmp, tmp
100
 
101
  return _inner
102
 
103
  btn_start_or_finish_outputs = [cfg['btn_submit'], cfg['btn_vo'],
104
  cfg['btn_suggest'], cfg['btn_retry']]
105
 
106
+ cfg['btn_concurrency'] = {
107
+ 'trigger_mode': 'once',
108
+ 'concurrency_id': 'btn_com',
109
+ 'concurrency_limit': 1
110
+ }
111
+
112
  cfg['btn_start'] = {
113
  'fn': btn_start_or_finish(False),
114
  'outputs': btn_start_or_finish_outputs
 
119
  'outputs': btn_start_or_finish_outputs
120
  }
121
 
122
+ cfg['btn_start'].update(cfg['btn_concurrency'])
123
+ cfg['btn_finish'].update(cfg['btn_concurrency'])
124
+
125
  cfg['setting'] = [cfg[x] for x in ('setting_n_keep', 'setting_n_discard',
126
  'setting_temperature', 'setting_repeat_penalty', 'setting_frequency_penalty',
127
  'setting_presence_penalty', 'setting_repeat_last_n', 'setting_top_k',
mods/btn_reset.py CHANGED
@@ -20,16 +20,26 @@ def init(cfg):
20
  with cfg['session_lock']:
21
  _tmp = model.load_session(_cache_path)
22
  print(f'load cache from {_cache_path} {_tmp}')
23
- cfg['session_active'] = False
24
  return model.venv_info
25
  except Exception as e:
26
  restart_space()
27
  raise e
28
 
 
 
 
 
 
 
 
29
  cfg['btn_reset'].click(
 
 
30
  fn=btn_reset,
31
  inputs=cfg['setting_cache_path'],
32
- outputs=s_info
 
33
  ).success(
34
  **cfg['btn_finish']
35
  )
 
20
  with cfg['session_lock']:
21
  _tmp = model.load_session(_cache_path)
22
  print(f'load cache from {_cache_path} {_tmp}')
23
+ cfg['session_active'] = True
24
  return model.venv_info
25
  except Exception as e:
26
  restart_space()
27
  raise e
28
 
29
+ def btn_stop():
30
+ cfg['btn_stop_status'] = True
31
+
32
+ cfg['btn_stop'].click(
33
+ fn=btn_stop
34
+ )
35
+
36
  cfg['btn_reset'].click(
37
+ fn=btn_stop
38
+ ).success(
39
  fn=btn_reset,
40
  inputs=cfg['setting_cache_path'],
41
+ outputs=s_info,
42
+ **cfg['btn_concurrency']
43
  ).success(
44
  **cfg['btn_finish']
45
  )
mods/btn_retry.py CHANGED
@@ -52,7 +52,8 @@ def init(cfg):
52
  ).success(
53
  fn=btn_retry,
54
  inputs=[chatbot]+cfg['setting'],
55
- outputs=[chatbot, s_info]
 
56
  ).success(
57
  **cfg['btn_finish']
58
  )
 
52
  ).success(
53
  fn=btn_retry,
54
  inputs=[chatbot]+cfg['setting'],
55
+ outputs=[chatbot, s_info],
56
+ **cfg['btn_concurrency']
57
  ).success(
58
  **cfg['btn_finish']
59
  )
mods/btn_submit.py CHANGED
@@ -66,11 +66,13 @@ def init(cfg):
66
  ).success(
67
  fn=btn_submit_usr, api_name="submit",
68
  inputs=[msg, chatbot],
69
- outputs=[msg, chatbot]
 
70
  ).success(
71
  fn=btn_submit_bot,
72
  inputs=[chatbot]+cfg['setting'],
73
- outputs=[chatbot, s_info]
 
74
  ).success(
75
  **cfg['btn_finish']
76
  )
 
66
  ).success(
67
  fn=btn_submit_usr, api_name="submit",
68
  inputs=[msg, chatbot],
69
+ outputs=[msg, chatbot],
70
+ **cfg['btn_concurrency']
71
  ).success(
72
  fn=btn_submit_bot,
73
  inputs=[chatbot]+cfg['setting'],
74
+ outputs=[chatbot, s_info],
75
+ **cfg['btn_concurrency']
76
  ).success(
77
  **cfg['btn_finish']
78
  )
mods/btn_suggest.py CHANGED
@@ -35,7 +35,8 @@ def init(cfg):
35
  ).success(
36
  fn=btn_suggest,
37
  inputs=cfg['setting'],
38
- outputs=[cfg['msg'], s_info]
 
39
  ).success(
40
  **cfg['btn_finish']
41
  )
 
35
  ).success(
36
  fn=btn_suggest,
37
  inputs=cfg['setting'],
38
+ outputs=[cfg['msg'], s_info],
39
+ **cfg['btn_concurrency']
40
  ).success(
41
  **cfg['btn_finish']
42
  )
mods/btn_vo.py CHANGED
@@ -35,7 +35,8 @@ def init(cfg):
35
  ).success(
36
  fn=btn_vo,
37
  inputs=cfg['setting'],
38
- outputs=[cfg['vo'], s_info]
 
39
  ).success(
40
  **cfg['btn_finish']
41
  )
 
35
  ).success(
36
  fn=btn_vo,
37
  inputs=cfg['setting'],
38
+ outputs=[cfg['vo'], s_info],
39
+ **cfg['btn_concurrency']
40
  ).success(
41
  **cfg['btn_finish']
42
  )