Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
52 changes: 37 additions & 15 deletions .github/workflows/scripts/translate.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
import os
import openai
import sys
import time

openai.api_key = os.environ["OPENAI_API_KEY"]
openai.organization = os.environ["OPENAI_ORG"]
Expand Down Expand Up @@ -41,21 +42,42 @@ def main():
for key, value in en_data.items():
if key not in translated_data:
prompt='You are an i18n-compatible translation service. Translate the English string on the next line to {lang}. Maintain whitespace. Do not modify or translate interpolated variables in any way.\n"{text}"'.format(lang=lang_name, text=value)
response=openai.ChatCompletion.create(
model='gpt-4',
messages=[
{
"role": "user",
"content": prompt
}
],
temperature=0.3,
max_tokens=500,
n=1,
stop=None
)
translated_data[key] = response.choices[0].message.content.strip('"')
changed = True

max_retries = 3
retry_count = 0
timeout = 15

while retry_count < max_retries:
try:
start_time = time.time()
response=openai.ChatCompletion.create(
model='gpt-4',
messages=[
{
"role": "user",
"content": prompt
}
],
temperature=0.3,
max_tokens=500,
n=1,
stop=None
)
elapsed_time = time.time() - start_time
if elapsed_time < timeout:
translated_data[key] = response.choices[0].message.content.strip('"')
changed = True
break
else:
raise Exception("API call took too long")
except Exception as e:
print(f"Error: {e}")
retry_count += 1
if retry_count < max_retries:
print(f"Retrying... ({retry_count}/{max_retries})")
else:
print("Max retries reached. Exiting.")
sys.exit(1)

if changed:
with open(file_path, 'w') as f:
Expand Down
12 changes: 9 additions & 3 deletions .github/workflows/translate_strings.yml
Original file line number Diff line number Diff line change
Expand Up @@ -27,24 +27,30 @@ jobs:
run: |
python -m pip install --upgrade pip
pip install openai

- name: Download translation script
run: wget https://raw.githubusercontent.com/ProcessMaker/processmaker/develop/.github/workflows/scripts/translate.py

- name: Translate changed strings to Spanish
env:
OPENAI_API_KEY: ${{ secrets.OPENAI_API_KEY }}
OPENAI_ORG: ${{ secrets.OPENAI_ORG }}
run: python .github/workflows/scripts/translate.py es
run: python translate.py es

- name: Translate changed strings to French
env:
OPENAI_API_KEY: ${{ secrets.OPENAI_API_KEY }}
OPENAI_ORG: ${{ secrets.OPENAI_ORG }}
run: python .github/workflows/scripts/translate.py fr
run: python translate.py fr

- name: Translate changed strings to German
env:
OPENAI_API_KEY: ${{ secrets.OPENAI_API_KEY }}
OPENAI_ORG: ${{ secrets.OPENAI_ORG }}
run: python .github/workflows/scripts/translate.py de
run: python translate.py de

- name: Remove translation script
run: rm -rf translate.py

- name: Delete existing branch if it exists
run: |
Expand Down