Spaces:
Sleeping
Sleeping
Upload 4 files
Browse files- .gitattributes +1 -0
- README.txt +7 -0
- app.py +121 -60
- recipe_index.faiss +3 -0
- requirements.txt +6 -1
.gitattributes
CHANGED
|
@@ -33,3 +33,4 @@ saved_model/**/* filter=lfs diff=lfs merge=lfs -text
|
|
| 33 |
*.zip filter=lfs diff=lfs merge=lfs -text
|
| 34 |
*.zst filter=lfs diff=lfs merge=lfs -text
|
| 35 |
*tfevents* filter=lfs diff=lfs merge=lfs -text
|
|
|
|
|
|
| 33 |
*.zip filter=lfs diff=lfs merge=lfs -text
|
| 34 |
*.zst filter=lfs diff=lfs merge=lfs -text
|
| 35 |
*tfevents* filter=lfs diff=lfs merge=lfs -text
|
| 36 |
+
recipe_index.faiss filter=lfs diff=lfs merge=lfs -text
|
README.txt
ADDED
|
@@ -0,0 +1,7 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
🍳 Recipe Genius: Your AI Kitchen Assistant
|
| 2 |
+
|
| 3 |
+
Ever wonder what to cook with the ingredients you already have? Recipe Genius solves that problem.
|
| 4 |
+
|
| 5 |
+
How it works: Simply list your ingredients (e.g., "chicken, rice, tomatoes").
|
| 6 |
+
|
| 7 |
+
What you get: The app uses vector search to find the 3 most similar recipes from a database of over 50,000. It also uses a generative AI to invent a completely new recipe title for you on the spot.
|
app.py
CHANGED
|
@@ -1,64 +1,125 @@
|
|
|
|
|
|
|
|
| 1 |
import gradio as gr
|
| 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 |
-
demo.launch()
|
|
|
|
| 1 |
+
# app.py
|
| 2 |
+
|
| 3 |
import gradio as gr
|
| 4 |
+
import faiss
|
| 5 |
+
import numpy as np
|
| 6 |
+
from datasets import load_dataset
|
| 7 |
+
from sentence_transformers import SentenceTransformer, util
|
| 8 |
+
from transformers import pipeline
|
| 9 |
+
import time
|
| 10 |
+
|
| 11 |
+
# --- 1. LOAD MODELS AND DATA (GLOBAL SCOPE) ---
|
| 12 |
+
# This section runs only once when the app starts.
|
| 13 |
+
|
| 14 |
+
print("Loading dataset and embedding model...")
|
| 15 |
+
start_time = time.time()
|
| 16 |
+
|
| 17 |
+
# Load the dataset
|
| 18 |
+
dataset = load_dataset("corbt/all-recipes", split="train[:20000]")
|
| 19 |
+
|
| 20 |
+
# Extract title and ingredients for embedding
|
| 21 |
+
def extract_title_and_ingredients(sample):
|
| 22 |
+
"""
|
| 23 |
+
Extract the title and ingredients of a recipe from a sample.
|
| 24 |
+
"""
|
| 25 |
+
extraction = sample['input'][:sample['input'].find("Directions")]
|
| 26 |
+
return {
|
| 27 |
+
"text_for_embedding": extraction
|
| 28 |
+
}
|
| 29 |
+
|
| 30 |
+
dataset = dataset.map(extract_title_and_ingredients)
|
| 31 |
+
|
| 32 |
+
|
| 33 |
+
# Extract all features
|
| 34 |
+
def extract_each_feature(sample):
|
| 35 |
+
"""
|
| 36 |
+
Extract each feature of a recipe from a sample.
|
| 37 |
+
"""
|
| 38 |
+
title = sample['input'][:sample['input'].find("\n")]
|
| 39 |
+
ingredients = sample['input'][sample['input'].find("Ingredients")+len("Ingredients:\n"):sample['input'].find("Directions")].strip()
|
| 40 |
+
directions = sample['input'][sample['input'].find("Directions")+len("Directions:\n"):].strip()
|
| 41 |
+
return {
|
| 42 |
+
"title": title,
|
| 43 |
+
"ingredients": ingredients,
|
| 44 |
+
"directions": directions,
|
| 45 |
+
}
|
| 46 |
+
|
| 47 |
+
dataset = dataset.map(extract_each_feature)
|
| 48 |
+
|
| 49 |
+
# Load the embedding model
|
| 50 |
+
embedding_model = SentenceTransformer("sentence-transformers/all-MiniLM-L6-v2")
|
| 51 |
+
|
| 52 |
+
# Load the pre-built FAISS index
|
| 53 |
+
try:
|
| 54 |
+
index = faiss.read_index("recipe_index.faiss")
|
| 55 |
+
except Exception as e:
|
| 56 |
+
print(f"Could not load FAISS index. Error: {e}. Please ensure 'recipe_index.faiss' is in the same directory.")
|
| 57 |
+
# Handle error gracefully, maybe by disabling the search feature
|
| 58 |
+
index = None
|
| 59 |
+
|
| 60 |
+
# Load the text generation model
|
| 61 |
+
generator = pipeline('text-generation', model='distilgpt2')
|
| 62 |
+
|
| 63 |
+
end_time = time.time()
|
| 64 |
+
print(f"Models and data loaded in {end_time - start_time:.2f} seconds.")
|
| 65 |
+
|
| 66 |
+
|
| 67 |
+
# --- 2. DEFINE THE CORE LOGIC FUNCTION ---
|
| 68 |
+
def find_and_generate(user_ingredients):
|
| 69 |
+
"""
|
| 70 |
+
Takes user ingredients, finds similar recipes, and generates a new one.
|
| 71 |
+
"""
|
| 72 |
+
if not user_ingredients:
|
| 73 |
+
return "<p>Please enter some ingredients.</p>", "<p></p>"
|
| 74 |
+
|
| 75 |
+
# --- Recommendation Logic ---
|
| 76 |
+
if index:
|
| 77 |
+
query_vector = embedding_model.encode([user_ingredients])
|
| 78 |
+
distances, indices = index.search(np.array(query_vector, dtype=np.float32), 3)
|
| 79 |
+
|
| 80 |
+
recs_html = "<h2>Top 3 Similar Recipes:</h2>"
|
| 81 |
+
for i, idx in enumerate(indices[0]):
|
| 82 |
+
# Use .get() for safety in case a key is missing
|
| 83 |
+
title = dataset[int(idx)].get('title', dataset[int(idx)].get('Name', 'No Title'))
|
| 84 |
+
ingredients_list = dataset[int(idx)].get('ingredients', dataset[int(idx)].get('RecipeIngredientParts', []))
|
| 85 |
+
ingredients = "<br>".join(ingredients_list)
|
| 86 |
+
|
| 87 |
+
recs_html += f"<h3>{i+1}. {title}</h3>"
|
| 88 |
+
recs_html += f"<b>Ingredients:</b><br>{ingredients}<hr>"
|
| 89 |
+
else:
|
| 90 |
+
recs_html = "<h2>Recommendation engine not available.</h2> <p>Could not load the FAISS index file.</p>"
|
| 91 |
+
|
| 92 |
+
|
| 93 |
+
# --- Generation Logic ---
|
| 94 |
+
prompt = f"A creative and delicious recipe that features {user_ingredients}.\n\nRecipe Title:"
|
| 95 |
+
generated_outputs = generator(prompt, max_new_tokens=100, num_return_sequences=1)
|
| 96 |
+
generated_text = generated_outputs[0]['generated_text']
|
| 97 |
+
|
| 98 |
+
gen_html = "<h2>AI-Generated Idea:</h2>"
|
| 99 |
+
gen_html += generated_text.replace("\n", "<br>")
|
| 100 |
+
|
| 101 |
+
return recs_html, gen_html
|
| 102 |
+
|
| 103 |
+
# --- 3. CREATE AND LAUNCH THE GRADIO INTERFACE ---
|
| 104 |
+
|
| 105 |
+
examples = [
|
| 106 |
+
["chicken, potatoes, carrots, onions"],
|
| 107 |
+
["beef, soy sauce, ginger, rice"],
|
| 108 |
+
["tomatoes, basil, mozzarella, olive oil"],
|
| 109 |
+
]
|
| 110 |
+
|
| 111 |
+
demo = gr.Interface(
|
| 112 |
+
fn=find_and_generate,
|
| 113 |
+
inputs=gr.Textbox(lines=3, label="Enter Your Ingredients (comma-separated)"),
|
| 114 |
+
outputs=[
|
| 115 |
+
gr.HTML(label="Similar Recipes"),
|
| 116 |
+
gr.HTML(label="AI Generated Recipe")
|
| 117 |
],
|
| 118 |
+
title="🍳 Recipe Genius",
|
| 119 |
+
description="Your personal AI chef! Enter the ingredients you have, and get 3 real recipe recommendations plus 1 new AI-generated idea.",
|
| 120 |
+
examples=examples,
|
| 121 |
+
theme=gr.themes.Soft()
|
| 122 |
)
|
| 123 |
|
| 124 |
+
# Launch the app!
|
| 125 |
+
demo.launch()
|
|
|
recipe_index.faiss
ADDED
|
@@ -0,0 +1,3 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
version https://git-lfs.github.com/spec/v1
|
| 2 |
+
oid sha256:90b9a5c8797e28a0fe4130d9af7ccdb897d0849110ea43765aee3b7b670b14ef
|
| 3 |
+
size 30720045
|
requirements.txt
CHANGED
|
@@ -1 +1,6 @@
|
|
| 1 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
datasets
|
| 2 |
+
sentence-transformers
|
| 3 |
+
torch
|
| 4 |
+
faiss-cpu
|
| 5 |
+
transformers
|
| 6 |
+
gradio
|