Skip to content

Instantly share code, notes, and snippets.

@crakdelpol
Created January 11, 2024 15:42
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save crakdelpol/052fd8e50c1d5bc8bb0e09868209409f to your computer and use it in GitHub Desktop.
Save crakdelpol/052fd8e50c1d5bc8bb0e09868209409f to your computer and use it in GitHub Desktop.
translate code from one language to another with chatgpt
dir_path = '/home/gigi/project'
def translate():
for path, currentDirectory, files in os.walk(dir_path):
for file in files:
f = os.path.join(path, file)
if f.endswith('.php'):
# print(f"working on {f}")
file = open(f, "r")
content = file.read()
print(get_completion("translate this code to italian \n" + content))
print("I am done!")
def get_completion(prompt, model="gpt-3.5-turbo-16k"):
openai.api_key = "your-api-key"
messages = [{"role": "user", "content": prompt}]
response = openai.ChatCompletion.create(model=model,messages=messages,temperature=0)
return response.choices[0].message["content"]
if __name__ == '__main__':
translate()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment