Update app.py
Browse files
app.py
CHANGED
@@ -1,6 +1,7 @@
|
|
1 |
import streamlit as st
|
2 |
from PIL import Image
|
3 |
-
|
|
|
4 |
from transformers import pipeline
|
5 |
import numpy as np
|
6 |
from transformers import AutoFeatureExtractor
|
@@ -38,16 +39,6 @@ def main():
|
|
38 |
st.markdown("This is a Food Category Image Classifier model that has been trained by [Kaludi](https://huggingface.co/Kaludi) to recognize **12** different categories of foods, which includes **Bread**, **Dairy**, **Dessert**, **Egg**, **Fried Food**, **Fruit**, **Meat**, **Noodles**, **Rice**, **Seafood**, **Soup**, and **Vegetable**. It can accurately classify an image of food into one of these categories by analyzing its visual features. This model can be used by food bloggers, restaurants, and recipe websites to quickly categorize and sort their food images, making it easier to manage their content and provide a better user experience.")
|
39 |
st.header("Try it out!")
|
40 |
|
41 |
-
# images = ["examples/example_0.jpg",
|
42 |
-
# "examples/example_1.jpg",
|
43 |
-
# "examples/example_2.jpg",
|
44 |
-
# "examples/example_3.jpg",
|
45 |
-
# "examples/example_4.jpg",
|
46 |
-
# "examples/example_5.jpg",
|
47 |
-
# "examples/example_6.jpg",
|
48 |
-
# "examples/example_7.jpg"]
|
49 |
-
# show_images = False
|
50 |
-
|
51 |
if st.checkbox("Show/Hide Examples"):
|
52 |
st.header("Example Images")
|
53 |
|
@@ -76,13 +67,23 @@ def main():
|
|
76 |
# for image in images:
|
77 |
# st.image(image, width=260)
|
78 |
|
79 |
-
select_health = st.radio("Select One (Not Functional Yet):", ["Regular", "Low-Calorie"], horizontal=True)
|
80 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
81 |
calories = st.slider("Select Max Calories (Per Serving)", 50, 2000)
|
82 |
|
83 |
# print the calories
|
84 |
st.write("Selected: **{}** Max Calories.".format(calories))
|
85 |
-
|
86 |
uploaded_file = st.file_uploader("Upload Files", type=['png','jpeg','jpg'])
|
87 |
|
88 |
loading_text = st.empty()
|
@@ -98,6 +99,7 @@ def main():
|
|
98 |
loading_text.empty()
|
99 |
label_num=outputs.logits.softmax(1).argmax(1)
|
100 |
label_num=label_num.item()
|
|
|
101 |
|
102 |
probs = outputs.logits.softmax(dim=1)
|
103 |
percentage = round(probs[0, label_num].item() * 100, 2)
|
@@ -128,18 +130,36 @@ def main():
|
|
128 |
st.write("**Soup** (" + f"{percentage}%)")
|
129 |
else:
|
130 |
st.write("**Vegetable** (" + f"{percentage}%)")
|
131 |
-
|
132 |
|
133 |
-
if
|
134 |
-
|
135 |
-
|
136 |
-
elif select_health == "Low-Calorie":
|
137 |
-
st.write("You Selected **_Low-Calorie_** With Max", calories, "Calories For", "**Bread**" if label_num==0 else "**Dairy**" if label_num==1 else "**Dessert**" if label_num==2 else "**Egg**" if label_num==3 else "**Fried Food**" if label_num==4 else "**Fruit**" if label_num==5 else "**Meat**" if label_num==6 else "**Noodles**" if label_num==7 else "**Rice**" if label_num==8 else "**Seafood**" if label_num==9 else "**Soup**" if label_num==10 else "**Vegetable**")
|
138 |
-
# Add code to fetch unhealthy recipe here (line #125-138)
|
139 |
|
140 |
st.image(img, width=260)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
141 |
st.markdown("<hr style='text-align: center;'>", unsafe_allow_html=True)
|
142 |
-
st.markdown("<p style='text-align: center'><a href='https://github.com/Kaludii'>Github</a> | <a href='https://huggingface.co/Kaludi'>HuggingFace</a></p>", unsafe_allow_html=True)
|
143 |
|
144 |
if __name__ == '__main__':
|
145 |
main()
|
@@ -158,4 +178,4 @@ if __name__ == '__main__':
|
|
158 |
|
159 |
## Add code to display the healthy and unhealthy recipes
|
160 |
#st.write("Healthy Recipe: ", healthy_recipe['name'], "Calories: ", healthy_recipe['calories'])
|
161 |
-
#st.write("Unhealthy Recipe: ", unhealthy_recipe['name'], "Calories: ", unhealthy_recipe['calories'])
|
|
|
1 |
import streamlit as st
|
2 |
from PIL import Image
|
3 |
+
import requests
|
4 |
+
import json
|
5 |
from transformers import pipeline
|
6 |
import numpy as np
|
7 |
from transformers import AutoFeatureExtractor
|
|
|
39 |
st.markdown("This is a Food Category Image Classifier model that has been trained by [Kaludi](https://huggingface.co/Kaludi) to recognize **12** different categories of foods, which includes **Bread**, **Dairy**, **Dessert**, **Egg**, **Fried Food**, **Fruit**, **Meat**, **Noodles**, **Rice**, **Seafood**, **Soup**, and **Vegetable**. It can accurately classify an image of food into one of these categories by analyzing its visual features. This model can be used by food bloggers, restaurants, and recipe websites to quickly categorize and sort their food images, making it easier to manage their content and provide a better user experience.")
|
40 |
st.header("Try it out!")
|
41 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
42 |
if st.checkbox("Show/Hide Examples"):
|
43 |
st.header("Example Images")
|
44 |
|
|
|
67 |
# for image in images:
|
68 |
# st.image(image, width=260)
|
69 |
|
70 |
+
# select_health = st.radio("Select One (Not Functional Yet):", ["Regular", "Low-Calorie"], horizontal=True)
|
71 |
+
|
72 |
+
# Dropdown for Diet
|
73 |
+
diet_options = ['All', 'Gluten-Free', 'Vegan', 'Vegetarian', 'Dairy-Free']
|
74 |
+
diet = st.selectbox('Diet', diet_options)
|
75 |
+
|
76 |
+
# Dropdown for Cuisine
|
77 |
+
cuisine_options = ['All', 'African', 'Asian', 'Caribbean', 'Central American', 'Europe', 'Middle Eastern', 'North American', 'Oceanic', 'South American']
|
78 |
+
|
79 |
+
cuisine = st.selectbox('Cuisine', cuisine_options)
|
80 |
+
|
81 |
+
# Slider for Calories
|
82 |
calories = st.slider("Select Max Calories (Per Serving)", 50, 2000)
|
83 |
|
84 |
# print the calories
|
85 |
st.write("Selected: **{}** Max Calories.".format(calories))
|
86 |
+
|
87 |
uploaded_file = st.file_uploader("Upload Files", type=['png','jpeg','jpg'])
|
88 |
|
89 |
loading_text = st.empty()
|
|
|
99 |
loading_text.empty()
|
100 |
label_num=outputs.logits.softmax(1).argmax(1)
|
101 |
label_num=label_num.item()
|
102 |
+
|
103 |
|
104 |
probs = outputs.logits.softmax(dim=1)
|
105 |
percentage = round(probs[0, label_num].item() * 100, 2)
|
|
|
130 |
st.write("**Soup** (" + f"{percentage}%)")
|
131 |
else:
|
132 |
st.write("**Vegetable** (" + f"{percentage}%)")
|
|
|
133 |
|
134 |
+
st.write("You Selected **{}** For Diet and **{}** For Cuisine with Max".format(diet, cuisine), calories, "Calories For", ( "**Bread**" if label_num==0 else "**Dairy**" if label_num==1 else "**Dessert**" if label_num==2 else "**Egg**" if label_num==3 else "**Fried Food**" if label_num==4 else "**Fruit**" if label_num==5 else "**Meat**" if label_num==6 else "**Noodles**" if label_num==7 else "**Rice**" if label_num==8 else "**Seafood**" if label_num==9 else "**Soup**" if label_num==10 else "**Vegetable**"))
|
135 |
+
|
136 |
+
# Add code to fetch healthy recipe here (line #125-138)
|
|
|
|
|
|
|
137 |
|
138 |
st.image(img, width=260)
|
139 |
+
|
140 |
+
url = "https://alcksyjrmd.execute-api.us-east-2.amazonaws.com/default/nutrients_response"
|
141 |
+
|
142 |
+
category = ("Bread" if label_num==0 else "Dairy" if label_num==1 else "Dessert" if label_num==2 else "Egg" if label_num==3 else "Fried Food" if label_num==4 else "Fruit" if label_num==5 else "Meat" if label_num==6 else "Noodles" if label_num==7 else "Rice" if label_num==8 else "Seafood" if label_num==9 else "**Soup**" if label_num==10 else "Vegetable")
|
143 |
+
|
144 |
+
params = {"f": category}
|
145 |
+
|
146 |
+
if diet != "All":
|
147 |
+
params["d"] = diet
|
148 |
+
|
149 |
+
if cuisine != "All":
|
150 |
+
params["c"] = cuisine
|
151 |
+
|
152 |
+
st.markdown("### Recommended Recipe:")
|
153 |
+
response = requests.get(url, params=params)
|
154 |
+
response_json = json.loads(response.content)
|
155 |
+
|
156 |
+
if len(response_json) == 0:
|
157 |
+
st.write("**No Recipe Found, please try another option from the dropdown menus.**")
|
158 |
+
else:
|
159 |
+
st.write(response_json)
|
160 |
+
|
161 |
st.markdown("<hr style='text-align: center;'>", unsafe_allow_html=True)
|
162 |
+
st.markdown("<p style='text-align: center'><a href='https://github.com/Kaludii'>Github</a> | <a href='https://huggingface.co/Kaludi'>HuggingFace</a></p>", unsafe_allow_html=True)
|
163 |
|
164 |
if __name__ == '__main__':
|
165 |
main()
|
|
|
178 |
|
179 |
## Add code to display the healthy and unhealthy recipes
|
180 |
#st.write("Healthy Recipe: ", healthy_recipe['name'], "Calories: ", healthy_recipe['calories'])
|
181 |
+
#st.write("Unhealthy Recipe: ", unhealthy_recipe['name'], "Calories: ", unhealthy_recipe['calories'])
|