CMLL commited on
Commit
da06a28
1 Parent(s): b1d449b

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +6 -9
app.py CHANGED
@@ -12,7 +12,7 @@ device = torch.device("cuda:{}".format(device_ids_parallel[0]) if USE_CUDA else
12
  # 初始化
13
  peft_model_id = "CMLM/ZhongJing-2-1_8b"
14
  base_model_id = "Qwen/Qwen1.5-1.8B-Chat"
15
- model = AutoModelForCausalLM.from_pretrained(base_model_id, device_map="auto")
16
  model.load_adapter(peft_model_id)
17
  tokenizer = AutoTokenizer.from_pretrained(
18
  "CMLM/ZhongJing-2-1_8b",
@@ -29,8 +29,8 @@ def single_turn_chat(question):
29
  {"role": "system", "content": "You are a helpful TCM medical assistant named 仲景中医大语言模型, created by 医哲未来 of Fudan University."},
30
  {"role": "user", "content": prompt}
31
  ]
32
- input = tokenizer.apply_chat_template(messages, tokenize=False, add_generation_prompt=True)
33
- model_inputs = tokenizer([input], return_tensors="pt").to(device)
34
  generated_ids = model.generate(model_inputs.input_ids, max_new_tokens=512)
35
  generated_ids = [output_ids[len(input_ids):] for input_ids, output_ids in zip(model_inputs.input_ids, generated_ids)]
36
  response = tokenizer.batch_decode(generated_ids, skip_special_tokens=True)[0]
@@ -63,16 +63,13 @@ def multi_turn_chat(question, chat_history=None):
63
  chat_history.append({"role": "assistant", "content": response})
64
 
65
  # Format the chat history for output
66
- tempass = ""
67
- tempuser = ""
68
  formatted_history = []
 
69
  for entry in chat_history:
70
  if entry['role'] == 'user':
71
  tempuser = entry['content']
72
  elif entry['role'] == 'assistant':
73
- tempass = entry['content']
74
- temp = (tempuser, tempass)
75
- formatted_history.append(temp)
76
 
77
  return formatted_history, chat_history
78
 
@@ -102,4 +99,4 @@ with gr.Blocks() as multi_turn_interface:
102
  user_input.submit(multi_turn_chat, [user_input, state], [chatbot, state])
103
 
104
  single_turn_interface.launch()
105
- multi_turn_interface.launch()
 
12
  # 初始化
13
  peft_model_id = "CMLM/ZhongJing-2-1_8b"
14
  base_model_id = "Qwen/Qwen1.5-1.8B-Chat"
15
+ model = AutoModelForCausalLM.from_pretrained(base_model_id, device_map="auto").to(device)
16
  model.load_adapter(peft_model_id)
17
  tokenizer = AutoTokenizer.from_pretrained(
18
  "CMLM/ZhongJing-2-1_8b",
 
29
  {"role": "system", "content": "You are a helpful TCM medical assistant named 仲景中医大语言模型, created by 医哲未来 of Fudan University."},
30
  {"role": "user", "content": prompt}
31
  ]
32
+ input_text = tokenizer.apply_chat_template(messages, tokenize=False, add_generation_prompt=True)
33
+ model_inputs = tokenizer([input_text], return_tensors="pt").to(device)
34
  generated_ids = model.generate(model_inputs.input_ids, max_new_tokens=512)
35
  generated_ids = [output_ids[len(input_ids):] for input_ids, output_ids in zip(model_inputs.input_ids, generated_ids)]
36
  response = tokenizer.batch_decode(generated_ids, skip_special_tokens=True)[0]
 
63
  chat_history.append({"role": "assistant", "content": response})
64
 
65
  # Format the chat history for output
 
 
66
  formatted_history = []
67
+ tempuser = ""
68
  for entry in chat_history:
69
  if entry['role'] == 'user':
70
  tempuser = entry['content']
71
  elif entry['role'] == 'assistant':
72
+ formatted_history.append((tempuser, entry['content']))
 
 
73
 
74
  return formatted_history, chat_history
75
 
 
99
  user_input.submit(multi_turn_chat, [user_input, state], [chatbot, state])
100
 
101
  single_turn_interface.launch()
102
+ multi_turn_interface.launch()