forked from AIObjectives/talk-to-the-city-reports
-
Notifications
You must be signed in to change notification settings - Fork 0
/
translate_v0.ts
225 lines (201 loc) · 6.13 KB
/
translate_v0.ts
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
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
import gpt from '$lib/gpt';
import _ from 'lodash';
import jsonpath from 'jsonpath';
import nodes from '$lib/node_register';
import { readFileFromGCS, uploadJSONToGCS, quickChecksum } from '$lib/utils';
import type { DGNodeInterface, GCSBaseData } from '$lib/node_data_types';
import categories from '$lib/node_categories';
function defaultdict(factory) {
const dict = new Proxy(
{},
{
get: function (target, key) {
let value = target[key];
if (typeof value === 'undefined') {
value = factory();
target[key] = value;
}
return value;
}
}
);
return dict;
}
function sizeofDefaultDict(defaultdict) {
return Object.keys(defaultdict).length;
}
export default class TranslateNode {
id: string;
data: TranslateData;
position: { x: number; y: number };
type: string;
constructor(node_data: TranslateNodeInterface) {
const { id, data, position, type } = node_data;
this.id = id;
this.data = data;
this.position = position;
this.type = type;
}
async translate(target_languages, data, keys, open_ai_key, info, error, success) {
const translations = {};
translations[this.data.input_language] = _.cloneDeep(data);
const todo = defaultdict(() => []);
for (const language of target_languages) {
translations[language] = _.cloneDeep(data);
for (const key of keys) {
const valuesByJsonPath = jsonpath.query(translations[language], key);
for (const [index, valueToTranslate] of valuesByJsonPath.entries()) {
if (valueToTranslate !== null) {
todo[JSON.stringify([language, valueToTranslate])].push({
key,
index,
translation: ''
});
}
}
}
}
const num = sizeofDefaultDict(todo);
const numTodo = new Set(_.range(0, num));
async function delay(milliseconds) {
return new Promise((resolve) => setTimeout(resolve, milliseconds));
}
const output = [];
let i = 0;
for (const t in todo) {
i += 1;
const [language, valueToTranslate] = JSON.parse(t);
const promise = (async () => {
await delay(100 * i);
const res = await gpt(
open_ai_key,
{
text: valueToTranslate,
language: language
},
this.data.prompt,
this.data.system_prompt,
info,
error,
success,
i,
num,
numTodo,
null
);
const keys = _.uniq(todo[t].map((x) => x.key));
for (const key of keys)
jsonpath.apply(translations[language], key, (value, path) => {
if (_.isEqual(value, valueToTranslate)) {
return res;
}
return value;
});
})();
output.push(promise);
}
await Promise.all(output);
return translations;
}
async compute(
inputData: Record<string, any>,
context: string,
info: (arg: string) => void,
error: (arg: string) => void,
success: (arg: string) => void,
slug: string,
Cookies: any
): Promise<Record<string, any>> {
const open_ai_key =
inputData.open_ai_key || inputData[this.data.input_ids.open_ai_key as string];
const data = inputData.data || inputData[this.data.input_ids.data as string];
const target_languages = this.data.target_languages;
const keys = this.data.keys;
if (_.isEmpty(data) || _.isEmpty(target_languages) || _.isEmpty(keys)) {
return;
}
const length = quickChecksum(data);
const length_changed = length !== this.data.length;
const languageSelector = this.data.language_selector;
if (!this.data.dirty && this.data.gcs_path && !length_changed) {
let storedData: any = await readFileFromGCS(this);
if (typeof storedData === 'string') storedData = JSON.parse(storedData);
return {
translations: storedData,
translation: storedData[languageSelector] || storedData[this.data.input_language]
};
}
if (context == 'run') {
const translations = await this.translate(
target_languages,
data,
keys,
open_ai_key,
info,
error,
success
);
await uploadJSONToGCS(this, translations, slug);
this.data.length = length;
return {
translations,
translation: translations[languageSelector] || translations[this.data.input_language]
};
}
}
}
interface TranslateData extends GCSBaseData {
target_languages: string[];
gcs_path: string;
keys: string[];
language_selector: string;
input_language: string;
cache: Record<string, any>;
system_prompt: string;
prompt: string;
length: number;
locale_is_selector: boolean;
}
type TranslateNodeInterface = DGNodeInterface<GCSBaseData> & {
data: TranslateData;
};
export const translate_node_data: TranslateNodeInterface = {
id: 'translate',
data: {
label: 'translate',
target_languages: ['zh-TW'],
keys: [
'$.topics[*].topicName',
'$.topics[*].topicShortDescription',
'$.topics[*].subtopics[*].subtopicName',
'$.topics[*].subtopics[*].subtopicShortDescription',
'$.topics[*].subtopics[*].claims[*].claim',
'$.topics[*].subtopics[*].claims[*].quote',
'$.topics[*].subtopics[*].claims[*].topicName',
'$.topics[*].subtopics[*].claims[*].subtopicName'
],
dirty: false,
gcs_path: '',
compute_type: 'translate_v0',
input_ids: { open_ai_key: '', data: '' },
output_ids: { translation: '', translations: [] },
category: categories.ml.id,
icon: 'translate_v0',
message: '',
show_in_ui: false,
filename: '',
size_kb: 0,
language_selector: 'en-US',
input_language: 'en-US',
cache: {},
system_prompt:
'You are a professional translator. You respond with the correct translation and nothing else.',
prompt: 'Translate the following text to {language}.\n\n{text}',
length: 0,
locale_is_selector: true
},
position: { x: 0, y: 0 },
type: 'translate_v0'
};
export const translate_node = new TranslateNode(translate_node_data);
nodes.register(TranslateNode, translate_node_data);