Update README.md
Browse files
README.md
CHANGED
@@ -18,66 +18,51 @@ pip install -qU transformers==4.36.2 datasets python-dotenv peft bitsandbytes
|
|
18 |
## Example Usage
|
19 |
```python
|
20 |
|
21 |
-
from transformers import AutoModelForCausalLM, AutoTokenizer
|
|
|
22 |
|
23 |
# Define the name of your fine-tuned model
|
24 |
finetuned_model = 'ruslanmv/Medical-Mixtral-7B-v2k'
|
25 |
|
26 |
-
# Load tokenizer
|
27 |
-
tokenizer = AutoTokenizer.from_pretrained(finetuned_model, trust_remote_code=True)
|
28 |
-
|
29 |
-
# Load the model with the provided adapter configuration and weights
|
30 |
-
model_pretrained = AutoModelForCausalLM.from_pretrained(finetuned_model, trust_remote_code=True, torch_dtype=torch.float16)
|
31 |
-
|
32 |
-
messages = [
|
33 |
-
{'role': 'user', 'content': 'What should I do to reduce my weight gained due to genetic hypothyroidism?'},
|
34 |
-
{'role': 'assistant', 'content': ''},
|
35 |
-
]
|
36 |
-
|
37 |
-
input_ids = tokenizer.apply_chat_template(messages, return_tensors='pt').to('cuda')
|
38 |
-
outputs = model_pretrained.generate(input_ids, max_new_tokens=500)
|
39 |
-
|
40 |
-
print(tokenizer.decode(outputs[0], skip_special_tokens=True))
|
41 |
-
|
42 |
-
|
43 |
-
```
|
44 |
-
|
45 |
-
For Gpus
|
46 |
-
```python
|
47 |
-
|
48 |
-
# Define the name of your fine-tuned model
|
49 |
-
finetuned_model = 'ruslanmv/{new_model}'
|
50 |
-
|
51 |
# Load fine-tuned model
|
52 |
bnb_config = BitsAndBytesConfig(
|
53 |
-
load_in_4bit=
|
54 |
-
bnb_4bit_quant_type=
|
55 |
-
bnb_4bit_compute_dtype=
|
56 |
-
bnb_4bit_use_double_quant=
|
57 |
)
|
58 |
model_pretrained = AutoModelForCausalLM.from_pretrained(
|
59 |
-
|
60 |
-
|
61 |
-
|
62 |
-
|
63 |
-
|
64 |
-
|
65 |
-
cache_dir=cache_dir
|
66 |
)
|
|
|
67 |
# Load tokenizer
|
68 |
-
tokenizer = AutoTokenizer.from_pretrained(finetuned_model,
|
69 |
-
|
70 |
-
|
71 |
-
|
72 |
-
|
73 |
-
|
|
|
74 |
def build_prompt(question):
|
75 |
-
|
76 |
-
|
77 |
|
78 |
-
question = "
|
79 |
prompt = build_prompt(question)
|
80 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
81 |
|
82 |
|
83 |
```
|
|
|
18 |
## Example Usage
|
19 |
```python
|
20 |
|
21 |
+
from transformers import AutoModelForCausalLM, AutoTokenizer, pipeline, logging, BitsAndBytesConfig
|
22 |
+
import os, torch
|
23 |
|
24 |
# Define the name of your fine-tuned model
|
25 |
finetuned_model = 'ruslanmv/Medical-Mixtral-7B-v2k'
|
26 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
27 |
# Load fine-tuned model
|
28 |
bnb_config = BitsAndBytesConfig(
|
29 |
+
load_in_4bit=True,
|
30 |
+
bnb_4bit_quant_type="nf4",
|
31 |
+
bnb_4bit_compute_dtype=torch.bfloat16,
|
32 |
+
bnb_4bit_use_double_quant=False,
|
33 |
)
|
34 |
model_pretrained = AutoModelForCausalLM.from_pretrained(
|
35 |
+
finetuned_model,
|
36 |
+
load_in_4bit=True,
|
37 |
+
quantization_config=bnb_config,
|
38 |
+
torch_dtype=torch.bfloat16,
|
39 |
+
device_map="auto",
|
40 |
+
trust_remote_code=True
|
|
|
41 |
)
|
42 |
+
|
43 |
# Load tokenizer
|
44 |
+
tokenizer = AutoTokenizer.from_pretrained(finetuned_model, trust_remote_code=True)
|
45 |
+
|
46 |
+
# Set pad_token_id to eos_token_id
|
47 |
+
model_pretrained.config.pad_token_id = tokenizer.eos_token_id
|
48 |
+
|
49 |
+
pipe = pipeline(task="text-generation", model=model_pretrained, tokenizer=tokenizer, max_length=500)
|
50 |
+
|
51 |
def build_prompt(question):
|
52 |
+
prompt = f"[INST]@Enlighten. [/INST] {question}"
|
53 |
+
return prompt
|
54 |
|
55 |
+
question = "Are my symptoms due to HIV infection? I had a high-risk exposure 15 months ago"
|
56 |
prompt = build_prompt(question)
|
57 |
+
|
58 |
+
# Generate text based on the prompt
|
59 |
+
result = pipe(prompt)[0]
|
60 |
+
generated_text = result['generated_text']
|
61 |
+
|
62 |
+
# Remove the prompt from the generated text
|
63 |
+
generated_text = generated_text.replace(prompt, "", 1).strip()
|
64 |
+
|
65 |
+
print(generated_text)
|
66 |
|
67 |
|
68 |
```
|