Spaces:
Runtime error
Runtime error
Update app.py
Browse files
app.py
CHANGED
|
@@ -3,46 +3,41 @@ from huggingface_hub import InferenceClient
|
|
| 3 |
from datasets import load_dataset
|
| 4 |
import pandas as pd
|
| 5 |
|
| 6 |
-
# --- Load the Indian Recipes Dataset ---
|
| 7 |
dataset = load_dataset("nf-analyst/indian_recipe")
|
| 8 |
recipes = dataset["train"].to_pandas()
|
| 9 |
|
| 10 |
-
#
|
|
|
|
|
|
|
|
|
|
|
|
|
| 11 |
recipes["search_text"] = (
|
| 12 |
-
recipes["
|
| 13 |
-
recipes["
|
| 14 |
-
recipes["
|
| 15 |
)
|
| 16 |
|
| 17 |
-
# ---
|
| 18 |
from sentence_transformers import SentenceTransformer
|
| 19 |
import faiss
|
| 20 |
import numpy as np
|
| 21 |
|
| 22 |
-
# Initialize semantic search
|
| 23 |
model = SentenceTransformer("all-MiniLM-L6-v2")
|
| 24 |
embeddings = model.encode(recipes["search_text"].tolist())
|
| 25 |
index = faiss.IndexFlatL2(embeddings.shape[1])
|
| 26 |
index.add(embeddings)
|
| 27 |
|
| 28 |
def search_recipes(query, top_k=3):
|
| 29 |
-
# Semantic search first
|
| 30 |
query_embedding = model.encode([query])
|
| 31 |
distances, indices = index.search(query_embedding, top_k)
|
| 32 |
-
|
| 33 |
-
|
| 34 |
-
|
| 35 |
-
|
| 36 |
-
|
| 37 |
-
|
| 38 |
-
|
| 39 |
-
"name": recipe["name"],
|
| 40 |
-
"ingredients": recipe["ingredients"],
|
| 41 |
-
"instructions": recipe["instructions"],
|
| 42 |
-
"cook_time": recipe["cook_time"],
|
| 43 |
-
"diet": recipe["diet"]
|
| 44 |
-
})
|
| 45 |
-
return results if results else None
|
| 46 |
|
| 47 |
# --- Modified Respond Function ---
|
| 48 |
client = InferenceClient("HuggingFaceH4/zephyr-7b-beta")
|
|
@@ -55,36 +50,30 @@ def respond(
|
|
| 55 |
temperature,
|
| 56 |
top_p,
|
| 57 |
):
|
| 58 |
-
# Search our dataset first
|
| 59 |
found_recipes = search_recipes(message)
|
| 60 |
|
| 61 |
if not found_recipes:
|
| 62 |
yield "No matching Indian recipes found. Try terms like 'butter chicken', 'biryani', or 'dal tadka'."
|
| 63 |
return
|
| 64 |
|
| 65 |
-
# Format recipes as strict context
|
| 66 |
recipe_context = "\n\n".join([
|
| 67 |
-
f"Recipe {i+1}: {r['name']} ({r['
|
| 68 |
-
f"Ingredients: {
|
| 69 |
-
f"Method: {
|
| 70 |
for i, r in enumerate(found_recipes)
|
| 71 |
])
|
| 72 |
|
| 73 |
-
|
| 74 |
-
strict_system_prompt = f"""You are an Indian food expert. ONLY recommend from these verified recipes.
|
| 75 |
-
NEVER invent recipes. If asked for variations, suggest only minor modifications to these:
|
| 76 |
|
| 77 |
{recipe_context}
|
| 78 |
|
| 79 |
Respond in this format:
|
| 80 |
-
1. First
|
| 81 |
-
2.
|
| 82 |
-
3.
|
| 83 |
-
"""
|
| 84 |
|
| 85 |
messages = [{"role": "system", "content": strict_system_prompt}]
|
| 86 |
|
| 87 |
-
# Add conversation history
|
| 88 |
for user_msg, bot_msg in history:
|
| 89 |
if user_msg:
|
| 90 |
messages.append({"role": "user", "content": user_msg})
|
|
@@ -93,7 +82,6 @@ def respond(
|
|
| 93 |
|
| 94 |
messages.append({"role": "user", "content": message})
|
| 95 |
|
| 96 |
-
# Generate response
|
| 97 |
response = ""
|
| 98 |
for chunk in client.chat_completion(
|
| 99 |
messages,
|
|
@@ -106,7 +94,7 @@ def respond(
|
|
| 106 |
response += token
|
| 107 |
yield response
|
| 108 |
|
| 109 |
-
# --- Gradio Interface
|
| 110 |
demo = gr.ChatInterface(
|
| 111 |
respond,
|
| 112 |
additional_inputs=[
|
|
@@ -121,11 +109,8 @@ demo = gr.ChatInterface(
|
|
| 121 |
examples=[
|
| 122 |
"Vegetarian North Indian dinner",
|
| 123 |
"Quick chicken curry",
|
| 124 |
-
"Traditional South Indian breakfast"
|
| 125 |
-
|
| 126 |
-
],
|
| 127 |
-
title="π Authentic Indian Recipe Assistant",
|
| 128 |
-
description="Get recommendations ONLY from verified Indian recipes"
|
| 129 |
)
|
| 130 |
|
| 131 |
if __name__ == "__main__":
|
|
|
|
| 3 |
from datasets import load_dataset
|
| 4 |
import pandas as pd
|
| 5 |
|
| 6 |
+
# --- Load and Inspect the Indian Recipes Dataset ---
|
| 7 |
dataset = load_dataset("nf-analyst/indian_recipe")
|
| 8 |
recipes = dataset["train"].to_pandas()
|
| 9 |
|
| 10 |
+
# Print column names to verify structure
|
| 11 |
+
print("Available columns:", recipes.columns.tolist())
|
| 12 |
+
|
| 13 |
+
# --- Adjusted Preprocessing ---
|
| 14 |
+
# Based on the dataset's actual columns (replace these with actual column names from the print output)
|
| 15 |
recipes["search_text"] = (
|
| 16 |
+
recipes["RecipeName"] + " " +
|
| 17 |
+
recipes["TranslatedIngredients"] + " " +
|
| 18 |
+
recipes["TranslatedInstructions"]
|
| 19 |
)
|
| 20 |
|
| 21 |
+
# --- Semantic Search Setup ---
|
| 22 |
from sentence_transformers import SentenceTransformer
|
| 23 |
import faiss
|
| 24 |
import numpy as np
|
| 25 |
|
|
|
|
| 26 |
model = SentenceTransformer("all-MiniLM-L6-v2")
|
| 27 |
embeddings = model.encode(recipes["search_text"].tolist())
|
| 28 |
index = faiss.IndexFlatL2(embeddings.shape[1])
|
| 29 |
index.add(embeddings)
|
| 30 |
|
| 31 |
def search_recipes(query, top_k=3):
|
|
|
|
| 32 |
query_embedding = model.encode([query])
|
| 33 |
distances, indices = index.search(query_embedding, top_k)
|
| 34 |
+
return [{
|
| 35 |
+
"name": recipes.iloc[i]["RecipeName"],
|
| 36 |
+
"ingredients": recipes.iloc[i]["TranslatedIngredients"],
|
| 37 |
+
"instructions": recipes.iloc[i]["TranslatedInstructions"],
|
| 38 |
+
"cuisine": recipes.iloc[i]["Cuisine"],
|
| 39 |
+
"course": recipes.iloc[i]["Course"]
|
| 40 |
+
} for i in indices[0]]
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 41 |
|
| 42 |
# --- Modified Respond Function ---
|
| 43 |
client = InferenceClient("HuggingFaceH4/zephyr-7b-beta")
|
|
|
|
| 50 |
temperature,
|
| 51 |
top_p,
|
| 52 |
):
|
|
|
|
| 53 |
found_recipes = search_recipes(message)
|
| 54 |
|
| 55 |
if not found_recipes:
|
| 56 |
yield "No matching Indian recipes found. Try terms like 'butter chicken', 'biryani', or 'dal tadka'."
|
| 57 |
return
|
| 58 |
|
|
|
|
| 59 |
recipe_context = "\n\n".join([
|
| 60 |
+
f"Recipe {i+1}: {r['name']} ({r['cuisine']}, {r['course']})\n"
|
| 61 |
+
f"Ingredients: {r['ingredients']}\n"
|
| 62 |
+
f"Method: {r['instructions'][:200]}..."
|
| 63 |
for i, r in enumerate(found_recipes)
|
| 64 |
])
|
| 65 |
|
| 66 |
+
strict_system_prompt = f"""You are an Indian food expert. ONLY recommend from these verified recipes:
|
|
|
|
|
|
|
| 67 |
|
| 68 |
{recipe_context}
|
| 69 |
|
| 70 |
Respond in this format:
|
| 71 |
+
1. First list matching recipe names
|
| 72 |
+
2. Only provide details when explicitly asked
|
| 73 |
+
3. Never invent recipes or ingredients"""
|
|
|
|
| 74 |
|
| 75 |
messages = [{"role": "system", "content": strict_system_prompt}]
|
| 76 |
|
|
|
|
| 77 |
for user_msg, bot_msg in history:
|
| 78 |
if user_msg:
|
| 79 |
messages.append({"role": "user", "content": user_msg})
|
|
|
|
| 82 |
|
| 83 |
messages.append({"role": "user", "content": message})
|
| 84 |
|
|
|
|
| 85 |
response = ""
|
| 86 |
for chunk in client.chat_completion(
|
| 87 |
messages,
|
|
|
|
| 94 |
response += token
|
| 95 |
yield response
|
| 96 |
|
| 97 |
+
# --- Gradio Interface ---
|
| 98 |
demo = gr.ChatInterface(
|
| 99 |
respond,
|
| 100 |
additional_inputs=[
|
|
|
|
| 109 |
examples=[
|
| 110 |
"Vegetarian North Indian dinner",
|
| 111 |
"Quick chicken curry",
|
| 112 |
+
"Traditional South Indian breakfast"
|
| 113 |
+
]
|
|
|
|
|
|
|
|
|
|
| 114 |
)
|
| 115 |
|
| 116 |
if __name__ == "__main__":
|