File size: 6,570 Bytes
318b810
1644de3
 
 
d957e12
1644de3
 
 
8c23da3
1644de3
 
 
 
8c23da3
1644de3
 
 
 
 
 
 
8c23da3
1644de3
 
 
 
 
8c23da3
1644de3
 
 
8c23da3
1644de3
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1a63bf3
 
 
 
 
 
 
 
 
5a9a9c8
1a63bf3
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1644de3
 
 
9fded46
 
 
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
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
import streamlit as st
import cv2
import os
from backend import *

# Create a folder to save captured images
if not os.path.exists("captured_images"):
    os.makedirs("captured_images")

# Initialize the session state
session_state = st.session_state
if 'ingredientsList' not in session_state:
    session_state['ingredientsList'] = ["apple", "banana", "orange", "strawberries"]

def main():
    
    st.title('πŸ§‘πŸ½β€πŸ³ RecipeBud')
    
    st.sidebar.header('Ingredients & Nutrition')
    # List of items
    #items = ['Item 1', 'Item 2', 'Item 3']

    #list to of Ingredients camptured
    #ingredientsList =["apple", "orange", "mango"] #list()
   
    # Create a VideoCapture object to access the webcam
    cap = cv2.VideoCapture(0)

    # Set the video frame width and height (optional)
    cap.set(cv2.CAP_PROP_FRAME_WIDTH, 800)
    cap.set(cv2.CAP_PROP_FRAME_HEIGHT, 400)

    # Check if the webcam is opened correctly
    if not cap.isOpened():
        st.error("Error: Unable to access the webcam.")
        return

    # Display a placeholder for the video stream
    video_placeholder = st.empty()
    # Button to capture image
    if st.button("Capture Image"):
        image_path = capture_image()
        classification = classifyImage(image_path)
        session_state['ingredientsList'].append(classification)

    # Button to indicate done
    #done_button = st.sidebar.button('Done')

    # Display the captured ingredients
    #st.write("Captured Ingredients:", session_state['ingredientsList'])

    button_clicked = st.sidebar.button('Done')
    
    # Display recipes if "Done" is clicked
    while not button_clicked:
    # Read a frame from the webcam
        ret, frame = cap.read()

        if not ret:
            st.error("Error: Unable to read frame from the webcam.")
            break

        # Display the frame in the Streamlit app
        video_placeholder.image(cv2.cvtColor(frame, cv2.COLOR_BGR2RGB), channels="RGB", use_column_width=True)
    if button_clicked:
        cap.release()
        if session_state['ingredientsList']:
            session_state['ingredientsList'].pop()
        #st.write("Updated Ingredients List:", session_state['ingredientsList'])

        displayRecipes(session_state['ingredientsList'])
        # Define content for each item
        content = {}
        for ingredient in session_state['ingredientsList']:
            content[ingredient] = askGPT(f"Give me your estimate the calories, grams of protein, grams of sugar, grams of fat, and grams of carbohydrates per 100g of {ingredient} as a list")

        # Display expanders for each item
        for ingredient in session_state['ingredientsList']:
            with st.sidebar.expander(ingredient):
                st.write(content[ingredient])
        displayRecipes(session_state['ingredientsList'])

def displayRecipes(ingredientsList):
    items = []
    #now we are gonna send the ingredient list to ask gpt
    prompt = f"I have following Ingredients :{','.join(ingredientsList)}. What can I make with these \
            Ingredients? Give me A list of detailed recipes with measurements containing these ingredients with Nutrition Facts per 100g based on the widely accepted nutritional value of each of these ingredients. Rank the list from \
            highest nutritional value to lowest. Give me results in \
            following format and do not deviate from this format:\
            ['Recipe Title', 'content of recipe and nutritional facts per 100g']. Only give me the list. Do not add commentary or personalized responses. Keep it under 200 words."
    #prompt = f"You are going to act as a nutritional expert who has a lot of knowledge about food. I have the following ingredients: {','.join(ingredientsList)}. What can I make with these ingredients? Give me a list of names of recipes, maximum five."
    LLMResult = askGPT(prompt)
    lystOfRecipes = LLMResult.split('\n\n')
    # print(lystOfRecipes)
    for recipe in range(1,len(lystOfRecipes)-1):
        items.append({"title": lystOfRecipes[recipe].split(":")[0], "content": ""})
    # Display the items with =expanding boxes
    for item in items:
        #for side bar's item
        #with st.sidebar.expander(item):
            #st.write("man-holding-banana.jpeg")
        #main page items
        with st.expander(item["title"]):
            st.write(askGPT(f"Give me a detailed recipe for a dish called {item['title']} containing all of the following ingredients: {','.join(ingredientsList)}. Make sure your response is easy to follow and comprehensive."))
    

def capture_image():
    # Create a VideoCapture object to access the webcam
    cap = cv2.VideoCapture(0)

    # Set the video frame width and height (optional)
    cap.set(cv2.CAP_PROP_FRAME_WIDTH, 800)
    cap.set(cv2.CAP_PROP_FRAME_HEIGHT, 400)

    # Check if the webcam is opened correctly
    if not cap.isOpened():
        st.error("Error: Unable to access the webcam.")
        return

    # Read a frame from the webcam
    ret, frame = cap.read()

    if not ret:
        st.error("Error: Unable to read frame from the webcam.")
        return

    # Save the frame as an image
    image_path = f"captured_images/captured_image_{len(os.listdir('captured_images')) + 1}.jpg"
    cv2.imwrite(image_path, cv2.cvtColor(frame, cv2.COLOR_BGR2RGB))
    st.success(f"Image captured and saved as {image_path}")

    # Release the VideoCapture and close the OpenCV window
    cap.release()
    return image_path

# This is the main logic file that contains hugging face model interaction

# This model is for detecting food in the image.
# Use a pipeline as a high-level helper
from transformers import pipeline
import os
import openai
openai.organization = "org-5Z0c3Uk1VG7t3TsczN6M4FCi"
#openai.api_key = os.getenv("OPENAI_API_KEY")
openai.api_key ="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" 

def askGPT(prompt="what can I make with potato?"):
    response = openai.ChatCompletion.create(
      model="gpt-3.5-turbo",
      messages=[
        {
          "role": "system",
          "content":prompt 
        },
        {
          "role": "user",
          "content": ""
        }      ],
      temperature=1,
      max_tokens=256,
      top_p=1,
      frequency_penalty=0,
      presence_penalty=0
    )
    result = response["choices"][0]["message"]["content"]
    return result

def classifyImage(image):
    pipe = pipeline("image-classification", model="microsoft/resnet-50")
    result = pipe(image)
    return result[0]['label']


if __name__ == '__main__':
    main()