forked from Bklieger/infinite-bookshelf
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.py
207 lines (175 loc) · 6.87 KB
/
main.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
# 1: Import libraries
import streamlit as st
from groq import Groq
import json
import os
import io
import ebooklib
from ebooklib import epub
from docx import Document
from infinite_bookshelf.agents import (
generate_section,
generate_book_structure,
generate_book_title,
)
from infinite_bookshelf.inference import GenerationStatistics
from infinite_bookshelf.tools import create_markdown_file, create_pdf_file
from infinite_bookshelf.ui.components import (
render_groq_form,
display_statistics,
)
from infinite_bookshelf.ui import Book, load_return_env, ensure_states
# Utility Functions for Book Export
def create_epub_file(book):
"""Create an ePub file from the book content"""
epub_book = epub.EpubBook()
epub_book.set_title(book.book_title)
epub_book.set_language('en')
# Create chapters
chapters = []
for title, content in book.contents.items():
chapter = epub.EpubHtml(title=title, file_name=f'{title}.xhtml', lang='en')
chapter.set_content(f'<h1>{title}</h1><p>{content}</p>')
epub_book.add_item(chapter)
chapters.append(chapter)
# Create Table of Contents
epub_book.toc = tuple(chapters)
epub_book.add_item(epub.EpubNcx())
epub_book.add_item(epub.EpubNav())
# Basic styling
style = 'BODY {color: white;}'
nav_css = epub.EpubItem(uid="style_nav", file_name="style/nav.css", media_type="text/css", content=style)
epub_book.add_item(nav_css)
# Write ePub
epub_buffer = io.BytesIO()
epub.write_epub(epub_buffer, epub_book, {})
epub_buffer.seek(0)
return epub_buffer
def create_docx_file(book):
"""Create a DOCX file from the book content"""
doc = Document()
doc.add_heading(book.book_title, level=1)
for title, content in book.contents.items():
doc.add_heading(title, level=2)
doc.add_paragraph(content)
docx_buffer = io.BytesIO()
doc.save(docx_buffer)
docx_buffer.seek(0)
return docx_buffer
def get_available_groq_models():
"""Fetch available Groq models"""
try:
groq = Groq()
models = groq.models.list()
return [model.id for model in models.data]
except Exception as e:
st.error(f"Could not fetch Groq models: {e}")
return ["llama3-8b-8192", "llama3-70b-8192", "gemma-7b-it"]
def advanced_book_generation():
"""Advanced book generation workflow"""
st.title("Advanced Book Generation")
# Model Selection
available_models = get_available_groq_models()
col1, col2, col3 = st.columns(3)
with col1:
title_model = st.selectbox("Title Generation Model", available_models)
with col2:
structure_model = st.selectbox("Structure Generation Model", available_models)
with col3:
content_model = st.selectbox("Content Generation Model", available_models)
# Book Topic and Instructions
topic = st.text_input("Book Topic")
additional_instructions = st.text_area("Additional Instructions")
# Generation Workflow
if st.button("Generate Book"):
with st.spinner("Generating Book..."):
# Title Generation
book_title = generate_book_title(
prompt=topic,
model=title_model,
groq_provider=Groq()
)
# Structure Generation
_, book_structure = generate_book_structure(
prompt=topic,
additional_instructions=additional_instructions,
model=structure_model,
groq_provider=Groq()
)
# Create Book Object
book_structure_json = json.loads(book_structure)
book = Book(book_title, book_structure_json)
# Content Generation
def generate_book_content(sections):
for title, content in sections.items():
if isinstance(content, str):
generated_content = generate_section(
prompt=f"{title}: {content}",
additional_instructions=additional_instructions,
model=content_model,
groq_provider=Groq()
)
book.update_content(title, generated_content)
elif isinstance(content, dict):
generate_book_content(content)
generate_book_content(book_structure_json)
# Display Book
st.write(f"# {book.book_title}")
book.display_structure()
# Export Options
st.subheader("Export Book")
col1, col2, col3, col4 = st.columns(4)
with col1:
if st.download_button(
label="Download PDF",
data=create_pdf_file(book.get_markdown_content()),
file_name=f"{book_title}.pdf"
):
st.success("PDF Downloaded")
with col2:
if st.download_button(
label="Download ePub",
data=create_epub_file(book),
file_name=f"{book_title}.epub"
):
st.success("ePub Downloaded")
with col3:
if st.download_button(
label="Download DOCX",
data=create_docx_file(book),
file_name=f"{book_title}.docx"
):
st.success("DOCX Downloaded")
with col4:
if st.download_button(
label="Download Markdown",
data=create_markdown_file(book.get_markdown_content()),
file_name=f"{book_title}.md"
):
st.success("Markdown Downloaded")
# Edit Book Option
st.subheader("Edit Book")
edit_mode = st.checkbox("Enable Editing")
if edit_mode:
for title in book.contents.keys():
st.subheader(title)
edited_content = st.text_area(
f"Edit {title}",
value=book.contents[title]
)
if st.button(f"Save Changes for {title}"):
book.update_content(title, edited_content)
st.success(f"Updated {title}")
def main():
st.sidebar.title("Infinite Bookshelf")
# Navigation
page = st.sidebar.radio(
"Choose a Page",
["Advanced Book Generation", "Simple Book Generation"]
)
if page == "Advanced Book Generation":
advanced_book_generation()
else:
st.write("Simple Book Generation (To be implemented)")
if __name__ == "__main__":
main()