-
Notifications
You must be signed in to change notification settings - Fork 0
/
translate.js
109 lines (84 loc) · 3.03 KB
/
translate.js
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
const fs = require('fs')
const OpenAI = require("openai")
const ProgressBars = require('@zhangfuxing/multi-progress')
require('dotenv').config()
const openai = new OpenAI()
const workouts = require('./workouts.json')
const bars = new ProgressBars({
title: 'Traduções',
complete: '*',
incomplete: '.',
display: ' [:bar] :text :percent :time :completed/:total'
})
let total = 0
let toDoCound = 0
let completed = 0
const log = (message) => bars.console(`---> ${message}`)
const wait = ms => new Promise(r => setTimeout(r, ms));
const progressUpdate = () => {
bars.render([
{ completed: toDoCound, total, text: 'ToDo' },
{ completed, total, text: 'Completed' }
])
}
const createTranslationsFolder = () => {
const dir = `${__dirname}/translations`
if (!fs.existsSync(dir)) {
fs.mkdirSync(dir)
log(`Translatioons folder created: ${dir}.`)
}
return dir
}
const merge = ({ path, data }) => {
if (!fs.existsSync(path)) {
fs.writeFileSync(path, '[]')
}
const json = JSON.parse(data)
const writedData = require(path)
writedData.push(json)
const dataString = JSON.stringify(writedData)
fs.writeFileSync(path, dataString)
}
const translate = async ({ index, json, language }) => {
const dir = createTranslationsFolder()
const path = `${dir}/${language.acronym}.json`
const userContent = JSON.stringify(json)
toDoCound += 1
progressUpdate()
const completion = await openai.chat.completions.create({
messages: [
{
"role": "system",
"content": `The user will provide a JSON with an array of objects containing keys and values. It's necessary for the value of each key to be translated from English to ${language.name}. For example, the input: [{ "sex": "Male" }, {"job": "programmer"}] should yield an output of ${language.example}. For the key "instructions," a literal translation should be created for each item without line breaks. Keys of type array should remain as arrays, even if they contain only a single value. If any key contains an invalid value, it should become null. The structure of the JSON must always be valid.`
},
{
"role": "user",
"content": userContent
}
],
model: "gpt-3.5-turbo",
})
const data = completion.choices[0].message.content
if (data) {
try {
merge({ path, data })
completed += 1
progressUpdate()
} catch(err) {
log(`It was not possible merge index: ${index}. Error: ${err}`)
}
}
}
async function main() {
const language = { acronym: 'ptBR', name: 'português', example: '[{ "sex": "masculino" }, {"job": "programador"}]' }
const start = 0
const data = workouts
total = data.length
await wait(1000)
for (let index = start; index < data.length; index++) {
const json = data[index];
translate({ index, json, language })
await wait(600)
}
}
main()