File size: 3,803 Bytes
85a758a
 
 
8eaafe5
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
d3ce4d9
 
85a758a
 
d3ce4d9
85a758a
 
 
 
 
 
 
 
d3ce4d9
85a758a
 
 
 
 
 
d3ce4d9
85a758a
d3ce4d9
85a758a
d3ce4d9
 
85a758a
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
d3ce4d9
85a758a
 
 
 
d3ce4d9
85a758a
 
 
d3ce4d9
85a758a
 
 
 
d3ce4d9
85a758a
 
 
 
 
d3ce4d9
85a758a
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
import gradio as gr
import requests

def response(message, history):
    bitebot_chunks = get_top_chunks(message, chunk_embeddings, cleaned_chunks)
    print(bitebot)
    str_bitebot_chunks = "\n".join(bitebot_chunks)
    messages = [{"role" : "system", "content" : "You are a helpful chef who provides recipes according the the user's dietary choices and cusine preferences."}]
                                                # update response person on provided context; use f-strings

    if history:
        messages.extend(history)

    messages.append({"role" : "user", "content" : message})

    response = client.chat_completion(
        messages,
        max_tokens = 100
        # temperature == 0.2
    )
    return response['choices'][0]['message']['content'].strip()




SPOONACULAR_API_KEY = "71259036cfb3405aa5d49c1220a988c5" #our api key
recipe_id_map = {} #creating dictionary to keep track of which recipe title for which recipe id

# search for recipes
def search_recipes(ingredient, cuisine, dietary): #filter results based on these sends requests
    global recipe_id_map
    url = "https://api.spoonacular.com/recipes/complexSearch"
    params = {
        "query": ingredient,
        "cuisine": cuisine,
        "diet": dietary,
        "number": 3,
        "apiKey": SPOONACULAR_API_KEY
    } #updates the dropdown with these titles and stores ids for later

    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.") #if no recipe 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.")
#asks user to choose a recipe from dropdown
# get recipe details from the recipe the user selects
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")
#creates interface using rows and columns layout
    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")
#creates 3 input fields side by side for ingredient, cuisine, restrictions
    search_button = gr.Button("Search Recipes")
    recipe_dropdown = gr.Dropdown(label="Select a recipe", visible=False)
    recipe_output = gr.Markdown()
#triggers the search, displays up to 3 recipes, shows the recipe selected
    search_button.click(
        fn=search_recipes,
        inputs=[ingredient, cuisine, diet],
        outputs=[recipe_dropdown, recipe_output]
    ) #when user clicks search runs search recipes function and fills dropdown

    recipe_dropdown.change(
        fn=get_recipe_details,
        inputs=recipe_dropdown,
        outputs=recipe_output
    ) #when user picks option, runs get recipe details and displays full recipe

demo.launch()