Skip to content

Commit

Permalink
Fixed the process of building the final PDF
Browse files Browse the repository at this point in the history
  • Loading branch information
btfranklin committed Aug 25, 2024
1 parent a03a68a commit d9e6130
Show file tree
Hide file tree
Showing 4 changed files with 84 additions and 62 deletions.
66 changes: 66 additions & 0 deletions src/spooklight/completion/build_pdf_from_story_files.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,66 @@
import os
from colorama import Back, Fore
from openai import OpenAI
from reportlab.lib.pagesizes import letter
from reportlab.lib.styles import getSampleStyleSheet
from reportlab.platypus import SimpleDocTemplate, Paragraph, Image, Spacer
from reportlab.lib.units import inch

from spooklight.model import Story
from spooklight.settings import Settings


def build_pdf_from_story_files(
llm_client: OpenAI,
story: Story,
) -> None:
"""
Generate a PDF from the text and image files in the output directory.
"""

print(Back.BLUE + "BUILDING PDF FROM STORY FILES")

output_dir = Settings.get_output_directory()
output_file = os.path.join(output_dir, "story.pdf")

# Create a PDF document
pdf = SimpleDocTemplate(output_file, pagesize=letter)
styles = getSampleStyleSheet()
story_flow = []

# Add a title page
title_file = os.path.join(output_dir, "title.txt")
if os.path.exists(title_file):
with open(title_file, "r") as f:
title = f.read().strip()
title_style = styles["Title"]
story_flow.append(Paragraph(title, title_style))
story_flow.append(Spacer(1, 2 * inch)) # Adds some space after the title
else:
print(Fore.RED + f"Warning: Title file '{title_file}' not found.")

# Add each step to the PDF
step = 0
while True:
narrative_file = os.path.join(output_dir, f"{step}.txt")
image_file = os.path.join(output_dir, f"{step}.png")

if not os.path.exists(narrative_file) or not os.path.exists(image_file):
break

# Add narrative
with open(narrative_file, "r") as f:
narrative = f.read().strip()
story_flow.append(Paragraph(narrative, styles["BodyText"]))
story_flow.append(Spacer(1, 0.5 * inch)) # Space between narrative and image

# Add image
story_flow.append(Image(image_file, width=6 * inch, height=4.5 * inch))
story_flow.append(Spacer(1, 1 * inch)) # Space before the next step

step += 1

# Build the PDF
pdf.build(story_flow)

print(Back.BLUE + f"Story saved to {output_file}")
58 changes: 0 additions & 58 deletions src/spooklight/completion/finalize_story.py

This file was deleted.

20 changes: 17 additions & 3 deletions src/spooklight/generate_story.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,10 +3,16 @@
from dotenv import load_dotenv
from openai import OpenAI

from spooklight.completion.generate_title_from_narrative import (
generate_title_from_narrative,
)
from spooklight.initialization.initialize_story import initialize_story
from spooklight.settings import Settings
from spooklight.stepgeneration.generate_step import generate_step
from spooklight.completion.story_finished import story_finished
from spooklight.completion.finalize_story import finalize_story
from spooklight.completion.build_pdf_from_story_files import (
build_pdf_from_story_files,
)


def generate_story(
Expand Down Expand Up @@ -38,5 +44,13 @@ def generate_story(
# Generate the next image and narrative
generate_step(llm_client, story)

# Finalize and save the story title
finalize_story(llm_client, story)
# Generate the story title by reading the story narrative
story.title = generate_title_from_narrative(llm_client, story)

# Save the title to a file in the output directory with a name like "title.txt"
output_dir = Settings.get_output_directory()
with open(f"{output_dir}/title.txt", "w") as f:
f.write(story.title)

# Generate a PDF from the text and image files in the output directory
build_pdf_from_story_files(story)
2 changes: 1 addition & 1 deletion src/spooklight/imageprocessing/describe_image.py
Original file line number Diff line number Diff line change
Expand Up @@ -59,6 +59,6 @@ def describe_encoded_image(llm_client: OpenAI, base64_encoded_image: bytes) -> s
)

image_description = response.choices[0].message.content
print(Fore.YELLOW + f"Image description: {image_description}")
print(Fore.BLUE + "Image description: " + Fore.YELLOW + image_description)

return image_description

0 comments on commit d9e6130

Please sign in to comment.