-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathdata_generation.py
More file actions
152 lines (117 loc) · 3.85 KB
/
data_generation.py
File metadata and controls
152 lines (117 loc) · 3.85 KB
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
import requests
from tqdm import tqdm
import os
from dotenv import load_dotenv
from typing import List
import random
from threading import Thread
import time
import json
load_dotenv()
random.seed(7777)
API_ENDPOINT = os.getenv("API_ENDPOINT")
MODEL_NAME = os.getenv("MODEL_NAME")
def vocab_used(vocab: List, sentence: str) -> List:
sentence = sentence.lower()
res = []
for term in vocab:
word = term.get("word").lower()
if word in sentence:
res.append(word)
return res
def read_json(path: str) -> any:
try:
with open(path, 'r') as r:
data = json.load(r)
return data
except Exception as e:
print(e)
return []
def write_json(path: str, data: any) -> None:
try:
with open(path, 'w') as f:
json.dump(data, f)
except Exception as e:
print(e)
return []
def get_generate_prompt(vocabulary: List, topic: str = "anything", vocab_size: int = 1) -> str:
vocab = random.sample(vocabulary, random.randint(int(vocab_size/1.2), vocab_size))
vocab_string = ""
for term in vocab:
vocab_string += f"\t - {term.get('word')}: {term.get('definition')}\n"
return f"""
Provide one sentence about {topic}, then rewrite the sentence using only a few slang terms from {vocab_string}.
Do not provide definitions or explanations. Keep the slang version no longer than two sentences.
Example:
Original: I value my family deeply.
Translated: Ain't nothing like my fam, they got my back.
"""
def generate(prompt: str) -> str | None:
res = requests.post(API_ENDPOINT, json={
"model": MODEL_NAME,
"prompt": prompt,
"stream": False
})
return res.json().get("response")
def extract_from_generate(raw: str) -> List[str]:
if "Original" not in raw or "Translated" not in raw:
return []
else:
try:
res = raw.split("Translated: ")
res[0] = res[0].replace("Original: ", "")
return [res[0].strip(), res[1].strip()]
except Exception as e:
print(e)
return []
def generate_worker(max_vocab: int, vocabulary: List, turns: int, topics: List, return_list: List, pbar = None, en_to_slang = True) -> None:
cnt = 0
while(cnt < turns):
prompt = get_generate_prompt(vocabulary, random.choice(topics), max_vocab)
res = generate(prompt)
extracted = extract_from_generate(res)
if len(extracted) != 2:
continue
words_used = vocab_used(vocabulary, extracted[1])
if len(words_used) == 0:
continue
cnt += 1
if en_to_slang:
return_list.append({
"original": extracted[0],
"translated": extracted[1],
"terms": words_used
})
else:
return_list.append({
"original": extracted[1],
"translated": extracted[0],
"terms": words_used
})
write_json("./generated.json", generated_data)
if pbar:
pbar.update(1)
if __name__ == "__main__":
total_datasets = 4000
generated_data = []
thread_num = 10
threads = []
vocabulary = read_json("./vocabulary.json")
topics = read_json("./topics.json")
print(f"Generating a dataset of length {total_datasets}")
pbar = tqdm(total=total_datasets)
for i in range(thread_num):
t = Thread(target=generate_worker, args=[
len(vocabulary),
vocabulary,
int(total_datasets/thread_num),
topics,
generated_data,
pbar,
True
])
t.start()
threads.append(t)
for t in threads:
t.join()
print("Completed generation.")