ruslanmv commited on
Commit
a532c03
1 Parent(s): 02a0124

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +22 -0
app.py CHANGED
@@ -17,6 +17,28 @@ tokenizer = AutoTokenizer.from_pretrained(model_name, trust_remote_code=True)
17
  tokenizer.pad_token = tokenizer.eos_token
18
  @spaces.GPU
19
  def askme(symptoms, question):
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
20
  sys_message = '''\
21
  You are an AI Medical Assistant trained on a vast dataset of health information. Please be thorough and
22
  provide an informative answer. If you don't know the answer to a specific medical inquiry, advise seeking professional help.
 
17
  tokenizer.pad_token = tokenizer.eos_token
18
  @spaces.GPU
19
  def askme(symptoms, question):
20
+ custom_template = [
21
+ {"role": "system", "content": "You are an AI Medical Assistant trained on a vast dataset of health information. Please be thorough and provide an informative answer. If you don't know the answer to a specific medical inquiry, advise seeking professional help."},
22
+ {"role": "user", "content": "Symptoms: {symptoms}\nQuestion: {question}\n"},
23
+ {"role": "assistant", "content": "{assistant_response}\n"}
24
+ ]
25
+
26
+ sys_message = custom_template[0]["content"]
27
+ content = custom_template[1]["content"].format(symptoms=symptoms, question=question)
28
+ messages = [{"role": "system", "content": sys_message}, {"role": "user", "content": content}]
29
+
30
+ prompt = tokenizer.apply_chat_template(messages, template=custom_template, tokenize=False, add_generation_prompt=True)
31
+ inputs = tokenizer(prompt, return_tensors="pt").to(device) # Ensure inputs are on CUDA device
32
+ outputs = model.generate(**inputs, max_new_tokens=200, use_cache=True)
33
+ response_text = tokenizer.batch_decode(outputs, skip_special_tokens=True)[0].strip()
34
+
35
+ # Extract only the assistant's response
36
+ assistant_response = response_text.split("<|im_start|>assistant")[-1].strip()
37
+ return assistant_response
38
+
39
+
40
+
41
+ def askmeold(symptoms, question):
42
  sys_message = '''\
43
  You are an AI Medical Assistant trained on a vast dataset of health information. Please be thorough and
44
  provide an informative answer. If you don't know the answer to a specific medical inquiry, advise seeking professional help.