TheHuriShow commited on
Commit
2e29b2f
·
verified ·
1 Parent(s): 08f5251

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +32 -36
app.py CHANGED
@@ -19,9 +19,6 @@ def extract_title_and_ingredients(sample):
19
  return {"text_for_embedding": extraction}
20
 
21
  def extract_each_feature(sample):
22
- """
23
- FIXED: More robustly extracts recipe features and cleans duplicated content.
24
- """
25
  full_text = sample['input']
26
 
27
  title = full_text[:full_text.find("\n")]
@@ -65,7 +62,7 @@ embedding_model = SentenceTransformer(f"sentence-transformers/{model_name}")
65
  index_file = "recipe_index.faiss"
66
 
67
  print(f"Loading FAISS index from {index_file}...")
68
- # Load the pre-computed FAISS index instead of building it
69
  index = faiss.read_index(index_file)
70
  print(f"Index is ready. Total vectors in index: {index.ntotal}")
71
 
@@ -74,8 +71,8 @@ print("Loading generative model...")
74
  generator = pipeline('text-generation', model='gpt2-medium')
75
 
76
  def get_recommendations_and_generate(query_ingredients, k=3):
77
- # 1. Get Recommendations
78
- query_vector = embedding_model.encode([query_ingredients]) # We still need the model here for the query
79
  query_vector = np.array(query_vector, dtype=np.float32)
80
  distances, indices = index.search(query_vector, k)
81
 
@@ -89,22 +86,9 @@ def get_recommendations_and_generate(query_ingredients, k=3):
89
  }
90
  results.append(recipe)
91
 
92
- # 2. Generate a new recipe with a better "few-shot" prompt
93
- prompt = f"""Given the ingredients: {query_ingredients}, create a complete recipe.
94
 
95
- ### Title ###
96
- [A creative recipe title]
97
-
98
- ### Ingredients ###
99
- - [List of ingredients]
100
-
101
- ### Directions ###
102
- 1. [First step]
103
- 2. [Second step]
104
- 3. [And so on...]
105
-
106
- ### Title ###
107
- """
108
  # Generate the recipe text
109
  generated_outputs = generator(prompt, max_new_tokens=250, num_return_sequences=1, pad_token_id=50256)
110
  generated_text = generated_outputs[0]['generated_text'].replace(prompt, "").strip()
@@ -112,21 +96,33 @@ def get_recommendations_and_generate(query_ingredients, k=3):
112
  # 3. More robustly parse the generated text
113
  try:
114
  title = "AI Generated Recipe"
115
- ingredients = ""
116
- directions = ""
117
-
118
- if "### Ingredients ###" in generated_text:
119
- parts = generated_text.split("### Ingredients ###")
120
- title = parts[0].strip()
121
- rest = parts[1]
122
- if "### Directions ###" in rest:
123
- dir_parts = rest.split("### Directions ###")
124
- ingredients = dir_parts[0].strip()
125
- directions = dir_parts[1].strip()
126
- else:
127
- ingredients = rest.strip()
128
- else:
129
- directions = generated_text # Fallback
 
 
 
 
 
 
 
 
 
 
 
 
130
 
131
  generated_recipe = {
132
  "title": title,
 
19
  return {"text_for_embedding": extraction}
20
 
21
  def extract_each_feature(sample):
 
 
 
22
  full_text = sample['input']
23
 
24
  title = full_text[:full_text.find("\n")]
 
62
  index_file = "recipe_index.faiss"
63
 
64
  print(f"Loading FAISS index from {index_file}...")
65
+ # Load the pre-computed FAISS index
66
  index = faiss.read_index(index_file)
67
  print(f"Index is ready. Total vectors in index: {index.ntotal}")
68
 
 
71
  generator = pipeline('text-generation', model='gpt2-medium')
72
 
73
  def get_recommendations_and_generate(query_ingredients, k=3):
74
+ # 1. Get Recommendations (This part is unchanged)
75
+ query_vector = embedding_model.encode([query_ingredients])
76
  query_vector = np.array(query_vector, dtype=np.float32)
77
  distances, indices = index.search(query_vector, k)
78
 
 
86
  }
87
  results.append(recipe)
88
 
89
+ # 2. Generate a new recipe with a much simpler and more direct prompt
90
+ prompt = f"Create a simple and delicious recipe using the following ingredients: {query_ingredients}"
91
 
 
 
 
 
 
 
 
 
 
 
 
 
 
92
  # Generate the recipe text
93
  generated_outputs = generator(prompt, max_new_tokens=250, num_return_sequences=1, pad_token_id=50256)
94
  generated_text = generated_outputs[0]['generated_text'].replace(prompt, "").strip()
 
96
  # 3. More robustly parse the generated text
97
  try:
98
  title = "AI Generated Recipe"
99
+ ingredients = "Could not be determined."
100
+ directions = "Could not be determined."
101
+
102
+ # Split the text into lines for easier parsing
103
+ lines = generated_text.split('\n')
104
+
105
+ title = lines[0].strip() # The first line after the prompt is the title
106
+
107
+ # Find the start of ingredients and directions by looking for keywords
108
+ ing_index = -1
109
+ dir_index = -1
110
+ for i, line in enumerate(lines):
111
+ if "ingredients" in line.lower() and ing_index == -1:
112
+ ing_index = i
113
+ if "directions" in line.lower() and dir_index == -1:
114
+ dir_index = i
115
+
116
+ # Extract the sections based on where the keywords were found
117
+ if ing_index != -1 and dir_index != -1:
118
+ ingredients = "\n".join(lines[ing_index+1:dir_index]).strip()
119
+ directions = "\n".join(lines[dir_index+1:]).strip()
120
+ elif ing_index != -1: # Only ingredients were found
121
+ ingredients = "\n".join(lines[ing_index+1:]).strip()
122
+ elif dir_index != -1: # Only directions were found
123
+ directions = "\n".join(lines[dir_index+1:]).strip()
124
+ else: # If no headers are found, assume the rest of the text is the directions
125
+ directions = "\n".join(lines[1:]).strip()
126
 
127
  generated_recipe = {
128
  "title": title,