Skip to content

Commit

Permalink
0.6.2
Browse files Browse the repository at this point in the history
  • Loading branch information
mikavehns committed Jan 8, 2023
1 parent 6e4ab95 commit f4a6ea4
Show file tree
Hide file tree
Showing 4 changed files with 52 additions and 40 deletions.
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,7 @@ https://user-images.githubusercontent.com/66560242/210459589-751c82d7-e874-4119-
- The program may take some time to run, depending on the specified parameters and the performance of the GPT-3 API. Please be patient while the book is being generated.
- The program may not always generate the wished amount of words for each chapter. This can happen, if there is not enough data available for the specified topic.
- Currently, it is only possible to generate Non-Fiction books.
- Since this is a really early version (v0.6.0), there are many missing features, that will be added by time
- Since this is a really early version (v0.6.2), there are many missing features, that will be added by time


## License
Expand Down
4 changes: 3 additions & 1 deletion requirements.txt
Original file line number Diff line number Diff line change
@@ -1,2 +1,4 @@
openai~=0.25.0
pyfiglet~=0.8.post1
pyfiglet~=0.8.post1
markdown~=3.4.1
pdfkit~=1.0.0
85 changes: 48 additions & 37 deletions src/book.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import openai
import os
import markdown
import pdfkit
from categories import *


Expand Down Expand Up @@ -38,6 +39,8 @@ def __init__(self, chapter_amount: int, words_per_chapter: int, topic: str, cate
# Define category
self.category = category

print('Initializing..')

# Get book using `get_category` method
self.book = self.get_category()

Expand All @@ -50,15 +53,6 @@ def __init__(self, chapter_amount: int, words_per_chapter: int, topic: str, cate
# Set the structure of the book
self.structure = self.get_structure()

def __str__(self):
"""
This method returns the book as a string.
:return: The book as a string.
"""

# Return the combined chapters
return self.combine()

@staticmethod
def __get_edit(input_text, instruction, temperature: float = 0):
"""
Expand Down Expand Up @@ -229,15 +223,41 @@ def get_content(self):
# Return the content
return content

def combine(self):
def generate(self):
"""
This method combines the title, chapter titles, and structure of the book.
:return: The combined book.
This method generates the book.
"""

# Get the content of the book
# Get content
content = self.get_content()

# Pack book
book = {'title': self.title, 'chapters': content, 'chapter_titles': self.chapter_titles, 'structure': self.structure}

# Return the book
self.packed = book

def get_md(self):
"""
This method returns the book in Markdown format.
"""

# Check if variable packed is exists
if self.packed is None:
print('Book not generated yet. Generating book...')
self.generate()

# Get the book
book = self.packed

# Define the variables
title = book['title']
content = book['chapters']
chapter_titles = book['chapter_titles']
structure = book['structure']

# Build the book

# Add the title of the book
book = '# ' + self.title + '\n\n\n'

Expand All @@ -263,33 +283,24 @@ def combine(self):
# Return the combined book.
return book

def save_md(self):
def get_html(self):
"""
This method saves the book as a text file.
This method returns the book in HTML format.
"""

# Check if the directory to save the book in exists
if not os.path.exists('books'):

# If it does not exist, create the directory
os.mkdir('books')
# Getting Markdown element
md = markdown.markdown(self.get_md())

# Define the variable for the file name as the title of the book with all spaces replaced by underscores
file_name = (self.title + '.txt').replace(' ', '_').replace('"', '').replace('?', '')
# Return the HTML
return md

# Checking the file name has a colon
if ':' in file_name:
# If it does, split the file name at the colon and set the file name as the first part
file_name = file_name.split(':')[0]

# Define the file path as the file name plus the directory to save the book in
path = 'books/' + file_name + '.md'

# Open the file
with open(path, 'w') as f:
def get_pdf(self):
"""
This method returns the book in PDF format.
"""

# Write the book to the file
f.write(str(self))
# Get the HTML
html = self.get_html()

# Print the file path
print(f'Book saved as {path}.')
# Return the PDF
return pdfkit.from_string(html, False)
1 change: 0 additions & 1 deletion src/run.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@
from utils import *
import json
import openai
import os


# Get the OpenAI API key from the config file
Expand Down

0 comments on commit f4a6ea4

Please sign in to comment.