Skip to content

Commit

Permalink
No commit message
Browse files Browse the repository at this point in the history
  • Loading branch information
philffm committed Nov 2, 2024
1 parent f43ca86 commit bf3535e
Show file tree
Hide file tree
Showing 3 changed files with 51 additions and 6 deletions.
10 changes: 5 additions & 5 deletions .github/workflows/translate.yml
Original file line number Diff line number Diff line change
Expand Up @@ -13,18 +13,18 @@ jobs:
- name: Checkout repository
uses: actions/checkout@v2

- name: Set up Node.js
uses: actions/setup-node@v2
- name: Set up Python
uses: actions/setup-python@v2
with:
node-version: '18'
python-version: '3.11' # Use the latest Python 3 version

- name: Install dependencies
run: npm install openai@latest
run: pip install requests

- name: Translate JSON
env:
OPENAI_API_KEY: ${{ secrets.OPENAI_API_KEY }}
run: node translate.js
run: python translate.py

- name: Commit changes
uses: stefanzweifel/git-auto-commit-action@v4
Expand Down
2 changes: 1 addition & 1 deletion docs/translations.json
Original file line number Diff line number Diff line change
Expand Up @@ -62,7 +62,7 @@
"aboutText": "Transform your browsing into a powerhouse of productivity with AI Summary Helper. Never let another article slip through the cracks! 🌟",
"features": "Features πŸ”",
"featureList": [
"Summarize articles with a personal touch ✨",
"Summarize articles with a personal touch ✨ ",
"OpenAI, Mistral AI, Ollama (local) and more models supported",
"Integrate summaries into your reading flow πŸ“š",
"Read on the go: Share to Kindle with Reabble πŸ“– ",
Expand Down
45 changes: 45 additions & 0 deletions translate.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
import os
import json
import requests

# Load your OpenAI API key from environment variables
api_key = os.getenv('OPENAI_API_KEY')
translations_path = 'docs/translations.json'
lang_dir = 'lang'

# Ensure the lang directory exists
if not os.path.exists(lang_dir):
os.makedirs(lang_dir)

def translate_content(content, target_lang):
prompt = f"Translate the following JSON content to {target_lang} organically with SEO optimization and maintain the JSON structure: {json.dumps(content)}"
headers = {
'Authorization': f'Bearer {api_key}',
'Content-Type': 'application/json'
}
data = {
'model': 'gpt-4o',
'prompt': prompt,
'max_tokens': 2000
}
response = requests.post('https://api.openai.com/v1/completions', headers=headers, json=data)
response.raise_for_status()
return json.loads(response.json()['choices'][0]['text'].strip())

def translate_file():
with open(translations_path, 'r', encoding='utf-8') as file:
data = json.load(file)
languages = data['languages']
content = data['content']

for lang in languages:
translated_content = translate_content(content, lang['code'])
output_path = os.path.join(lang_dir, f"{lang['code']}.json")
with open(output_path, 'w', encoding='utf-8') as file:
json.dump(translated_content, file, ensure_ascii=False, indent=2)

if __name__ == '__main__':
try:
translate_file()
except Exception as e:
print(f"An error occurred: {e}")

0 comments on commit bf3535e

Please sign in to comment.