Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
|
@@ -4,6 +4,7 @@ import numpy as np
|
|
| 4 |
from PIL import Image
|
| 5 |
import pandas as pd
|
| 6 |
import matplotlib.pyplot as plt
|
|
|
|
| 7 |
|
| 8 |
# Load the saved model
|
| 9 |
model_path = "fahrnphi_exam_project.keras"
|
|
@@ -33,6 +34,10 @@ def predict_ingredient(image):
|
|
| 33 |
st.title("Ingredient Classifier")
|
| 34 |
st.write("A simple MLP classification model for image classification using a pretrained model.")
|
| 35 |
|
|
|
|
|
|
|
|
|
|
|
|
|
| 36 |
# Upload image
|
| 37 |
uploaded_image = st.file_uploader("Choose an image...", type=["jpg", "png"])
|
| 38 |
|
|
@@ -46,12 +51,51 @@ if uploaded_image is not None:
|
|
| 46 |
|
| 47 |
# Display predictions as a DataFrame
|
| 48 |
st.write("### Prediction Probabilities")
|
| 49 |
-
df = pd.DataFrame(predictions.items(), columns=["
|
| 50 |
st.dataframe(df)
|
| 51 |
|
| 52 |
# Display predictions as a pie chart
|
| 53 |
st.write("### Prediction Chart")
|
| 54 |
fig, ax = plt.subplots()
|
| 55 |
-
ax.pie(df["Probability"], labels=df["
|
| 56 |
ax.set_title('Prediction Probabilities')
|
| 57 |
-
st.pyplot(fig)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 4 |
from PIL import Image
|
| 5 |
import pandas as pd
|
| 6 |
import matplotlib.pyplot as plt
|
| 7 |
+
import json
|
| 8 |
|
| 9 |
# Load the saved model
|
| 10 |
model_path = "fahrnphi_exam_project.keras"
|
|
|
|
| 34 |
st.title("Ingredient Classifier")
|
| 35 |
st.write("A simple MLP classification model for image classification using a pretrained model.")
|
| 36 |
|
| 37 |
+
# Initialize session state for storing ingredients
|
| 38 |
+
if 'ingredients' not in st.session_state:
|
| 39 |
+
st.session_state['ingredients'] = []
|
| 40 |
+
|
| 41 |
# Upload image
|
| 42 |
uploaded_image = st.file_uploader("Choose an image...", type=["jpg", "png"])
|
| 43 |
|
|
|
|
| 51 |
|
| 52 |
# Display predictions as a DataFrame
|
| 53 |
st.write("### Prediction Probabilities")
|
| 54 |
+
df = pd.DataFrame(predictions.items(), columns=["Ingredient", "Probability"])
|
| 55 |
st.dataframe(df)
|
| 56 |
|
| 57 |
# Display predictions as a pie chart
|
| 58 |
st.write("### Prediction Chart")
|
| 59 |
fig, ax = plt.subplots()
|
| 60 |
+
ax.pie(df["Probability"], labels=df["Ingredient"], autopct='%1.1f%%', colors=plt.cm.Paired.colors)
|
| 61 |
ax.set_title('Prediction Probabilities')
|
| 62 |
+
st.pyplot(fig)
|
| 63 |
+
|
| 64 |
+
# Allow user to select the best guess
|
| 65 |
+
best_guess = st.selectbox("Select the best guess", df["Ingredient"])
|
| 66 |
+
if st.button("Add Ingredient"):
|
| 67 |
+
st.session_state.ingredients.append(best_guess)
|
| 68 |
+
st.write(f"Added {best_guess} to ingredients list")
|
| 69 |
+
|
| 70 |
+
# Display the ingredients added so far
|
| 71 |
+
st.write("### Selected Ingredients")
|
| 72 |
+
st.write(st.session_state.ingredients)
|
| 73 |
+
|
| 74 |
+
# Finish button to finalize ingredient selection and find recipes
|
| 75 |
+
if st.button("Finish"):
|
| 76 |
+
st.write("Finding recipes...")
|
| 77 |
+
# Placeholder: Replace with actual recipe finding logic
|
| 78 |
+
def find_recipes(ingredients):
|
| 79 |
+
# This is a mock function, replace with actual recipe finding logic
|
| 80 |
+
sample_recipes = [
|
| 81 |
+
{"name": "Vegetable Stir Fry", "ingredients": ["Peperoni", "Carrot", "Onion"]},
|
| 82 |
+
{"name": "Tomato Garlic Pasta", "ingredients": ["Tomato", "Garlic"]},
|
| 83 |
+
{"name": "Ginger Potato Soup", "ingredients": ["Ginger", "Potato"]}
|
| 84 |
+
]
|
| 85 |
+
matching_recipes = [recipe for recipe in sample_recipes if all(item in recipe["ingredients"] for item in ingredients)]
|
| 86 |
+
return matching_recipes
|
| 87 |
+
|
| 88 |
+
matching_recipes = find_recipes(st.session_state.ingredients)
|
| 89 |
+
|
| 90 |
+
if matching_recipes:
|
| 91 |
+
st.write("### Matching Recipes")
|
| 92 |
+
for recipe in matching_recipes:
|
| 93 |
+
st.write(f"**{recipe['name']}**")
|
| 94 |
+
st.write(", ".join(recipe["ingredients"]))
|
| 95 |
+
else:
|
| 96 |
+
st.write("No matching recipes found.")
|
| 97 |
+
|
| 98 |
+
# Reset button to start over
|
| 99 |
+
if st.button("Reset"):
|
| 100 |
+
st.session_state.ingredients = []
|
| 101 |
+
st.write("Ingredients list has been reset.")
|