-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathwrite_a_book.py
89 lines (82 loc) · 3.74 KB
/
write_a_book.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
import chat_with_rtx_api.rtx_api_july_2024 as rtx_api
import python_interface
import datetime
import time
import re
import os
def write_section(book_title, chapter, section_desc):
prompt = "You are writing a book about: " + book_title + "." +\
"You are writing chapter about: " + chapter + "." +\
" Write a section title for: " + section_desc + ". Add double quotes around the new title."
title_tmp = rtx_api.send_message(prompt)
match = re.search(r'"(.*?)"', title_tmp)
title = ""
if match:
title = match.group(1)
prompt = "You are writing a book about: " + book_title + "." +\
"You are writing chapter about: " + chapter + "." +\
" Write a section about " + section_desc + "."
section_text = rtx_api.send_message(prompt)
lines = section_text.split('. ')
if "section" in lines[0]:
lines = lines[1:]
section_text = ". ".join(lines)
return title, section_text
def write_a_book(book_title):
start_time_unix_ms = int(time.time() * 1000)
res = []
chapters = python_interface.generate_list("Write list of chapters for book about " + book_title)
# chapters = chapters[:2]
res.append("# " + book_title)
print(chapters)
for c in chapters:
res.append("## " + c)
sections = python_interface.generate_list("You are writing a book about: " + book_title + ". Write list of sections for book chapter about " + c)
# sections = sections[:2]
print(c)
print("\t", sections)
for s in sections:
section_title, section_text = write_section(book_title, c, s)
res.append("### " + section_title)
res.append(section_text)
print(section_text)
end_time_unix_ms = int(time.time() * 1000)
formatted_time = str(datetime.timedelta(seconds=int(end_time_unix_ms - start_time_unix_ms) / 1000))[:-7]
# time_str = "# Chat with RTX Llama 2 took " + formatted_time + " to write this book"
time_str = "# Chat with RTX Mistral took " + formatted_time + " to write this book"
res = ["# Intended as parody and/or educational on generative llms"] + res
res = [time_str] + res
res.append(time_str)
if not os.path.exists("books"):
os.makedirs("books")
book_str = "\n".join(res)
book_filename = book_title.replace(' ', '_') + ".md"
with open("books/" + book_filename, 'w', encoding='utf-8') as file:
file.write(book_str)
if __name__ == '__main__':
write_a_book("Programming language C")
write_a_book("Programming language C++")
write_a_book("Programming language Python3")
write_a_book("Programming language Java")
write_a_book("Programming language JavaScript")
write_a_book("How to fly a 747")
write_a_book("Modern radio link stack")
write_a_book("RF modulation techniques and protocols explained")
write_a_book("RADAR System Design")
write_a_book("Fall of the Roman Empire")
write_a_book("Developing a nuclear reactor")
write_a_book("Developing a nuclear power plant")
write_a_book("Developing a scramjet (supersonic combustion ramjet) engine")
write_a_book("Developing an external pulsed plasma propulsion engine")
write_a_book("Developing a liquid-fuel rocket engine")
write_a_book("Developing a super heavy-lift launch vehicle")
write_a_book("Developing a rocket engine nozzle")
write_a_book("Isentrepic flow in liquid fuel rocket engine design")
write_a_book("Supersonic gas dynamics")
write_a_book("Orbital mechanics")
write_a_book("Spacecraft atmospheric re-entry")
write_a_book("Thermodynamics")
write_a_book("Lattice quantum field theory")
write_a_book("Free-space optical communication")
write_a_book("How to become a master yapper")
write_a_book("How to write a book")