-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathcreate_es_wildchat_subset.py
180 lines (170 loc) · 8.16 KB
/
create_es_wildchat_subset.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
import os
import json
from datasets import load_dataset
from elasticsearch import Elasticsearch, helpers
# Load the dataset
dataset = load_dataset('allenai/WildChat-1M-Full')
# Initialize Elasticsearch client
#es = Elasticsearch('http://localhost:9200')
es = Elasticsearch('https://localhost:9200', basic_auth=('elastic', os.getenv('ES_PASSWD')), ssl_assert_fingerprint=os.getenv('ES_FINGERPRINT'))
#LANGUAGES = ['all', 'english', 'russian', 'chinese', 'spanish', 'german', 'french', 'portuguese', 'italian', 'japanese', 'korean']
def get_language_list():
languages = []
static_dir = 'static'
for item in os.listdir(static_dir):
folder_path = os.path.join(static_dir, item)
if os.path.isdir(folder_path):
if os.path.exists(os.path.join(folder_path, 'wildchat_embeddings.json')) and 'debug' not in item:
languages.append(item)
return languages
# Generate the LANGUAGES list
LANGUAGES = get_language_list()
#LANGUAGES = [ 'korean', 'portuguese', 'italian', 'french', 'turkish', 'german']
#LANGUAGES = [ 'english']
LANGUAGES = ['all', 'english', 'chinese', 'russian', 'spanish', 'french', 'portuguese', 'german', 'italian', 'turkish', 'arabic', 'japanese', 'korean', 'polish', 'vietnamese']
#LANGUAGES = ['english']
#LANGUAGES = ['chinese', 'russian', 'spanish']
#LANGUAGES = ['french', 'portuguese', 'german', 'italian', 'turkish']
#LANGUAGES = ['arabic', 'japanese', 'korean', 'polish', 'vietnamese']
print (LANGUAGES)
for language in LANGUAGES:
print (language)
index_name = f'wildchat_subset_{language}'
mappings = {
"properties": {
"conversation_hash": {"type": "keyword"},
"model": {"type": "keyword"},
"timestamp": {"type": "date"},
"conversation": {
"type": "nested",
"properties": {
"content": {"type": "text"},
"country": {"type": "keyword"},
"hashed_ip": {"type": "keyword"},
"header": {
"type": "object",
"properties": {
"accept-language": {"type": "keyword"},
"user-agent": {"type": "keyword"}
}
},
"language": {"type": "keyword"},
"redacted": {"type": "boolean"},
"role": {"type": "keyword"},
"state": {"type": "keyword"},
"timestamp": {"type": "date"},
"toxic": {"type": "boolean"},
"turn_identifier": {"type": "integer"}
}
},
"turn": {"type": "integer"},
"language": {"type": "keyword"},
"openai_moderation": {
"type": "nested",
"properties": {
"categories": {
"type": "object",
"properties": {
"harassment": {"type": "boolean"},
"harassment/threatening": {"type": "boolean"},
"harassment_threatening": {"type": "boolean"},
"hate": {"type": "boolean"},
"hate/threatening": {"type": "boolean"},
"hate_threatening": {"type": "boolean"},
"self-harm": {"type": "boolean"},
"self-harm/instructions": {"type": "boolean"},
"self-harm/intent": {"type": "boolean"},
"self_harm": {"type": "boolean"},
"self_harm_instructions": {"type": "boolean"},
"self_harm_intent": {"type": "boolean"},
"sexual": {"type": "boolean"},
"sexual/minors": {"type": "boolean"},
"sexual_minors": {"type": "boolean"},
"violence": {"type": "boolean"},
"violence/graphic": {"type": "boolean"},
"violence_graphic": {"type": "boolean"}
}
},
"category_scores": {
"type": "object",
"properties": {
"harassment": {"type": "float"},
"harassment/threatening": {"type": "float"},
"harassment_threatening": {"type": "float"},
"hate": {"type": "float"},
"hate/threatening": {"type": "float"},
"hate_threatening": {"type": "float"},
"self-harm": {"type": "float"},
"self-harm/instructions": {"type": "float"},
"self-harm/intent": {"type": "float"},
"self_harm": {"type": "float"},
"self_harm_instructions": {"type": "float"},
"self_harm_intent": {"type": "float"},
"sexual": {"type": "float"},
"sexual/minors": {"type": "float"},
"sexual_minors": {"type": "float"},
"violence": {"type": "float"},
"violence/graphic": {"type": "float"},
"violence_graphic": {"type": "float"}
}
},
"flagged": {"type": "boolean"}
}
},
"detoxify_moderation": {
"type": "nested",
"properties": {
"identity_attack": {"type": "float"},
"insult": {"type": "float"},
"obscene": {"type": "float"},
"severe_toxicity": {"type": "float"},
"sexual_explicit": {"type": "float"},
"threat": {"type": "float"},
"toxicity": {"type": "float"}
}
},
"toxic": {"type": "boolean"},
"redacted": {"type": "boolean"},
"state": {"type": "keyword"},
"country": {"type": "keyword"},
"hashed_ip": {"type": "keyword"},
"header": {
"type": "object",
"properties": {
"accept-language": {"type": "keyword"},
"user-agent": {"type": "keyword"}
}
}
}
}
# Create the index with the specified mappings
if es.indices.exists(index=index_name):
es.indices.delete(index=index_name)
assert not es.indices.exists(index=index_name)
if not es.indices.exists(index=index_name):
es.indices.create(index=index_name, mappings=mappings)
import tqdm
# Prepare data for bulk indexing
actions = []
import json
wildchat_embeddings = json.load(open(f'static/{language}/wildchat_embeddings.json'))
conversation_ids = set([item['i'] for item in wildchat_embeddings])
chunk_size = 10000 # Set your desired chunk size
for i, record in enumerate(tqdm.tqdm(dataset['train']), 1):
#if i > 10000:
# break
conversation_id = record['conversation'][0]['turn_identifier']
if conversation_id not in conversation_ids:
continue
action = {
"_index": index_name,
"_source": record
}
actions.append(action)
if i % chunk_size == 0:
helpers.bulk(es, actions)
actions = [] # Reset actions after each chunk is indexed
# Index any remaining actions
if actions:
helpers.bulk(es, actions)
print("Indexing completed.")