wip2: title generation with OpenAI

This commit is contained in:
Ashik K 2023-04-06 17:02:31 +02:00
parent 16328b5a53
commit 08e711074f
1 changed files with 15 additions and 12 deletions

View File

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