File size: 4,855 Bytes
b5ab758
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
import streamlit as st
import numpy as np
import time
import PIL
import PIL.Image as Image


# from utils import make_pred_outside_india,getmodel_outside_india,load_prepare_img
from utils import make_pred_outside_india
from utils import getmodel_outside_india
from utils import getmodel_india
from utils import load_prepare_img
from transformers import CLIPProcessor, CLIPModel
import sys
from RecipeData import fetchRecipeData

IMG_SIZE = (224, 224)
model_V2 = 'efficientnet_b0.pt'
model_V1 = 'indian_efficientnet_b0.pt'


@st.cache()
def model_prediction(model_path, img_file, rescale,selected_location):
    input_img, device = load_prepare_img(img_file)
    if(selected_location=='Outside_India'):
        model = getmodel_outside_india(model_path)
        prediction = make_pred_outside_india(input_img, model, device, selected_location)
    elif(selected_location=='India'):
        model = getmodel_india(model_path)
        prediction = make_pred_outside_india(input_img, model, device, selected_location)
    sorceCode, recipe_data = fetchRecipeData(prediction)
    return prediction, sorceCode, recipe_data 

def food_pred(input_image):
    # input labels for clip model
        label = ['food ', 'Not food']

        # CLIP Model for classification
        model = CLIPModel.from_pretrained("openai/clip-vit-base-patch32")
        processor = CLIPProcessor.from_pretrained("openai/clip-vit-base-patch32")
  
        image = Image.open(requests.get(uploaded_file, stream=True).raw)
        inputs = processor(text=label, images=image, return_tensors="pt", padding=True)
        return inputs
        

def main():
    st.set_page_config(
        page_title="SeeFood",
        page_icon="🍔",
        layout="wide",
        initial_sidebar_state="expanded"
    )

    st.title('SeeFood🍔')
    st.write('Upload a food image and get the recipe for that food and other details of that food')

    col1, col2 = st.columns(2)

    with col1: 
        # image uploading button
        uploaded_file = st.file_uploader("Choose a file")
        selected_location = st.selectbox('Select loaction',('India', 'Outside_India'), index=1)
        if uploaded_file is not None:
            display_img = uploaded_file.read()
            uploaded_img = Image.open(uploaded_file)
            col2.image(display_img, width=500)
            
            
        

       
        predict = st.button('Get Recipe!')
        
        

    if predict:
        if uploaded_file is not None:
            with st.spinner('Please Wait 👩‍🍳'):

                # setting model and rescalling 
                if selected_location == 'India':
                    pred_model = model_V1 
                    pred_rescale = True
                if selected_location == 'Outside_India':
                    pred_model = model_V2
                    pred_rescale =True
      
                
                # makeing prediction and fetching food recipe form api
                food, source_code, recipe_data = model_prediction(pred_model, uploaded_img, pred_rescale,selected_location)
                
                # asssigning caleoric breakdown data
                percent_Protein = recipe_data['percentProtein']
                percent_fat = recipe_data['percentFat']
                percent_carbs = recipe_data['percentCarbs'] 
                
                # food name message
                col1.success(f"It's an {food}")
                
                if source_code == 200:
                    # desplay food recipe
                    st.header(recipe_data['title']+" Recipe")
                
                    col3, col4 = st.columns(2)

                    with col3:
                        # Ingridents of recipie
                        st.subheader('Ingredients')
                        # st.info(recipe_data['ingridents'])
                        for i in recipe_data['ingridents']:
                            st.info(f"{i}")
                    # Inctuction for recipe
                    with col4:
                        st.subheader('Instructions')
                        st.info(recipe_data['instructions'])
                        # st.subheader('Caloric Breakdown')
                        '''
                        ## Caloric Breakdown
                        '''
                        st.success(f'''
                                    * Protien:  {percent_Protein}%
                                    * Fat: {percent_fat}%
                                    * Carbohydrates: {percent_carbs}%
                                    ''')
                        
                       
                else:
                    st.error('Something went wrong please try again :(')
                

        else:
            st.warning('Please Upload Image')

                


if __name__=='__main__': 
    main()