Advertisement
incomestreamsurfer

sop for guest posts

Mar 21st, 2024 (edited)
118
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 4.54 KB | None | 0 0
  1. import pandas as pd
  2. import openai
  3. import requests
  4. import os
  5.  
  6. # Load your OpenAI API key
  7. openai.api_key = 'Your_api_key'
  8.  
  9. def download_image(image_url, file_path):
  10. """
  11. Downloads an image from a given URL and saves it to a specified path.
  12. """
  13. response = requests.get(image_url)
  14. if response.status_code == 200:
  15. with open(file_path, 'wb') as file:
  16. file.write(response.content)
  17. else:
  18. print(f"Failed to download image from {image_url}")
  19.  
  20. def generate_image(prompt, image_folder, image_number):
  21. """
  22. Generates an image, downloads it, and saves it locally with a sequential filename.
  23. """
  24. response = openai.Image.create(
  25. model="dall-e-3",
  26. size="1792x1024",
  27. prompt=prompt,
  28. n=1
  29. )
  30. image_url = response['data'][0]['url']
  31. image_path = os.path.join(image_folder, f'{image_number}.jpg')
  32. download_image(image_url, image_path)
  33. return image_path
  34.  
  35. # Create a directory for images if it doesn't exist
  36. image_folder = 'downloaded_images'
  37. if not os.path.exists(image_folder):
  38. os.makedirs(image_folder)
  39.  
  40. # Read the CSV file
  41. df = pd.read_csv('Copy of Content Request - Content - April Month.csv')
  42.  
  43. # Prepare a new DataFrame to store the results with the desired column order
  44. new_df = pd.DataFrame(columns=['ImageURL', 'Article'])
  45.  
  46. # Iterate through each row
  47. for index, row in df.iterrows():
  48. try:
  49. print(f"Processing row {index + 1}")
  50.  
  51. # Combine topics and keywords for the first prompt
  52. input_data = f"{row['Topics']}, {row['Keywords']}"
  53.  
  54. # First prompt - Generate outline
  55. response_outline = openai.ChatCompletion.create(
  56. model="gpt-4-1106-preview",
  57. messages=[{"role": "system", "content": f"Write an outline for this article it should have 10 headings including subheadings: {input_data}"}]
  58. )
  59. outline = response_outline.choices[0].message['content']
  60. print("Generated Outline:", outline)
  61.  
  62. # Second prompt - Generate content
  63. response_content = openai.ChatCompletion.create(
  64. model="gpt-4-1106-preview",
  65. messages=[
  66. {"role": "system", "content": f"Every subheader and header must have several lines of content. You are writing long form content. Every single header of the outline must be covered in detail. Write 3 lengthy paragraphs per header or subheader in the outline. only one title should have # h1 tags, the first title, all the others should have h2 or h3 Ensure each header is represented in the content with an h2 or h3 header. Do not mention h2 or h3. ONLY USE ONE H1 PER ARTICLE. This should be the {input_data} column Topic ALWAYS WRITE A COMPLETE ARTICLE WITH A CONCLUSION You must include at least once each keyword in {input_data}, this is to link to later. Never mention guest posts. Write engaging and helpful content using this outline, output a guest post with sufficient markdown formatting to make it look professional, but don't overdo it with tables and lists. Just have a few paragraphs, a few lists, and maybe one table. It needs to be a completed article and actually give useful information and specifics. This can include tables, lists, and any other formatting and styling that you think will help get the guest post accepted. You should also use non-generic language patterns, and make the content both unique, without using superflous terms and language. Make the content actually helpful and engaging without being strange. Never use placeholder content or try to use additional resources.{outline}"},
  67. {"role": "user", "content": outline}
  68. ],
  69. max_tokens=3750
  70. )
  71. content = response_content.choices[0].message['content']
  72. print("Generated Article:", content)
  73.  
  74. # Generate an image using DALL-E and save it locally
  75. image_prompt = f"Focus on specific, visually representable elements, aesthetic, unadorned photo visually representing one of these {row['Keywords']}"
  76. image_path = generate_image(image_prompt, image_folder, index+1)
  77. print("Generated Image Path:", image_path)
  78.  
  79. # Add the new row to the new DataFrame
  80. new_df.loc[index] = [image_path, content]
  81.  
  82. print(f"Row {index + 1} processed successfully.")
  83.  
  84. except Exception as e:
  85. print(f"Error processing row {index + 1}: {e}")
  86.  
  87. # Save the new DataFrame to a CSV file
  88. new_csv_file = 'updated_csv_file_with_images_first.csv'
  89. new_df.to_csv(new_csv_file, index=False)
  90. print(f"CSV file saved as {new_csv_file}")
  91.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement