Nelsonlin0321
commited on
Commit
·
5724f4c
1
Parent(s):
ec07c1f
Create README.md
Browse files
README.md
ADDED
@@ -0,0 +1,65 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
# Further instruct Tuning stanford alpaca based on <a href="https://huggingface.co/tloen/alpaca-lora-7" target="_blank">tloen/alpaca-lora-7b</a> on <a href="https://www.consumptionvoucher.gov.hk/public/pdf/2023cvs/FAQ-2023_en.pdf" target="_blank">Hong Kong 2023 Consumption Voucher Scheme Frequently Asked Questions</a>
|
2 |
+
|
3 |
+
## How to use it
|
4 |
+
|
5 |
+
```python
|
6 |
+
|
7 |
+
from transformers import LlamaForCausalLM, LlamaTokenizer,GenerationConfig
|
8 |
+
from peft import PeftModel
|
9 |
+
|
10 |
+
|
11 |
+
device_map = "auto"
|
12 |
+
|
13 |
+
tokenizer = LlamaTokenizer.from_pretrained("decapoda-research/llama-7b-hf")
|
14 |
+
model = LlamaForCausalLM.from_pretrained(
|
15 |
+
"decapoda-research/llama-7b-hf",
|
16 |
+
load_in_8bit=True,
|
17 |
+
device_map="auto",
|
18 |
+
)
|
19 |
+
|
20 |
+
### load model after fine tuned on alpaca datasets
|
21 |
+
model = PeftModel.from_pretrained(model, "Nelsonlin0321/alpaca-lora-7b-tuned-on-hk-csv-fqa_causal_lm")
|
22 |
+
|
23 |
+
tokenizer = LlamaTokenizer.from_pretrained("decapoda-research/llama-7b-hf")
|
24 |
+
tokenizer.pad_token_id = 0
|
25 |
+
|
26 |
+
|
27 |
+
def generate_prompt_eval(instruction):
|
28 |
+
template = f"""Below is an instruction that describes a task. Write a response that appropriately completes the request.
|
29 |
+
### Instruction:
|
30 |
+
{instruction}
|
31 |
+
### Response:"""
|
32 |
+
return template
|
33 |
+
|
34 |
+
eval_generation_config = GenerationConfig(
|
35 |
+
temperature=0.1,
|
36 |
+
top_p=0.75,
|
37 |
+
num_beams=4,
|
38 |
+
)
|
39 |
+
|
40 |
+
|
41 |
+
def generate_answer(instruction):
|
42 |
+
prompt = generate_prompt_eval(instruction)
|
43 |
+
inputs = tokenizer(prompt, return_tensors="pt")
|
44 |
+
input_ids = inputs["input_ids"].cuda()
|
45 |
+
generation_output = model.generate(
|
46 |
+
input_ids=input_ids,
|
47 |
+
generation_config=eval_generation_config,
|
48 |
+
return_dict_in_generate=True,
|
49 |
+
output_scores=True,
|
50 |
+
max_new_tokens=256
|
51 |
+
)
|
52 |
+
for s in generation_output.sequences:
|
53 |
+
output = tokenizer.decode(s)
|
54 |
+
# print(output)
|
55 |
+
print("Response:", output.split("### Response:")[1].strip())
|
56 |
+
|
57 |
+
|
58 |
+
question = "Who are eligible to be disbursed with the first-instalment voucher of $1,500 on 16 April?"
|
59 |
+
|
60 |
+
generate_answer(question)
|
61 |
+
>> Response: All eligible people who have successfully registered under 2022 CVS and met the relevant eligibility criteria will be disbursed with the first-instalment voucher of $1,500 on 16 April.
|
62 |
+
|
63 |
+
|
64 |
+
|
65 |
+
```
|