Last active
November 10, 2023 19:01
-
-
Save celina-lopez/693ccc025535617a9a61a67061d8eddb to your computer and use it in GitHub Desktop.
quick chapgpt text editor
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 | |
openai.api_key = "OPENAI_API_KEY" # Update with openai key | |
MODEL = 'gpt-3.5-turbo' | |
def length_of_story(story): | |
# Want to half the tokens needed so chatgpt can respond | |
return len(story / 3000) | |
def editor(file_name): | |
with open(file_name) as f: | |
lines = f.readlines() | |
size = length_of_story("".join(lines)) | |
not_finished = True | |
start = 0 | |
while not_finished: | |
not_finished = edit(start, size, lines) | |
start = start + size | |
def edit(start, size, lines, save_file='edited_story.txt'): | |
end = start + size | |
portion = lines[start:end] | |
if portion == []: | |
return False | |
redone_file = create_editor("".join(portion)) | |
with open(save_file, 'a') as file: | |
file.write(redone_file) | |
return True | |
def create_editor(content): | |
messages = [ | |
{ | |
"role": "system", | |
"content": """ | |
I am a book editor. I am editing a book. | |
ONLY GIVE BACK THE STORY. DO NOT GIVE BACK NOTES. | |
""" | |
}, | |
{ | |
"role": "user", | |
"content": content | |
}, | |
] | |
response = openai.ChatCompletion.create(model=MODEL, messages=messages) | |
return response.choices[0].message.content |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment