Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
|
@@ -14,13 +14,10 @@ start_time = time.time()
|
|
| 14 |
dataset = load_dataset("corbt/all-recipes", split="train[:20000]")
|
| 15 |
|
| 16 |
# Preprocessing functions to extract features from the raw text
|
| 17 |
-
def extract_title_and_ingredients(sample):
|
| 18 |
-
extraction = sample['input'][:sample['input'].find("Directions")]
|
| 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")]
|
| 25 |
|
| 26 |
ingredients = "Not available"
|
|
@@ -51,7 +48,6 @@ def extract_each_feature(sample):
|
|
| 51 |
"directions": directions,
|
| 52 |
}
|
| 53 |
|
| 54 |
-
|
| 55 |
# Apply preprocessing
|
| 56 |
dataset = dataset.map(extract_each_feature)
|
| 57 |
|
|
@@ -89,9 +85,11 @@ def get_recommendations_and_generate(query_ingredients, k=3):
|
|
| 89 |
while len(results) < 3:
|
| 90 |
results.append({"title": "No recipe found", "ingredients": "", "directions": ""})
|
| 91 |
|
| 92 |
-
# 2. Generate 10 new recipe ideas
|
| 93 |
-
prompt = f"
|
| 94 |
-
|
|
|
|
|
|
|
| 95 |
|
| 96 |
# 3. Find the best recipe out of the 10 generated
|
| 97 |
generated_texts = [output['generated_text'].replace(prompt, "").strip() for output in generated_outputs]
|
|
@@ -116,7 +114,7 @@ def format_recipe(recipe):
|
|
| 116 |
return f"### {recipe['title']}\n**Ingredients:**\n{recipe['ingredients']}\n\n**Directions:**\n{recipe['directions']}"
|
| 117 |
|
| 118 |
def format_generated_recipe(recipe_text):
|
| 119 |
-
# Formats the AI-generated recipe as simple text
|
| 120 |
return recipe_text
|
| 121 |
|
| 122 |
def recipe_wizard(ingredients):
|
|
@@ -143,7 +141,7 @@ with gr.Blocks(theme=gr.themes.Soft()) as demo:
|
|
| 143 |
output_rec3 = gr.Markdown()
|
| 144 |
with gr.Column(scale=1):
|
| 145 |
gr.Markdown("### ✨ AI-Generated Idea")
|
| 146 |
-
output_gen = gr.
|
| 147 |
|
| 148 |
submit_btn.click(
|
| 149 |
fn=recipe_wizard,
|
|
|
|
| 14 |
dataset = load_dataset("corbt/all-recipes", split="train[:20000]")
|
| 15 |
|
| 16 |
# Preprocessing functions to extract features from the raw text
|
|
|
|
|
|
|
|
|
|
|
|
|
| 17 |
def extract_each_feature(sample):
|
| 18 |
full_text = sample['input']
|
| 19 |
|
| 20 |
+
# User's fix: Use "\n" instead of "\\n" to correctly find the title
|
| 21 |
title = full_text[:full_text.find("\n")]
|
| 22 |
|
| 23 |
ingredients = "Not available"
|
|
|
|
| 48 |
"directions": directions,
|
| 49 |
}
|
| 50 |
|
|
|
|
| 51 |
# Apply preprocessing
|
| 52 |
dataset = dataset.map(extract_each_feature)
|
| 53 |
|
|
|
|
| 85 |
while len(results) < 3:
|
| 86 |
results.append({"title": "No recipe found", "ingredients": "", "directions": ""})
|
| 87 |
|
| 88 |
+
# 2. Generate 10 new recipe ideas with a simpler, more direct prompt
|
| 89 |
+
prompt = f"Write a complete recipe that includes a title, a list of ingredients, and step-by-step directions. The recipe must use the following ingredients: {query_ingredients}."
|
| 90 |
+
|
| 91 |
+
# Optimized for speed by reducing max_new_tokens
|
| 92 |
+
generated_outputs = generator(prompt, max_new_tokens=180, num_return_sequences=10, pad_token_id=50256)
|
| 93 |
|
| 94 |
# 3. Find the best recipe out of the 10 generated
|
| 95 |
generated_texts = [output['generated_text'].replace(prompt, "").strip() for output in generated_outputs]
|
|
|
|
| 114 |
return f"### {recipe['title']}\n**Ingredients:**\n{recipe['ingredients']}\n\n**Directions:**\n{recipe['directions']}"
|
| 115 |
|
| 116 |
def format_generated_recipe(recipe_text):
|
| 117 |
+
# Formats the AI-generated recipe as simple text, without extra markdown
|
| 118 |
return recipe_text
|
| 119 |
|
| 120 |
def recipe_wizard(ingredients):
|
|
|
|
| 141 |
output_rec3 = gr.Markdown()
|
| 142 |
with gr.Column(scale=1):
|
| 143 |
gr.Markdown("### ✨ AI-Generated Idea")
|
| 144 |
+
output_gen = gr.Textbox(label="AI Generated Recipe", lines=15) # Changed to Textbox for plain text
|
| 145 |
|
| 146 |
submit_btn.click(
|
| 147 |
fn=recipe_wizard,
|