danlou commited on
Commit
03d5009
1 Parent(s): 13f9fc8

include example code

Browse files
Files changed (1) hide show
  1. README.md +93 -0
README.md CHANGED
@@ -1,3 +1,96 @@
1
  ---
2
  license: llama2
3
  ---
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
  ---
2
  license: llama2
3
  ---
4
+
5
+ The code below shows how this Buyer Persona generator can be used.
6
+
7
+ More documentation coming soon...
8
+
9
+ ```python
10
+ import torch
11
+ from transformers import AutoTokenizer, AutoModelForCausalLM
12
+ from tqdm import tqdm
13
+
14
+ device = "cuda" if torch.cuda.is_available() else "cpu"
15
+
16
+ model_id = "danlou/persona-generator-llama-2-7b-qlora-merged"
17
+ tokenizer = AutoTokenizer.from_pretrained(model_id)
18
+ model = AutoModelForCausalLM.from_pretrained(model_id, device_map="auto", torch_dtype=torch.float16)
19
+
20
+
21
+ def chunks(lst, n):
22
+ """Yield successive n-sized chunks from lst."""
23
+ for i in range(0, len(lst), n):
24
+ yield lst[i:i + n]
25
+
26
+
27
+ def parse_outputs(output_text):
28
+
29
+ try:
30
+ output_lns = output_text.split('\n')
31
+ assert len(output_lns) == 2
32
+ assert len(output_lns[0].split(',')) == 2
33
+ assert len(output_lns[1]) > 16
34
+
35
+ name, age = [s.strip() for s in output_lns[0].split(',')]
36
+ desc = output_lns[1].strip()
37
+
38
+ except AssertionError:
39
+ raise Exception('Malformed output.')
40
+
41
+ try:
42
+ age = int(age)
43
+ except ValueError:
44
+ raise Exception('Malformed output (age).')
45
+
46
+ return {'name': name, 'age': age, 'description': desc}
47
+
48
+
49
+
50
+ def generate_personas(product, n=1, batch_size=32, parse=True):
51
+
52
+ prompt = f"### Instruction:\nDescribe the ideal persona for this product:\n{product}\n\n### Response:\n"
53
+ input_ids = tokenizer(prompt, return_tensors="pt").input_ids.to(device)
54
+
55
+ personas = []
56
+ with tqdm(total=n) as pbar:
57
+ for batch in chunks(range(n), batch_size):
58
+ outputs = model.generate(input_ids,
59
+ do_sample=True,
60
+ num_beams=1,
61
+ num_return_sequences=len(batch),
62
+ max_length=512,
63
+ min_length=32,
64
+ temperature=0.9)
65
+
66
+ for output_ids in outputs:
67
+ output_decoded = tokenizer.decode(output_ids, skip_special_tokens=True)
68
+ output_decoded = output_decoded[len(prompt):].strip()
69
+
70
+ try:
71
+ if parse:
72
+ personas.append(parse_outputs(output_decoded))
73
+ else:
74
+ personas.append(output_decoded)
75
+ except Exception as e:
76
+ print(e)
77
+ continue
78
+
79
+ pbar.update(len(batch))
80
+
81
+ return personas
82
+
83
+
84
+ product = "Koonie 10000mAh Rechargeable Desk Fan, 8-Inch Battery Operated Clip on Fan, USB Fan, 4 Speeds, Strong Airflow, Sturdy Clamp for Golf Cart Office Desk Outdoor Travel Camping Tent Gym Treadmill, Black (USB Gadgets > USB Fans)"
85
+ personas = generate_personas(product, n=3)
86
+
87
+ for e in personas:
88
+ print(e)
89
+
90
+ # Persona 1 - The yoga instructor
91
+ # {'name': 'Sarah', 'age': 28, 'description': 'Yoga instructor who is passionate about health and fitness. She works from a home studio where she also practices yoga and meditation. Sarah values products that are eco-friendly and sustainable. She loves products that are versatile and can be used for different purposes. Sarah is looking for a product that is durable and can withstand frequent use. She values products that are stylish and aesthetically pleasing.'}
92
+ # Persona 2 - The golf enthusiast
93
+ #{'name': 'Sophia', 'age': 60, 'description': "Golf enthusiast. Sophia spends most of her weekends on the golf course, and she needs a fan that she can carry around in her golf cart. She needs a fan that's lightweight, easy to clip on, and has a long battery life. She also wants a fan that's affordable, especially since she plays at different courses."}
94
+ # Persona 3 - The truck driver
95
+ # {'name': 'Mike', 'age': 32, 'description': "Truck driver who spends most of his day on the road. The cab of his truck can get hot and stuffy, and Mike needs a fan that can keep him comfortable and alert while he's driving. He needs a fan that's easy to install and adjust, so he can keep it on his dashboard and direct the airflow where he needs it most."}
96
+ ```