Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
|
@@ -1,93 +0,0 @@
|
|
| 1 |
-
import gradio as gr
|
| 2 |
-
import requests
|
| 3 |
-
|
| 4 |
-
def response(message, history):
|
| 5 |
-
bitebot_chunks = get_top_chunks(message, chunk_embeddings, cleaned_chunks)
|
| 6 |
-
print(bitebot)
|
| 7 |
-
str_bitebot_chunks = "\n".join(bitebot_chunks)
|
| 8 |
-
messages = [{"role" : "system", "content" : "You are a helpful chef who provides recipes according the the user's dietary choices and cusine preferences."}]
|
| 9 |
-
# update response person on provided context; use f-strings
|
| 10 |
-
|
| 11 |
-
if history:
|
| 12 |
-
messages.extend(history)
|
| 13 |
-
|
| 14 |
-
messages.append({"role" : "user", "content" : message})
|
| 15 |
-
|
| 16 |
-
response = client.chat_completion(
|
| 17 |
-
messages,
|
| 18 |
-
max_tokens = 100
|
| 19 |
-
# temperature == 0.2
|
| 20 |
-
)
|
| 21 |
-
return response['choices'][0]['message']['content'].strip()
|
| 22 |
-
|
| 23 |
-
|
| 24 |
-
|
| 25 |
-
|
| 26 |
-
SPOONACULAR_API_KEY = "71259036cfb3405aa5d49c1220a988c5" #our api key
|
| 27 |
-
recipe_id_map = {} #creating dictionary to keep track of which recipe title for which recipe id
|
| 28 |
-
|
| 29 |
-
# search for recipes
|
| 30 |
-
def search_recipes(ingredient, cuisine, dietary): #filter results based on these sends requests
|
| 31 |
-
global recipe_id_map
|
| 32 |
-
url = "https://api.spoonacular.com/recipes/complexSearch"
|
| 33 |
-
params = {
|
| 34 |
-
"query": ingredient,
|
| 35 |
-
"cuisine": cuisine,
|
| 36 |
-
"diet": dietary,
|
| 37 |
-
"number": 3,
|
| 38 |
-
"apiKey": SPOONACULAR_API_KEY
|
| 39 |
-
} #updates the dropdown with these titles and stores ids for later
|
| 40 |
-
|
| 41 |
-
res = requests.get(url, params=params)
|
| 42 |
-
data = res.json()
|
| 43 |
-
|
| 44 |
-
if "results" not in data or not data["results"]:
|
| 45 |
-
recipe_id_map = {}
|
| 46 |
-
return gr.update(choices=[], visible=True, label="No recipes found"), gr.update(value="No recipes found.") #if no recipe found
|
| 47 |
-
|
| 48 |
-
recipe_id_map = {r["title"]: r["id"] for r in data["results"]}
|
| 49 |
-
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.")
|
| 50 |
-
#asks user to choose a recipe from dropdown
|
| 51 |
-
# get recipe details from the recipe the user selects
|
| 52 |
-
def get_recipe_details(selected_title):
|
| 53 |
-
if not selected_title or selected_title not in recipe_id_map:
|
| 54 |
-
return "Please select a valid recipe."
|
| 55 |
-
|
| 56 |
-
recipe_id = recipe_id_map[selected_title]
|
| 57 |
-
url = f"https://api.spoonacular.com/recipes/{recipe_id}/information"
|
| 58 |
-
params = {"apiKey": SPOONACULAR_API_KEY}
|
| 59 |
-
res = requests.get(url, params=params)
|
| 60 |
-
data = res.json()
|
| 61 |
-
|
| 62 |
-
title = data.get("title", "Unknown Title")
|
| 63 |
-
time = data.get("readyInMinutes", "N/A")
|
| 64 |
-
instructions = data.get("instructions") or "No instructions available."
|
| 65 |
-
return f"### 🍽️ {title}\n**⏱️ Cook Time:** {time} minutes\n\n**📋 Instructions:**\n{instructions}"
|
| 66 |
-
|
| 67 |
-
|
| 68 |
-
# UI
|
| 69 |
-
with gr.Blocks() as demo:
|
| 70 |
-
gr.Markdown("## 🥗 The BiteBot")
|
| 71 |
-
#creates interface using rows and columns layout
|
| 72 |
-
with gr.Row():
|
| 73 |
-
ingredient = gr.Textbox(label="Preferred Ingredient", placeholder="e.g., chicken")
|
| 74 |
-
cuisine = gr.Textbox(label="Preferred Cuisine", placeholder="e.g., Indian")
|
| 75 |
-
diet = gr.Textbox(label="Dietary Restrictions", placeholder="e.g., vegetarian")
|
| 76 |
-
#creates 3 input fields side by side for ingredient, cuisine, restrictions
|
| 77 |
-
search_button = gr.Button("Search Recipes")
|
| 78 |
-
recipe_dropdown = gr.Dropdown(label="Select a recipe", visible=False)
|
| 79 |
-
recipe_output = gr.Markdown()
|
| 80 |
-
#triggers the search, displays up to 3 recipes, shows the recipe selected
|
| 81 |
-
search_button.click(
|
| 82 |
-
fn=search_recipes,
|
| 83 |
-
inputs=[ingredient, cuisine, diet],
|
| 84 |
-
outputs=[recipe_dropdown, recipe_output]
|
| 85 |
-
) #when user clicks search runs search recipes function and fills dropdown
|
| 86 |
-
|
| 87 |
-
recipe_dropdown.change(
|
| 88 |
-
fn=get_recipe_details,
|
| 89 |
-
inputs=recipe_dropdown,
|
| 90 |
-
outputs=recipe_output
|
| 91 |
-
) #when user picks option, runs get recipe details and displays full recipe
|
| 92 |
-
|
| 93 |
-
demo.launch()
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|