Skip to content

Instantly share code, notes, and snippets.

@bigsnarfdude
Created February 20, 2024 23:55
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 bigsnarfdude/aa3115bf2e6c1053b2361e62d4477ce0 to your computer and use it in GitHub Desktop.
Save bigsnarfdude/aa3115bf2e6c1053b2361e62d4477ce0 to your computer and use it in GitHub Desktop.
import os
from openai import OpenAI
import prompt
def truncate_words(input_string, max_words):
words = input_string.split()
truncated_words = words[:max_words]
return ' '.join(truncated_words)
def process_file(file_path, client, prompt, max_tokens):
with open(file_path, 'r') as file:
content = file.read()
input_string = truncate_words(prompt + content, max_tokens)
try:
completion = client.chat.completions.create(
model='gpt-4-0125-preview', #gpt-3.5-turbo-0125',
messages=[
{"role": "system", "content": "You are a helpful assistant, skilled in summarizing lectures."},
{"role": "user", "content": input_string}
]
)
return completion.choices[0].message.content
except Exception as e:
print(f"Error with OpenAI API for file {os.path.basename(file_path)}: {e}")
return None
def save_summary(file_path, summary):
if summary:
try:
with open(file_path, "w") as file:
file.write(summary)
except Exception as e:
print(f"Error with writing summary file {file_path}: {e}")
return None
def summarize_files(directory_path, processed_directory, prompt, max_tokens, finished_files):
client = OpenAI()
client.api_key = 'sk-8tBg'
for filename in os.listdir(directory_path):
if filename.endswith(".txt") and filename not in finished_files:
file_path = os.path.join(directory_path, filename)
output_path = os.path.join(processed_directory, filename)
summary = process_file(file_path, client, prompt, max_tokens)
if summary:
save_summary(output_path, summary)
if __name__ == "__main__":
PROMPT = prompt.PromptText().summary_text
MODEL_MAX_TOKENS = 128000
target_directory = '/Users/vincent/Desktop/unprocessed/'
processed_directory = '/Users/vincent/Desktop/processed/'
finished_files = set() # here you can add a list of files already processed
# python3 summary.py
summarize_files(target_directory, processed_directory, PROMPT, MODEL_MAX_TOKENS, finished_files)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment