sreeramajay commited on
Commit
ca99adc
·
1 Parent(s): 37cb0bd

added usage

Browse files
Files changed (1) hide show
  1. README.md +61 -0
README.md CHANGED
@@ -1,3 +1,64 @@
1
  ---
2
  license: apache-2.0
3
  ---
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
  ---
2
  license: apache-2.0
3
  ---
4
+ How to use:
5
+ ```
6
+ import torch
7
+ from transformers import AutoTokenizer, AutoModelForCausalLM, BitsAndBytesConfig
8
+
9
+ # Load Base Model
10
+ base_model_id = "mistralai/Mistral-7B-v0.1"
11
+ bnb_config = BitsAndBytesConfig(
12
+ load_in_4bit=True,
13
+ bnb_4bit_use_double_quant=True,
14
+ bnb_4bit_quant_type="nf4",
15
+ bnb_4bit_compute_dtype=torch.bfloat16
16
+ )
17
+
18
+ model = AutoModelForCausalLM.from_pretrained(base_model_id, quantization_config=bnb_config)
19
+
20
+ eval_tokenizer = AutoTokenizer.from_pretrained(
21
+ base_model_id,
22
+ add_bos_token=True,
23
+ trust_remote_code=True,
24
+ )
25
+ eval_tokenizer.pad_token = eval_tokenizer.eos_token
26
+
27
+ # Load Peft Weights
28
+ from peft import PeftModel
29
+
30
+ ft_model = PeftModel.from_pretrained(model, "mistral-samsum-finetune/checkpoint-100")
31
+
32
+ # Format the Sample Input
33
+ def formatting_func(example):
34
+ text = f"### Summarize this dialog:\n{example['dialogue']}\n### Summary:\n{example['summary']}"
35
+ return text
36
+
37
+ max_length = 256
38
+ eval_prompt = {'dialogue': "Amanda: I baked cookies. Do you want some? Jerry: Sure! Amanda: I'll bring you tomorrow :-)",
39
+ 'summary': ''}
40
+ eval_prompt = formatting_func(eval_prompt)
41
+
42
+ # Generate summary for sample Input
43
+ model_input = eval_tokenizer(
44
+ eval_prompt,
45
+ truncation=True,
46
+ max_length=max_length,
47
+ padding="max_length",
48
+ return_tensors="pt").to("cuda")
49
+
50
+ ft_model.eval()
51
+ with torch.no_grad():
52
+ print(eval_tokenizer.decode(ft_model.generate(**model_input,
53
+ max_new_tokens=256,
54
+ repetition_penalty=1.15)[0],
55
+ skip_special_tokens=True))
56
+
57
+ # here is the output:
58
+ """
59
+ ### Summarize this dialog:
60
+ Amanda: I baked cookies. Do you want some? Jerry: Sure! Amanda: I'll bring you tomorrow :-)
61
+ ### Summary:
62
+ Jerry will get some cookies from Amanda tomorrow.
63
+ """
64
+ ```