Skip to content

Commit 8d6c5e0

Browse files
committed
Requested changes and bug fixes
1 parent 8a20273 commit 8d6c5e0

File tree

3 files changed

+47
-66
lines changed

3 files changed

+47
-66
lines changed

Magic_AI_Storybook/bookprompt.txt

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1-
Write a complete story with a title and a body of approximately
1+
Write a complete story. It must begin with a title and have a body of approximately
22
{STORY_WORD_LENGTH} words long and a happy ending. The specific
3-
story request is "{STORY_REQUEST}".
3+
story request is "{STORY_REQUEST}". The title should each be
4+
specified and the body should start on the next line.
11.1 KB
Loading

Magic_AI_Storybook/story.py

Lines changed: 44 additions & 64 deletions
Original file line numberDiff line numberDiff line change
@@ -44,6 +44,7 @@
4444
LOADING_IMAGE = "loading.png"
4545
BUTTON_BACK_IMAGE = "button_back.png"
4646
BUTTON_NEXT_IMAGE = "button_next.png"
47+
BUTTON_NEW_IMAGE = "button_new.png"
4748

4849
# Asset Paths
4950
IMAGES_PATH = os.path.dirname(sys.argv[0]) + "images/"
@@ -167,10 +168,9 @@ def __init__(self, rotation=0):
167168
self.rotation = rotation
168169
self.images = {}
169170
self.fonts = {}
171+
self.buttons = {}
170172
self.width = 0
171173
self.height = 0
172-
self.back_button = None
173-
self.next_button = None
174174
self.textarea = None
175175
self.screen = None
176176
self.saved_screen = None
@@ -224,23 +224,37 @@ def start(self):
224224
# Add buttons
225225
back_button_image = pygame.image.load(IMAGES_PATH + BUTTON_BACK_IMAGE)
226226
next_button_image = pygame.image.load(IMAGES_PATH + BUTTON_NEXT_IMAGE)
227+
new_button_image = pygame.image.load(IMAGES_PATH + BUTTON_NEW_IMAGE)
227228
button_spacing = (
228-
self.width - (back_button_image.get_width() + next_button_image.get_width())
229-
) // 3
229+
self.width - (back_button_image.get_width() + next_button_image.get_width() + new_button_image.get_width())
230+
) // 4
230231
button_ypos = (
231232
self.height
232233
- PAGE_NAV_HEIGHT
233234
+ (PAGE_NAV_HEIGHT - next_button_image.get_height()) // 2
234235
)
235-
self.back_button = Button(
236+
237+
self._load_button(
238+
"back",
236239
button_spacing,
237240
button_ypos,
238241
back_button_image,
239242
self.previous_page,
240243
self._display_surface,
241244
)
242-
self.next_button = Button(
243-
self.width - button_spacing - next_button_image.get_width(),
245+
246+
self._load_button(
247+
"new",
248+
button_spacing * 2 + back_button_image.get_width(),
249+
button_ypos,
250+
new_button_image,
251+
self.new_story,
252+
self._display_surface,
253+
)
254+
255+
self._load_button(
256+
"next",
257+
button_spacing * 3 + back_button_image.get_width() + new_button_image.get_width(),
244258
button_ypos,
245259
next_button_image,
246260
self.next_page,
@@ -255,8 +269,6 @@ def start(self):
255269
self.height - PAGE_NAV_HEIGHT - PAGE_TOP_MARGIN - PAGE_BOTTOM_MARGIN,
256270
)
257271

258-
self.load_settings()
259-
260272
# Start the sleep check thread after everything is initialized
261273
self._sleep_check_thread = threading.Thread(target=self._handle_sleep)
262274
self._sleep_check_thread.start()
@@ -309,7 +321,7 @@ def _handle_mousedown_event(self, event):
309321
if event.button == 1:
310322
# If button pressed while visible, trigger action
311323
coords = self._rotate_mouse_pos(event.pos)
312-
for button in [self.back_button, self.next_button]:
324+
for button in self.buttons.values():
313325
if button.visible and button.is_in_bounds(coords):
314326
button.action()
315327

@@ -334,6 +346,9 @@ def _load_image(self, name, filename):
334346
except pygame.error:
335347
pass
336348

349+
def _load_button(self, name, x, y, image, action, display_surface):
350+
self.buttons[name] = Button(x, y, image, action, display_surface)
351+
337352
def _load_font(self, name, details):
338353
self.fonts[name] = pygame.font.Font(details[0], details[1])
339354

@@ -369,6 +384,7 @@ def display_current_page(self):
369384
self._display_surface(self.images["background"], 0, 0)
370385
pygame.display.update()
371386

387+
print(f"Loading page {self.page} of {len(self.pages)}")
372388
page_data = self.pages[self.page]
373389

374390
# Display the title
@@ -385,8 +401,9 @@ def display_current_page(self):
385401

386402
# Display the navigation buttons
387403
if self.page > 0 or self.story > 0:
388-
self.back_button.show()
389-
self.next_button.show()
404+
self.buttons["back"].show()
405+
self.buttons["next"].show()
406+
self.buttons["new"].show()
390407
pygame.display.update()
391408
self._busy = False
392409

@@ -463,7 +480,11 @@ def next_page(self):
463480
self.load_story(self.stories[self.story])
464481
self.page = 0
465482
else:
466-
self.new_story()
483+
self.generate_new_story()
484+
self.display_current_page()
485+
486+
def new_story(self):
487+
self.generate_new_story()
467488
self.display_current_page()
468489

469490
def display_loading(self):
@@ -485,10 +506,13 @@ def load_story(self, story):
485506
# Parse out the title and story and render into pages
486507
self._busy = True
487508
self.pages = []
488-
title = story.split("Title: ")[1].split("\n\n")[0]
509+
if not story.startswith("Title: "):
510+
print("Unexpected story format from ChatGPT. Missing Title.")
511+
title = "A Story"
512+
else:
513+
title = story.split("Title: ")[1].split("\n\n")[0]
489514
page = self._add_page(title)
490515
paragraphs = story.split("\n\n")[1:]
491-
paragraphs.append("The End.")
492516
for paragraph in paragraphs:
493517
lines = self._wrap_text(paragraph, self.fonts["text"], self.textarea.width)
494518
for line in lines:
@@ -524,47 +548,9 @@ def _add_page(self, title=None):
524548
self.pages.append(page)
525549
return page
526550

527-
def load_settings(self):
528-
storydata = {
529-
"history": [],
530-
"settings": {
531-
"story": 0,
532-
"page": 0,
533-
},
534-
}
535-
# Load the story data if it exists
536-
if os.path.exists(os.path.dirname(sys.argv[0]) + "storydata.bin"):
537-
print("Loading previous story data")
538-
with open(os.path.dirname(sys.argv[0]) + "storydata.bin", "rb") as f:
539-
storydata = pickle.load(f)
540-
self.stories = storydata["history"]
541-
self.story = storydata["settings"]["story"]
542-
543-
if storydata["history"] and storydata["settings"]["story"] < len(
544-
storydata["history"]
545-
):
546-
# Load the last story
547-
self.load_story(storydata["history"][storydata["settings"]["story"]])
548-
self.page = storydata["settings"]["page"]
549-
# If something changed and caused the current page to be too
550-
# large, just go to the last page of the story
551-
if self.page >= len(self.pages):
552-
self.page = len(self.pages) - 1
553-
554-
def save_settings(self):
555-
storydata = {
556-
"history": self.stories,
557-
"settings": {
558-
"story": self.story,
559-
"page": self.page,
560-
},
561-
}
562-
with open(os.path.dirname(sys.argv[0]) + "storydata.bin", "wb") as f:
563-
pickle.dump(storydata, f)
564-
565-
def new_story(self):
551+
def generate_new_story(self):
566552
self._busy = True
567-
self.display_message("What story would you like to hear today?")
553+
self.display_message("Speak aloud the story you wish to read.")
568554

569555
if self._sleep_request:
570556
self._busy = False
@@ -604,10 +590,9 @@ def show_waiting():
604590
self.stories.append(response)
605591
self.story = len(self.stories) - 1
606592
self.page = 0
607-
self.save_settings()
593+
self._busy = False
608594

609595
self.load_story(response)
610-
self._busy = False
611596

612597
def _sleep(self):
613598
# Set a sleep request flag so that any busy threads know to finish up
@@ -699,19 +684,14 @@ def main(args):
699684
book = Book(args.rotation)
700685
try:
701686
book.start()
702-
703-
# If no stories, start a new one
704-
if not book.stories:
705-
book.new_story()
706-
687+
book.generate_new_story()
707688
book.display_current_page()
708689

709690
while True:
710691
book.handle_events()
711692
except KeyboardInterrupt:
712693
pass
713694
finally:
714-
book.save_settings()
715695
book.deinit()
716696
pygame.quit()
717697

@@ -721,4 +701,4 @@ def main(args):
721701

722702
# TODO:
723703
# * Figure out how to get the script to start on boot
724-
# * Play with prompt parameters
704+
# * Play with chatgpt prompt parameters

0 commit comments

Comments
 (0)