Skip to content

Commit 277721c

Browse files
committed
Add cleaned data and rewrite the code
1 parent dcc2373 commit 277721c

File tree

5 files changed

+257
-196
lines changed

5 files changed

+257
-196
lines changed
Lines changed: 128 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,128 @@
1+
# spell-checker: disable
2+
"""
3+
Clean recipe CSV and create SQLite DB with diet type.
4+
"""
5+
6+
import re
7+
import sqlite3
8+
9+
import pandas as pd
10+
11+
# Load CSV
12+
df = pd.read_csv("smart_pantry_manager/data/Recipe_Dataset.csv")
13+
14+
# Forbidden (haram) ingredients
15+
haram_keywords = [
16+
"pork",
17+
"ham",
18+
"bacon",
19+
"prosciutto",
20+
"pancetta",
21+
"sausage",
22+
"wine",
23+
"beer",
24+
"bourbon",
25+
"rum",
26+
"whisky",
27+
"vodka",
28+
"tequila",
29+
"cognac",
30+
"brandy",
31+
"liqueur",
32+
"alcohol",
33+
"champagne",
34+
"sake",
35+
"sherry",
36+
"gin",
37+
]
38+
39+
# Meat ingredients
40+
meat_keywords = [
41+
"chicken",
42+
"beef",
43+
"lamb",
44+
"turkey",
45+
"fish",
46+
"shrimp",
47+
"salmon",
48+
"tuna",
49+
"meat",
50+
"steak",
51+
"duck",
52+
"anchovy",
53+
"crab",
54+
"lobster",
55+
"clam",
56+
"oyster",
57+
"scallop",
58+
"mussel",
59+
"squid",
60+
"sausage",
61+
]
62+
63+
# Animal products (vegetarian)
64+
animal_product_keywords = [
65+
"egg",
66+
"milk",
67+
"cheese",
68+
"butter",
69+
"cream",
70+
"yogurt",
71+
"ghee",
72+
"honey",
73+
"mayonnaise",
74+
"whey",
75+
"casein",
76+
"gelatin",
77+
]
78+
79+
80+
# Check haram
81+
def contains_haram(ingredient_text):
82+
if pd.isna(ingredient_text):
83+
return False
84+
text = ingredient_text.lower()
85+
for kw in haram_keywords:
86+
if re.search(rf"\b{kw}\b", text):
87+
return True
88+
return False
89+
90+
91+
# Classify diet type
92+
def classify_diet_type(ingredient_text):
93+
if pd.isna(ingredient_text):
94+
return "Unknown"
95+
text = ingredient_text.lower()
96+
for kw in meat_keywords:
97+
if re.search(rf"\b{kw}\b", text):
98+
return "Non-Vegetarian"
99+
for kw in animal_product_keywords:
100+
if re.search(rf"\b{kw}\b", text):
101+
return "Vegetarian"
102+
return "Vegan"
103+
104+
105+
# Filter haram
106+
df["contains_haram"] = df["Ingredients"].apply(contains_haram)
107+
df_clean = df[~df["contains_haram"]].copy()
108+
df_clean["Diet_Type"] = df_clean["Ingredients"].apply(classify_diet_type)
109+
df_clean.drop("contains_haram", axis=1, inplace=True)
110+
111+
# SQLite DB
112+
conn = sqlite3.connect("smart_pantry_manager/data/cleaned_data.sqlite")
113+
114+
# Tables for each diet type
115+
for diet in ["Vegan", "Vegetarian", "Non-Vegetarian"]:
116+
diet_df = df_clean[df_clean["Diet_Type"] == diet].copy()
117+
diet_df.to_sql(
118+
diet.lower().replace("-", "_") + "_recipes",
119+
conn,
120+
if_exists="replace",
121+
index=False,
122+
)
123+
124+
# Combined table
125+
df_clean.to_sql("all_recipes", conn, if_exists="replace", index=False)
126+
127+
conn.close()
128+
print("βœ… SQLite DB created with all_recipes and diet tables.")
45.8 MB
Binary file not shown.
Lines changed: 62 additions & 71 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
# spell-checker: disable
22
"""
3-
All Recipes Page for Smart Pantry Application (SQLite version)
3+
All Recipes Page for Smart Pantry Manager (SQLite version)
4+
Shows all recipes with diet type info.
45
"""
56

67
import ast
@@ -11,96 +12,86 @@
1112
import streamlit as st
1213

1314
st.set_page_config(page_title="All Recipes", page_icon="πŸ“œ", layout="wide")
14-
1515
st.title("πŸ“œ All Recipes")
16-
st.caption("Browse all available recipes in the Smart Pantry system.")
16+
st.caption("Browse all recipes and see diet type (Vegan/Vegetarian/Non-Vegetarian)")
1717

1818

19-
# ---------- Load recipes from SQLite ----------
19+
# ---------- Load recipes ----------
2020
@st.cache_data
2121
def load_recipes():
22-
"""
23-
Load recipes from the SQLite database and normalize column names.
24-
Returns a DataFrame with columns: Recipe, Ingredients, Instructions
25-
"""
26-
db_path = "smart_pantry_manager/data/Recipe_Dataset.sqlite"
27-
28-
# Check if database file exists
22+
db_path = "smart_pantry_manager/data/cleaned_data.sqlite"
2923
if not os.path.exists(db_path):
30-
st.error(f"❌ Database file not found at: {db_path}")
31-
return pd.DataFrame(columns=["Recipe", "Ingredients", "Instructions"])
32-
33-
# Connect to SQLite database
24+
st.error("⚠️ Recipes database not found.")
25+
return pd.DataFrame(
26+
columns=["Recipe", "Ingredients", "Instructions", "Diet_type"]
27+
)
3428
conn = sqlite3.connect(db_path)
35-
3629
try:
37-
# Load the table "recipes"
38-
df = pd.read_sql_query("SELECT * FROM recipes", conn)
30+
df = pd.read_sql_query("SELECT * FROM all_recipes", conn)
3931
except Exception as e:
40-
st.error(f"Error reading database: {e}")
32+
st.error(f"Error reading recipes: {e}")
4133
conn.close()
42-
return pd.DataFrame(columns=["Recipe", "Ingredients", "Instructions"])
43-
34+
return pd.DataFrame(
35+
columns=["Recipe", "Ingredients", "Instructions", "Diet_type"]
36+
)
4437
conn.close()
45-
46-
# Normalize column names
38+
# Normalize columns
4739
df.columns = [c.strip().lower() for c in df.columns]
48-
49-
# Rename columns if they exist
50-
rename_map = {
51-
"title": "Recipe",
52-
"cleaned_ingredients": "Ingredients",
53-
"instruction": "Instructions",
54-
"instructions": "Instructions",
55-
}
5640
df.rename(
57-
columns={k: v for k, v in rename_map.items() if k in df.columns}, inplace=True
41+
columns={
42+
"title": "Recipe",
43+
"instruction": "Instructions",
44+
"instructions": "Instructions",
45+
"diet_type": "Diet_type",
46+
"ingredient": "Ingredients",
47+
"cleaned_ingredients": "Ingredients",
48+
},
49+
inplace=True,
5850
)
51+
for col in ["Recipe", "Ingredients", "Instructions", "Diet_type"]:
52+
if col not in df.columns:
53+
df[col] = ""
54+
return df[["Recipe", "Ingredients", "Instructions", "Diet_type"]]
5955

60-
# Keep only required columns
61-
required_cols = ["Recipe", "Ingredients", "Instructions"]
62-
df = df[[col for col in required_cols if col in df.columns]]
63-
64-
return df
6556

66-
67-
def format_ingredients(ingredients_str):
68-
"""
69-
Convert ingredients from string representation of list to a Python list.
70-
"""
57+
def parse_ingredients(ingredients_str):
58+
if not ingredients_str:
59+
return []
60+
s = str(ingredients_str)
7161
try:
72-
# Try to parse as a Python list
73-
if ingredients_str.startswith("["):
74-
return ast.literal_eval(ingredients_str)
75-
# If it's comma-separated
76-
elif "," in ingredients_str:
77-
return [item.strip() for item in ingredients_str.split(",")]
78-
else:
79-
return [ingredients_str]
62+
if s.startswith("[") and s.endswith("]"):
63+
parsed = ast.literal_eval(s)
64+
return [str(x).strip() for x in parsed if str(x).strip()]
65+
if "," in s:
66+
return [x.strip() for x in s.split(",") if x.strip()]
67+
return [s]
8068
except:
81-
# If parsing fails, return as single item list
82-
return [ingredients_str]
69+
return [s]
8370

8471

8572
recipes = load_recipes()
86-
87-
# ---------- Display recipes ----------
8873
if recipes.empty:
8974
st.info("No recipes found.")
90-
else:
91-
search = st.text_input("πŸ” Search for a recipe:")
92-
filtered = (
93-
recipes[recipes["Recipe"].str.contains(search, case=False, na=False)]
94-
if search
95-
else recipes
96-
)
97-
98-
for _, row in filtered.iterrows():
99-
with st.expander(f"πŸ“– {row['Recipe']}"):
100-
st.markdown("**πŸ§‚ Ingredients:**")
101-
ingredients_list = format_ingredients(row["Ingredients"])
102-
for ingredient in ingredients_list:
103-
st.write(f"β€’ {ingredient}")
104-
105-
st.markdown("**πŸ‘©β€πŸ³ Instructions:**")
106-
st.write(row["Instructions"])
75+
st.stop()
76+
77+
search = st.text_input("πŸ” Search for a recipe:")
78+
filtered = (
79+
recipes[recipes["Recipe"].str.contains(search, case=False, na=False)]
80+
if search
81+
else recipes
82+
)
83+
84+
for _, row in filtered.iterrows():
85+
diet = row.get("Diet_type", "Unknown").capitalize()
86+
recipe_name = row.get("Recipe", "Unnamed Recipe")
87+
with st.expander(f"πŸ“– {recipe_name} β€” {diet}"):
88+
st.markdown(f"**Diet Type:** {diet}")
89+
st.markdown("**πŸ§‚ Ingredients:**")
90+
ing_list = parse_ingredients(row["Ingredients"])
91+
if ing_list:
92+
for ing in ing_list:
93+
st.write(f"β€’ {ing}")
94+
else:
95+
st.write("No ingredient data available.")
96+
st.markdown("**πŸ‘©β€πŸ³ Instructions:**")
97+
st.write(row["Instructions"] or "No instructions available.")

0 commit comments

Comments
Β (0)