RecipeMachine / app.py
CazC's picture
chore: Add recipe generation and formatting functions
a094d9f
raw
history blame
2.14 kB
import gradio as gr
import json
from pydantic import BaseModel
from typing import List
# Define the Pydantic models
class Ingredient(BaseModel):
quantity: str
unit: str
name: str
class Steps(BaseModel):
stepNumber: int
instruction: str
class ProduceRecipe(BaseModel):
mealName: str
ingredients: List[Ingredient]
steps: List[Steps]
# This is a placeholder for your actual function
def generate_recipe(meal_name: str, calories: int, meal_time: str) -> ProduceRecipe:
# This is where you'll implement your recipe generation logic
# For now, we'll return a dummy recipe
dummy_recipe = {
"mealName": meal_name,
"ingredients": [
{"quantity": "2", "unit": "large", "name": "eggs"},
{"quantity": "1/4", "unit": "cup", "name": "onion, diced"}
],
"steps": [
{"stepNumber": 1, "instruction": "Crack the eggs into a bowl."},
{"stepNumber": 2, "instruction": "Whisk the eggs and add diced onions."}
]
}
return ProduceRecipe(**dummy_recipe)
def format_recipe(recipe: ProduceRecipe) -> str:
formatted = f"<h2>{recipe.mealName}</h2>\n\n"
formatted += "<h3>Ingredients:</h3>\n<ul>\n"
for ingredient in recipe.ingredients:
formatted += f"<li>{ingredient.quantity} {ingredient.unit} {ingredient.name}</li>\n"
formatted += "</ul>\n\n"
formatted += "<h3>Instructions:</h3>\n<ol>\n"
for step in recipe.steps:
formatted += f"<li>{step.instruction}</li>\n"
formatted += "</ol>"
return formatted
def recipe_interface(meal_name: str, calories: int, meal_time: str) -> str:
recipe = generate_recipe(meal_name, calories, meal_time)
return format_recipe(recipe)
iface = gr.Interface(
fn=recipe_interface,
inputs=[
gr.Textbox(label="Meal Name"),
gr.Number(label="Calories"),
gr.Dropdown(["breakfast", "lunch", "dinner"], label="Meal Time")
],
outputs=gr.HTML(label="Recipe"),
title="Meal Recipe Generator",
description="Generate a recipe based on meal name, calories, and meal time."
)
iface.launch()