sounar commited on
Commit
121448b
1 Parent(s): 3b56e84

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +43 -17
app.py CHANGED
@@ -1,36 +1,62 @@
1
  from transformers import AutoModelForCausalLM, AutoTokenizer, BitsAndBytesConfig
2
  import torch
 
 
3
  model_name = "ruslanmv/Medical-Llama3-8B"
4
- device_map = 'auto'
5
- bnb_config = BitsAndBytesConfig(load_in_4bit=True, bnb_4bit_quant_type="nf4",bnb_4bit_compute_dtype=torch.float16,)
6
- model = AutoModelForCausalLM.from_pretrained( model_name,quantization_config=bnb_config, trust_remote_code=True,use_cache=False,device_map=device_map)
 
 
 
 
 
 
 
 
 
 
 
 
7
  tokenizer = AutoTokenizer.from_pretrained(model_name, trust_remote_code=True)
8
- tokenizer.pad_token = tokenizer.eos_token
9
 
 
 
 
 
 
 
 
 
 
 
 
 
10
  def askme(question):
11
- sys_message = '''
12
  You are an AI Medical Assistant trained on a vast dataset of health information. Please be thorough and
13
  provide an informative answer. If you don't know the answer to a specific medical inquiry, advise seeking professional help.
14
- '''
15
- # Create messages structured for the chat template
16
  messages = [{"role": "system", "content": sys_message}, {"role": "user", "content": question}]
17
 
18
- # Applying chat template
19
  prompt = tokenizer.apply_chat_template(messages, tokenize=False, add_generation_prompt=True)
20
  inputs = tokenizer(prompt, return_tensors="pt").to("cuda")
 
 
21
  outputs = model.generate(**inputs, max_new_tokens=100, use_cache=True)
22
 
23
- # Extract and return the generated text, removing the prompt
24
- response_text = tokenizer.batch_decode(outputs)[0].strip()
25
- answer = response_text.split('<|im_start|>assistant')[-1].strip()
26
  return answer
27
- # Example usage
28
- # - Context: First describe your problem.
29
- # - Question: Then make the question.
30
 
31
- question = '''I'm a 35-year-old male and for the past few months, I've been experiencing fatigue,
 
 
32
  increased sensitivity to cold, and dry, itchy skin.
33
  Could these symptoms be related to hypothyroidism?
34
- If so, what steps should I take to get a proper diagnosis and discuss treatment options?'''
35
-
36
  print(askme(question))
 
1
  from transformers import AutoModelForCausalLM, AutoTokenizer, BitsAndBytesConfig
2
  import torch
3
+
4
+ # Model name and configuration
5
  model_name = "ruslanmv/Medical-Llama3-8B"
6
+ device_map = "auto"
7
+ bnb_config = BitsAndBytesConfig(
8
+ load_in_4bit=True,
9
+ bnb_4bit_quant_type="nf4",
10
+ bnb_4bit_compute_dtype=torch.float16,
11
+ )
12
+
13
+ # Load the model and tokenizer
14
+ model = AutoModelForCausalLM.from_pretrained(
15
+ model_name,
16
+ quantization_config=bnb_config,
17
+ trust_remote_code=True,
18
+ use_cache=False,
19
+ device_map=device_map,
20
+ )
21
  tokenizer = AutoTokenizer.from_pretrained(model_name, trust_remote_code=True)
 
22
 
23
+ # Set the chat template
24
+ chat_template = """<|im_start|>system
25
+ {system}
26
+ <|im_end|>
27
+ <|im_start|>user
28
+ {user}
29
+ <|im_end|>
30
+ <|im_start|>assistant
31
+ """
32
+ tokenizer.chat_template = chat_template
33
+
34
+ # Define the askme function
35
  def askme(question):
36
+ sys_message = """
37
  You are an AI Medical Assistant trained on a vast dataset of health information. Please be thorough and
38
  provide an informative answer. If you don't know the answer to a specific medical inquiry, advise seeking professional help.
39
+ """
40
+ # Structure messages for the chat
41
  messages = [{"role": "system", "content": sys_message}, {"role": "user", "content": question}]
42
 
43
+ # Apply the chat template
44
  prompt = tokenizer.apply_chat_template(messages, tokenize=False, add_generation_prompt=True)
45
  inputs = tokenizer(prompt, return_tensors="pt").to("cuda")
46
+
47
+ # Generate response
48
  outputs = model.generate(**inputs, max_new_tokens=100, use_cache=True)
49
 
50
+ # Extract and return the generated text
51
+ response_text = tokenizer.decode(outputs[0], skip_special_tokens=True)
52
+ answer = response_text.split("<|im_start|>assistant")[-1].strip()
53
  return answer
 
 
 
54
 
55
+ # Example usage
56
+ question = """
57
+ I'm a 35-year-old male and for the past few months, I've been experiencing fatigue,
58
  increased sensitivity to cold, and dry, itchy skin.
59
  Could these symptoms be related to hypothyroidism?
60
+ If so, what steps should I take to get a proper diagnosis and discuss treatment options?
61
+ """
62
  print(askme(question))