wikimedia-museumproject/titlegen.py

39 lines
1.4 KiB
Python
Raw Normal View History

2023-04-06 14:31:03 +00:00
import sys
import csv
import openai
2023-04-06 15:02:31 +00:00
# check if the user supplied the correct number of command line arguments
if len(sys.argv) != 4:
print("Usage: python titlegen.py inputfilename.csv outputfilename.csv API_key")
sys.exit(1)
# set the api key from the command line argument
openai.api_key = sys.argv[3]
with open(sys.argv[1], "r") as file:
outfile = open(sys.argv[2], "w")
writer = csv.writer(outfile)
2023-04-06 14:31:03 +00:00
reader = csv.reader(file)
for row in reader:
print(row[4])
# summarize the text in the 5th column using OpenAi's GPT-3
2023-04-06 15:02:31 +00:00
# create a variable called prompt and set it as the concatenatenation of the string "Summarize this
2023-04-06 14:31:03 +00:00
# in one sentence:" and the text in the 5th column
2023-04-06 15:02:31 +00:00
prompt = "Summarize this in one English sentence of not more than 4 words:" + row[4]
2023-04-06 14:31:03 +00:00
response = openai.Completion.create(
model="gpt-3.5-turbo",
prompt=prompt,
temperature=0.7,
max_tokens=64,
top_p=1.0,
frequency_penalty=0.0,
presence_penalty=0.0
)
# write the response as the last column in each row of the same csv file
row.append(response)
print(row)
2023-04-06 15:02:31 +00:00
# write the row to the csv file
2023-04-06 14:31:03 +00:00
2023-04-06 15:02:31 +00:00
writer.writerow(row)
outfile.close()
file.close()