Skip to content

Instantly share code, notes, and snippets.

@celina-lopez
Last active November 10, 2023 19:01
Show Gist options
  • Save celina-lopez/693ccc025535617a9a61a67061d8eddb to your computer and use it in GitHub Desktop.
Save celina-lopez/693ccc025535617a9a61a67061d8eddb to your computer and use it in GitHub Desktop.
quick chapgpt text editor
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