summaryrefslogtreecommitdiff
path: root/Biz/Storybook.py
blob: 2f362c4f2afb4ab0a658a9aeaa023873d50e37de (plain)
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
"""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, Flask, HTMX, and bootstrap. All of the code is in
this single file.

"""

# : out storybook
# : dep flask
# : dep openai
import flask
import json
import openai
import os
import pydantic
import sys
import unittest

app = flask.Flask(__name__)
app.secret_key = os.urandom(24)


def main() -> None:
    """Run the Flask application."""
    if sys.argv[1] == "test":
        test()
    else:
        move()


def move() -> None:
    """Run the application."""
    app.run()


def test() -> None:
    """Run the unittest suite manually."""
    suite = unittest.TestSuite()
    tests = [StorybookTestCase, StoryTestCase]
    suite.addTests([
        unittest.defaultTestLoader.loadTestsFromTestCase(tc) for tc in tests
    ])
    unittest.TextTestRunner(verbosity=2).run(suite)


@app.route("/")
def index() -> str:
    """Render the main page."""
    return flask.render_template_string(f"""
<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Storybook Generator</title>
    <link href="https://stackpath.bootstrapcdn.com/bootstrap/4.5.2/css/bootstrap.min.css"
          rel="stylesheet">
    <script src="https://unpkg.com/htmx.org@1.3.3"></script>
</head>
<body>
    <div class="container mt-5">
        <h1 class="text-center">Storybook Generator</h1>
        <form hx-post="{flask.url_for("generate_story")}" hx-target="#story"
              class="mt-4">
            <div class="form-group">
                <label for="theme">Select Theme:</label>
                <select class="form-control" id="theme" name="theme">
                    <option value="Christian">Christian</option>
                    <option value="Secular">Secular</option>
                </select>
            </div>
            <div class="form-group">
                <label for="character">Main Character's Name:</label>
                <input type="text" class="form-control" id="character"
                       name="character" required>
            </div>
            <div class="form-group">
                <label for="setting">Select Setting:</label>
                <select class="form-control" id="setting" name="setting">
                    <option value="rural">Rural</option>
                    <option value="urban">Urban</option>
                    <option value="beach">Beach</option>
                    <option value="forest">Forest</option>
                </select>
            </div>
            <button type="submit" class="btn btn-primary">
                Generate Story
            </button>
        </form>
        <div id="story" class="mt-5"></div>
    </div>
</body>
</html>
""")


class Page(pydantic.BaseModel):
    """Represents a single page in the storybook."""

    model_config = pydantic.ConfigDict(arbitrary_types_allowed=True)
    text: str
    image_prompt: str
    image_url: str | None


def load_image(page: Page) -> Page:
    """Load an image for a given page using the OpenAI API."""
    if page.image_url is not None:
        return page
    prompt = page.image_prompt
    client = openai.OpenAI(api_key=os.getenv("OPENAI_API_KEY"))
    image_response = client.images.generate(
        prompt=prompt,
        n=1,
        size="256x256",
    )
    page.image_url = image_response.data[0].url
    # Handle if image is None
    return page


class Story(pydantic.BaseModel):
    """Represents a story with multiple pages."""

    model_config = pydantic.ConfigDict(arbitrary_types_allowed=True)
    theme: str
    character: str
    setting: str
    moral: str
    pages: list[Page] | None = None


def system_prompt() -> str:
    """Generate the system prompt for the OpenAI API."""
    return (
        "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 user_prompt(story: Story) -> str:
    """Generate the user prompt based on the story details."""
    return " ".join([
        "Write a story with the following details.",
        "Output must be in valid JSON where each page is an array of text and"
        "image like the following example:",
        """[{"text": "<text of the story>",""",
        """"image": "<description of illustration>"}...],""",
        f"Character: {story.character}\n",
        f"Setting: {story.setting}\n",
        f"Theme: {story.theme}\n",
        f"Moral: {story.moral}\n",
    ])


def _openai_generate_text(story: Story) -> 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": user_prompt(story)},
    ]
    client = openai.OpenAI(api_key=os.getenv("OPENAI_API_KEY"))
    return client.chat.completions.create(
        model="gpt-4o-mini",
        messages=messages,
        max_tokens=1500,
    )


def generate_text(story: Story) -> Story:
    """Generate the text for a story and update its pages.

    Raises:
        ValueError: If the content is None or JSON parsing fails.
    """
    response = _openai_generate_text(story)
    content = response.choices[0].message.content
    if content is None:
        error_message = "No content in response"
        raise ValueError(error_message)
    try:
        response_messages = json.loads(content)
    except (json.JSONDecodeError, ValueError) as e:
        error_message = f"Failed to parse story JSON: {e}"
        raise ValueError(error_message) from e
    story.pages = [
        Page(text=msg["text"], image_prompt=msg["image"], image_url=None)
        for msg in response_messages
    ]
    return story


@app.route("/generate/story", methods=["POST"])
def generate_story() -> str:
    """Generate a story based on user input."""
    story = Story(
        theme=flask.request.form["theme"],
        character=flask.request.form["character"],
        setting=flask.request.form["setting"],
        moral="Honor thy mother and father",  # request.form["moral"],
    )
    story = generate_text(story)
    if story.pages is None:
        return "<p>error: no story pages found</p>"
    flask.session["story"] = story.model_dump_json()
    return "".join(
        f"<div class='card mb-3'>"
        f"<img src='/static/placeholder.png' data-src='"
        f"{flask.url_for('generate_image', n=i)}'"
        f"class='card-img-top' hx-trigger='load' hx-swap='outerHTML' "
        f"""hx-get='{flask.url_for("generate_image", n=i)}' alt='Loading...'>"""
        f"<div class='card-body'>"
        f"<p class='card-text'>{page.text}</p></div></div>"
        for i, page in enumerate(story.pages)
    )


@app.route("/generate/image/<int:n>", methods=["GET"])
def generate_image(n: int) -> str:
    """Generate an image for a specific page in the story."""
    story_data = flask.session.get("story")
    if story_data is None:
        return "<p>error: no story data found</p>"

    try:
        story = Story.model_validate_json(story_data)
    except pydantic.ValidationError as e:
        return f"<p>error: story validation failed: {e}</p>"
    if story.pages is not None and 0 <= n < len(story.pages):
        page = load_image(story.pages[n])
        return f"<img src='{page.image_url}' class='card-img-top'>"
    return "<p>Image not found</p>"


class StoryTestCase(unittest.TestCase):
    """Unit test case for the Story class and related functions."""

    def test_story_creation(self) -> None:
        """Test the creation of a story and its text generation."""
        s = Story(
            theme="Christian",
            character="Lia and her pet bunny",
            setting="A suburban park",
            moral="Honor thy mother and father",
        )
        s = generate_text(s)
        self.assertIsNotNone(s.pages)
        self.assertEqual(len(s.pages), 10)  # type: ignore[arg-type]


class StorybookTestCase(unittest.TestCase):
    """Unit test case for the Storybook application."""

    def setUp(self) -> None:
        """Set up the test client."""
        self.app = app.test_client()

    def test_index_page(self) -> None:
        """Test that the index page loads successfully."""
        response = self.app.get("/")
        self.assertEqual(response.status_code, 200)
        self.assertIn(b"Storybook Generator", response.data)

    def test_generate_story(self) -> None:
        """Test the story generation endpoint."""
        response = self.app.post(
            "/generate/story",
            data={
                "theme": "Christian",
                "character": "Alice",
                "setting": "forest",
            },
        )
        self.assertEqual(response.status_code, 200)
        self.assertIn(b"<div class='card mb-3'>", response.data)


if __name__ == "__main__":
    main()