File size: 1,529 Bytes
ef475e8
 
 
 
 
 
 
4545835
ef475e8
4545835
ef475e8
 
4545835
 
 
 
 
ef475e8
 
4545835
ef475e8
4545835
 
 
 
 
 
 
ef475e8
 
 
4545835
 
 
ef475e8
 
 
 
 
4545835
ef475e8
 
4545835
 
 
ef475e8
 
 
 
 
 
 
8f9a495
4545835
ef475e8
 
 
 
 
4545835
 
ef475e8
 
 
 
 
 
 
 
 
 
 
4545835
 
 
 
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
# /// script
# requires-python = ">=3.12"
# dependencies = [
#     "marimo",
#     "openai==1.53.0",
# ]
# ///

import marimo

__generated_with = "0.9.20"
app = marimo.App(width="medium")


@app.cell
def __(mo):
    mo.md(
        r"""
        # Recipe Bot 🤖

        If you upload a screenshot of a recipe, this chatbot will catalog them for you.
        """
    )
    return


@app.cell
def __():
    import marimo as mo
    import os
    return mo, os


@app.cell
def __(mo, os):
    api_key = mo.ui.text(
        label="API Key",
        kind="password",
        value=os.environ.get("OPENAI_API_KEY") or "",
    )
    api_key
    return (api_key,)


@app.cell
def __(api_key, mo):
    chat = mo.ui.chat(
        mo.ai.llm.openai(
            "gpt-4o",
            system_message="""You are a helpful assistant that can
            parse my recipe and summarize them for me.
            Give me a title in the first line.""",
            api_key=api_key.value,
        ),
        allow_attachments=["image/png", "image/jpeg"],
        prompts=["What is the recipe?"],
    )
    chat
    return (chat,)


@app.cell
def __(chat, mo):
    mo.stop(not chat.value)

    last_message: str = chat.value[-1].content
    title = last_message.split("\n")[0]
    summary = "\n".join(last_message.split("\n")[1:])
    with open(f"{title}.md", "w") as f:
        f.write(summary)
        mo.status.toast("Receipt summary saved!", description=title)
    return f, last_message, summary, title


if __name__ == "__main__":
    app.run()