CMLL commited on
Commit
4fbe483
1 Parent(s): 7161b69

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +21 -11
app.py CHANGED
@@ -21,17 +21,27 @@ tokenizer = AutoTokenizer.from_pretrained(
21
 
22
  @spaces.GPU
23
  def single_turn_chat(question):
24
- prompt = f"Question: {question}"
25
- messages = [
26
- {"role": "system", "content": "You are a helpful TCM medical assistant named 仲景中医大语言模型, created by 医哲未来 of Fudan University."},
27
- {"role": "user", "content": prompt}
28
- ]
29
- input = tokenizer.apply_chat_template(messages, tokenize=False, add_generation_prompt=True)
30
- model_inputs = tokenizer([input], return_tensors="pt").to(device)
31
- generated_ids = model.generate(model_inputs.input_ids, max_new_tokens=512)
32
- generated_ids = [output_ids[len(input_ids):] for input_ids, output_ids in zip(model_inputs.input_ids, generated_ids)]
33
- response = tokenizer.batch_decode(generated_ids, skip_special_tokens=True)[0]
34
- return response
 
 
 
 
 
 
 
 
 
 
35
 
36
  @spaces.GPU
37
  def multi_turn_chat(question, chat_history=None):
 
21
 
22
  @spaces.GPU
23
  def single_turn_chat(question):
24
+ try:
25
+ prompt = f"Question: {question}"
26
+ messages = [
27
+ {"role": "system", "content": "You are a helpful TCM medical assistant named 仲景中医大语言模型, created by 医哲未来 of Fudan University."},
28
+ {"role": "user", "content": prompt}
29
+ ]
30
+ input = tokenizer.apply_chat_template(messages, tokenize=False, add_generation_prompt=True)
31
+ model_inputs = tokenizer([input], return_tensors="pt").to(device)
32
+ print("Debug: Model inputs prepared successfully.")
33
+
34
+ generated_ids = model.generate(model_inputs.input_ids, max_new_tokens=512)
35
+ print("Debug: Model generation completed successfully.")
36
+
37
+ generated_ids = [output_ids[len(input_ids):] for input_ids, output_ids in zip(model_inputs.input_ids, generated_ids)]
38
+ response = tokenizer.batch_decode(generated_ids, skip_special_tokens=True)[0]
39
+ return response
40
+ except Exception as e:
41
+ print(f"Error during model invocation: {str(e)}")
42
+ raise
43
+
44
+
45
 
46
  @spaces.GPU
47
  def multi_turn_chat(question, chat_history=None):