Spaces:
Runtime error
Runtime error
Upload app.py
Browse files
app.py
ADDED
@@ -0,0 +1,139 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
import streamlit as st
|
2 |
+
import numpy as np
|
3 |
+
import time
|
4 |
+
import PIL
|
5 |
+
import PIL.Image as Image
|
6 |
+
|
7 |
+
|
8 |
+
# from utils import make_pred_outside_india,getmodel_outside_india,load_prepare_img
|
9 |
+
from utils import make_pred_outside_india
|
10 |
+
from utils import getmodel_outside_india
|
11 |
+
from utils import getmodel_india
|
12 |
+
from utils import load_prepare_img
|
13 |
+
from transformers import CLIPProcessor, CLIPModel
|
14 |
+
import sys
|
15 |
+
from RecipeData import fetchRecipeData
|
16 |
+
|
17 |
+
IMG_SIZE = (224, 224)
|
18 |
+
model_V2 = 'efficientnet_b0.pt'
|
19 |
+
model_V1 = 'indian_efficientnet_b0.pt'
|
20 |
+
|
21 |
+
|
22 |
+
@st.cache()
|
23 |
+
def model_prediction(model_path, img_file, rescale,selected_location):
|
24 |
+
input_img, device = load_prepare_img(img_file)
|
25 |
+
if(selected_location=='Outside_India'):
|
26 |
+
model = getmodel_outside_india(model_path)
|
27 |
+
prediction = make_pred_outside_india(input_img, model, device, selected_location)
|
28 |
+
elif(selected_location=='India'):
|
29 |
+
model = getmodel_india(model_path)
|
30 |
+
prediction = make_pred_outside_india(input_img, model, device, selected_location)
|
31 |
+
sorceCode, recipe_data = fetchRecipeData(prediction)
|
32 |
+
return prediction, sorceCode, recipe_data
|
33 |
+
|
34 |
+
def food_pred(input_image):
|
35 |
+
# input labels for clip model
|
36 |
+
label = ['food ', 'Not food']
|
37 |
+
|
38 |
+
# CLIP Model for classification
|
39 |
+
model = CLIPModel.from_pretrained("openai/clip-vit-base-patch32")
|
40 |
+
processor = CLIPProcessor.from_pretrained("openai/clip-vit-base-patch32")
|
41 |
+
|
42 |
+
image = Image.open(requests.get(uploaded_file, stream=True).raw)
|
43 |
+
inputs = processor(text=label, images=image, return_tensors="pt", padding=True)
|
44 |
+
return inputs
|
45 |
+
|
46 |
+
|
47 |
+
def main():
|
48 |
+
st.set_page_config(
|
49 |
+
page_title="SeeFood",
|
50 |
+
page_icon="🍔",
|
51 |
+
layout="wide",
|
52 |
+
initial_sidebar_state="expanded"
|
53 |
+
)
|
54 |
+
|
55 |
+
st.title('SeeFood🍔')
|
56 |
+
st.write('Upload a food image and get the recipe for that food and other details of that food')
|
57 |
+
|
58 |
+
col1, col2 = st.columns(2)
|
59 |
+
|
60 |
+
with col1:
|
61 |
+
# image uploading button
|
62 |
+
uploaded_file = st.file_uploader("Choose a file")
|
63 |
+
selected_location = st.selectbox('Select loaction',('India', 'Outside_India'), index=1)
|
64 |
+
if uploaded_file is not None:
|
65 |
+
display_img = uploaded_file.read()
|
66 |
+
uploaded_img = Image.open(uploaded_file)
|
67 |
+
col2.image(display_img, width=500)
|
68 |
+
|
69 |
+
|
70 |
+
|
71 |
+
|
72 |
+
|
73 |
+
predict = st.button('Get Recipe!')
|
74 |
+
|
75 |
+
|
76 |
+
|
77 |
+
if predict:
|
78 |
+
if uploaded_file is not None:
|
79 |
+
with st.spinner('Please Wait 👩🍳'):
|
80 |
+
|
81 |
+
# setting model and rescalling
|
82 |
+
if selected_location == 'India':
|
83 |
+
pred_model = model_V1
|
84 |
+
pred_rescale = True
|
85 |
+
if selected_location == 'Outside_India':
|
86 |
+
pred_model = model_V2
|
87 |
+
pred_rescale =True
|
88 |
+
|
89 |
+
|
90 |
+
# makeing prediction and fetching food recipe form api
|
91 |
+
food, source_code, recipe_data = model_prediction(pred_model, uploaded_img, pred_rescale,selected_location)
|
92 |
+
|
93 |
+
# asssigning caleoric breakdown data
|
94 |
+
percent_Protein = recipe_data['percentProtein']
|
95 |
+
percent_fat = recipe_data['percentFat']
|
96 |
+
percent_carbs = recipe_data['percentCarbs']
|
97 |
+
|
98 |
+
# food name message
|
99 |
+
col1.success(f"It's an {food}")
|
100 |
+
|
101 |
+
if source_code == 200:
|
102 |
+
# desplay food recipe
|
103 |
+
st.header(recipe_data['title']+" Recipe")
|
104 |
+
|
105 |
+
col3, col4 = st.columns(2)
|
106 |
+
|
107 |
+
with col3:
|
108 |
+
# Ingridents of recipie
|
109 |
+
st.subheader('Ingredients')
|
110 |
+
# st.info(recipe_data['ingridents'])
|
111 |
+
for i in recipe_data['ingridents']:
|
112 |
+
st.info(f"{i}")
|
113 |
+
# Inctuction for recipe
|
114 |
+
with col4:
|
115 |
+
st.subheader('Instructions')
|
116 |
+
st.info(recipe_data['instructions'])
|
117 |
+
# st.subheader('Caloric Breakdown')
|
118 |
+
'''
|
119 |
+
## Caloric Breakdown
|
120 |
+
'''
|
121 |
+
st.success(f'''
|
122 |
+
* Protien: {percent_Protein}%
|
123 |
+
* Fat: {percent_fat}%
|
124 |
+
* Carbohydrates: {percent_carbs}%
|
125 |
+
''')
|
126 |
+
|
127 |
+
|
128 |
+
else:
|
129 |
+
st.error('Something went wrong please try again :(')
|
130 |
+
|
131 |
+
|
132 |
+
else:
|
133 |
+
st.warning('Please Upload Image')
|
134 |
+
|
135 |
+
|
136 |
+
|
137 |
+
|
138 |
+
if __name__=='__main__':
|
139 |
+
main()
|