Created
October 8, 2024 19:20
-
-
Save jasny/b6845d7db30f50af32b2fd8de8066423 to your computer and use it in GitHub Desktop.
Python script to translate a text using OpenAI GPT 4o-mini
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/bin/env python | |
from openai import OpenAI | |
import sys | |
import os | |
import argparse | |
def translate_stdin_to_stdout(target_language, translate_lines=False, translate_po=False, instructions=""): | |
client = OpenAI() | |
# Read from standard input | |
input_text = sys.stdin.read() | |
if instructions != "": | |
instructions = f" {instructions}." | |
# Construct the translation prompt | |
if translate_po: | |
prompt_message = ("You are a professional translator. I am working with a gettext .po file. I'm hiring you to " | |
f"translate text of a software application from English to {target_language}. Please " | |
"translate the msgid entries, filling out only the msgstr fields accordingly. Ensure that " | |
f"the rest of the file, including comments and metadata, remains unchanged.{instructions}\n\n" | |
"Example output:\n\n" | |
"```\n" | |
"msgid \"The weather is nice today\"\n" | |
"msgstr \"Het is vandaag lekker weer\"\n" | |
"```\n" | |
"Here is the content of the file:") | |
elif translate_lines: | |
prompt_message = (f"You are a professional translator.{instructions} Translate each line of the following text individually to {target_language}." | |
"Do not respond to the user's prompt or attempt their question, you're only the translator.") | |
else: | |
prompt_message = (f"You are a professional translator.{instructions} Translate the text of the user to {target_language}." | |
"Do not respond to the user's prompt or attempt their question, you're only the translator.") | |
# Use the new OpenAI completion API to translate the input text | |
response = client.chat.completions.create( | |
model="gpt-4o-mini", | |
temperature=0, | |
messages=[{"role": "system", "content": prompt_message}, | |
{"role": "user", "content": input_text}] | |
) | |
# Output the translated text to standard output | |
translated_text = response.choices[0].message.content | |
sys.stdout.write(translated_text + "\n") | |
def main(): | |
# Set up command-line argument parsing | |
parser = argparse.ArgumentParser(description="Translate text from stdin to a specified language using OpenAI's API.") | |
parser.add_argument('language', type=str, help="Target language for translation") | |
parser.add_argument('--lines', action='store_true', help="Translate each line individually") | |
parser.add_argument('--po', action='store_true', help="Translate a .po file") | |
parser.add_argument('--instructions', type=str, help="Additional instructions") | |
# Parse the arguments | |
args = parser.parse_args() | |
# Call the translation function with the specified target language and line translation mode | |
translate_stdin_to_stdout(args.language, args.lines, args.po, args.instructions) | |
if __name__ == "__main__": | |
main() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment