-
-
Notifications
You must be signed in to change notification settings - Fork 4
/
aibag.py
484 lines (407 loc) · 20.3 KB
/
aibag.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
# AI Blog Article Generator
# Description: A Python script to generate a blog article using the Cohere API based on a given topic or prompt.
# Author: Nakshatra Ranjan Saha
# version: 2.0
import warnings
# Suppress specific Pydantic warnings
warnings.filterwarnings("ignore", category=UserWarning, module="pydantic._internal._config")
import random
from retrying import retry
import cohere
import argparse
from config import COHERE_API_KEY
from colorama import Fore, Style, init
# Initialize colorama
init(autoreset=True)
# Initialize the Cohere API client using the provided API key
co = cohere.Client(api_key=COHERE_API_KEY)
def print_step(step_text):
"""
Print a step message with Cyan color.
"""
print(f"{Fore.CYAN}[*] {step_text}{Style.RESET_ALL}")
def print_success(step_text):
"""
Print a success message with Green color.
"""
print(f"{Fore.GREEN}[+] {step_text}{Style.RESET_ALL}")
def print_warning(step_text):
"""
Print a warning message with Yellow color.
"""
print(f"{Fore.YELLOW}[!] {step_text}{Style.RESET_ALL}")
def print_error(step_text):
"""
Print an error message with Red color.
"""
print(f"{Fore.RED}[X] {step_text}{Style.RESET_ALL}")
@retry(stop_max_attempt_number=3, wait_fixed=2000)
def fetch_blog_content(prompt, max_words=None, min_words=None, language='English'):
"""
Generate a blog article based on the provided prompt using the Cohere API.
Args:
prompt (str): The topic or prompt for the blog article.
max_words (int): The maximum number of words for the blog article.
min_words (int): The minimum number of words for the blog article.
language (str): The language for the blog article (default is English).
Returns:
str: The generated blog content based on the prompt
"""
# Create a detailed prompt for the Cohere API
engineered_prompt = f"""
I want you to act as a professional content writer with expertise in SEO and blog writing. Create a comprehensive 2000-word blog article in {language} on the topic provided. The article should include:
1. An outline with at least 15 headings and subheadings.
2. Detailed, engaging content under each heading.
3. SEO-optimized keywords and a meta description.
Use conversational and human-like writing style, ensuring the content is unique, informative, and engaging. End with a conclusion paragraph and 5 unique FAQs after the conclusion. Bold the title and all headings.
Here is the topic: "{prompt}"
"""
# Add max_words and min_words to the prompt if specified
if max_words:
engineered_prompt += f"\nMaximum Words: {max_words}"
if min_words:
engineered_prompt += f"\nMinimum Words: {min_words}"
# Call the Cohere API to generate the blog content
stream = co.chat_stream(
model='command-r-plus', # Specify the model to be used for generation
message=engineered_prompt, # Pass the engineered prompt to the API
temperature=0.3, # Set the temperature for creativity
chat_history=[], # No prior chat history
prompt_truncation='AUTO' # Handle prompt truncation automatically
)
# Accumulate the generated blog content
blog_content = ""
for event in stream:
if event.event_type == "text-generation":
blog_content += event.text
return blog_content
def generate_image_topics(headline):
"""
Generate image topics based on the provided headline using the Cohere API.
Args:
headline (str): The headline or title for the blog article.
Returns:
str: The generated image topics based on the headline
"""
# Generate image topics based on the headline
topics_prompt = f"""
Generate a list of keywords or topics for images based on the following headline. Provide the topics separated by commas. Here is the headline:
{headline}
"""
try:
response = co.generate(
model='command-r-plus',
prompt=topics_prompt,
max_tokens=50,
temperature=0.5,
)
topics = response.generations[0].text.strip()
except Exception as e:
print_error(f"Failed to generate image topics: {e}")
topics = headline # Fallback to headline as topics if AI fails
return topics
def generate_image_url(meta_keywords):
"""
Generate a random image URL based on the provided meta keywords.
Args:
meta_keywords (str): The meta keywords for the blog article.
Returns:
str: A random image URL based on the meta keywords
"""
# Extract keywords from the meta_keywords
keywords_list = [keyword.strip() for keyword in meta_keywords.split(',')]
# Choose a single random keyword from the list
random_keyword = random.choice(keywords_list) if keywords_list else 'default'
# Get the first word from the random keyword
random_keyword = random_keyword.split()[0]
# Generate the URL with the selected keyword
return f"https://loremflickr.com/800/600/{random_keyword.replace(' ', ',')}"
def generate_meta_keywords(content):
"""
Generate SEO meta keywords based on the provided content using the Cohere API.
Args:
content (str): The blog content for generating meta keywords.
Returns:
str: The generated SEO meta keywords based on the content
"""
# Generate SEO meta keywords from the content
keywords_prompt = f"""
Generate a list of SEO keywords relevant to the following blog content. The keywords should be separated by commas and should be highly relevant to the content. Here is the content:
{content}
"""
try:
response = co.generate(
model='command-r-plus',
prompt=keywords_prompt,
max_tokens=50,
temperature=0.5,
)
keywords = response.generations[0].text.strip()
except Exception as e:
print_error(f"Failed to generate keywords: {e}")
keywords = "default, keywords, here"
return keywords
def github_readme_font(content):
"""
Convert the blog content into GitHub README specific font formatting.
Args:
content (str): The blog content to be converted.
Returns:
str: The blog content formatted in Markdown suitable for a GitHub README file.
"""
# Generate GitHub README specific font formatting
readme_prompt = f"""
Convert the following blog content into GitHub README style formatting. The content should be formatted in Markdown suitable for a GitHub README file. Here is the content:
{content}
"""
try:
response = co.generate(
model='command-r-plus',
prompt=readme_prompt,
max_tokens=1000,
temperature=0.5,
)
readme_content = response.generations[0].text.strip()
except Exception as e:
print_error(f"Failed to generate README formatting: {e}")
readme_content = content
return readme_content
try:
# Log step: Starting blog content generation
print_step(f"Generating blog content for the topic: {prompt}")
# Fetch blog content with retry
blog_content = fetch_blog_content(prompt, max_words, min_words, language)
# Log step: Cleaning up blog content
print_step("Cleaning up the generated blog content...")
# Clean up the blog content by removing unwanted prefixes and adjusting markdown formatting
blog_content = blog_content.replace("## Outline:", "").strip()
blog_content = blog_content.replace("## Article:", "").strip()
blog_content = blog_content.replace("#### H4:", "####")
blog_content = blog_content.replace("### H3:", "###")
blog_content = blog_content.replace("## H2:", "##")
blog_content = blog_content.replace("# H1:", "#")
# Ensure the first line is a top-level heading
if not blog_content.startswith("# "):
blog_content = f"# {prompt}\n\n" + blog_content
# Remove trailing punctuation from headings
lines = blog_content.split('\n')
for i in range(len(lines)):
if lines[i].startswith('#'):
lines[i] = lines[i].rstrip(':')
# Generate meta keywords
meta_keywords = generate_meta_keywords(blog_content)
# Replace section headings with image placeholders from loremflickr.com
for i, line in enumerate(lines):
if line.startswith('# '): # Heading line
section_title = line[2:] # Remove the '# ' prefix
image_topics = generate_image_topics(section_title)
image_url = generate_image_url(image_topics, meta_keywords)
lines[i] = f'{line}\n![Image]({image_url})'
# Join the lines to form the final Markdown content
markdown_content = '\n'.join(lines)
# Generate SEO meta description
description_prompt = f"Generate a brief and relevant meta description for this content. Just give the meta description that is SEO friendly and relevant, don't give any extra words, or any prefix or suffix. Here is the content:\n{markdown_content}"
try:
description_response = co.generate(
model='command-r-plus',
prompt=description_prompt,
max_tokens=50,
temperature=0.5,
)
description = description_response.generations[0].text.strip()
except Exception as e:
print_error(f"Failed to generate description: {e}")
description = "Default SEO description"
# Convert to GitHub README style if requested
if output_format.lower() == 'github':
markdown_content = github_readme_font(markdown_content)
# Log step: Creating the output file
print_step(f"Creating the output file in {output_format} format...")
# Generate the output file based on the requested format
try:
if output_format.lower() == 'html':
output_file = f"{file_name or prompt}.html"
with open(output_file, 'w', encoding='utf-8') as f:
f.write('<!DOCTYPE html>\n')
f.write('<html lang="en">\n<head>\n')
f.write(f'<meta charset="UTF-8">\n')
f.write(f'<meta name="description" content="{description}">\n')
f.write(f'<meta name="keywords" content="{meta_keywords}">\n')
f.write('<meta name="viewport" content="width=device-width, initial-scale=1.0">\n')
f.write(f'<title>{prompt}</title>\n')
f.write('</head>\n<body>\n')
f.write('<h1>' + prompt + '</h1>\n')
f.write('<markdown>\n')
f.write(markdown_content)
f.write('\n</markdown>\n')
f.write('<script src="https://cdn.jsdelivr.net/gh/OCEANOFANYTHINGOFFICIAL/mdonhtml.js/scripts/mdonhtml.min.js"></script>\n')
f.write('\n</body>\n</html>')
elif output_format.lower() in ['md', 'github']:
output_file = f"{file_name or prompt}.md"
with open(output_file, 'w', encoding='utf-8') as f:
f.write(markdown_content)
else:
print_error(f"Invalid output format: {output_format}")
except Exception as e:
print_error(f"Failed to save the blog content: {e}")
except Exception as e:
print_error(f"Failed to generate the blog: {e}")
def generate_blog(prompt, max_words=None, min_words=None, output_format='HTML', file_name=None, language='English'):
"""
Generate a blog article based on the provided prompt and save it to an output file.
Args:
prompt (str): The topic or prompt for the blog article.
max_words (int): The maximum number of words for the blog article.
min_words (int): The minimum number of words for the blog article.
output_format (str): The output format for the blog article (HTML, Markdown, GitHub).
file_name (str): The name of the output file to be generated.
language (str): The language for the blog article (default is English).
Returns:
None
"""
try:
# Log step: Starting blog content generation
print_step(f"Generating blog content for the topic: {prompt}")
# Fetch blog content with retry
blog_content = fetch_blog_content(prompt, max_words, min_words, language)
print_success("Blog content generated successfully!")
# Log step: Cleaning up blog content
print_step("Cleaning up the generated blog content...")
# Clean up the blog content by removing unwanted prefixes and adjusting markdown formatting
blog_content = blog_content.replace("## Outline:", "").strip()
blog_content = blog_content.replace("## Article:", "").strip()
blog_content = blog_content.replace("#### H4:", "####")
blog_content = blog_content.replace("### H3:", "###")
blog_content = blog_content.replace("## H2:", "##")
blog_content = blog_content.replace("# H1:", "#")
print_success("Blog content cleaned up successfully!")
# Ensure the first line is a top-level heading
if not blog_content.startswith("# "):
blog_content = f"# {prompt}\n\n" + blog_content
# Remove trailing punctuation from headings
lines = blog_content.split('\n')
for i in range(len(lines)):
if lines[i].startswith('#'):
lines[i] = lines[i].rstrip(':')
# Replace section headings with image placeholders from loremflickr.com
print_step("Generating & inserting image into the blog...")
try:
for i, line in enumerate(lines):
if line.startswith('# '): # Heading line
section_title = line[2:] # Remove the '# ' prefix
meta_keywords = generate_meta_keywords(blog_content)
image_url = generate_image_url(meta_keywords)
lines[i] = f'{line}\n![Image]({image_url})'
# Join the lines to form the final Markdown content
markdown_content = '\n'.join(lines)
print_success("Image generated and inserted successfully!")
except Exception as e:
print_error(f"Failed to generate and insert image: {e}")
print_warning("Continuing without inserting images...")
pass
# Generate SEO meta description
print_step(f"Generating SEO meta description for the blog: {prompt}")
description_prompt = f"Generate a brief and relevant meta description for this content. Just give the meta description that is SEO friendly and relevant, don't give any extra words, or any prefix or suffix. Here is the content:\n{markdown_content}"
try:
description_response = co.generate(
model='command-r-plus',
prompt=description_prompt,
max_tokens=50,
temperature=0.5,
)
description = description_response.generations[0].text.strip()
print_success("SEO meta description generated successfully!")
except Exception as e:
print_error(f"Failed to generate description: {e}")
# Fallback to a default description which is the title of the blog separated by commas
description = str(prompt)
print_warning(f"Using the default description: {description}")
# Generate meta keywords
print_step(f"Generating meta keywords for the blog: {prompt}")
try:
meta_keywords = generate_meta_keywords(markdown_content)
print_success("Meta keywords generated successfully!")
except Exception as e:
print_error(f"Failed to generate keywords: {e}")
meta_keywords = ', '.join(prompt.split())
print_warning(f"Using the blog title as meta keywords: {meta_keywords}")
# Convert to GitHub README style if requested
if output_format.lower() == 'github':
try:
markdown_content = github_readme_font(markdown_content)
print_success("GitHub README formatting applied successfully!")
except Exception as e:
print_error(f"Failed to apply GitHub README formatting: {e}")
print_warning("Continuing without GitHub README formatting...")
pass
# Log step: Creating the output file
print_step(f"Creating the output file in {output_format} format...")
# Generate the output file based on the requested format
try:
if output_format.lower() == 'html':
output_file = f"{file_name or prompt}.html"
with open(output_file, 'w', encoding='utf-8') as f:
# Format the HTML content with the generated blog Markdown content.
# IMPORTANT: Dont change the format of the HTML content. It is required for perfect rendering and indentation of the blog content.
f.write(f"""<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="description" content="{description}">
<meta name="keywords"content="{meta_keywords}">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>{prompt}</title>""")
f.write("""
<style>
body {
font-family: -apple-system, BlinkMacSystemFont, "Segoe UI", Roboto, "Helvetica Neue", Arial, sans-serif;
}
</style>""")
f.write(f"""
</head>
<body>
<markdown>
{markdown_content}
</markdown>
<script src="https://cdn.jsdelivr.net/gh/OCEANOFANYTHINGOFFICIAL/mdonhtml.js/scripts/mdonhtml.min.js"></script>
</body>
</html>""")
print_success(f"Blog content saved to: {output_file}")
elif output_format.lower() in ['md', 'github']:
try:
output_file = f"{file_name or prompt}.md"
with open(output_file, 'w', encoding='utf-8') as f:
f.write(markdown_content)
print_success(f"Blog content saved to: {output_file}")
except Exception as e:
print_error(f"Failed to save the blog content: {e}")
else:
print_error(f"Invalid output format: {output_format}")
except Exception as e:
print_error(f"Failed to save the blog content: {e}")
except Exception as e:
print_error(f"Failed to generate the blog: {e}")
def main():
"""
Main function to parse command-line arguments and generate a blog article.
"""
# Set up argument parser for command-line interface
parser = argparse.ArgumentParser(description='AI Blog Generator')
parser.add_argument('topic', type=str, help='Topic of the blog') # Required argument for blog topic
parser.add_argument('-mw', '--max_words', type=int, help='Maximum number of words') # Optional max words argument
parser.add_argument('-mnw', '--min_words', type=int, help='Minimum number of words') # Optional min words argument
parser.add_argument('-of', '--output_format', type=str, choices=['HTML', 'Markdown', 'md', 'github'], default='HTML', help='Output format (HTML, Markdown, md, GitHub)') # Optional output format argument
parser.add_argument('-fn', '--file_name', type=str, help='Output file name') # Optional file name argument
parser.add_argument('-l', '--language', type=str, default='English', help='Language of the article') # Optional language argument
parser.add_argument('-gr', '--github_readme', action='store_true', help='Convert content to GitHub README format') # Small flag for GitHub README formatting
args = parser.parse_args()
# Ensure that at least one of max_words or min_words is provided
if not args.max_words and not args.min_words:
parser.error('At least one of --max_words or --min_words is required.')
# Check if the GitHub README formatting flag is set
if args.github_readme:
args.output_format = 'github'
# Generate the blog based on parsed arguments
generate_blog(args.topic, args.max_words, args.min_words, args.output_format, args.file_name, args.language)
if __name__ == '__main__':
main()