feat: gpt integration
This commit is contained in:
parent
cb9b0266e5
commit
ed5a39df92
3 changed files with 68 additions and 3 deletions
3
.env.example
Normal file
3
.env.example
Normal file
|
|
@ -0,0 +1,3 @@
|
|||
key="<OPEN_AI_KEY>"
|
||||
openai.organization='<ORG_KEY>'
|
||||
|
||||
2
.gitignore
vendored
2
.gitignore
vendored
|
|
@ -1 +1 @@
|
|||
token.json
|
||||
.env
|
||||
66
main.py
66
main.py
|
|
@ -1,4 +1,66 @@
|
|||
import os
|
||||
from googletrans import Translator
|
||||
from pathlib import Path
|
||||
import json
|
||||
import time
|
||||
from dotenv import load_dotenv
|
||||
import openai
|
||||
openai.organization = 'org-2z5LhALP271kXfae71dt6NLg'
|
||||
os.getenv
|
||||
|
||||
load_dotenv()
|
||||
openai.organization = os.getenv('org')
|
||||
openai.api_key = os.getenv('key')
|
||||
|
||||
# Load JSON file into a Python dictionary
|
||||
with open('Map013.json', 'r', encoding='utf-8') as f:
|
||||
data = json.load(f)
|
||||
|
||||
# Extract 401 events text
|
||||
events = data['events']
|
||||
text = []
|
||||
for event in events:
|
||||
if event is not None:
|
||||
for page in event['pages']:
|
||||
for command in page['list']:
|
||||
if command['code'] == 401:
|
||||
text.append(command['parameters'][0])
|
||||
|
||||
# Split into batches
|
||||
pipe = '/p'
|
||||
script = pipe.join(text)
|
||||
|
||||
# Translate text using GPT
|
||||
def translate(t):
|
||||
system = "You are a professional Japanese visual novel translator. \
|
||||
You always manages to translate all of the little nuances of the original \
|
||||
Japanese text to your output, while still making it a prose masterpiece, \
|
||||
and localizing it in a way that an average American would understand. \
|
||||
You always include the '/p' from the original text in your translation." \
|
||||
|
||||
response = openai.ChatCompletion.create(
|
||||
temperature=0.2,
|
||||
max_tokens=500,
|
||||
model="gpt-3.5-turbo",
|
||||
messages=[
|
||||
{"role": "system", "content": system},
|
||||
{"role": "user", "content": t}
|
||||
]
|
||||
)
|
||||
return response.choices[0].message.content
|
||||
|
||||
# Make translated list the same size
|
||||
translatedText += [""] * (len(text) - len(translatedText))
|
||||
for i in range(len(translatedText)):
|
||||
translatedText[i] = translatedText[i].strip()
|
||||
|
||||
# Place translated text back in its original location in the JSON file
|
||||
i = 0
|
||||
for event in events:
|
||||
if event is not None:
|
||||
for page in event['pages']:
|
||||
for command in page['list']:
|
||||
if command['code'] == 401:
|
||||
command['parameters'][0] = translatedText[i]
|
||||
i += 1
|
||||
|
||||
with open('file.json', 'w') as f:
|
||||
json.dump(data, f)
|
||||
|
|
|
|||
Loading…
Reference in a new issue