Close Menu
    Trending
    • Revisiting Benchmarking of Tabular Reinforcement Learning Methods
    • Is Your AI Whispering Secrets? How Scientists Are Teaching Chatbots to Forget Dangerous Tricks | by Andreas Maier | Jul, 2025
    • Qantas data breach to impact 6 million airline customers
    • He Went From $471K in Debt to Teaching Others How to Succeed
    • An Introduction to Remote Model Context Protocol Servers
    • Blazing-Fast ML Model Serving with FastAPI + Redis (Boost 10x Speed!) | by Sarayavalasaravikiran | AI Simplified in Plain English | Jul, 2025
    • AI Knowledge Bases vs. Traditional Support: Who Wins in 2025?
    • Why Your Finance Team Needs an AI Strategy, Now
    AIBS News
    • Home
    • Artificial Intelligence
    • Machine Learning
    • AI Technology
    • Data Science
    • More
      • Technology
      • Business
    AIBS News
    Home»Artificial Intelligence»Transforming Data into Solutions: Building a Smart App with Python and AI | by Vianney Mixtur | Jan, 2025
    Artificial Intelligence

    Transforming Data into Solutions: Building a Smart App with Python and AI | by Vianney Mixtur | Jan, 2025

    Team_AIBS NewsBy Team_AIBS NewsJanuary 1, 2025No Comments4 Mins Read
    Share Facebook Twitter Pinterest LinkedIn Tumblr Reddit Telegram Email
    Share
    Facebook Twitter LinkedIn Pinterest Email


    On this part, I’ll share some implementation particulars of Baker. Once more it’s open-source so I invite my technical readers to go verify the code on GitHub. Some readers may need to bounce to the next section.

    The appliance is minimalist with a easy 3 tier structure and is constructed virtually completely in Python.

    An architecture diagram of the Baker application
    Photograph by writer

    It’s fabricated from the next elements:

    1. Frontend: A Streamlit interface offers an intuitive platform for customers to work together with the system, question recipes, and obtain suggestions.
    2. Backend: Constructed with FastAPI, the backend serves because the interface for dealing with consumer queries and delivering suggestions.
    3. Engine: The engine accommodates the core logic for locating and filtering recipes, leveraging monggregate as a question builder.
    4. Database: The recipes are saved in a MongoDB database that processes the aggregation pipelines generated by the engine.

    Backend Setup

    The backend is initialized in app.py, the place FastAPI endpoints are outlined. For example:

    from fastapi import FastAPI
    from baker.engine.core import find_recipes
    from baker.fashions.ingredient import Ingredient

    app = FastAPI()
    @app.get("/")
    def welcome():
    return {"message": "Welcome to the Baker API!"}
    @app.submit("/recipes")
    def _find_recipes(substances: record[Ingredient], serving_size: int = 1) -> record[dict]:
    return find_recipes(substances, serving_size)

    The /recipes endpoint accepts an inventory of substances and a serving measurement then delegates the processing to the engine.

    Recipe Engine Logic

    The center of the applying resides in core.py inside the engine listing. It manages database connections and question pipelines. Under is an instance of the find_recipes operate:

    # Imports and the get_recipes_collection operate should not included

    def find_recipes(substances, serving_size=1):
    # Get the recipes assortment
    recipes = get_recipes_collection()

    # Create the pipeline
    pipeline = Pipeline()
    pipeline = include_normalization_steps(pipeline, serving_size)
    question = generate_match_query(substances, serving_size)
    print(question)
    pipeline.match(question=question).challenge(
    embrace=[
    "id",
    "title",
    "preparation_time",
    "cooking_time",
    "original_serving_size",
    "serving_size",
    "ingredients",
    "steps",
    ],
    exclude="_id",
    )

    # Discover the recipes
    outcome = recipes.combination(pipeline.export()).to_list(size=None)

    return outcome

    def generate_match_query(substances: record[Ingredient], serving_size: int = 1) -> dict:
    """Generate the match question."""

    operands = []
    for ingredient in substances:
    operand = {
    "substances.identify": ingredient.identify,
    "substances.unit": ingredient.unit,
    "substances.amount": {"$gte": ingredient.amount / serving_size},
    }
    operands.append(operand)

    question = {"$and": operands}

    return question

    def include_normalization_steps(pipeline: Pipeline, serving_size: int = 1):
    """Provides steps in a pipeline to normalize the substances amount within the db

    The steps under normalize the portions of the substances within the recipes within the DB by the recipe serving measurement.

    """

    # Unwind the substances
    pipeline.unwind(path="$substances")

    pipeline.add_fields({"original_serving_size": "$serving_size"})
    # Add the normalized amount
    pipeline.add_fields(
    {
    # "orignal_serving_size": "$serving_size",
    "serving_size": serving_size,
    "substances.amount": S.multiply(
    S.subject("substances.amount"),
    S.divide(serving_size, S.max([S.field("serving_size"), 1])),
    ),
    }
    )

    # Group the outcomes
    pipeline.group(
    by="_id",
    question={
    "id": {"$first": "$id"},
    "title": {"$first": "$title"},
    "original_serving_size": {"$first": "$original_serving_size"},
    "serving_size": {"$first": "$serving_size"},
    "preparation_time": {"$first": "$preparation_time"},
    "cooking_time": {"$first": "$cooking_time"},
    # "directions_source_text": {"$first": "$directions_source_text"},
    "substances": {"$addToSet": "$substances"},
    "steps": {"$first": "$steps"},
    },
    )
    return pipeline

    The core logic of Baker resides within the find_recipes operate.

    This operate creates a MongoDB aggregation pipeline because of monggregate. This aggregation pipeline consists of a number of steps.

    The primary steps are generated by the include_normalization_steps operate that’s going to dynamically replace the portions of the substances within the database to make sure we’re evaluating apples to apples. That is finished by updating the substances portions within the database to the consumer desired serving.

    Then the precise matching logic is created by the generate_match_query operate. Right here we guarantee, that the recipes don’t require greater than what the consumer have for the substances involved.

    Lastly a projection filters out the fields that we don’t have to return.

    Baker helps you uncover a greater destiny on your substances by discovering recipes that match what you have already got at house.

    The app incorporates a easy form-based interface. Enter the substances you have got, specify their portions, and choose the unit of measurement from the accessible choices.

    A screenshot showing the Baker interface to enter ingredients
    Photograph by writer

    Within the instance above, I’m trying to find a recipe for two servings to make use of up 4 tomatoes and a couple of carrots which were sitting in my kitchen for a bit too lengthy.

    Baker discovered two recipes! Clicking on a recipe enables you to view the total particulars.

    A screenshot of the Baker interface showing how recipes are displayed
    Photograph by writer

    Baker adapts the portions within the recipe to match the serving measurement you’ve set. For instance, in case you alter the serving measurement from two to 4 folks, the app recalculates the ingredient portions accordingly.

    Updating the serving measurement may change the recipes that seem. Baker ensures that the instructed recipes match not solely the serving measurement but additionally the substances and portions you have got available. For example, in case you solely have 4 tomatoes and a couple of carrots for 2 folks, Baker will keep away from recommending recipes that require 4 tomatoes and 4 carrots.



    Source link

    Share. Facebook Twitter Pinterest LinkedIn Tumblr Email
    Previous Article論文筆記 CLAD: Robust Audio Deepfake Detection Against Manipulation Attacks with Contrastive Learning | by Gordon Fang | Jan, 2025
    Next Article Your Secret Weapon for Creating Stunning Business Images
    Team_AIBS News
    • Website

    Related Posts

    Artificial Intelligence

    Revisiting Benchmarking of Tabular Reinforcement Learning Methods

    July 2, 2025
    Artificial Intelligence

    An Introduction to Remote Model Context Protocol Servers

    July 2, 2025
    Artificial Intelligence

    How to Access NASA’s Climate Data — And How It’s Powering the Fight Against Climate Change Pt. 1

    July 1, 2025
    Add A Comment
    Leave A Reply Cancel Reply

    Top Posts

    Revisiting Benchmarking of Tabular Reinforcement Learning Methods

    July 2, 2025

    I Tried Buying a Car Through Amazon: Here Are the Pros, Cons

    December 10, 2024

    Amazon and eBay to pay ‘fair share’ for e-waste recycling

    December 10, 2024

    Artificial Intelligence Concerns & Predictions For 2025

    December 10, 2024

    Barbara Corcoran: Entrepreneurs Must ‘Embrace Change’

    December 10, 2024
    Categories
    • AI Technology
    • Artificial Intelligence
    • Business
    • Data Science
    • Machine Learning
    • Technology
    Most Popular

    Traditional Techniques and Machine Learning for IRB Models | by 46 | Mar, 2025

    March 27, 2025

    Saudi Investment Fund pays $3.5bn to capture Pokémon Go

    March 12, 2025

    Mercedes’s Most Affordable Sedan Will Be Electric

    March 13, 2025
    Our Picks

    Revisiting Benchmarking of Tabular Reinforcement Learning Methods

    July 2, 2025

    Is Your AI Whispering Secrets? How Scientists Are Teaching Chatbots to Forget Dangerous Tricks | by Andreas Maier | Jul, 2025

    July 2, 2025

    Qantas data breach to impact 6 million airline customers

    July 2, 2025
    Categories
    • AI Technology
    • Artificial Intelligence
    • Business
    • Data Science
    • Machine Learning
    • Technology
    • Privacy Policy
    • Disclaimer
    • Terms and Conditions
    • About us
    • Contact us
    Copyright © 2024 Aibsnews.comAll Rights Reserved.

    Type above and press Enter to search. Press Esc to cancel.