Test
All checks were successful
Trigger build for deploying blog updates / build (push) Successful in 0s
All checks were successful
Trigger build for deploying blog updates / build (push) Successful in 0s
This commit is contained in:
104
translate.py
Normal file
104
translate.py
Normal file
@@ -0,0 +1,104 @@
|
||||
import argparse
|
||||
import os
|
||||
import requests
|
||||
|
||||
LLAMA_URL = "https://ollama.epsem.aranroig.com/v1/chat/completions" # adjust if needed
|
||||
|
||||
|
||||
def load_api_key(path=".api_key"):
|
||||
with open(path, "r", encoding="utf-8") as f:
|
||||
return f.read().strip()
|
||||
|
||||
|
||||
def translate_text(text, system_prompt, api_key, model="llama"):
|
||||
headers = {
|
||||
"Authorization": f"Bearer {api_key}",
|
||||
"Content-Type": "application/json"
|
||||
}
|
||||
|
||||
payload = {
|
||||
"model": model,
|
||||
"messages": [
|
||||
{"role": "system", "content": system_prompt},
|
||||
{"role": "user", "content": text}
|
||||
],
|
||||
"temperature": 0.2
|
||||
}
|
||||
|
||||
response = requests.post(LLAMA_URL, json=payload, headers=headers)
|
||||
response.raise_for_status()
|
||||
|
||||
return response.json()["choices"][0]["message"]["content"]
|
||||
|
||||
|
||||
def ensure_dir(path):
|
||||
os.makedirs(path, exist_ok=True)
|
||||
|
||||
|
||||
def load_preamble(path="PREAMBLE.md"):
|
||||
with open(path, "r", encoding="utf-8") as f:
|
||||
return f.read()
|
||||
|
||||
|
||||
def build_output_path(input_file, lang):
|
||||
"""
|
||||
Converts:
|
||||
original/blog/test.md
|
||||
into:
|
||||
content/blog/ca/test.md
|
||||
"""
|
||||
input_path = os.path.normpath(input_file)
|
||||
parts = input_path.split(os.sep)
|
||||
|
||||
# Replace root folder "original" -> "content"
|
||||
if parts[0] == "original":
|
||||
parts[0] = "content"
|
||||
|
||||
filename = parts[-1]
|
||||
name_no_ext = os.path.splitext(filename)[0]
|
||||
|
||||
base_path = parts[:-1] # everything except filename
|
||||
|
||||
output_dir = os.path.join(*base_path, lang)
|
||||
return os.path.join(output_dir, f"{name_no_ext}.md")
|
||||
|
||||
|
||||
def main():
|
||||
parser = argparse.ArgumentParser()
|
||||
parser.add_argument("file", help="Path to markdown file")
|
||||
parser.add_argument("--targets", nargs="+", required=True,
|
||||
help="Target languages, e.g. ca es fr en")
|
||||
parser.add_argument("--output_dir", default=".",
|
||||
help="Base output directory (currently unused in path logic)")
|
||||
parser.add_argument("--preamble_file", default="PREAMBLE.md",
|
||||
help="Path to preamble markdown file")
|
||||
|
||||
args = parser.parse_args()
|
||||
|
||||
api_key = load_api_key()
|
||||
system_prompt = load_preamble(args.preamble_file)
|
||||
|
||||
with open(args.file, "r", encoding="utf-8") as f:
|
||||
content = f.read()
|
||||
|
||||
for lang in args.targets:
|
||||
print(f"Translating to {lang}")
|
||||
full_prompt = (
|
||||
f"{system_prompt}\n\n"
|
||||
f"Translate into {lang}. Preserve markdown formatting."
|
||||
)
|
||||
|
||||
translated = translate_text(content, full_prompt, api_key)
|
||||
|
||||
output_path = build_output_path(args.file, lang)
|
||||
ensure_dir(os.path.dirname(output_path))
|
||||
|
||||
with open(output_path, "w", encoding="utf-8") as f:
|
||||
f.write(translated)
|
||||
|
||||
print(f"Written: {output_path}")
|
||||
|
||||
|
||||
if __name__ == "__main__":
|
||||
main()
|
||||
|
||||
Reference in New Issue
Block a user