Advertisement
incomestreamsurfer

python script auto outline maker with products

Dec 8th, 2023
237
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 4.22 KB | None | 0 0
  1. import os
  2. import openai
  3. import time
  4. import csv
  5. from pprint import pprint
  6.  
  7. # Set your OpenAI API key
  8. OPENAI_API_TOKEN = "xd"
  9. api_key = os.environ["OPENAI_API_KEY"] = OPENAI_API_TOKEN
  10.  
  11. # Initialize the OpenAI client
  12. client = openai.OpenAI()
  13.  
  14. # Function to upload a file to OpenAI
  15. def upload_file(file_path, purpose):
  16. with open(file_path, "rb") as file:
  17. response = client.files.create(file=file, purpose=purpose)
  18. return response.id # Access the 'id' attribute directly
  19.  
  20.  
  21.  
  22.  
  23. # Upload your files
  24. internal_links_file_id = upload_file('internallinks.txt', 'assistants')
  25. products_file_id = upload_file('products.txt', 'assistants')
  26. content_plan_file_id = upload_file('2men_it_blog_content_plan_expanded (1).csv', 'assistants')
  27.  
  28. # Create an Assistant
  29. assistant = client.beta.assistants.create(
  30. name="Content Creation Assistant",
  31. model="gpt-4-1106-preview",
  32. 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.",
  33. tools=[{"type": "retrieval"}],
  34. file_ids=[internal_links_file_id, products_file_id, content_plan_file_id]
  35. )
  36.  
  37. # Function to append the outline to the existing CSV file
  38. def append_to_csv(row, outline):
  39. row['Blog Outline'] = outline # Add the generated outline to the row
  40. with open('2men_it_blog_content_plan_expanded (1).csv', 'a', newline='', encoding='utf-8') as f:
  41. writer = csv.DictWriter(f, fieldnames=row.keys())
  42. writer.writerow(row)
  43.  
  44. def process_content_plan():
  45. input_file = '2men_it_blog_content_plan_expanded (1).csv'
  46. output_file = 'processed_content_plan.csv'
  47. processed_rows = []
  48.  
  49. with open(input_file, newline='', encoding='utf-8') as csvfile:
  50. reader = csv.DictReader(csvfile)
  51. for row in reader:
  52. if row.get('Processed', 'No') == 'Yes': # Skip already processed rows
  53. continue
  54.  
  55. blog_post_idea = row['Blog Post Ideas'] # Extract the blog post idea
  56.  
  57. # Create a Thread
  58. thread = client.beta.threads.create()
  59.  
  60. # Add a Message to a Thread
  61. 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."
  62. message = client.beta.threads.messages.create(thread_id=thread.id, role="user", content=message_content)
  63.  
  64. # Run the Assistant
  65. run = client.beta.threads.runs.create(thread_id=thread.id, assistant_id=assistant.id)
  66.  
  67. # Wait for the run to complete and retrieve the outline
  68. outline = None
  69. while True:
  70. run_status = client.beta.threads.runs.retrieve(thread_id=thread.id, run_id=run.id)
  71. if run_status.status == 'completed':
  72. # Retrieve messages from the thread to see the assistant's response
  73. messages = client.beta.threads.messages.list(thread_id=thread.id)
  74. for message in messages.data:
  75. if message.role == "assistant":
  76. outline = message.content
  77. break
  78. break
  79. time.sleep(10)
  80.  
  81. if outline:
  82. row['Blog Outline'] = outline # Add the generated outline to the row
  83. pprint(outline)
  84.  
  85. # Mark the row as processed and store it
  86. row['Processed'] = 'Yes'
  87. processed_rows.append(row)
  88.  
  89. # Write the processed rows to the output file
  90. with open(output_file, 'w', newline='', encoding='utf-8') as f:
  91. writer = csv.DictWriter(f, fieldnames=processed_rows[0].keys())
  92. writer.writeheader()
  93. writer.writerows(processed_rows)
  94.  
  95. process_content_plan()
  96.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement