Created
October 26, 2023 02:07
-
-
Save TheNextGuy32/e08df8aae6274a171c6838fb5f3c3e1e to your computer and use it in GitHub Desktop.
Convert all lua files in directory to ts
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
import openai | |
import math | |
import os | |
def convertLuaFileContentsToTypeScript(luaFileContents): | |
conversation = [{ | |
"role": "user", "content": "Convert the following Lua code to typescript. To provide context for the above prompt, I will send you text in parts. When I am finished, I will tell you ALL PARTS ARE SENT. Do not answer until you have received all the parts." | |
}] | |
# luaFileContents = luaFileContents[0:min(len(luaFileContents), int(4096/2))] | |
messageCount = math.ceil(len(luaFileContents) / 2490) | |
messageCharacterIndex = 0 | |
while(messageCharacterIndex < messageCount): | |
messageCharacterStart = messageCharacterIndex*2490 | |
content = "```%s```" % luaFileContents[messageCharacterStart: min(len(luaFileContents), messageCharacterStart+2490)] | |
conversation.append({ | |
"role": "user", | |
"content": content | |
}) | |
# print(len(content)) | |
messageCharacterIndex += 1 | |
conversation.append({ | |
"role": "user", | |
"content": "ALL PARTS ARE SENT" | |
}) | |
# print(conversation) | |
chat = openai.ChatCompletion.create( | |
model="gpt-3.5-turbo-16k", | |
max_tokens=2500, | |
n=1, | |
stop=None, | |
temperature=0.5, | |
messages=conversation | |
) | |
response = chat["choices"][0]["message"] | |
typescriptContent = response["content"] | |
return typescriptContent[0:len(typescriptContent)-3] | |
def convertCurrentDirectoriesToTypescript(): | |
currentDirectory = os.getcwd() | |
outputDirectoryPath = os.path.join(currentDirectory, "output") | |
os.makedirs(outputDirectoryPath, exist_ok=True) | |
fileCount = 5 | |
count = 0 | |
for root, _, files in os.walk(".", topdown=False): | |
for name in files: | |
if not name.endswith(".lua"): | |
continue | |
convertFile(os.path.join(root, name), outputDirectoryPath) | |
count += 1 | |
if count == fileCount: | |
return | |
def convertFile(inputFilepath, outputDirectory): | |
luaContents = "" | |
with open(inputFilepath) as f: | |
luaContents = f.read() | |
typescriptContents = convertLuaFileContentsToTypeScript(luaContents) | |
outputFilepath = os.path.join(outputDirectory, inputFilepath[2:len(inputFilepath)]) | |
outputFilepath = outputFilepath[0: len(outputFilepath)-3] + "ts" | |
os.makedirs(os.path.dirname(outputFilepath), exist_ok=True) | |
with open(outputFilepath, "w") as output: | |
output.write(typescriptContents) | |
convertCurrentDirectoriesToTypescript() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment