| import gradio as gr |
| from fastapi import FastAPI |
| from pydantic import BaseModel |
| from typing import List |
| import random |
|
|
| app = FastAPI() |
|
|
| class Ingredients(BaseModel): |
| ingredients: List[str] |
|
|
| class Dish(BaseModel): |
| dish: str |
|
|
| @app.post("/generate_recipe/") |
| def generate_recipe_api(data: Ingredients): |
| recipes = [ |
| "Pasta Primavera", |
| "Stir-fry", |
| "Vegetable Soup", |
| "Frittata", |
| "Curry" |
| ] |
| return {"recipe": f"With those ingredients, you could make: {random.choice(recipes)}"} |
|
|
| @app.get("/cooking_tip/") |
| def cooking_tip_api(): |
| tips = [ |
| "Always preheat your oven for consistent results.", |
| "Salt your pasta water generously for better flavor.", |
| "Let meat rest after cooking to retain juices.", |
| "Sharpen your knives regularly for safer, easier chopping." |
| ] |
| return {"tip": random.choice(tips)} |
|
|
| @app.post("/wine_pairing/") |
| def wine_pairing_api(data: Dish): |
| pairings = { |
| "pasta": "Chianti", |
| "steak": "Cabernet Sauvignon", |
| "fish": "Sauvignon Blanc", |
| "chicken": "Chardonnay" |
| } |
| return {"wine": pairings.get(data.dish.lower(), "A versatile Pinot Noir")} |
|
|
| |
| def generate_recipe(ingredients): |
| recipes = [ |
| "Pasta Primavera", |
| "Stir-fry", |
| "Vegetable Soup", |
| "Frittata", |
| "Curry" |
| ] |
| return f"With those ingredients, you could make: {random.choice(recipes)}" |
|
|
| def cooking_tip(): |
| tips = [ |
| "Always preheat your oven for consistent results.", |
| "Salt your pasta water generously for better flavor.", |
| "Let meat rest after cooking to retain juices.", |
| "Sharpen your knives regularly for safer, easier chopping." |
| ] |
| return random.choice(tips) |
|
|
| def wine_pairing(dish): |
| pairings = { |
| "pasta": "Chianti", |
| "steak": "Cabernet Sauvignon", |
| "fish": "Sauvignon Blanc", |
| "chicken": "Chardonnay" |
| } |
| return pairings.get(dish.lower(), "A versatile Pinot Noir") |
|
|
| def culinary_genius(message, history): |
| if "recipe" in message.lower(): |
| return "Sure! What ingredients do you have on hand?" |
| elif "tip" in message.lower(): |
| return cooking_tip() |
| elif "wine" in message.lower(): |
| return "Certainly! What's the main dish you're serving?" |
| elif "ingredient" in message.lower(): |
| ingredients = message.split(',') |
| return generate_recipe(ingredients) |
| else: |
| return "I can help with recipes, cooking tips, and wine pairings. What would you like assistance with?" |
|
|
| |
| with gr.Blocks() as demo: |
| gr.Markdown("# Welcome to CulinaryGenius!") |
| |
| with gr.Row(): |
| with gr.Column(): |
| chat = gr.ChatInterface( |
| culinary_genius, |
| examples=[ |
| "I need a recipe idea", |
| "Can you give me a cooking tip?", |
| "What wine goes with pasta?" |
| ] |
| ) |
| |
| with gr.Column(): |
| gr.Markdown("## Quick Actions") |
| ingredients = gr.Textbox(label="Enter ingredients (comma-separated)") |
| generate_recipe_btn = gr.Button("Generate Recipe") |
| |
| tip_btn = gr.Button("Get Cooking Tip") |
| |
| dish = gr.Textbox(label="Enter main dish for wine pairing") |
| wine_pair_btn = gr.Button("Suggest Wine Pairing") |
| |
| output = gr.Textbox(label="Result") |
| |
| generate_recipe_btn.click( |
| generate_recipe, |
| inputs=ingredients, |
| outputs=output |
| ) |
| |
| tip_btn.click( |
| cooking_tip, |
| inputs=None, |
| outputs=output |
| ) |
| |
| wine_pair_btn.click( |
| wine_pairing, |
| inputs=dish, |
| outputs=output |
| ) |
|
|
| |
| app = gr.mount_gradio_app(app, demo, path="/") |
|
|
| if __name__ == "__main__": |
| import uvicorn |
| uvicorn.run(app, host="0.0.0.0", port=7860) |