Skip to content

Instantly share code, notes, and snippets.

@hmmhmmhm
Created December 5, 2023 06:23
Show Gist options
  • Save hmmhmmhm/31528e6955b909fcc2fb994d3f0cccff to your computer and use it in GitHub Desktop.
Save hmmhmmhm/31528e6955b909fcc2fb994d3f0cccff to your computer and use it in GitHub Desktop.
Massive JSON Convert GPT
import fs from "fs";
import OpenAI from "openai";
const openai = new OpenAI({
apiKey: "여기에 코드를 넣으시오",
});
const prompt = `아래 주어지는 영어 JSON을 일본어로 번역해줘`
export const chunk = (arr, size) => {
const result = [];
for (let i = 0; i < arr.length; i += size) {
result.push(arr.slice(i, i + size));
}
return result;
};
export const delay = (ms) => new Promise((resolve) => setTimeout(resolve, ms));
export async function translate(json) {
console.log({
request: `${prompt}\n${JSON.stringify(
json
)}`,
});
const chatCompletion = await openai.chat.completions.create({
messages: [
{
role: "system",
content: `${prompt}\n${JSON.stringify(
json
)}`,
},
],
model: "gpt-3.5-turbo",
});
const translated = chatCompletion.choices[0].message.content;
return translated;
}
void (async () => {
const originalJSON = JSON.parse(
fs.readFileSync("./translation.json", "utf8")
);
const translatedJSON = {};
const chunked = chunk(
Object.entries(originalJSON).map(([key, value]) => ({ key, value })),
20
);
for (const [index, chunk] of chunked.entries()) {
const trying = async () => {
const translatedText = await translate(chunk.map(({ value }) => value));
// 순회하며 실제 키 값을 넣어서 만들기
console.log({ translatedText });
const translated = JSON.parse(translatedText);
for (const [index, { key }] of chunk.entries()) {
translatedJSON[key] = translated[index];
}
};
while (true) {
try {
const remainTime = Math.round(
(chunked.length - index) * 0.5 + (chunked.length - index) * 0.5 * 0.5
);
console.log(
`\nChunk ${index + 1}/${
chunked.length
}, Remaining time: ${remainTime} seconds`
);
await trying();
break;
} catch (e) {
console.log("Error occured, retrying...");
await delay(1000);
}
}
}
fs.writeFileSync(
"./translated.json",
JSON.stringify(translatedJSON, null, 4)
);
})();
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment