-
Notifications
You must be signed in to change notification settings - Fork 0
/
app.py
147 lines (116 loc) · 4.67 KB
/
app.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
import streamlit as st
import requests
from streamlit_lottie import st_lottie
from PIL import Image
import plotly.express as px
from streamlit_cropper import st_cropper
import io
### Function for the nutrition dataframe to implement and change units
def format_food_table(table):
new_table = {}
for title, col in table.items():
new_col = {}
new_table[title] = new_col
for row_name, value in col.items():
new_row_name = str(int(row_name) + 1)
if isinstance(value, float):
if title in ["protein", "fat", "carbohydrates"]:
new_col[new_row_name] = str(value) + " g"
else:
new_col[new_row_name] = str(round(value * 1000)) + " mg"
else:
new_col[new_row_name] = value
return new_table
st.set_page_config(page_title="FoodScore", page_icon=":food:", layout="wide")
### DEFINE PAGE CONTAINERS
# Header
with st.container():
st.markdown(
"""<style>
@import url('https://fonts.googleapis.com/css2?family=Monoton&display=swap');
.big-font {
font-size:100px !important;
color: 2B5DC1;
font-family:'Monoton';
text-align: center
}
</style>""",
unsafe_allow_html=True,
)
st.markdown('<p class="big-font">FoodScore</p>', unsafe_allow_html=True)
st.write(
f'<p style="text-align: center;font-size:35px; color:"#959EC6";">Do you really know what you eat?</p>',
unsafe_allow_html=True,
)
# Lottie Animation
def load_lottieurl(url):
r = requests.get(url)
if r.status_code != 200:
return None
return r.json()
with st.container():
lottie2 = load_lottieurl(
"https://assets5.lottiefiles.com/packages/lf20_tll0j4bb.json"
)
st_lottie(lottie2, speed=0.8, height=200)
st.write(
f'<p style="text-align: center;font-size:20px; color:"#959EC6";">Upload an image of your food and we will tell you its nutrition facts!</p>',
unsafe_allow_html=True,
)
with st.container():
left_lottie, uploading, right_lottie = st.columns([3, 5, 3])
with left_lottie:
lottie1 = load_lottieurl('https://assets5.lottiefiles.com/temp/lf20_nXwOJj.json')
st_lottie(lottie1, speed = 0.5, height=100)
with uploading:
uploaded_file = st.file_uploader(" ", type=["jpg", "jpeg", "png"])
if uploaded_file:
st.markdown(
"""<style>
.uploadedFile {display: none} <style>""",
unsafe_allow_html=True,
)
with right_lottie:
lottie2 = load_lottieurl('https://assets9.lottiefiles.com/packages/lf20_yBiWrEStmy.json')
st_lottie(lottie2, speed = 0.5, height=100)
# Image Container
with st.container():
if uploaded_file is not None:
url = "https://fastfoodscore-j5kdnfjkoa-ew.a.run.app/upload_image"
img = Image.open(uploaded_file)
col1, col2, col3 = st.columns([5, 1, 4])
with col1:
# Get a cropped image from the frontend
cropped_img = st_cropper(img, aspect_ratio=None, box_color='white')
with col3:
# Manipulate cropped image at will
st.write("Cropped Image")
# _ = cropped_img.thumbnail((600, 600))
col3.image(cropped_img, width=400)
col1, col2, col3 = st.columns([2, 0.1, 5])
with col1:
with st.spinner('Nutritions are coming..'):
if st.button("Show Nutritions"):
buffer = io.BytesIO()
cropped_img.save(buffer, format="jpeg")
# dict_food = requests.post(url,data=buffer.getvalue()).json()
dict_food = requests.post(url, files={"img": buffer.getvalue()}).json()
with col3:
st.write(
f'<p style="font-size:35px; color:"#959EC6";">Is it {dict_food["name"]["0"]}?</p>',
unsafe_allow_html=True,
)
st.write(
'<p style="font-size:20px; color:"#959EC6";">Here you can find the nutrition data for our top predictions per 100g</p>',
unsafe_allow_html=True,
)
dict_food = format_food_table(dict_food)
st.dataframe(dict_food)
# Remove the Menu Button and Streamlit Icon
hide_default_format = """
<style>
#MainMenu {visibility: hidden; }
footer {visibility: hidden;}
</style>
"""
st.markdown(hide_default_format, unsafe_allow_html=True)