Spaces:
Sleeping
Sleeping
| 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() | |