bitebot_app / app.py
aashnaj's picture
create app
85a758a verified
raw
history blame
2.42 kB
import gradio as gr
import requests
SPOONACULAR_API_KEY = "71259036cfb3405aa5d49c1220a988c5"
recipe_id_map = {}
# search for recipes
def search_recipes(ingredient, cuisine, dietary):
global recipe_id_map
url = "https://api.spoonacular.com/recipes/complexSearch"
params = {
"query": ingredient,
"cuisine": cuisine,
"diet": dietary,
"number": 3,
"apiKey": SPOONACULAR_API_KEY
}
res = requests.get(url, params=params)
data = res.json()
if "results" not in data or not data["results"]:
recipe_id_map = {}
return gr.update(choices=[], visible=True, label="No recipes found"), gr.update(value="No recipes found.")
recipe_id_map = {r["title"]: r["id"] for r in data["results"]}
return gr.update(choices=list(recipe_id_map.keys()), visible=True, label="Select a recipe"), gr.update(value="Select a recipe from the dropdown above.")
# get recipe details
def get_recipe_details(selected_title):
if not selected_title or selected_title not in recipe_id_map:
return "Please select a valid recipe."
recipe_id = recipe_id_map[selected_title]
url = f"https://api.spoonacular.com/recipes/{recipe_id}/information"
params = {"apiKey": SPOONACULAR_API_KEY}
res = requests.get(url, params=params)
data = res.json()
title = data.get("title", "Unknown Title")
time = data.get("readyInMinutes", "N/A")
instructions = data.get("instructions") or "No instructions available."
return f"### 🍽️ {title}\n**⏱️ Cook Time:** {time} minutes\n\n**πŸ“‹ Instructions:**\n{instructions}"
# UI
with gr.Blocks() as demo:
gr.Markdown("## πŸ₯— The BiteBot")
with gr.Row():
ingredient = gr.Textbox(label="Preferred Ingredient", placeholder="e.g., chicken")
cuisine = gr.Textbox(label="Preferred Cuisine", placeholder="e.g., Indian")
diet = gr.Textbox(label="Dietary Restrictions", placeholder="e.g., vegetarian")
search_button = gr.Button("Search Recipes")
recipe_dropdown = gr.Dropdown(label="Select a recipe", visible=False)
recipe_output = gr.Markdown()
search_button.click(
fn=search_recipes,
inputs=[ingredient, cuisine, diet],
outputs=[recipe_dropdown, recipe_output]
)
recipe_dropdown.change(
fn=get_recipe_details,
inputs=recipe_dropdown,
outputs=recipe_output
)
demo.launch()