Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import os
- import openai
- import time
- import csv
- from pprint import pprint
- # Set your OpenAI API key
- OPENAI_API_TOKEN = "xd"
- api_key = os.environ["OPENAI_API_KEY"] = OPENAI_API_TOKEN
- # Initialize the OpenAI client
- client = openai.OpenAI()
- # Function to upload a file to OpenAI
- def upload_file(file_path, purpose):
- with open(file_path, "rb") as file:
- response = client.files.create(file=file, purpose=purpose)
- return response.id # Access the 'id' attribute directly
- # Upload your files
- internal_links_file_id = upload_file('internallinks.txt', 'assistants')
- products_file_id = upload_file('products.txt', 'assistants')
- content_plan_file_id = upload_file('2men_it_blog_content_plan_expanded (1).csv', 'assistants')
- # Create an Assistant
- assistant = client.beta.assistants.create(
- name="Content Creation Assistant",
- model="gpt-4-1106-preview",
- instructions="you never invent any internal links, you only use products.txt and internalinks.txt You generate blog post outlines based on {blog_post_idea}. You use {products_file_id} (which is products.txt) to add suggested RELEVANT (NOT JUST THE FIRST) 10 product images and product URLs and {internal_links_file_id} (which is internallinks.txt) to add 10 suggested collectoin internal links to the outline.",
- tools=[{"type": "retrieval"}],
- file_ids=[internal_links_file_id, products_file_id, content_plan_file_id]
- )
- # Function to append the outline to the existing CSV file
- def append_to_csv(row, outline):
- row['Blog Outline'] = outline # Add the generated outline to the row
- with open('2men_it_blog_content_plan_expanded (1).csv', 'a', newline='', encoding='utf-8') as f:
- writer = csv.DictWriter(f, fieldnames=row.keys())
- writer.writerow(row)
- def process_content_plan():
- input_file = '2men_it_blog_content_plan_expanded (1).csv'
- output_file = 'processed_content_plan.csv'
- processed_rows = []
- with open(input_file, newline='', encoding='utf-8') as csvfile:
- reader = csv.DictReader(csvfile)
- for row in reader:
- if row.get('Processed', 'No') == 'Yes': # Skip already processed rows
- continue
- blog_post_idea = row['Blog Post Ideas'] # Extract the blog post idea
- # Create a Thread
- thread = client.beta.threads.create()
- # Add a Message to a Thread
- message_content = f"Generate an outline for a blog post about '{blog_post_idea}'. Please include a diverse range of product suggestions from different categories in our product sitemap. You never invent any internal links, you only use products.txt and internalinks.txt. A maximum of 10 products and 10 internal links should be used."
- message = client.beta.threads.messages.create(thread_id=thread.id, role="user", content=message_content)
- # Run the Assistant
- run = client.beta.threads.runs.create(thread_id=thread.id, assistant_id=assistant.id)
- # Wait for the run to complete and retrieve the outline
- outline = None
- while True:
- run_status = client.beta.threads.runs.retrieve(thread_id=thread.id, run_id=run.id)
- if run_status.status == 'completed':
- # Retrieve messages from the thread to see the assistant's response
- messages = client.beta.threads.messages.list(thread_id=thread.id)
- for message in messages.data:
- if message.role == "assistant":
- outline = message.content
- break
- break
- time.sleep(10)
- if outline:
- row['Blog Outline'] = outline # Add the generated outline to the row
- pprint(outline)
- # Mark the row as processed and store it
- row['Processed'] = 'Yes'
- processed_rows.append(row)
- # Write the processed rows to the output file
- with open(output_file, 'w', newline='', encoding='utf-8') as f:
- writer = csv.DictWriter(f, fieldnames=processed_rows[0].keys())
- writer.writeheader()
- writer.writerows(processed_rows)
- process_content_plan()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement