-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathgenerate_codemix_data.py
More file actions
129 lines (105 loc) · 7.13 KB
/
Copy pathgenerate_codemix_data.py
File metadata and controls
129 lines (105 loc) · 7.13 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
import argparse
import copy
import json
import os
from datasets import load_dataset
from openai import OpenAI
from pydantic import BaseModel
from tqdm import tqdm
if __name__ == "__main__":
parser = argparse.ArgumentParser(description="Parser")
parser.add_argument("--openai_key", type=str, default=None, help="OpenAI key")
args = parser.parse_args()
openai_api_key = args.openai_key
client = OpenAI(timeout=1500, api_key=openai_api_key)
ds = load_dataset("google/simpleqa-verified")
problems = []
for i in range(len(ds["eval"])):
problem = ds["eval"][i]["problem"]
original_index = ds["eval"][i]["original_index"]
id = original_index
problems.append({"id":id, "problem":problem})
class AnswerFormat(BaseModel):
explanation: str
answer: str
random_percentages_list = ["50"]
selective_percentages_list = ["50"]
languages_list = ["Klingon","Dothraki","Afrikaans","Albanian","Amharic","Arabic","Armenian","Azerbaijani","Burmese","Danish","Dutch","Finnish","French","Georgian","German","Greek","Hebrew","Hungarian","Icelandic","Javanese","Kannada","Khmer","Korean","Latvian","Malay","Malayalam","Mongolian","Norwegian","Persian","Polish","Portuguese","Romanian","Russian","Slovenian","Sundanese","Swahili","Traditional Chinese","Swedish","Tagalog","Tamil","Telugu","Thai","Turkish","Vietnamese","Welsh","Japanese","Indonesian","Italian","Simplified Chinese","Hindi","Marathi","Spanish"]
NUM_SPLIT = 20
SAMPLES_PER_SPLIT = len(problems) // NUM_SPLIT
for split in range(10, NUM_SPLIT):
min_id = split * SAMPLES_PER_SPLIT
max_id = (split+1) * SAMPLES_PER_SPLIT
if split == NUM_SPLIT-1:
max_id = len(problems)
for language_list in languages_list:
if type(language_list) is list:
languages = "_".join(lang for lang in language_list)
else:
languages = language_list
language_list = [language_list]
os.system(f"mkdir -p \"output/{languages}/\"")
print(languages, "from", languages_list, "split", split, "min_id", min_id, "max_id", max_id)
outputs = {}
outputs[languages] = {}
for i in tqdm(range(min_id, max_id)):
all_responses = {"original": problems[i]["problem"]}
text = problems[i]["problem"]
for percentage in random_percentages_list:
all_responses[percentage + "_random"] = []
for j in range(1):
completion = client.chat.completions.parse(
model="gpt-5.2",
reasoning_effort="low",
messages=[
{"role": "developer", "content": "You are a multilingual speaker."},
{"role": "user", "content": f"""Given an English text, produce a code-switched version with roughly about {percentage}% of the words or phrases into {languages}, while preserving the original meaning.\nApply code-switching by random. Don't write new punctuation if not needed. The answer must not have any preamble.\nEnglish text: {text}"""}
],
service_tier="flex",
timeout=1500,
response_format=AnswerFormat
)
output = completion.choices[0].message.content
all_responses[percentage + "_random"].append(output)
for percentage in selective_percentages_list:
all_responses[percentage + "_selective"] = []
for lang in language_list:
all_responses["translate_" + lang] = []
all_responses[percentage + "_grammarforce_" + lang] = []
all_responses[percentage + "_backtranslate_grammarforce_" + lang] = []
all_responses[percentage + "_grammarforce_English"] = []
all_responses[percentage + "_backtranslate_grammarforce_English"] = []
all_responses[percentage + "_backtranslate"] = []
for j in range(1):
completion = client.chat.completions.parse(
model="gpt-5.2",
reasoning_effort="low",
messages=[
{"role": "developer", "content": "You are a multilingual speaker."},
{"role": "user", "content": f"""Given an English text, produce a code-switched version with roughly about {percentage}% of the words or phrases into {languages}, while preserving the original meaning.\nApply code-switching selectively, BUT always try to code-switch if possible, so that the final output naturally mixes English with the target language(s). Don't write new punctuation if not needed. The answer must not have any preamble.\nEnglish text: {text}"""}
],
service_tier="flex",
timeout=1500,
response_format=AnswerFormat
)
output = completion.choices[0].message.content
all_responses[percentage + "_selective"].append(output)
for lang_id in range(len(language_list)):
lang = language_list[lang_id]
for gf_lang in language_list + ["English"]:
completion = client.chat.completions.parse(
model="gpt-5.2",
reasoning_effort="low",
messages=[
{"role": "developer", "content": "You are a multilingual speaker."},
{"role": "user", "content": f"""Given an English text, produce a code-switched version with roughly about {percentage}% of the words or phrases into {lang}, while preserving the original meaning.\nApply code-switching selectively, BUT always try to code-switch if possible, so that the final output naturally mixes English with the target language(s) and random the code-switched text to follow {gf_lang} grammar. Don't write new punctuation if not needed. The answer must not have any preamble.\nEnglish text: {text}"""}
],
service_tier="flex",
timeout=1500,
response_format=AnswerFormat
)
output = completion.choices[0].message.content
all_responses[percentage + "_grammarforce_" + gf_lang].append(output)
outputs[languages][i] = all_responses
with open(f'output/{languages}/{languages}_split_{split}.json', 'w', encoding="utf-8") as f:
json.dump(outputs, f, indent=4, ensure_ascii=False)