반응형
[ChatGPT] Automate blog creation with OPENAI API (ChatGPT)
# Reviewing other people's code
https://teddylee777.github.io/python/chatgpt-blog-automation/
It is for my son's education.
(For middle and high school students )
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
''' | |
[ChatGPT] Automate blog creation with OPENAI API (ChatGPT) | |
# Reviewing other people's code | |
https://teddylee777.github.io/python/chatgpt-blog-automation/ | |
It is for my son's education. | |
(For middle and high school students ) | |
''' | |
import openai | |
import re | |
import os | |
from datetime import datetime, timedelta | |
class BlogGenerator: | |
def __init__(self): | |
self.api_key = 'your-key' | |
self.model_engine = "text-davinci-003" | |
self.max_tokens = 2048 | |
self.temperature = 0.3 | |
self.top_p = 1 | |
self.frequency_penalty = 0 | |
self.presence_penalty = 0 | |
self.hashtag_pattern = r'(#+[a-zA-Z0-9(_)]{1,})' | |
self.blog_directory = r"blog_path/_posts" | |
self.category = "travel" | |
self.age_range = "20-40" | |
def generate_blog(self, topic): | |
prompt = f''' | |
Write blog posts in markdown format. | |
Write the theme of your blog as "{topic}". | |
Highlight, bold, or italicize important words or sentences. | |
Please include the restaurant's address, menu recommendations and other helpful information(opening and closing hours) as a list style. | |
Please make the entire blog less than 10 minutes long. | |
The audience of this article is {self.age_range} years old. | |
Create several hashtags and add them only at the end of the line. | |
Add a summary of the entire article at the beginning of the blog post. | |
''' | |
openai.api_key = self.api_key | |
completion = openai.Completion.create( | |
engine=self.model_engine, | |
prompt=prompt, | |
max_tokens=self.max_tokens, | |
temperature=self.temperature, | |
top_p=self.top_p, | |
frequency_penalty=self.frequency_penalty, | |
presence_penalty=self.presence_penalty | |
) | |
return completion.choices[0].text | |
def extract_hashtags(self, text): | |
hashtags = [w[1:] for w in re.findall(self.hashtag_pattern, text)] | |
tag_string = "" | |
for w in hashtags: | |
if len(w) > 3: | |
tag_string += f'{w}, ' | |
tag_string = re.sub(r'[^a-zA-Z, ]', '', tag_string) | |
tag_string = tag_string.strip()[:-1] | |
return tag_string | |
def generate_output(self, topic, text): | |
tag_string = self.extract_hashtags(text) | |
page_head = f'''--- | |
layout: single | |
title: "{topic}" | |
categories: {self.category} | |
tag: [{tag_string}] | |
toc: false | |
author_profile: false | |
sidebar: | |
nav: "counts" | |
--- | |
''' | |
body = '\n'.join(text.strip().split('\n')[1:]) | |
return page_head + body | |
def create_blog_file(self, topic, output): | |
yesterday = datetime.now() - timedelta(days=1) | |
timestring = yesterday.strftime('%Y-%m-%d') | |
filename = f"{timestring}-{'-'.join(topic.lower().split())}.md" | |
if not os.path.exists(self.blog_directory): | |
os.makedirs(self.blog_directory) | |
filepath = os.path.join(self.blog_directory, filename) | |
with open(filepath, 'w') as f: | |
f.write(output) | |
f.close() | |
def generate_blocks(self, topic): | |
text = self.generate_blog(topic) | |
output = self.generate_output(topic, text) | |
print(f'output={output}') | |
self.create_blog_file(topic, output) | |
if __name__=="__main__" : | |
blog_generator = BlogGenerator() | |
city = "New York" | |
topic = f"Top 10 Restaurants you must visit when traveling to {city}" | |
blog_generator.generate_blocks(topic) | |
print("Thank you!") | |
반응형
'chatgpt > coding with ai' 카테고리의 다른 글
[Review other people's prompts - try autogen] - 그댄 행복에 살텐데 (0) | 2023.10.12 |
---|---|
[Review other people's prompts - try autogpt] - Stairway To Heaven (0) | 2023.10.12 |
Coding to chatgpt (0) | 2023.02.10 |
[golang] code2GPT (0) | 2023.02.09 |
[c++] code2GPT (0) | 2023.02.07 |