krisha06 commited on
Commit
8d6042f
·
verified ·
1 Parent(s): 81ac65f

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +24 -15
app.py CHANGED
@@ -15,6 +15,10 @@ def load_recipes():
15
 
16
  recipes_df = load_recipes()
17
 
 
 
 
 
18
  # Load embedding model
19
  @st.cache_resource
20
  def load_embedding_model():
@@ -26,18 +30,23 @@ embed_model = load_embedding_model()
26
  chroma_client = chromadb.PersistentClient(path="./chroma_db") # Saves vectors
27
  recipe_collection = chroma_client.get_or_create_collection(name="recipes")
28
 
29
- # If embeddings are not stored, create them
30
- if recipe_collection.count() == 0:
31
- st.info("Indexing recipes... This will take a few minutes.")
32
- for i, recipe in enumerate(recipes_df):
33
- title = recipe["title"]
34
- ingredients = ", ".join(recipe["ingredients"])
35
- embedding = embed_model.encode(title).tolist()
36
- recipe_collection.add(
37
- ids=[str(i)],
38
- embeddings=[embedding],
39
- metadatas=[{"title": title, "ingredients": ingredients, "index": i}],
40
- )
 
 
 
 
 
41
 
42
  # UI
43
  st.title("🍽️ AI Recipe Finder with ChromaDB RAG")
@@ -53,9 +62,9 @@ if query:
53
  for result in results["metadatas"][0]:
54
  index = result["index"]
55
  recipe = recipes_df[index]
56
- st.write(f"**🍴 {recipe['title']}**")
57
- st.write(f"**Ingredients:** {', '.join(recipe['ingredients'])}")
58
- st.write(f"**Instructions:** {recipe['instructions']}")
59
  st.write("---")
60
  else:
61
  st.info("Type a recipe name to find similar recipes.")
 
15
 
16
  recipes_df = load_recipes()
17
 
18
+ if recipes_df is None:
19
+ st.error("❌ Failed to load dataset! Check internet or dataset availability.")
20
+ st.stop() # Stops Streamlit from running further if the dataset isn't loaded
21
+
22
  # Load embedding model
23
  @st.cache_resource
24
  def load_embedding_model():
 
30
  chroma_client = chromadb.PersistentClient(path="./chroma_db") # Saves vectors
31
  recipe_collection = chroma_client.get_or_create_collection(name="recipes")
32
 
33
+ # Ensure recipes_df is iterable
34
+ if isinstance(recipes_df, list) or isinstance(recipes_df, dict):
35
+ if recipe_collection.count() == 0:
36
+ st.info("Indexing recipes... This will take a few minutes.")
37
+ for i, recipe in enumerate(recipes_df):
38
+ title = recipe.get("title", "Unknown Title") # Handle missing keys
39
+ ingredients = ", ".join(recipe.get("ingredients", []))
40
+ instructions = recipe.get("instructions", "No instructions available")
41
+
42
+ embedding = embed_model.encode(title).tolist()
43
+ recipe_collection.add(
44
+ ids=[str(i)],
45
+ embeddings=[embedding],
46
+ metadatas=[{"title": title, "ingredients": ingredients, "index": i}],
47
+ )
48
+ else:
49
+ st.error("❌ Dataset is not in the correct format!")
50
 
51
  # UI
52
  st.title("🍽️ AI Recipe Finder with ChromaDB RAG")
 
62
  for result in results["metadatas"][0]:
63
  index = result["index"]
64
  recipe = recipes_df[index]
65
+ st.write(f"**🍴 {recipe.get('title', 'No title available')}**")
66
+ st.write(f"**Ingredients:** {', '.join(recipe.get('ingredients', []))}")
67
+ st.write(f"**Instructions:** {recipe.get('instructions', 'No instructions available')}")
68
  st.write("---")
69
  else:
70
  st.info("Type a recipe name to find similar recipes.")