forked from Bklieger/infinite-bookshelf
-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.py
457 lines (389 loc) · 17.7 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
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
import streamlit as st
# from groq import Groq
from openai import Client
import json
import os
from io import BytesIO
from markdown import markdown
from weasyprint import HTML, CSS
from dotenv import load_dotenv
from half_json.core import JSONFixer
# load .env file to environment
load_dotenv()
API_KEY = os.getenv("OPENAI_API_KEY", None)
MODEL_NAME = os.getenv("OPENAI_MODEL_NAME", "gpt-4-turbo")
json_fixer = JSONFixer()
if 'api_key' not in st.session_state:
st.session_state.api_key = API_KEY
if 'client' not in st.session_state:
if API_KEY:
st.session_state.client = Client(api_key=API_KEY)
class GenerationStatistics:
def __init__(self, input_time=0,output_time=0,input_tokens=0,output_tokens=0,total_time=0,model_name=MODEL_NAME):
self.input_time = input_time
self.output_time = output_time
self.input_tokens = input_tokens
self.output_tokens = output_tokens
self.total_time = total_time # Sum of queue, prompt (input), and completion (output) times
self.model_name = model_name
def get_input_speed(self):
"""
Tokens per second calculation for input
"""
if self.input_time != 0:
return self.input_tokens / self.input_time
else:
return 0
def get_output_speed(self):
"""
Tokens per second calculation for output
"""
if self.output_time != 0:
return self.output_tokens / self.output_time
else:
return 0
def add(self, other):
"""
Add statistics from another GenerationStatistics object to this one.
"""
if not isinstance(other, GenerationStatistics):
raise TypeError("Can only add GenerationStatistics objects")
self.input_time += other.input_time
self.output_time += other.output_time
self.input_tokens += other.input_tokens
self.output_tokens += other.output_tokens
self.total_time += other.total_time
def __str__(self):
return (f"\n## {self.get_output_speed():.2f} T/s ⚡\nRound trip time: {self.total_time:.2f}s Model: {self.model_name}\n\n"
f"| Metric | Input | Output | Total |\n"
f"|-----------------|----------------|-----------------|----------------|\n"
f"| Speed (T/s) | {self.get_input_speed():.2f} | {self.get_output_speed():.2f} | {(self.input_tokens + self.output_tokens) / self.total_time if self.total_time != 0 else 0:.2f} |\n"
f"| Tokens | {self.input_tokens} | {self.output_tokens} | {self.input_tokens + self.output_tokens} |\n"
f"| Inference Time (s) | {self.input_time:.2f} | {self.output_time:.2f} | {self.total_time:.2f} |")
class Book:
def __init__(self, book_title, structure):
self.book_title = book_title
self.structure = structure
self.contents = {title: "" for title in self.flatten_structure(structure)}
self.placeholders = {title: st.empty() for title in self.flatten_structure(structure)}
st.markdown(f"# {self.book_title}")
st.markdown("## Generating the following:")
toc_columns = st.columns(4)
self.display_toc(self.structure, toc_columns)
st.markdown("---")
def flatten_structure(self, structure):
sections = []
for title, content in structure.items():
sections.append(title)
if isinstance(content, dict):
sections.extend(self.flatten_structure(content))
return sections
def update_content(self, title, new_content):
try:
self.contents[title] += new_content
self.display_content(title)
except TypeError as e:
pass
def display_content(self, title):
if self.contents[title].strip():
self.placeholders[title].markdown(f"## {title}\n{self.contents[title]}")
def display_structure(self, structure=None, level=1):
if structure is None:
structure = self.structure
for title, content in structure.items():
if self.contents[title].strip(): # Only display title if there is content
st.markdown(f"{'#' * level} {title}")
self.placeholders[title].markdown(self.contents[title])
if isinstance(content, dict):
self.display_structure(content, level + 1)
def display_toc(self, structure, columns, level=1, col_index=0):
for title, content in structure.items():
with columns[col_index % len(columns)]:
st.markdown(f"{' ' * (level-1) * 2}- {title}")
col_index += 1
if isinstance(content, dict):
col_index = self.display_toc(content, columns, level + 1, col_index)
return col_index
def get_markdown_content(self, structure=None, level=1):
"""
Returns the markdown styled pure string with the contents.
"""
if structure is None:
structure = self.structure
if level==1:
markdown_content = f"# {self.book_title}\n\n"
else:
markdown_content = ""
for title, content in structure.items():
if self.contents[title].strip(): # Only include title if there is content
markdown_content += f"{'#' * level} {title}\n{self.contents[title]}\n\n"
if isinstance(content, dict):
markdown_content += self.get_markdown_content(content, level + 1)
return markdown_content
def create_markdown_file(content: str) -> BytesIO:
"""
Create a Markdown file from the provided content.
"""
markdown_file = BytesIO()
markdown_file.write(content.encode('utf-8'))
markdown_file.seek(0)
return markdown_file
def create_pdf_file(content: str) -> BytesIO:
"""
Create a PDF file from the provided Markdown content.
Converts Markdown to styled HTML, then HTML to PDF.
"""
html_content = markdown(content, extensions=['extra', 'codehilite'])
styled_html = f"""
<html>
<head>
<style>
@page {{
margin: 2cm;
}}
body {{
font-family: Arial, sans-serif;
line-height: 1.6;
font-size: 12pt;
}}
h1, h2, h3, h4, h5, h6 {{
color: #333366;
margin-top: 1em;
margin-bottom: 0.5em;
}}
p {{
margin-bottom: 0.5em;
}}
code {{
background-color: #f4f4f4;
padding: 2px 4px;
border-radius: 4px;
font-family: monospace;
font-size: 0.9em;
}}
pre {{
background-color: #f4f4f4;
padding: 1em;
border-radius: 4px;
white-space: pre-wrap;
word-wrap: break-word;
}}
blockquote {{
border-left: 4px solid #ccc;
padding-left: 1em;
margin-left: 0;
font-style: italic;
}}
table {{
border-collapse: collapse;
width: 100%;
margin-bottom: 1em;
}}
th, td {{
border: 1px solid #ddd;
padding: 8px;
text-align: left;
}}
th {{
background-color: #f2f2f2;
}}
</style>
</head>
<body>
{html_content}
</body>
</html>
"""
pdf_buffer = BytesIO()
HTML(string=styled_html).write_pdf(pdf_buffer)
pdf_buffer.seek(0)
return pdf_buffer
def generate_book_title(prompt: str):
"""
Generate a book title using AI.
"""
completion = st.session_state.client.chat.completions.create(
model=MODEL_NAME,
messages=[
{
"role": "system",
"content": "Generate suitable book titles for the provided topics. There is only one generated book title! Don't give any explanation or add any symbols, just write the title of the book. The requirement for this title is that it must be between 7 and 25 words long, and it must be attractive enough!"
},
{
"role": "user",
"content": f"Generate a book title for the following topic. There is only one generated book title! Don't give any explanation or add any symbols, just write the title of the book. The requirement for this title is that it must be at least 7 words and 25 words long, and it must be attractive enough:\n\n{prompt}"
}
],
temperature=0.7,
max_tokens=100,
top_p=1,
stream=False,
stop=None,
)
return completion.choices[0].message.content.strip()
def generate_book_structure(prompt: str):
"""
Returns book structure content as well as total tokens and total time for generation.
"""
completion = st.session_state.client.chat.completions.create(
model=MODEL_NAME,
messages=[
{
"role": "system",
"content": "Write in JSON format: {\"Title of section goes here\":\"Description of section goes here\",\"Title of section goes here\":{\"Title of section goes here\":\"Description of section goes here\",\"Title of section goes here\":\"Description of section goes here\",\"Title of section goes here\":\"Description of section goes here\"}}"
},
{
"role": "user",
"content": f"Write a comprehensive structure in JSON format, omiting introduction and conclusion sections (forward, author's note, summary), for a short (3 page) book on the following subject:\n\n<subject>Data Structures and Algorithms in Java</subject>"
},
{
"role": "assistant",
"content": '{ "Data Structures and Algorithms in Java" : {"What is Data Structure?" : "Description of Data Structures and Their Importance", "Why Java for Data Structures?" : "Reasons for choosing Java for data structures and algorithms" , "Overview of the Book" : { "Java Basics":"Review of Java Syntax and Basics" } } }'
},
{
"role": "user",
"content": f"Write a comprehensive structure in JSON format, Do not use ``` to wrap, omiting introduction and conclusion sections (forward, author's note, summary), for a long (>300 page) book on the following subject:\n\n<subject>{prompt}</subject>"
}
],
temperature=0.3,
max_tokens=8000,
top_p=1,
stream=False,
response_format={"type": "json_object"},
stop=None,
)
usage = completion.usage
statistics_to_return = GenerationStatistics(input_time=getattr(usage,"prompt_time",0), output_time=getattr(usage,"completion_time",0), input_tokens=usage.prompt_tokens, output_tokens=usage.completion_tokens, total_time=getattr(usage,"total_time",0),model_name=MODEL_NAME)
return statistics_to_return, completion.choices[0].message.content
def generate_section(prompt: str):
stream = st.session_state.client.chat.completions.create(
model=MODEL_NAME,
messages=[
{
"role": "system",
"content": "You are an expert writer. Generate a long, comprehensive, structured chapter for the section provided."
},
{
"role": "user",
"content": f"Generate a long, comprehensive, structured chapter for the following section:\n\n<section_title>{prompt}</section_title>"
}
],
temperature=0.3,
max_tokens=8000,
top_p=1,
stream=True,
stop=None,
)
for chunk in stream:
tokens = chunk.choices[0].delta.content
if tokens:
yield tokens
if x_groq := getattr(chunk,"x_groq",None):
if not x_groq.usage:
continue
usage = x_groq.usage
statistics_to_return = GenerationStatistics(input_time=usage.prompt_time, output_time=usage.completion_time, input_tokens=usage.prompt_tokens, output_tokens=usage.completion_tokens, total_time=usage.total_time,model_name=MODEL_NAME)
yield statistics_to_return
# Initialize
if 'button_disabled' not in st.session_state:
st.session_state.button_disabled = False
if 'button_text' not in st.session_state:
st.session_state.button_text = "Generate"
if 'statistics_text' not in st.session_state:
st.session_state.statistics_text = ""
if 'book_title' not in st.session_state:
st.session_state.book_title = ""
st.write("""
# LlamaEdgeBook: Write full books using Open source LLMs
""")
def disable():
st.session_state.button_disabled = True
def enable():
st.session_state.button_disabled = False
def empty_st():
st.empty()
try:
if st.button('End Generation and Download Book'):
if "book" in st.session_state:
# Create markdown file
markdown_file = create_markdown_file(st.session_state.book.get_markdown_content())
st.download_button(
label='Download Text',
data=markdown_file,
file_name=f'{st.session_state.book_title}.txt',
mime='text/plain'
)
# Create pdf file (styled)
pdf_file = create_pdf_file(st.session_state.book.get_markdown_content())
st.download_button(
label='Download PDF',
data=pdf_file,
file_name=f'{st.session_state.book_title}.pdf',
mime='application/pdf'
)
else:
raise ValueError("Please generate content first before downloading the book.")
with st.form("groqform"):
if not API_KEY:
groq_input_key = st.text_input("Enter your API Key (gsk_yA...):", "",type="password")
topic_text = st.text_input("What do you want the book to be about?", "")
# Generate button
submitted = st.form_submit_button(st.session_state.button_text,on_click=disable,disabled=st.session_state.button_disabled)
# Statistics
placeholder = st.empty()
def display_statistics():
with placeholder.container():
if st.session_state.statistics_text:
if "Generating structure in background" not in st.session_state.statistics_text:
st.markdown(st.session_state.statistics_text+"\n\n---\n") # Format with line if showing statistics
else:
st.markdown(st.session_state.statistics_text)
else:
placeholder.empty()
if submitted:
if len(topic_text) < 10:
raise ValueError("Book topic must be at least 10 characters long")
st.session_state.button_disabled = True
st.session_state.statistics_text = "Generating book title and structure in background...."
display_statistics()
if not API_KEY:
st.session_state.client = Client(api_key=API_KEY)
# Generate AI book title
st.session_state.book_title = generate_book_title(topic_text)
st.write(f"## {st.session_state.book_title}")
large_model_generation_statistics, book_structure = generate_book_structure(topic_text)
total_generation_statistics = GenerationStatistics(model_name=MODEL_NAME)
try:
book_structure = json_fixer.fix(book_structure).line
book_structure_json = json.loads(book_structure)
book = Book(st.session_state.book_title, book_structure_json)
if 'book' not in st.session_state:
st.session_state.book = book
# Print the book structure to the terminal to show structure
print(json.dumps(book_structure_json, indent=2))
st.session_state.book.display_structure()
def stream_section_content(sections):
for title, content in sections.items():
if isinstance(content, str):
content_stream = generate_section(title+": "+content)
for chunk in content_stream:
# Check if GenerationStatistics data is returned instead of str tokens
chunk_data = chunk
if (type(chunk_data)==GenerationStatistics):
total_generation_statistics.add(chunk_data)
st.session_state.statistics_text = str(total_generation_statistics)
display_statistics()
elif chunk!=None:
st.session_state.book.update_content(title, chunk)
elif isinstance(content, dict):
stream_section_content(content)
stream_section_content(book_structure_json)
except json.JSONDecodeError:
st.error("Failed to decode the book structure. Please try again.")
print(book_structure)
enable()
except Exception as e:
st.session_state.button_disabled = False
st.error(e)
if st.button("Clear"):
st.rerun()