Skip to content

Instantly share code, notes, and snippets.

@danielvlopes
Created May 8, 2024 04:50
Show Gist options
  • Save danielvlopes/4b3d0fb0c4a0c4d42a9479e9dc726d0a to your computer and use it in GitHub Desktop.
Save danielvlopes/4b3d0fb0c4a0c4d42a9479e9dc726d0a to your computer and use it in GitHub Desktop.
import csv
import openai
import os
import sys
import pdb
# Set API key
openai.api_key = '…'
input_file_path = 'input.csv'
output_file_path = 'output.csv'
# Function to generate summary using OpenAI API with context
def generate_summary(data, date):
if not data:
return ""
response = openai.chat.completions.create(
model="gpt-4-turbo-preview",
messages=[
{
"role": "system",
"content": (
"You are a summarizer assistant for what the person did during the week. I'll give you raw data with what a person did in their week of work. "
"The data will likely contain multiple days of their week sharing what they wanted to do either for the whole week or in the day (Today) and what they actually (Yesterday) did the day before."
"I want you to summarize this into a coherent two to three paragraphs text"
"explaining what this person was working on that week and what they accomplished. The summary should be in the first person, like if I'm remmebering what I did. Use straightforward and succinct language."
"The summary should be short and to the point with easy straightforward words. Use markdown to split the paragraphs. IMPORTANT: don't apologize if you can't do the task, just try your best."
)
},
{
"role": "user",
"content": data
}
]
)
print(date)
print(response.choices[0].message.content)
print("====================================")
return response.choices[0].message.content
# Read CSV, generate summaries, and write results
with open(input_file_path, mode='r', newline='', encoding='utf-8') as infile, \
open(output_file_path, mode='w', newline='', encoding='utf-8') as outfile:
reader = csv.reader(infile)
writer = csv.writer(outfile)
# Read the header and write a new header to the output
headers = next(reader)
writer.writerow(headers + ['Summary'])
# Loop through each row in the CSV
for row in reader:
# Assume the data to summarize is in the first column, adjust as necessary
summary = generate_summary(row[1], row[0])
writer.writerow(row + [summary])
print("Processing complete. Summaries added to new CSV file.")
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment