DEV Community

Mohamed Adib Shaikh
Mohamed Adib Shaikh

Posted on

SafePlate: Cooking Safe, Allergen-Free Meals for My Friend with Lightweight Open AI

This is a submission for the Hacktoberfest Weekend Challenge: Build for a Friend

What I Built

SafePlate is an open-source meal companion designed for a close friend dealing with severe dietary restrictions (lactose and nut allergies).

Cooking together or finding quick recipes often involves anxiety over cross-contamination or hidden allergens in commercial recipe blogs. SafePlate takes my friend's exact restrictions along with whatever raw ingredients we have in the pantry, and generates a personalized, allergen-verified recipe in seconds.

Demo

Code

import torch
import gradio as gr
from transformers import pipeline

 ## Load lightweight open-weight model (Ungated, no login required)
pipe = pipeline(
    "text-generation",
    model="Qwen/Qwen2.5-1.5B-Instruct",
    torch_dtype=torch.bfloat16,
    device_map="auto"
)

def plan_meal_for_friend(friend_name, allergies, pantry_items):
    system_instruction = (
        "You are an empathetic, precise meal planner designed for the 'Build for a Friend' challenge. "
        "Your top priority is food safety and simplicity. "
        "Output format:\n"
        "1. Dish Name (Safe & custom-made for the friend)\n"
        "2. Safety Verification (explicitly confirm why the dangerous allergens are omitted)\n"
        "3. 3-Step Simple Recipe (clear instructions under 20 minutes)\n"
        "End with a short, warm note to serve to the friend."
    )

    user_query = (
        f"Friend's Name: {friend_name}\n"
        f"Strict Allergies/Dislikes: {allergies}\n"
        f"Available Ingredients: {pantry_items}"
    )

    messages = [
        {"role": "system", "content": system_instruction},
        {"role": "user", "content": user_query}
    ]

    response = pipe(messages, max_new_tokens=300, temperature=0.6)
    return response[0]["generated_text"][-1]["content"]

## Build Gradio UI
with gr.Blocks(theme=gr.themes.Soft(), title="SafePlate: Cook for a Friend") as demo:
    gr.Markdown("# 🍲 SafePlate: Build for a Friend")
    gr.Markdown("Create safe, personalized home-cooked meals tailored around your friend's strict food allergies.")

    with gr.Row():
        name_input = gr.Textbox(label="Friend's Name", placeholder="e.g., Rahul / Sarah")
        allergies_input = gr.Textbox(label="Strict Allergies / Restrictions", placeholder="e.g., Peanuts, Dairy, Gluten")

    pantry_input = gr.Textbox(label="Available Pantry Ingredients", placeholder="e.g., Rice, chicken, carrots, olive oil, garlic, soy sauce")
    action_btn = gr.Button("Generate Safe Meal Plan", variant="primary")
    output_display = gr.Markdown(label="Custom Safe Recipe")

    action_btn.click(
        fn=plan_meal_for_friend, 
        inputs=[name_input, allergies_input, pantry_input], 
        outputs=output_display
    )

demo.launch(share=True)
Enter fullscreen mode Exit fullscreen mode

How I Built It

SafePlate runs on the open-weight Qwen 2.5 1.5B Instruct model via Hugging Face transformers and PyTorch, running in an accelerated Kaggle GPU environment:

  1. Lightweight Edge Inference: Utilizing bfloat16 precision, the model performs fast inference in under 2 seconds without external paid APIs.
  2. Deterministic Safety Prompting: The prompt constrains the model to verify allergen absence explicitly before outputting the 3-step cooking method.
  3. Interactive UI: Served through Gradio with an ephemeral live URL.

Why Does Open Innovation Matter?

  1. Health & Data Privacy: Personal dietary sensitivities, medical allergies, and household pantry habits shouldn't be logged or monetized by proprietary SaaS platforms.
  2. Offline Kitchen Utility: Recipes are needed in home kitchens and cabins where internet connections can be unreliable. Compact open-weight models can run offline directly on cheap hardware.
  3. Community Customization: Open weights allow anyone to adapt the model for regional diets and specialized culinary traditions without recurring API bills.

Prize Categories

Top comments (0)