From 579f0ae314ec01eef6938a5f35365cf04ed8f2b0 Mon Sep 17 00:00:00 2001 From: "meuer.design" Date: Tue, 1 Aug 2023 03:02:39 +0200 Subject: [PATCH] added requirements.txt + dotenv --- .env.example | 11 +++++++++++ .gitignore | 1 + README.md | 5 +++-- createblogpost.py | 19 +++++++++++-------- createpage.py | 19 +++++++++++-------- requirements.txt | 7 +++++++ test.py | 27 +++++++++++++++------------ uploadblogpost.py | 10 ++++++---- uploadpage.py | 9 ++++++--- 9 files changed, 71 insertions(+), 37 deletions(-) create mode 100644 .env.example create mode 100644 .gitignore create mode 100644 requirements.txt diff --git a/.env.example b/.env.example new file mode 100644 index 0000000..7e33015 --- /dev/null +++ b/.env.example @@ -0,0 +1,11 @@ +OPENAI_API_KEY=YOUR_OPENAI_API_KEY +MODEL_NAME=gpt-4 +MAX_TOKENS=4500 +MAX_OUTLINE_TOKENS=1024 +TEMPERATURE=0.2 + +STABILITY_AI_API_KEY=YOUR_STABILITY_AI_API_KEY + +WP_URL=YOUR_WP_URL +WP_USERNAME=YOUR_WP_USERNAME +WP_PASSWORD=YOUR_WP_PASSWORD \ No newline at end of file diff --git a/.gitignore b/.gitignore new file mode 100644 index 0000000..2eea525 --- /dev/null +++ b/.gitignore @@ -0,0 +1 @@ +.env \ No newline at end of file diff --git a/README.md b/README.md index 40f8722..3c6563a 100644 --- a/README.md +++ b/README.md @@ -2,10 +2,11 @@ This is an autoblogger that uses the ChatGPT API and Stable Diffusion API to create fully optimized blog posts and post them automatically to wordpress ## First step - +- Install all Dependencies with pip install -r requirements.txt - Get your stability API key (I am using [dreamstudio]([url](https://dreamstudio.com/api/))) -- Get your OpenAI API Key (GPT-4 is recommended, haven't tested with GPT3.5, 300-500 articles cost me about $50) +- Get your OpenAI API Key (GPT-4 is recommended, GPT3.5 works (but you need to lower the max_tokens), 300-500 articles cost me about $50) - Create a WordPress App Password, get your user log-in, and your site's URL +- Rename the provided .env.example file to .env and replace the placeholder values with your actual details. - A plan for your website, this way you can already internally link without worrying about doing it afterwards. If you keep a nice, uniform structure, you will be able to plan out all the content and post it all to your website without using any other internal link system. [Example Website](https://giucas.com) diff --git a/createblogpost.py b/createblogpost.py index c2a8890..42cdded 100644 --- a/createblogpost.py +++ b/createblogpost.py @@ -5,11 +5,14 @@ import base64 import time from tqdm import tqdm +from dotenv import load_dotenv +load_dotenv() -openai.api_key = 'YOUR_OPEN_AI_KEY' # Add your OpenAI API Key +openai.api_key = os.getenv('OPENAI_API_KEY') +model_name = os.getenv('MODEL_NAME') def generate_featured_image(text): - api_key = 'YOUR_STABILITY_API_KEY' + api_key = os.getenv('STABILITY_AI_API_KEY') api_host = 'https://api.stability.ai' engine_id = 'stable-diffusion-xl-beta-v2-2-2' response = requests.post( @@ -85,10 +88,10 @@ def generate_featured_image(text): ] response_outline = openai.ChatCompletion.create( - model="gpt-4", + model=model_name, messages=conversation_outline, - max_tokens=1024, - temperature=0.2 + max_tokens = int(os.getenv('MAX_OUTLINE_TOKENS')), + temperature = float(os.getenv('TEMPERATURE')) ) essay_outline = response_outline['choices'][0]['message']['content'] @@ -105,10 +108,10 @@ def generate_featured_image(text): ] response = openai.ChatCompletion.create( - model="gpt-4", + model=model_name, messages=conversation, - max_tokens=6400, - temperature=0.2 + max_tokens = int(os.getenv('MAX_TOKENS')), + temperature = float(os.getenv('TEMPERATURE')) ) diff --git a/createpage.py b/createpage.py index 204165f..f6c4ddf 100644 --- a/createpage.py +++ b/createpage.py @@ -5,11 +5,14 @@ import base64 import time from tqdm import tqdm +from dotenv import load_dotenv +load_dotenv() -openai.api_key = 'your_open_ai_key' # Add your OpenAI API Key +openai.api_key = os.getenv('OPENAI_API_KEY') +model_name = os.getenv('MODEL_NAME') def generate_featured_image(brand): - api_key = 'your_stability_key' + api_key = os.getenv('STABILITY_AI_API_KEY') api_host = 'https://api.stability.ai' engine_id = 'stable-diffusion-xl-beta-v2-2-2' response = requests.post( @@ -86,10 +89,10 @@ def generate_featured_image(brand): ] response_outline = openai.ChatCompletion.create( - model="gpt-4", + model=model_name, messages=conversation_outline, - max_tokens=1024, - temperature=0.2 + max_tokens = int(os.getenv('MAX_OUTLINE_TOKENS')), + temperature = float(os.getenv('TEMPERATURE')) ) essay_outline = response_outline['choices'][0]['message']['content'] @@ -106,10 +109,10 @@ def generate_featured_image(brand): ] response = openai.ChatCompletion.create( - model="gpt-4", + model=model_name, messages=conversation, - max_tokens=6400, - temperature=0.2 + max_tokens = int(os.getenv('MAX_TOKENS')), + temperature = float(os.getenv('TEMPERATURE')) ) diff --git a/requirements.txt b/requirements.txt new file mode 100644 index 0000000..a7c1c8c --- /dev/null +++ b/requirements.txt @@ -0,0 +1,7 @@ +openai +pandas +requests +tqdm +backoff +tenacity +python-dotenv \ No newline at end of file diff --git a/test.py b/test.py index 0305295..a6dec68 100644 --- a/test.py +++ b/test.py @@ -13,6 +13,8 @@ stop_after_attempt, wait_random_exponential, ) # for exponential backoff +from dotenv import load_dotenv +load_dotenv() @retry(wait=wait_random_exponential(multiplier=1, min=4, max=10), stop=stop_after_attempt(10)) def completion_with_backoff(**kwargs): @@ -29,7 +31,8 @@ def completion_with_backoff(**kwargs): -openai.api_key = 'YOUR_OPEN_AI_KEY' +openai.api_key = os.getenv('OPENAI_API_KEY') +model_name = os.getenv('MODEL_NAME') output_df = pd.DataFrame(columns=['URL Slug', 'Meta Title', 'Description', 'Blog Content', 'Featured Image']) output_lock = threading.Lock() @@ -37,7 +40,7 @@ def completion_with_backoff(**kwargs): # Retry on rate limit error with exponential backoff @retry(wait=wait_random_exponential(multiplier=1, min=4, max=10), stop=stop_after_attempt(10)) def generate_featured_image(text, meta_title): - api_key = 'YOUR_STABILITY_API_KEY' + api_key = os.getenv('STABILITY_AI_API_KEY') api_host = 'https://api.stability.ai' engine_id = 'stable-diffusion-xl-1024-v1-0' response = requests.post( @@ -92,10 +95,10 @@ def generate_blog_post(row): }, ] response_outline = completion_with_backoff( - model="gpt-4", + model=model_name, messages=conversation_outline, - max_tokens=1024, - temperature=0.2 + max_tokens = int(os.getenv('MAX_OUTLINE_TOKENS')), + temperature = float(os.getenv('TEMPERATURE')) ) essay_outline = response_outline['choices'][0]['message']['content'] @@ -111,10 +114,10 @@ def generate_blog_post(row): }, ] response = completion_with_backoff( - model="gpt-4", + model=model_name, messages=conversation, - max_tokens=4500, - temperature=0.2 + max_tokens = int(os.getenv('MAX_TOKENS')), + temperature = float(os.getenv('TEMPERATURE')) ) @@ -132,12 +135,12 @@ def generate_blog_post(row): tqdm.write(f"Saved blog post for URL Slug {url_slug} to output.csv") - # WordPress site URL - wp_url = 'YOUR_WP_URL' + # WordPress site URL + wp_url = os.getenv('WP_URL') # WordPress username and password - username = 'your_wp_username' - password = 'your_wp_app_password' + username = os.getenv('WP_USERNAME') + password = os.getenv('WP_PASSWORD') # Open the image file in binary mode with open(result['Featured Image'], 'rb') as img: diff --git a/uploadblogpost.py b/uploadblogpost.py index 080a1cf..ad064d7 100644 --- a/uploadblogpost.py +++ b/uploadblogpost.py @@ -2,16 +2,18 @@ import requests import base64 import os +from dotenv import load_dotenv +load_dotenv() # Load the CSV file into a pandas dataframe df = pd.read_csv('output.csv') -# WordPress site URL -wp_url = 'YOUR_WEBSITE_URL' +# WordPress site URL +wp_url = os.getenv('WP_URL') # WordPress username and password -username = 'YOUR_WORDPRESS_USERNAME' -password = 'YOUR_WP_APP_PASSWORD' +username = os.getenv('WP_USERNAME') +password = os.getenv('WP_PASSWORD') # Iterate through each row in the CSV file for index, row in df.iterrows(): diff --git a/uploadpage.py b/uploadpage.py index 5762531..9a01f63 100644 --- a/uploadpage.py +++ b/uploadpage.py @@ -2,15 +2,18 @@ import requests import base64 import os +from dotenv import load_dotenv +load_dotenv() # Load the CSV file into a pandas dataframe df = pd.read_csv('output.csv') -wp_url = 'YOUR_WEBSITE_URL' +# WordPress site URL +wp_url = os.getenv('WP_URL') # WordPress username and password -username = 'YOUR_WORDPRESS_USERNAME' -password = 'YOUR_WP_APP_PASSWORD' +username = os.getenv('WP_USERNAME') +password = os.getenv('WP_PASSWORD') # Parent page ID parent_page_id = 10