Tuchuanhuhuhu commited on
Commit
cede152
·
1 Parent(s): 60a3927

新功能:默认加载模板文件;文件名和模板名按字母顺序排序

Browse files
Files changed (1) hide show
  1. ChuanhuChatbot.py +7 -4
ChuanhuChatbot.py CHANGED
@@ -222,7 +222,7 @@ def load_chat_history(filename):
222
  def get_file_names(dir, plain=False, filetype=".json"):
223
  # find all json files in the current directory and return their names
224
  try:
225
- files = [f for f in os.listdir(dir) if f.endswith(filetype)]
226
  except FileNotFoundError:
227
  files = []
228
  if plain:
@@ -233,13 +233,16 @@ def get_file_names(dir, plain=False, filetype=".json"):
233
  def get_history_names(plain=False):
234
  return get_file_names(HISTORY_DIR, plain)
235
 
236
- def load_template(filename):
237
  lines = []
238
  with open(os.path.join(TEMPLATES_DIR, filename), "r", encoding="utf8") as csvfile:
239
  reader = csv.reader(csvfile)
240
  lines = list(reader)
241
  lines = lines[1:]
242
- return {row[0]:row[1] for row in lines}, gr.Dropdown.update(choices=[row[0] for row in lines])
 
 
 
243
 
244
  def get_template_names(plain=False):
245
  return get_file_names(TEMPLATES_DIR, plain, filetype=".csv")
@@ -331,7 +334,7 @@ with gr.Blocks(css=customCSS) as demo:
331
  templaeFileReadBtn = gr.Button("📂 读入模板")
332
  with gr.Row():
333
  with gr.Column(scale=6):
334
- templateSelectDropdown = gr.Dropdown(label="从Prompt模板中加载", choices=[], multiselect=False)
335
  with gr.Column(scale=1):
336
  templateApplyBtn = gr.Button("⬇️ 应用")
337
  with gr.Accordion(label="保存/加载对话历史记录(在文本框中输入文件名,点击“保存对话”按钮,历史记录文件会被存储到Python文件旁边)", open=False):
 
222
  def get_file_names(dir, plain=False, filetype=".json"):
223
  # find all json files in the current directory and return their names
224
  try:
225
+ files = sorted([f for f in os.listdir(dir) if f.endswith(filetype)])
226
  except FileNotFoundError:
227
  files = []
228
  if plain:
 
233
  def get_history_names(plain=False):
234
  return get_file_names(HISTORY_DIR, plain)
235
 
236
+ def load_template(filename, plain=False):
237
  lines = []
238
  with open(os.path.join(TEMPLATES_DIR, filename), "r", encoding="utf8") as csvfile:
239
  reader = csv.reader(csvfile)
240
  lines = list(reader)
241
  lines = lines[1:]
242
+ if plain:
243
+ return sorted([row[0] for row in lines])
244
+ else:
245
+ return {row[0]:row[1] for row in lines}, gr.Dropdown.update(choices=sorted([row[0] for row in lines]))
246
 
247
  def get_template_names(plain=False):
248
  return get_file_names(TEMPLATES_DIR, plain, filetype=".csv")
 
334
  templaeFileReadBtn = gr.Button("📂 读入模板")
335
  with gr.Row():
336
  with gr.Column(scale=6):
337
+ templateSelectDropdown = gr.Dropdown(label="从Prompt模板中加载", choices=load_template(get_template_names(plain=True)[0], plain=True), multiselect=False)
338
  with gr.Column(scale=1):
339
  templateApplyBtn = gr.Button("⬇️ 应用")
340
  with gr.Accordion(label="保存/加载对话历史记录(在文本框中输入文件名,点击“保存对话”按钮,历史记录文件会被存储到Python文件旁边)", open=False):