MT Kit
Custom MT Code
{
"configVersion": 3,
"name": "mtkit",
"title": "mtkit",
"description": "",
"schema": [],
"lang": {
"source": [
{ "value": "Arabic", "name": "Arabic" },
{ "value": "Bulgarian", "name": "Bulgarian" },
{ "value": "Chinese", "name": "Chinese"},
{ "value": "Czech", "name": "Czech" },
{ "value": "Danish", "name": "Danish" },
{ "value": "German", "name": "German" },
{ "value": "Greek", "name": "Greek" },
{ "value": "English", "name": "English" },
{ "value": "Spanish", "name": "Spanish" },
{ "value": "Estonian", "name": "Estonian" },
{ "value": "Finnish", "name": "Finnish" },
{ "value": "French", "name": "French" },
{ "value": "Hungarian", "name": "Hungarian" },
{ "value": "Indonesian", "name": "Indonesian" },
{ "value": "Italian", "name": "Italian" },
{ "value": "Japanese", "name": "Japanese" },
{ "value": "Korean", "name": "Korean" },
{ "value": "Lithuanian", "name": "Lithuanian" },
{ "value": "Latvian", "name": "Latvian" },
{ "value": "Norwegian", "name": "Norwegian" },
{ "value": "Dutch", "name": "Dutch" },
{ "value": "Polish", "name": "Polish" },
{ "value": "Portuguese", "name": "Portuguese" },
{ "value": "Romanian", "name": "Romanian" },
{ "value": "Russian", "name": "Russian" },
{ "value": "Slovak", "name": "Slovak" },
{ "value": "Slovenian", "name": "Slovenian" },
{ "value": "Swedish", "name": "Swedish" },
{ "value": "Thai", "name": "Thai" },
{ "value": "Turkish", "name": "Turkish" },
{ "value": "Ukrainian", "name": "Ukrainian" }
],
"target": [
{ "value": "Arabic", "name": "Arabic" },
{ "value": "Bulgarian", "name": "Bulgarian" },
{ "value": "Chinese", "name": "Chinese"},
{ "value": "Czech", "name": "Czech" },
{ "value": "Danish", "name": "Danish" },
{ "value": "German", "name": "German" },
{ "value": "Greek", "name": "Greek" },
{ "value": "English", "name": "English" },
{ "value": "Spanish", "name": "Spanish" },
{ "value": "Estonian", "name": "Estonian" },
{ "value": "Finnish", "name": "Finnish" },
{ "value": "French", "name": "French" },
{ "value": "Hungarian", "name": "Hungarian" },
{ "value": "Indonesian", "name": "Indonesian" },
{ "value": "Italian", "name": "Italian" },
{ "value": "Japanese", "name": "Japanese" },
{ "value": "Korean", "name": "Korean" },
{ "value": "Lithuanian", "name": "Lithuanian" },
{ "value": "Latvian", "name": "Latvian" },
{ "value": "Norwegian", "name": "Norwegian" },
{ "value": "Dutch", "name": "Dutch" },
{ "value": "Polish", "name": "Polish" },
{ "value": "Portuguese", "name": "Portuguese" },
{ "value": "Romanian", "name": "Romanian" },
{ "value": "Russian", "name": "Russian" },
{ "value": "Slovak", "name": "Slovak" },
{ "value": "Slovenian", "name": "Slovenian" },
{ "value": "Swedish", "name": "Swedish" },
{ "value": "Thai", "name": "Thai" },
{ "value": "Turkish", "name": "Turkish" },
{ "value": "Ukrainian", "name": "Ukrainian" }
]
},
"request": {
"method": "web_scraping",
"encodeURI": false,
"encodeURIComponent": true,
"initialUrl": "http://127.0.0.1:5454/translate",
"url": "http://127.0.0.1:5454/translate?sl=$SOURCE_LANG&tl=$TARGET_LANG&text=$SOURCE_TEXT",
"querySelector": "body",
"querySelectorAll": null,
"queryProperty": "innerText"
}
}
Python Script
You will need python >= 3.10
# VNTranslator - Custom MT Kit
from flask import Flask, request
import json
import requests
import urllib.parse
APP_DEBUG = True
APP_HOST = "127.0.0.1"
APP_PORT = 5454
OPENAI_API_URL = "https://api.openai.com/v1/chat/completions"
OPENAI_API_KEY = "YOUR_API_KEY"
OPENAI_MODEL = "gpt-4o-mini"
OPENAI_PROMPT = "You are a professional translator specializing in {SOURCE_LANG} to {TARGET_LANG} translation for Visual Novels. \
You will be provided with text, please translate it and provide the best translation: {SOURCE_TEXT}"
app = Flask(__name__)
@app.route('/translate', methods=['GET'])
def translate():
print("===== New Request =====")
print(f"{request.method} {request.path}")
source_text = request.args.get("text", "")
source_lang = request.args.get("sl", "Japanese")
target_lang = request.args.get("tl", "English")
if not source_text:
print(f"Err: Missing text!")
return f"Err: Missing text!", 400
# Decode text & format prompt
source_text = urllib.parse.unquote(source_text)
prompt = OPENAI_PROMPT.format(SOURCE_LANG=source_lang, TARGET_LANG=target_lang, SOURCE_TEXT=source_text).strip()
print(json.dumps({'source_lang': source_lang, 'target_lang': target_lang, 'text': source_text}, ensure_ascii=False, indent=4))
print(f"Prompt: {prompt}")
# Set header
headers = {
"Content-Type": "application/json",
"Authorization": f"Bearer {OPENAI_API_KEY}",
}
# Set data
data = {
"model": OPENAI_MODEL,
"messages": [
{"role": "system", "content": prompt},
{"role": "user", "content": source_text},
],
"temperature": 0.7,
}
# Send request
try:
response = requests.post(OPENAI_API_URL, headers=headers, json=data)
response.raise_for_status()
except requests.exceptions.RequestException as e:
print(f"Err: Failed to request: {e}")
return f"Err: Failed to request: {e}", 500
# Extract response
try:
response_data = response.json()
translation = response_data["choices"][0]["message"]["content"]
except (KeyError, IndexError) as e:
print(f"Err: Invalid response: {e}")
return f"Err: Invalid response: {e}", 500
# Return response
print(f"Response: {translation}\n")
return translation, response.status_code
if __name__ == '__main__':
app.run(debug=APP_DEBUG, host=APP_HOST, port=APP_PORT)
Last updated