Created
April 26, 2023 20:47
-
-
Save martinbowling/7995aac46c1d091fa24df268a72d770d to your computer and use it in GitHub Desktop.
Rudimentary Article Generator with gpt 3.5 turbo api
This file contains hidden or 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
import openai | |
import os | |
# Set up the API key | |
openai.api_key = "your_openai_api_key" | |
# Function to call GPT-3.5 Turbo API with chat completion | |
def generate_text(messages): | |
response = openai.ChatCompletion.create( | |
model="gpt-3.5-turbo", | |
messages=messages, | |
) | |
return response.choices[0].text.strip() | |
# Ask for topic | |
topic = input("Enter a topic: ") | |
# Generate outline | |
messages = [ | |
{ | |
"role": "system", | |
"content": "You are ChatGPT, a large language model trained by OpenAI. Create an outline and content for articles on a given topic.", | |
}, | |
{"role": "user", "content": f"Create an outline for an article on the topic: {topic}"}, | |
] | |
outline = generate_text(messages) | |
print("Outline:") | |
print(outline) | |
# Generate content for each section | |
content = "" | |
sections = outline.split("\n") | |
for section in sections: | |
messages.append({"role": "user", "content": f"Write content for the following section: {section}"}) | |
section_content = generate_text(messages) | |
content += f"<h2>{section}</h2>\n<p>{section_content}</p>\n" | |
# Combine content into an HTML file | |
html_template = f"""<!DOCTYPE html> | |
<html lang="en"> | |
<head> | |
<meta charset="UTF-8"> | |
<meta name="viewport" content="width=device-width, initial-scale=1.0"> | |
<title>{topic}</title> | |
</head> | |
<body> | |
<h1>{topic}</h1> | |
{content} | |
</body> | |
</html> | |
""" | |
# Save the HTML file | |
with open("output.html", "w") as f: | |
f.write(html_template) | |
print("Content generated and saved to output.html") |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment