TheHuriShow commited on
Commit
d782710
·
verified ·
1 Parent(s): a412584

Upload 3 files

Browse files
Files changed (4) hide show
  1. .gitattributes +1 -0
  2. app.py +131 -0
  3. recipe_index.faiss +3 -0
  4. requirements.txt +8 -0
.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
app.py ADDED
@@ -0,0 +1,131 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import gradio as gr
2
+ from datasets import load_dataset
3
+ from sentence_transformers import SentenceTransformer
4
+ import faiss
5
+ import numpy as np
6
+ import os
7
+ from transformers import pipeline
8
+ import time
9
+
10
+ # --- 1. DATA LOADING AND PREPROCESSING ---
11
+ print("===== Application Startup =====")
12
+ start_time = time.time()
13
+
14
+ # Load dataset
15
+ dataset = load_dataset("corbt/all-recipes", split="train[:20000]")
16
+
17
+ # Preprocessing functions
18
+ def extract_title_and_ingredients(sample):
19
+ extraction = sample['input'][:sample['input'].find("Directions")]
20
+ return {"text_for_embedding": extraction}
21
+
22
+ def extract_each_feature(sample):
23
+ title = sample['input'][:sample['input'].find("\\n")]
24
+ ingredients = sample['input'][sample['input'].find("Ingredients")+len("Ingredients:\\n"):sample['input'].find("Directions")].strip()
25
+ directions = sample['input'][sample['input'].find("Directions")+len("Directions:\\n"):].strip()
26
+ return {"title": title, "ingredients": ingredients, "directions": directions}
27
+
28
+ # Apply preprocessing
29
+ dataset = dataset.map(extract_title_and_ingredients)
30
+ dataset = dataset.map(extract_each_feature)
31
+
32
+ # --- 2. EMBEDDING AND RECOMMENDATION ENGINE ---
33
+ model_name = "all-MiniLM-L6-v2"
34
+ embedding_model = SentenceTransformer(f"sentence-transformers/{model_name}")
35
+
36
+ # Compute embeddings
37
+ print("Loading dataset and embedding model...")
38
+ embeddings = embedding_model.encode(dataset['text_for_embedding'], show_progress_bar=True)
39
+ embeddings = np.array(embeddings, dtype=np.float32)
40
+
41
+ # Build FAISS index
42
+ dimension = embeddings.shape[1]
43
+ index = faiss.IndexFlatL2(dimension)
44
+ index.add(embeddings)
45
+ print(f"Index is ready. Total vectors in index: {index.ntotal}")
46
+
47
+ # --- 3. SYNTHETIC GENERATION ---
48
+ generator = pipeline('text-generation', model='gpt2')
49
+
50
+ def get_recommendations_and_generate(query_ingredients, k=3):
51
+ # 1. Get Recommendations
52
+ query_vector = embedding_model.encode([query_ingredients])
53
+ query_vector = np.array(query_vector, dtype=np.float32)
54
+ distances, indices = index.search(query_vector, k)
55
+
56
+ results = []
57
+ for i, idx_numpy in enumerate(indices[0]):
58
+ idx = int(idx_numpy) # FIX: Convert numpy.int64 to standard Python int
59
+ recipe = {
60
+ "title": dataset[idx]['title'],
61
+ "ingredients": dataset[idx]['ingredients'],
62
+ "directions": dataset[idx]['directions']
63
+ }
64
+ results.append(recipe)
65
+
66
+ # 2. Generate a new recipe idea
67
+ prompt = f"Create a short, simple recipe title and a list of ingredients using: {query_ingredients}."
68
+ generated_text = generator(prompt, max_length=100, num_return_sequences=1)[0]['generated_text']
69
+
70
+ # Clean up generated text to be more readable
71
+ # (This is a basic cleanup, can be improved)
72
+ generated_recipe_parts = generated_text.split("Ingredients:")
73
+ generated_title = generated_recipe_parts[0].replace(prompt.replace(f"using: {query_ingredients}",""), "").strip()
74
+ generated_ingredients = generated_recipe_parts[1].strip() if len(generated_recipe_parts) > 1 else "Could not determine ingredients."
75
+
76
+ generated_recipe = {
77
+ "title": generated_title,
78
+ "ingredients": generated_ingredients,
79
+ "directions": "This is an AI-generated idea. Directions are not provided."
80
+ }
81
+
82
+ return results[0], results[1], results[2], generated_recipe
83
+
84
+ # --- 4. GRADIO USER INTERFACE ---
85
+ def format_recipe(recipe):
86
+ if not recipe or not recipe['title']:
87
+ return "### No recipe found."
88
+ return f"### {recipe['title']}\n**Ingredients:**\n{recipe['ingredients']}\n\n**Directions:**\n{recipe['directions']}"
89
+
90
+ def recipe_wizard(ingredients):
91
+ rec1, rec2, rec3, gen_rec = get_recommendations_and_generate(ingredients)
92
+ return format_recipe(rec1), format_recipe(rec2), format_recipe(rec3), format_recipe(gen_rec)
93
+
94
+ end_time = time.time()
95
+ print(f"Models and data loaded in {end_time - start_time:.2f} seconds.")
96
+
97
+ # Gradio Interface
98
+ with gr.Blocks(theme=gr.themes.Soft()) as demo:
99
+ gr.Markdown("# 🍳 RecipeWizard AI")
100
+ gr.Markdown("Enter the ingredients you have, and get recipe recommendations plus a new AI-generated idea!")
101
+
102
+ with gr.Row():
103
+ ingredient_input = gr.Textbox(label="Your Ingredients", placeholder="e.g., chicken, rice, tomatoes, garlic")
104
+ submit_btn = gr.Button("Get Recipes")
105
+
106
+ with gr.Row():
107
+ with gr.Column():
108
+ gr.Markdown("### Recommended Recipes")
109
+ output_rec1 = gr.Markdown()
110
+ output_rec2 = gr.Markdown()
111
+ output_rec3 = gr.Markdown()
112
+ with gr.Column():
113
+ gr.Markdown("### ✨ AI-Generated Idea")
114
+ output_gen = gr.Markdown()
115
+
116
+ submit_btn.click(
117
+ fn=recipe_wizard,
118
+ inputs=ingredient_input,
119
+ outputs=[output_rec1, output_rec2, output_rec3, output_gen]
120
+ )
121
+
122
+ gr.Examples(
123
+ examples=[
124
+ ["chicken, broccoli, cheese"],
125
+ ["ground beef, potatoes, onions"],
126
+ ["flour, sugar, eggs, butter"]
127
+ ],
128
+ inputs=ingredient_input
129
+ )
130
+
131
+ 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 ADDED
@@ -0,0 +1,8 @@
 
 
 
 
 
 
 
 
 
1
+ torch==2.1.0
2
+ faiss-cpu==1.7.4
3
+ gradio==4.8.0
4
+ pyarrow==14.0.1
5
+ datasets==2.15.0
6
+ transformers==4.35.2
7
+ sentence-transformers==2.3.1
8
+ huggingface-hub==0.19.4