File size: 11,098 Bytes
4c6125c
75c5414
6d25a57
 
4c6125c
 
ae0736c
4c6125c
 
6d25a57
75c5414
 
 
 
4c6125c
 
 
 
 
 
 
 
 
1adc8cb
75c5414
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
6d25a57
75c5414
 
 
 
 
 
 
 
 
ba424d1
75c5414
6d25a57
75c5414
 
 
 
6d25a57
75c5414
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
6d25a57
75c5414
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
6d25a57
4c6125c
 
 
75c5414
4c6125c
 
75c5414
4c6125c
 
 
 
 
75c5414
 
 
 
4c6125c
75c5414
4c6125c
 
 
 
75c5414
4c6125c
75c5414
4c6125c
 
 
 
75c5414
 
 
 
 
 
 
 
 
 
4c6125c
75c5414
 
 
 
 
 
4c6125c
75c5414
 
 
 
 
4c6125c
75c5414
4c6125c
75c5414
4c6125c
 
 
 
 
75c5414
 
 
 
 
 
 
 
4c6125c
 
 
75c5414
 
 
 
 
 
4c6125c
 
 
75c5414
 
 
 
4c6125c
 
75c5414
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
4c6125c
 
 
75c5414
 
 
6d25a57
 
 
75c5414
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
6d25a57
4c6125c
 
 
 
2a0ada8
4c6125c
 
765ce61
4c6125c
 
d5a2be2
75c5414
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
292
293
import logging
logging.basicConfig(level=logging.INFO)
log = logging.getLogger(__name__)

from typing import Any

import gradio as gr
from PIL import Image

from src.agents.mise_en_place import identify_ingredients
from src.agents.progress_validator import validate
from src.agents.recipe_planner import plan_recipe, propose_dishes
from src.agents.step_illustrator import illustrate_recipe
from src.data.nutrition import compute_nutrition
from src.ui.components import (
    DishOptions,
    IngredientChips,
    NutritionGrid,
    RecipeHero,
    StepCard,
    VerdictBadge,
)
from src.ui.theme import CSS, theme
from src.ui.components import DishOptions, IngredientChips, NutritionGrid, RecipeHero, StepCard, VerdictBadge

# ---------------------------------------------------------------------------
# Callbacks
# ---------------------------------------------------------------------------

def _clean_ingredients(items: list | None) -> list[str]:
    """Normalize a raw ingredient list (dedup, lowercase, strip empties)."""
    out, seen = [], set()
    for it in (items or []):
        name = str(it).strip().lower()
        if name and name not in seen:
            seen.add(name)
            out.append(name)
    return out


def on_propose(fridge_image: Image.Image | None, state: dict | None):
    """Photo β†’ ingredients β†’ 3 dish options (and fill the editable list)."""
    state = state or {}
    if fridge_image is None:
        return (
            IngredientChips.render({}),
            DishOptions.render({}),
            gr.update(choices=[], value=None),
            state,
            gr.update(choices=[], value=[]),
        )

    ingredients = identify_ingredients(fridge_image)
    options = propose_dishes(ingredients)

    state.update({
        "ingredients_have": ingredients,
        "options": [o.model_dump() for o in options],
    })

    radio_choices = [o.name for o in options]
    return (
        IngredientChips.render({"have": ingredients, "missing": []}),
        DishOptions.render({"options": state["options"]}),
        gr.update(choices=radio_choices, value=radio_choices[0] if radio_choices else None),
        state,
        gr.update(choices=ingredients, value=ingredients),
    )


def on_update_ingredients(state: dict | None, ingredients: list | None):
    """Manual edit of the ingredient list β†’ refresh chips + re-propose dishes."""
    state = state or {}
    ingredients = _clean_ingredients(ingredients)
    state["ingredients_have"] = ingredients

    if not ingredients:
        state["options"] = []
        return (
            IngredientChips.render({}),
            DishOptions.render({}),
            gr.update(choices=[], value=None),
            state,
        )

    options = propose_dishes(ingredients)
    state["options"] = [o.model_dump() for o in options]
    radio_choices = [o.name for o in options]
    return (
        IngredientChips.render({"have": ingredients, "missing": []}),
        DishOptions.render({"options": state["options"]}),
        gr.update(choices=radio_choices, value=radio_choices[0] if radio_choices else None),
        state,
    )


def on_cook(state: dict | None, dish_name: str | None, illustrate: bool, ingredients: list | None):
    """Chosen dish β†’ full recipe + nutrition (+ FLUX images if requested)."""
    state = state or {}
    if not dish_name:
        return (
            RecipeHero.render({}),
            StepCard.render({}),
            NutritionGrid.render({"nutrition": {}}),
            state,
        )

    # Prefer the (possibly hand-edited) ingredient list from the editor.
    ingredients = _clean_ingredients(ingredients) or state.get("ingredients_have", [])
    state["ingredients_have"] = ingredients
    recipe = plan_recipe(dish_name, ingredients)

    nutrition = compute_nutrition(ingredients, recipe.servings)
    recipe.nutrition = nutrition
    state["recipe"] = recipe.model_dump()

    if illustrate:
        log.info("Generating FLUX step images via Modal...")
        recipe = illustrate_recipe(recipe)
        state["recipe"] = recipe.model_dump()

    return (
        RecipeHero.render(recipe.model_dump()),
        StepCard.render({"steps": [s.model_dump() for s in recipe.steps]}),
        NutritionGrid.render({"nutrition": nutrition}),
        state,
    )


def on_validate(state: dict | None, step_idx: float, progress_image: Image.Image | None):
    """Progress photo + step number β†’ verdict badge."""
    state = state or {}
    recipe = state.get("recipe", {})
    steps = recipe.get("steps", [])
    idx = max(0, int(step_idx) - 1)
    instruction = steps[idx]["instruction"] if idx < len(steps) else "Cook the dish properly."
    result = validate(progress_image, instruction)
    return VerdictBadge.render(result)


# ---------------------------------------------------------------------------
# UI
# ---------------------------------------------------------------------------

def build_ui() -> gr.Blocks:
    initial_state: dict[str, Any] = {}

    with gr.Blocks(title="Cook With Me", theme=theme, css=CSS) as demo:
        gr.Markdown(
            "# 🍲 Cook With Me\n"
            "_Snap your fridge Β· Pick a dish Β· Cook step by step Β· Check your progress._"
        )

        state = gr.State(initial_state)

        with gr.Tabs():
            # ----------------------------------------------------------------
            # Tab 1 β€” Cook
            # ----------------------------------------------------------------
            with gr.Tab("🍳 Cook"):
                with gr.Row():
                    # Left β€” inputs
                    with gr.Column(scale=1):
                        fridge_input = gr.Image(
                            label="πŸ“Έ Photo of your fridge or pantry",
                            type="pil",
                            height=300,
                        )
                        propose_btn = gr.Button("πŸ” What can I cook?", variant="primary")

                        gr.Markdown("### Ingredients I see")
                        chips = gr.HTML(IngredientChips.render({}))

                        ingredient_editor = gr.Dropdown(
                            choices=[],
                            value=[],
                            multiselect=True,
                            allow_custom_value=True,
                            label="✏️ Add or remove ingredients (type + Enter to add, βœ• to remove)",
                            interactive=True,
                        )
                        update_btn = gr.Button("πŸ”„ Update ingredients & dishes")

                        gr.Markdown("### Pick a dish")
                        dish_options_html = gr.HTML(DishOptions.render({}))
                        dish_radio = gr.Radio(
                            choices=[],
                            label="Choose one",
                            interactive=True,
                        )

                        with gr.Accordion("βš™οΈ Generation options", open=False):
                            illustrate_chk = gr.Checkbox(
                                value=False,
                                label="🎨 Generate step images with FLUX.2 (requires Modal deployment)",
                            )

                        cook_btn = gr.Button("πŸ‘¨β€πŸ³ Build my recipe", variant="primary")

                    # Right β€” recipe output
                    with gr.Column(scale=2):
                        hero = gr.HTML(RecipeHero.render({}))
                        steps_panel = gr.HTML(StepCard.render({}))
                        nutrition_panel = gr.HTML(NutritionGrid.render({"nutrition": {}}))

            # ----------------------------------------------------------------
            # Tab 2 β€” Check Progress
            # ----------------------------------------------------------------
            with gr.Tab("πŸ“· Check Progress"):
                gr.Markdown(
                    "Upload a photo of your pan or plate. The vision model compares it "
                    "against the current recipe step and tells you if you can move on."
                )
                with gr.Row():
                    with gr.Column():
                        step_idx = gr.Number(value=1, precision=0, label="Active step #")
                        progress_input = gr.Image(
                            label="πŸ“Έ Your pan / plate",
                            type="pil",
                            height=300,
                        )
                        validate_btn = gr.Button("βœ… How am I doing?", variant="primary")
                    with gr.Column():
                        verdict_panel = gr.HTML(VerdictBadge.render({}))

            # ----------------------------------------------------------------
            # Tab 3 β€” About
            # ----------------------------------------------------------------
            with gr.Tab("ℹ️ About"):
                gr.Markdown(
                    """
### How it works
1. **Snap** your fridge β€” the fine-tuned vision model (MiniCPM-V-4.6) identifies every ingredient.
2. **Pick** one of three AI-suggested dishes tailored to what you have.
3. **Cook** step by step with a generated recipe, per-serving nutrition, and optional FLUX.2 step images.
4. **Check** your progress β€” upload a photo of your pan and get a *go / wait / fix* verdict.

### Models
| Role | Model | Params |
|---|---|---|
| Vision (ingredients + validator) | `openbmb/MiniCPM-V-4.6` (fine-tuned) | ~4.6B |
| Recipe Planner | `openbmb/MiniCPM4.1-8B` (fine-tuned on Kaggle recipes) | ~8B |
| Step Illustrator | `FLUX.2-klein-9B` via Modal | ~9B |

**Total ≀ 21.6B params** (cap: 32B βœ“)

### Badges targeted
βœ“ Well-Tuned Β· βœ“ Off-Brand Β· βœ“ Sharing is Caring Β· βœ“ Field Notes

### Hackathon
Hugging Face Small Models / Big Adventures Β· June 2026 Β· Track: Backyard AI
                    """
                )

        # --------------------------------------------------------------------
        # Wire callbacks
        # --------------------------------------------------------------------
        propose_btn.click(
            fn=on_propose,
            inputs=[fridge_input, state],
            outputs=[chips, dish_options_html, dish_radio, state, ingredient_editor],
        )

        update_btn.click(
            fn=on_update_ingredients,
            inputs=[state, ingredient_editor],
            outputs=[chips, dish_options_html, dish_radio, state],
        )

        cook_btn.click(
            fn=on_cook,
            inputs=[state, dish_radio, illustrate_chk, ingredient_editor],
            outputs=[hero, steps_panel, nutrition_panel, state],
        )

        validate_btn.click(
            fn=on_validate,
            inputs=[state, step_idx, progress_input],
            outputs=[verdict_panel],
        )

    return demo


if __name__ == "__main__":
    demo = build_ui()
    demo.launch(
        server_name="0.0.0.0",
        server_port=int(__import__("os").environ.get("PORT", 7860)),
        show_error=True,
        inbrowser=True,
    )