amornpan commited on
Commit
4f0b5c1
·
verified ·
1 Parent(s): c3ad444

Update README.md

Browse files
Files changed (1) hide show
  1. README.md +17 -12
README.md CHANGED
@@ -103,17 +103,17 @@ Here’s how to load and use the model for generating medical responses in Thai:
103
 
104
  First, ensure you have installed the required libraries by running:
105
 
106
- python
107
  pip install torch transformers bitsandbytes
108
-
109
  ## 2. Load the Model and Tokenizer
110
 
111
  You can load the model and tokenizer directly from Hugging Face using the following code:
112
 
113
- python
114
  import torch
115
  from transformers import AutoModelForCausalLM, AutoTokenizer, BitsAndBytesConfig
116
-
117
  # Define the model path
118
  model_path = 'amornpan/openthaigpt-MedChatModelv11'
119
 
@@ -125,64 +125,69 @@ tokenizer.pad_token = tokenizer.eos_token
125
 
126
  Create a custom medical prompt that you want the model to respond to:
127
 
128
- python
129
  custom_prompt = "โปรดอธิบายลักษณะช่องปากที่เป็นมะเร็งในระยะเริ่มต้น"
130
  PROMPT = f'[INST] <You are a question answering assistant. Answer the question as truthfully and helpfully as possible. คุณคือผู้ช่วยตอบคำถาม จงตอบคำถามอย่างถูกต้องและมีประโยชน์ที่สุด<>{custom_prompt}[/INST]'
131
 
132
  # Tokenize the input prompt
133
  inputs = tokenizer(PROMPT, return_tensors="pt", padding=True, truncation=True)
134
-
135
 
136
  ## 4. Configure the Model for Efficient Loading (4-bit Quantization)
137
 
138
  The model uses 4-bit precision for efficient inference. Here’s how to set up the configuration:
139
 
140
- python
141
  bnb_config = BitsAndBytesConfig(
142
  load_in_4bit=True,
143
  bnb_4bit_quant_type="nf4",
144
  bnb_4bit_compute_dtype=torch.float16
145
  )
 
146
 
147
  ## 5. Load the Model with Quantization Support
148
 
149
  Now, load the model with the 4-bit quantization settings:
150
 
151
- python
152
  model = AutoModelForCausalLM.from_pretrained(
153
  model_path,
154
  quantization_config=bnb_config,
155
  trust_remote_code=True
156
  )
 
157
 
158
  ## 6. Move the Model and Inputs to the GPU (prefer GPU)
159
 
160
  For faster inference, move the model and input tensors to a GPU, if available:
161
 
162
- python
163
  device = torch.device("cuda" if torch.cuda.is_available() else "cpu")
164
  model.to(device)
165
  inputs = {k: v.to(device) for k, v in inputs.items()}
 
166
 
167
  ## 7. Generate a Response from the Model
168
 
169
  Now, generate the medical response by running the model:
170
 
171
- python
172
  outputs = model.generate(**inputs, max_new_tokens=200, do_sample=True)
 
173
 
174
  ## 8. Decode the Generated Text
175
 
176
  Finally, decode and print the response from the model:
177
 
178
- python
179
  generated_text = tokenizer.decode(outputs[0], skip_special_tokens=True)
180
  print(generated_text)
181
 
182
  ## 9. Output
183
 
184
- python
185
  [INST] <You are a question answering assistant. Answer the question as truthfully and helpfully as possible. คุณคือผู้ช่วยตอบคำถาม จงตอบคำถามอย่างถูกต้องและมีประโยชน์ที่สุด<>โปรดอธิบายลักษณะช่องปากที่เป็นมะเร็งในระยะเริ่มต้น[/INST] มะเร็งช่องปากเป็นมะเร็งเพียงชนิดเดียวที่ได้รับผลกระทบจากนิโคติน มันคือผู้ชายกลุ่มอายุ 60 – 75 คน คุณจะแสดงอาการและเกิดขึ้นอย่างรวดเร็วหากเกิดมะเร็งช่องปาก คุณจะสังเกตเห็นปื้นแพร่กระจายของเนื้องอก ส่วนใหญ่ในช่องปาก เนื้องอกแสดงว่าเป็นเจ้าแห่ที่กำลังทำลายเยียวยา ค้นหาทั้งภายในและภายนอกลิ้นที่อยู่ติดกางเกงป่อง มะเร็งกระเพาะปัสสาวะหรือมะเร็งกล้ามเนื้อกระเพาะ
 
186
 
187
  ### Authors
188
  * Amornpan Phornchaicharoen (amornpan@gmail.com)
 
103
 
104
  First, ensure you have installed the required libraries by running:
105
 
106
+ ```python
107
  pip install torch transformers bitsandbytes
108
+ ```
109
  ## 2. Load the Model and Tokenizer
110
 
111
  You can load the model and tokenizer directly from Hugging Face using the following code:
112
 
113
+ ```python
114
  import torch
115
  from transformers import AutoModelForCausalLM, AutoTokenizer, BitsAndBytesConfig
116
+ ```
117
  # Define the model path
118
  model_path = 'amornpan/openthaigpt-MedChatModelv11'
119
 
 
125
 
126
  Create a custom medical prompt that you want the model to respond to:
127
 
128
+ ```python
129
  custom_prompt = "โปรดอธิบายลักษณะช่องปากที่เป็นมะเร็งในระยะเริ่มต้น"
130
  PROMPT = f'[INST] <You are a question answering assistant. Answer the question as truthfully and helpfully as possible. คุณคือผู้ช่วยตอบคำถาม จงตอบคำถามอย่างถูกต้องและมีประโยชน์ที่สุด<>{custom_prompt}[/INST]'
131
 
132
  # Tokenize the input prompt
133
  inputs = tokenizer(PROMPT, return_tensors="pt", padding=True, truncation=True)
134
+ ```
135
 
136
  ## 4. Configure the Model for Efficient Loading (4-bit Quantization)
137
 
138
  The model uses 4-bit precision for efficient inference. Here’s how to set up the configuration:
139
 
140
+ ```python
141
  bnb_config = BitsAndBytesConfig(
142
  load_in_4bit=True,
143
  bnb_4bit_quant_type="nf4",
144
  bnb_4bit_compute_dtype=torch.float16
145
  )
146
+ ```
147
 
148
  ## 5. Load the Model with Quantization Support
149
 
150
  Now, load the model with the 4-bit quantization settings:
151
 
152
+ ```python
153
  model = AutoModelForCausalLM.from_pretrained(
154
  model_path,
155
  quantization_config=bnb_config,
156
  trust_remote_code=True
157
  )
158
+ ```
159
 
160
  ## 6. Move the Model and Inputs to the GPU (prefer GPU)
161
 
162
  For faster inference, move the model and input tensors to a GPU, if available:
163
 
164
+ ```python
165
  device = torch.device("cuda" if torch.cuda.is_available() else "cpu")
166
  model.to(device)
167
  inputs = {k: v.to(device) for k, v in inputs.items()}
168
+ ```
169
 
170
  ## 7. Generate a Response from the Model
171
 
172
  Now, generate the medical response by running the model:
173
 
174
+ ```python
175
  outputs = model.generate(**inputs, max_new_tokens=200, do_sample=True)
176
+ ```
177
 
178
  ## 8. Decode the Generated Text
179
 
180
  Finally, decode and print the response from the model:
181
 
182
+ ```python
183
  generated_text = tokenizer.decode(outputs[0], skip_special_tokens=True)
184
  print(generated_text)
185
 
186
  ## 9. Output
187
 
188
+ ```python
189
  [INST] <You are a question answering assistant. Answer the question as truthfully and helpfully as possible. คุณคือผู้ช่วยตอบคำถาม จงตอบคำถามอย่างถูกต้องและมีประโยชน์ที่สุด<>โปรดอธิบายลักษณะช่องปากที่เป็นมะเร็งในระยะเริ่มต้น[/INST] มะเร็งช่องปากเป็นมะเร็งเพียงชนิดเดียวที่ได้รับผลกระทบจากนิโคติน มันคือผู้ชายกลุ่มอายุ 60 – 75 คน คุณจะแสดงอาการและเกิดขึ้นอย่างรวดเร็วหากเกิดมะเร็งช่องปาก คุณจะสังเกตเห็นปื้นแพร่กระจายของเนื้องอก ส่วนใหญ่ในช่องปาก เนื้องอกแสดงว่าเป็นเจ้าแห่ที่กำลังทำลายเยียวยา ค้นหาทั้งภายในและภายนอกลิ้นที่อยู่ติดกางเกงป่อง มะเร็งกระเพาะปัสสาวะหรือมะเร็งกล้ามเนื้อกระเพาะ
190
+ ```
191
 
192
  ### Authors
193
  * Amornpan Phornchaicharoen (amornpan@gmail.com)