add example usage
Browse files
README.md
CHANGED
|
@@ -1,3 +1,62 @@
|
|
| 1 |
---
|
| 2 |
license: apache-2.0
|
| 3 |
---
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
---
|
| 2 |
license: apache-2.0
|
| 3 |
---
|
| 4 |
+
|
| 5 |
+
|
| 6 |
+
|
| 7 |
+
## usage :
|
| 8 |
+
```python
|
| 9 |
+
import os
|
| 10 |
+
import torch
|
| 11 |
+
|
| 12 |
+
from transformers import AutoTokenizer, AutoModelForCausalLM
|
| 13 |
+
|
| 14 |
+
# set HF_TOKEN in terminal as export HF_TOKEN=hf_***
|
| 15 |
+
auth_token = os.environ.get("HF_TOKEN", True)
|
| 16 |
+
|
| 17 |
+
model_name = "Writer/camel-5b"
|
| 18 |
+
|
| 19 |
+
tokenizer = AutoTokenizer.from_pretrained(
|
| 20 |
+
model_name, use_auth_token=auth_token
|
| 21 |
+
)
|
| 22 |
+
model = AutoModelForCausalLM.from_pretrained(
|
| 23 |
+
model_name,
|
| 24 |
+
device_map="auto",
|
| 25 |
+
torch_dtype=torch.float16,
|
| 26 |
+
use_auth_token=auth_token,
|
| 27 |
+
)
|
| 28 |
+
|
| 29 |
+
|
| 30 |
+
instruction = "Describe a futuristic device that revolutionizes space travel."
|
| 31 |
+
|
| 32 |
+
|
| 33 |
+
PROMPT_DICT = {
|
| 34 |
+
"prompt_input": (
|
| 35 |
+
"Below is an instruction that describes a task, paired with an input that provides further context. "
|
| 36 |
+
"Write a response that appropriately completes the request\n\n"
|
| 37 |
+
"### Instruction:\n{instruction}\n\n### Input:\n{input}\n\n### Response:"
|
| 38 |
+
),
|
| 39 |
+
"prompt_no_input": (
|
| 40 |
+
"Below is an instruction that describes a task. "
|
| 41 |
+
"Write a response that appropriately completes the request.\n\n"
|
| 42 |
+
"### Instruction:\n{instruction}\n\n### Response:"
|
| 43 |
+
),
|
| 44 |
+
}
|
| 45 |
+
|
| 46 |
+
text = (
|
| 47 |
+
PROMPT_DICT["prompt_no_input"].format(instruction=instruction)
|
| 48 |
+
if not input
|
| 49 |
+
else PROMPT_DICT["prompt_input"].format(instruction=instruction, input=input)
|
| 50 |
+
)
|
| 51 |
+
|
| 52 |
+
model_inputs = tokenizer(text, return_tensors="pt").to("cuda")
|
| 53 |
+
output_ids = model.generate(
|
| 54 |
+
**model_inputs,
|
| 55 |
+
max_length=100,
|
| 56 |
+
)
|
| 57 |
+
output_text = tokenizer.batch_decode(output_ids, skip_special_tokens=True)
|
| 58 |
+
clean_output = output_text.split("### Response:")[1].strip()
|
| 59 |
+
|
| 60 |
+
print(clean_output)
|
| 61 |
+
|
| 62 |
+
```
|