Tejasn commited on
Commit
9df472f
β€’
1 Parent(s): 6bc00cf
Files changed (1) hide show
  1. app.py +64 -66
app.py CHANGED
@@ -3,14 +3,11 @@ 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
 
@@ -27,22 +24,11 @@ def model_prediction(model_path, img_file, rescale,selected_location):
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(
@@ -76,59 +62,71 @@ def main():
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
 
 
3
  import time
4
  import PIL
5
  import PIL.Image as Image
 
 
 
6
  from utils import make_pred_outside_india
7
  from utils import getmodel_outside_india
8
  from utils import getmodel_india
9
  from utils import load_prepare_img
10
+ from utils import food_nofood_pred
11
  import sys
12
  from RecipeData import fetchRecipeData
13
 
 
24
  prediction = make_pred_outside_india(input_img, model, device, selected_location)
25
  elif(selected_location=='India'):
26
  model = getmodel_india(model_path)
27
+
28
  prediction = make_pred_outside_india(input_img, model, device, selected_location)
29
+ print(prediction)
30
  sorceCode, recipe_data = fetchRecipeData(prediction)
31
+ return prediction, sorceCode, recipe_data
 
 
 
 
 
 
 
 
 
 
 
 
 
32
 
33
  def main():
34
  st.set_page_config(
 
62
 
63
  if predict:
64
  if uploaded_file is not None:
65
+ with st.spinner('getting image type'):
66
+ img_type=food_nofood_pred(uploaded_img)
67
+ print(img_type)
 
 
 
 
 
 
 
68
 
69
+ if(img_type=='food'):
 
70
 
71
+ with st.spinner('Please Wait πŸ‘©β€πŸ³'):
72
+
73
+ # setting model and rescalling
74
+ if selected_location == 'India':
75
+ pred_model = model_V1
76
+ pred_rescale = True
77
+ if selected_location == 'Outside_India':
78
+ pred_model = model_V2
79
+ pred_rescale =True
80
+
81
+
82
+ # makeing prediction and fetching food recipe form api
83
+ food, source_code, recipe_data = model_prediction(pred_model, uploaded_img, pred_rescale,selected_location)
84
+
85
+ # asssigning caleoric breakdown data
86
+ percent_Protein = recipe_data['percentProtein']
87
+ percent_fat = recipe_data['percentFat']
88
+ percent_carbs = recipe_data['percentCarbs']
89
+
90
+ # food name message
91
+ col1.success(f"It's an {food}")
92
+
93
+ if source_code == 200:
94
+ # desplay food recipe
95
+ st.header(recipe_data['title']+" Recipe")
96
+
97
+ col3, col4 = st.columns(2)
98
+
99
+ with col3:
100
+ # Ingridents of recipie
101
+ st.subheader('Ingredients')
102
+ # st.info(recipe_data['ingridents'])
103
+ for i in recipe_data['ingridents']:
104
+ st.info(f"{i}")
105
+ # Inctuction for recipe
106
+ with col4:
107
+ st.subheader('Instructions')
108
+ st.info(recipe_data['instructions'])
109
+ # st.subheader('Caloric Breakdown')
110
+ '''
111
+ ## Caloric Breakdown
112
+ '''
113
+ st.success(f'''
114
+ * Protien: {percent_Protein}%
115
+ * Fat: {percent_fat}%
116
+ * Carbohydrates: {percent_carbs}%
117
+ ''')
118
+
119
+
120
+ else:
121
+ st.error('Something went wrong please try again :(')
122
 
123
+ elif(img_type=='not food'):
124
+
125
+ # Ingridents of recipie
126
+ st.warning('This is not food image Please try again!!')
127
 
128
 
129
+
130
  else:
131
  st.warning('Please Upload Image')
132