Update README.md
Browse files
README.md
CHANGED
@@ -14,4 +14,93 @@ widget:
|
|
14 |
library_name: peft
|
15 |
---
|
16 |
|
|
|
|
|
17 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
14 |
library_name: peft
|
15 |
---
|
16 |
|
17 |
+
```python
|
18 |
+
!huggingface-cli login
|
19 |
|
20 |
+
|
21 |
+
_| _| _| _| _|_|_| _|_|_| _|_|_| _| _| _|_|_| _|_|_|_| _|_| _|_|_| _|_|_|_|
|
22 |
+
_| _| _| _| _| _| _| _|_| _| _| _| _| _| _| _|
|
23 |
+
_|_|_|_| _| _| _| _|_| _| _|_| _| _| _| _| _| _|_| _|_|_| _|_|_|_| _| _|_|_|
|
24 |
+
_| _| _| _| _| _| _| _| _| _| _|_| _| _| _| _| _| _| _|
|
25 |
+
_| _| _|_| _|_|_| _|_|_| _|_|_| _| _| _|_|_| _| _| _| _|_|_| _|_|_|_|
|
26 |
+
|
27 |
+
To login, `huggingface_hub` requires a token generated from https://huggingface.co/settings/tokens .
|
28 |
+
Token: <your-hf-access-token>
|
29 |
+
```
|
30 |
+
|
31 |
+
|
32 |
+
```python
|
33 |
+
|
34 |
+
from peft import PeftModel, PeftConfig
|
35 |
+
from transformers import AutoModelForCausalLM
|
36 |
+
from transformers import AutoTokenizer
|
37 |
+
import torch
|
38 |
+
|
39 |
+
device = torch.device("cuda" if torch.cuda.is_available() else "cpu")
|
40 |
+
|
41 |
+
config = PeftConfig.from_pretrained("Ashishkr/llama2_medical_consultation")
|
42 |
+
model = AutoModelForCausalLM.from_pretrained("meta-llama/Llama-2-7b-hf")
|
43 |
+
model = PeftModel.from_pretrained(model, "Ashishkr/llama2_medical_consultation").to(device)
|
44 |
+
tokenizer = AutoTokenizer.from_pretrained("meta-llama/Llama-2-7b-hf")
|
45 |
+
|
46 |
+
```
|
47 |
+
|
48 |
+
|
49 |
+
|
50 |
+
```python
|
51 |
+
def llama_generate(
|
52 |
+
model: AutoModelForCausalLM,
|
53 |
+
tokenizer: AutoTokenizer,
|
54 |
+
prompt: str,
|
55 |
+
max_new_tokens: int = 128,
|
56 |
+
temperature: float = 0.92):
|
57 |
+
device = torch.device("cuda" if torch.cuda.is_available() else "cpu")
|
58 |
+
|
59 |
+
inputs = tokenizer(
|
60 |
+
[prompt],
|
61 |
+
return_tensors="pt",
|
62 |
+
return_token_type_ids=False,
|
63 |
+
).to(
|
64 |
+
device
|
65 |
+
)
|
66 |
+
|
67 |
+
with torch.autocast("cuda", dtype=torch.bfloat16):
|
68 |
+
response = model.generate(
|
69 |
+
**inputs,
|
70 |
+
max_new_tokens=max_new_tokens,
|
71 |
+
temperature=temperature,
|
72 |
+
return_dict_in_generate=True,
|
73 |
+
eos_token_id=tokenizer.eos_token_id,
|
74 |
+
pad_token_id=tokenizer.pad_token_id,
|
75 |
+
)
|
76 |
+
|
77 |
+
decoded_output = tokenizer.decode(
|
78 |
+
response["sequences"][0],
|
79 |
+
skip_special_tokens=True,
|
80 |
+
)
|
81 |
+
|
82 |
+
return decoded_output[len(prompt) :]
|
83 |
+
|
84 |
+
|
85 |
+
prompt = """
|
86 |
+
instruction: "If you are a doctor, please answer the medical questions based on the patient's description.",
|
87 |
+
|
88 |
+
input: "My baby has been pooing 5-6 times a day for a week.
|
89 |
+
In the last few days it has increased to 7 and they are very
|
90 |
+
watery with green stringy bits in them. He does not seem unwell
|
91 |
+
i.e no temperature and still eating. He now has a very bad nappy
|
92 |
+
rash from the pooing ...help!" .\n
|
93 |
+
response: """
|
94 |
+
|
95 |
+
response = llama_generate(
|
96 |
+
model,
|
97 |
+
tokenizer,
|
98 |
+
prompt,
|
99 |
+
max_new_tokens=100,
|
100 |
+
temperature=0.92,
|
101 |
+
)
|
102 |
+
|
103 |
+
print(response)
|
104 |
+
|
105 |
+
|
106 |
+
```
|