kismatconnection
Junior Member
- Jun 8, 2011
- 114
- 25
Try thisHi, nice one. can you make it so that it does not try to pull content again from the ones already processed? Just in case you have to start over again to prevent duplicates.
Like
Check if row already has an output column
{ skip that topic.}
else this is a new topic
{process it}
Python:
import openai
import pandas as pd
from tqdm import tqdm
df = pd.read_csv("input.csv")
# Set OpenAI API key
openai.api_key = "REPLACEAPIKEY"
results = []
errors = []
for index, row in tqdm(df.iterrows(), total=len(df)):
if not pd.isna(row['output']):
# Skip this row if it already has an output column
continue
prompt = f"Please write a detailed article about {row['topic']}. Use this information to write the article: {row['context']}"
try:
response = openai.Completion.create(engine="text-davinci-003", prompt=prompt, temperature=0.7, max_tokens=2048, top_p=0.5)
results.append([row['topic'], response.choices[0].text])
except Exception as e:
errors.append([row['topic'], row['context'], str(e)])
continue
output_df = pd.DataFrame(results, columns=['Topic', 'Generated Text'])
# Merge the original input dataframe with the output dataframe based on the 'Topic' column
merged_df = pd.merge(df, output_df, on='Topic', how='left')
# Save the merged dataframe to a new CSV file
merged_df.to_csv('output.csv', index=False)
if len(errors) > 0:
# Log any errors that occurred during the script
error_df = pd.DataFrame(errors, columns=['Topic', 'Context', 'Error'])
error_df.to_csv('errors.csv', index=False)
After all the articles have been generated, the script merges the original input dataframe with the generated articles dataframe based on the "Topic" column. This allows the script to preserve any existing columns in the input CSV file, including the "output" column if it already exists. The merged dataframe is then saved to a new CSV file called "output.csv".