#!/usr/bin/env run.sh """Storybook Generator Application. This application generates a children's storybook using the OpenAI API. The user can select a theme, specify the main character's name, and choose a setting. The app generates a 10-page storybook with images. The tech stack is: Python, Ludic, and HTMX. All of the code is in this single file. """ # : out storybook # : dep ludic # : dep openai # : dep uvicorn # : dep starlette # : dep sqids # : dep requests # : dep types-requests import enum import json import logging import ludic import ludic.catalog.buttons as buttons import ludic.catalog.forms as forms import ludic.catalog.headers as headers import ludic.catalog.layouts as layouts import ludic.catalog.loaders as loaders import ludic.catalog.pages as pages import ludic.catalog.typography as typography import ludic.web import Omni.Log as Log import openai import os import pathlib import requests import sqids import starlette.testclient import sys import threading import time import typing import unittest import uuid import uvicorn CODEROOT = pathlib.Path(os.getenv("CODEROOT", ".")) DATA_DIR = pathlib.Path( os.environ.get("DATA_DIR", CODEROOT / "_/var/storybook/"), ) PORT = int(os.environ.get("PORT", "3000")) class Area(enum.Enum): """The area we are running.""" Test = "Test" Live = "Live" def from_env() -> Area: """Load AREA from environment variable. Raises: ValueError: if AREA is not defined """ var = os.getenv("AREA", "Test") if var == "Test": return Area.Test if var == "Live": return Area.Live msg = "AREA not defined" raise ValueError(msg) area = from_env() app = ludic.web.LudicApp(debug=area == Area.Test) Sqids = sqids.Sqids() def main() -> None: """Run the Ludic application.""" area = from_env() if "test" in sys.argv: test(area) else: move(area) def move(area: Area) -> None: """Run the application.""" Log.setup(logging.DEBUG if area == Area.Test else logging.ERROR) logging.info("area: %s", area) # during test, bind to beryllium's VPN address, else localhost host = "100.127.197.132" if area == Area.Test else "127.0.0.1" uvicorn.run(app, host=host, port=PORT) def test(area: Area = Area.Test) -> None: """Run the unittest suite manually.""" Log.setup(logging.DEBUG if area == Area.Test else logging.ERROR) suite = unittest.TestSuite() tests = [IndexTest, StoryTest] suite.addTests([ unittest.defaultTestLoader.loadTestsFromTestCase(tc) for tc in tests ]) unittest.TextTestRunner(verbosity=2).run(suite) def const(s: str) -> str: """Just returns the input.""" return s class Image(ludic.attrs.Attrs): """Represents an image associated with a story page.""" story_id: str page: typing.Annotated[int, const] path: pathlib.Path class OpenAIOverview(ludic.attrs.Attrs): """Part of OpenAIStoryResponse.""" character: str setting: str summary: str class OpenAIPage(ludic.attrs.Attrs): """Part of OpenAIStoryResponse.""" text: str image: str class OpenAIStoryResponse(ludic.attrs.Attrs): """The message content of the API response.""" overview: OpenAIOverview pages: list[OpenAIPage] example_openai_story_response: OpenAIStoryResponse = { "overview": { "character": ( "Alice is a blond haired girl, age 5, " "wearing a blue dress, white shoes, and a whie wide-brimmed hat." ), "setting": ( "A farm on a hill. " "There is a red barn, a grain silo, and lots of pasture." ), "summary": "", }, "pages": [{"text": "", "image": ""}], } class Page(ludic.attrs.Attrs): """Represents a single page in the storybook.""" story_id: str page_number: int text: str image_prompt: str class Prompt(ludic.attrs.Attrs): """Represents a prompt for generating an image. This datatype is overkill except that we need to validate it over the wire, so its actually useful in that sense. """ text: typing.Annotated[str, const] class StoryInputs(ludic.attrs.Attrs): """Represents story inputs from the user.""" theme: typing.Annotated[str, const] character: typing.Annotated[str, const] setting: typing.Annotated[str, const] moral: typing.Annotated[str, const] example_story: StoryInputs = { "theme": "Christian", "character": "Lia and her pet bunny", "setting": "A suburban park", "moral": "Honor thy mother and father", } class Story(ludic.attrs.Attrs): """Represents a full generated story.""" id: typing.Annotated[str, const] inputs: StoryInputs system_prompt: str = ( "You are an author and illustrator of childrens books. " "Each book is 10 pages long. " "All output must be in valid JSON. " "Don't add explanation or markdown formatting beyond the JSON. " "In your output, include the text on the page and a description of the " "image to be generated with an AI image generator." ) def story_meta(story: StoryInputs) -> list[str]: """Format the `StoryInputs` for submission to an LLM.""" return [ f"Character: {story['character']}", f"Setting: {story['setting']}", f"Theme: {story['theme']}", f"Moral: {story['moral']}", ] user_prompt: str = " ".join([ "Write a children's story with the following details.", "Output must be in valid JSON.", "The overview key must contain a character sketch and setting description.", "The pages key must an array of objects.", "Each object must have the text of the page and image prompt.", "Here is an example:", json.dumps(example_openai_story_response), ]) def gen_image_prompt( story: StoryInputs, image_prompt: str, story_text: str, ) -> str: """Format and return the full image prompt with additional context.""" return "\n".join([ f"Illustration: {image_prompt}", f"Narrative text: {story_text}", *story_meta(story), "Style: a hand-drawn children's cartoon from the 1990s", "Use soft pastel colors.", "Do not include any text in the generated image.", ]) def _openai_generate_text( story: StoryInputs, ) -> openai.types.chat.ChatCompletion: """Generate story text using the OpenAI API.""" messages: list[ openai.types.chat.ChatCompletionUserMessageParam | openai.types.chat.ChatCompletionSystemMessageParam ] = [ {"role": "system", "content": system_prompt}, { "role": "user", "content": "\n".join([user_prompt, *story_meta(story)]), }, ] client = openai.OpenAI() logging.debug("calling openai.chat.completions.create") return client.chat.completions.create( model="gpt-4o-mini", messages=messages, max_tokens=1500, ) def generate_pages(inputs: StoryInputs) -> OpenAIStoryResponse: """Generate the text for a story and update its pages. Raises: ValueError: when openAI response is bad """ # when developing, don't run up the OpenAI tab if area == Area.Test: name = inputs["character"] ret = example_openai_story_response.copy() ret["pages"] = [ OpenAIPage( text=f"A story about {name}...", image="Lorem ipsum..", ) for n in range(10) ] return ret response = _openai_generate_text(inputs) content = response.choices[0].message.content if content is None: msg = "content is none" raise ValueError(msg) parsed_content = json.loads(content) overview = parsed_content["overview"] pages = parsed_content["pages"] return OpenAIStoryResponse( overview=OpenAIOverview( character=overview["character"], setting=overview["setting"], summary=overview["summary"], ), pages=[OpenAIPage(text=p["text"], image=p["image"]) for p in pages], ) def generate_image( area: Area, image_prompt: str, story_id: str, page: int, ) -> Image: """Generate an image with OpenAI. Raises: InternalServerError: when OpenAI API fails """ logging.info("generating image %s.%s", story_id, page) url = None if area == Area.Test: time.sleep(1) url = "https://placehold.co/1024.png" else: client = openai.OpenAI() logging.debug("calling openai.images.generate") logging.debug("prompt: %s", image_prompt) image_response = client.images.generate( model="dall-e-3", prompt=image_prompt, n=1, size="1024x1024", quality="standard", ) url = image_response.data[0].url if url is None: msg = "error getting image from OpenAI" logging.error(msg) raise ludic.web.exceptions.InternalServerError(msg) image = Image( story_id=story_id, path=Images.gen_path(story_id, page), page=page, ) Images.save(image, url) return image class Job(ludic.attrs.Attrs): """Simple wrapper class for background jobs. This will become more useful when I need to store and track jobs in the database. """ id: str def generate_story_in_background( area: Area, story_id: str, inputs: StoryInputs, ) -> Story: """Kick off `generate_story_pages` in a background thread.""" job_id = str(uuid.uuid4()) job = Job(id=job_id) thread = threading.Thread( target=generate_story_pages, args=( area, story_id, inputs, ), ) logging.info("starting job %s", job_id) thread.start() story = Story(id=story_id, inputs=inputs) # save stuff db["jobs"][job_id] = job db["stories"][story_id] = story return story def generate_story_pages( area: Area, story_id: str, inputs: StoryInputs, ) -> list[Page]: """Upsert a new story.""" logging.info("generating story pages %s", story_id) story_resp = generate_pages(inputs) pages = [ Page( page_number=i + 1, text=sr["text"], story_id=story_id, image_prompt=sr["image"], ) for i, sr in enumerate(story_resp["pages"]) ] db["pages"][story_id] = pages for page in pages: image_prompt = gen_image_prompt( inputs, page["image_prompt"], page["text"], ) n = page["page_number"] generate_image(area, image_prompt, story_id, n) # I *would* save the Image to the database here, but i'm not actually # tracking that currenlty, just putting them in a known location on disk return pages class StoryTest(unittest.TestCase): """Unit test case for the Story class and related functions.""" def test_story_creation(self) -> None: """Creates a story with 10 pages.""" story_id = "Uk" story = generate_story_pages(Area.Test, story_id, example_story) pages = db["pages"][story_id] self.assertIsNotNone(story) self.assertEqual(len(pages), 10) class AppPage( ludic.components.Component[ludic.types.AnyChildren, ludic.attrs.NoAttrs], ): """HTML wrapper for the app.""" @typing.override def render(self) -> pages.HtmlPage: dark = ludic.styles.themes.DarkTheme() ludic.styles.themes.set_default_theme(dark) return pages.HtmlPage( pages.Head( ludic.html.meta(charset="utf-8"), ludic.html.meta( name="viewport", content="width=device-width, initial-scale=1", ), ludic.html.style.load(), title="Storybook", favicon="favicon.ico", load_styles=True, ), pages.Body( layouts.Center(layouts.Stack(*self.children)), htmx_version="latest", ), ) @app.get("/") def index(_: ludic.web.Request) -> AppPage: """Render the main page.""" return AppPage( headers.H1("Storybook Generator"), Generate(), ludic.html.div(id="story"), ) class IndexTest(unittest.TestCase): """Test the home page.""" def setUp(self) -> None: """Create test client.""" self.client = starlette.testclient.TestClient(app) def test_index(self) -> None: """The index page loads successfully.""" response = self.client.get("/") self.assertEqual(response.status_code, 200) self.assertIn("Storybook Generator", response.text) db_last_story_id: str = "bM" # sqid.encode([0]) class Database(ludic.attrs.Attrs): """Represents a simple in-memory database for storing stories and images.""" # each of these corresponds to a table in a SQL database stories: dict[str, Story] pages: dict[str, list[Page]] images: dict[str, Image] jobs: dict[str, Job] db: Database = Database(stories={}, images={}, jobs={}, pages={}) @app.endpoint("/pages/{story_id:str}/{page:int}") class Pages(ludic.web.Endpoint[Page]): """Resource for retrieving individual pages in a story.""" @classmethod async def get(cls, story_id: str, page: int) -> typing.Self: """Get a single page. Raises: NotFoundError: when the requested page is not found """ pages = db["pages"].get(story_id, None) if pages is None: msg = "story: %s" raise ludic.web.exceptions.NotFoundError(msg.format(story_id)) this_page = pages[page] return cls(**this_page) @typing.override def render(self) -> ludic.base.BaseElement: """Render a single page as HTML.""" story_id = self.attrs["story_id"] page = self.attrs["page_number"] image_url = app.url_path_for( "Images", story_id=story_id, page=page, ) return layouts.Box( layouts.Stack( ludic.html.div( loaders.Loading(), hx_get=image_url, hx_trigger="every 1s", hx_swap="outerHTML", ), typography.Paragraph(self.attrs["text"]), ), ) @app.get("/images/{story_id:str}/{page:int}.png") def images_static(story_id: str, page: int) -> ludic.web.responses.Response: """Endpoint for accessing static images. This does no generation, it only loads static images from the filesystem. This must be separate to match on the `.png` suffix. For generation use the `Images` class/endpoint. Raises: NotFoundError: when the image doesn't exist """ image = Images.by_id(story_id, page) if image["path"].exists(): return ludic.web.responses.FileResponse(image["path"]) msg = "images_static: image not found" logging.error(msg) raise ludic.web.exceptions.NotFoundError(msg) @app.endpoint("/images/{story_id:str}/{page:int}") class Images(ludic.web.Endpoint[Image]): """Endpoint for handling image-related operations.""" @classmethod async def get(cls, story_id: str, page: int) -> typing.Self: """Load the image from the database, if not found return 404. Raises: NotFoundError: If the image is not found. """ image = Images.by_id(story_id, page) if image["path"].exists(): return cls(**image) msg = "image not found" raise ludic.web.exceptions.NotFoundError(msg) @classmethod def by_id(cls, story_id: str, page: int) -> Image: """Load an image by its story ID and page number.""" path = cls.gen_path(story_id, page) return Image( story_id=story_id, page=page, path=path, ) @classmethod def gen_path(cls, story_id: str, page: int) -> pathlib.Path: """Generate the file path for an image.""" return pathlib.Path( DATA_DIR / "images" / story_id / str(page), ).with_suffix(".png") @classmethod def save(cls, image: Image, original_url: str) -> None: """Save an image to the file system.""" response = requests.get(original_url, timeout=10) image["path"].parent.mkdir(parents=True, exist_ok=True) pathlib.Path(image["path"]).write_bytes(response.content) @classmethod def read(cls, image: Image) -> bytes: """Read an image from the file system.""" return pathlib.Path(image["path"]).read_bytes() @typing.override def render(self) -> ludic.base.BaseElement: return ludic.html.img( src=app.url_path_for( "images_static", story_id=self.attrs["story_id"], page=self.attrs["page"], ), loading="lazy", ) @app.endpoint("/stories/{story_id:str}") class Stories(ludic.web.Endpoint[Story]): """Resource for accessing a Story.""" @classmethod async def get(cls, story_id: str) -> typing.Self: """Get a single story. Raises: NotFoundError: if the story doesn't exist. """ story = db["stories"].get(story_id) if story is None: msg = f"story {story_id} not found" raise ludic.web.exceptions.NotFoundError(msg) return cls(**story) @typing.override def render(self) -> ludic.base.BaseElement: story_id = self.attrs["id"] return layouts.Stack( typography.Paragraph(f"Story id: {story_id}"), *( loaders.LazyLoader( load_url=app.url_path_for( "Pages", story_id=story_id, page=n, ), hx_trigger="every 2s", ) for n in range(1, 10) ), id="#story", ) @app.endpoint("/generate") class Generate(ludic.web.Endpoint[StoryInputs]): """Form for generating new stories.""" @classmethod async def post(cls, data: ludic.web.parsers.Parser[StoryInputs]) -> Stories: """Upsert a new story.""" inputs = StoryInputs(**data.validate()) next_id_num = 1 + Sqids.decode(db_last_story_id)[0] next_id = Sqids.encode([next_id_num]) story = generate_story_in_background(area, next_id, inputs) return Stories(**story) @typing.override def render(self) -> ludic.base.BaseElement: """Render the story as HTML.""" return layouts.Box( forms.Form( forms.SelectField( forms.Option("Christian", value="Christian"), forms.Option("Secular", value="Secular"), id="theme", name="theme", label="Select Theme:", for_="theme", ), forms.InputField( label="Main Character's Name:", for_="character", type="text", id="character", name="character", required=True, value="Alice", ), forms.SelectField( forms.Option("Rural", value="rural"), forms.Option("Urban", value="urban"), forms.Option("Beach", value="beach"), forms.Option("Forest", value="forest"), label="Select Setting:", for_="setting", id="setting", name="setting", ), forms.InputField( label="Moral:", for_="moral", type="text", id="moral", name="moral", required=True, value="Honor thy mother and father", ), buttons.ButtonSuccess( "Generate Story", type="submit", classes=["large"], ), hx_post=self.url_for(Generate), hx_target="#story", hx_trigger="submit", ), ) if __name__ == "__main__": main()