-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathrun_llm.py
191 lines (144 loc) · 4.49 KB
/
run_llm.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
# -*- coding: utf-8 -*-
import transformers
import torch
from transformers.pipelines.pt_utils import KeyDataset
import datasets
from tqdm import tqdm
import pickle
from icecream import ic
dataset = datasets.load_dataset("badrabdullah/emoji-dataset")
EMOJIES = dataset['train']['char']
ic(EMOJIES[0])
def prepare_emoji(dataset_item):
char = dataset_item['char']
desc = ' '.join(dataset_item['desc'][1:-1].split('_')).upper()
return (
f"\n"
f"Emoji: {char}\n"
f"Name: {desc}"
)
ic(prepare_emoji(dataset['train'][0]))
ic(dataset['train'].num_rows)
model_id = "NousResearch/Hermes-2-Pro-Llama-3-8B"
pipeline = transformers.pipeline(
"text-generation",
model=model_id,
model_kwargs={"torch_dtype": torch.bfloat16},
device_map="auto",
)
system_prompt = f"""
You are a helpful, positive, and polite assistant who enjoys describing and categorizing emojis.
Your task is to provide a description of a given emoji. The generated output will be used for indexing and discovering emojies using semantic search.
You have to generate a one-sentence summary in simple language, as well as a list of 5-7 semantic tags.
The output should be in a structured YAML format.
Follow the given examples below.
EXAMPLE 1:
INPUT:
Emoji: 🌈
Name: RAINBOW
OUTPUT:
´´´
Emoji: "🌈"
Description: "This emoji depicts a rainbow, symbolizing happiness, diversity, and hope, often used to express joy or to represent the LGBTQ+ community."
Semantic_Tags:
- rainbow
- joy
- pride
- peace
- diversity
- LGBTQ+
- community
´´´
EXAMPLE 2:
INPUT:
Emoji: 🐘
Name: ELEPHANT
OUTPUT:
´´´
Emoji: "🐘"
Description: "This emoji shows an elephant, a large and powerful animal known for its intelligence, long memory, and significant role in ecosystems and wildlife conservation."
Semantic_Tags:
- elephant
- animal
- memory
- strength
- mammal
- nature
- large
´´´
EXAMPLE 3:
INPUT:
Emoji: 🇾🇪
Name: YEMEN
OUTPUT:
´´´
Emoji: "🇾🇪"
Description: "This emoji represents Yemen, an Arab country known for its rich history, including ancient civilizations like Saba, and diverse landscapes ranging from desert plains to fertile mountains."
Semantic_Tags:
- Yemen
- flag
- culture
- Arab
- history
´´´
EXAMPLE 4:
INPUT:
Emoji: 🍏
Name: GREEN APPLE
OUTPUT:
´´´
Emoji: "🍏"
Description: "This emoji depicts a green apple, commonly associated with health, nutrition, and the symbol of teachers and education in many cultures."
Semantic_Tags:
- green apple
- health
- nutrition
- education
- fruit
´´´
The output must be in a YAML format. You must follow these instructions to win.
"""
user_message = "Describe this emoji according to your instructions."
def add_prompt_column(example):
user_input = f"{user_message}: {prepare_emoji(example)}"
messages = [
{"role": "system", "content": system_prompt},
{"role": "user", "content": user_input},
]
prompt = pipeline.tokenizer.apply_chat_template(
messages,
tokenize=False,
add_generation_prompt=True
)
return {
"prompt": prompt
}
dataset = dataset.map(add_prompt_column)
ic(dataset['train'][0]['prompt'])
mark_str = '<|im_end|>\n<|im_start|>assistant\n'
mark_str_len = len(mark_str)
terminators = [
pipeline.tokenizer.eos_top,
pipeline.tokenizer.convert_tokens_to_ids("<|eot_id|>")
]
i = 0
emoji2LLMstr = {}
outputs = pipeline(KeyDataset(dataset['train'], "prompt"),
max_new_tokens=512,
eos_token_id=terminators,
do_sample=False
)
for i, out in enumerate(outputs):
print('---')
if i == 128:
break
out_str = out[0]["generated_text"][out[0]["generated_text"].find(mark_str) + mark_str_len:]
emoji_char = dataset['train'][i]['char']
desc = dataset['train'][i]['desc']
emoji2LLMstr[emoji_char] = out_str
print(i, emoji_char, desc)
print(out_str)
i += 1
# Save the dict emoji2LLMstr as a pickled object
with open('emoji2LLMstr.pkl', 'wb') as f:
pickle.dump(emoji2LLMstr, f)