import streamlit as st from PIL import Image from NLP_code import preprocess_ingredients from yolo_detect import detect_objects from llm_module import generate_dish_options, generate_recipe import os from dotenv import load_dotenv import base64 # Page setup st.set_page_config(page_title="The Recipe Oracle", layout="wide") # Load Gemini API key from secrets.env load_dotenv("secrets.env") # Background image setup def set_background(image_file): with open(image_file, "rb") as f: data = f.read() encoded = base64.b64encode(data).decode() css = f""" """ st.markdown(css, unsafe_allow_html=True) # Apply background image set_background("background.png") # Add custom Google font st.markdown('', unsafe_allow_html=True) # Heading + subtitle st.markdown("
Your AI-powered kitchen companion
""", unsafe_allow_html=True) st.markdown("---") # Upload image uploaded_file = st.file_uploader("Upload a food image (optional)", type=["jpg", "jpeg", "png"]) # Manual ingredients input manual_input = st.text_input("Add ingredients (comma-separated)") manual_ingredients = [i.strip().lower() for i in manual_input.split(',') if i.strip()] # Allow dish suggestion from manual input if manual_ingredients and "ingredients" not in st.session_state: st.session_state.ingredients = manual_ingredients # Ensure 'uploads' directory exists os.makedirs("uploads", exist_ok=True) # Show uploaded image and save it if uploaded_file: st.image(uploaded_file, caption="Uploaded Image") with open("uploads/image.jpg", "wb") as f: f.write(uploaded_file.read()) # Run YOLO + NLP filtering if st.button("🔍 Detect Ingredients") and uploaded_file: raw_detected = detect_objects("uploads/image.jpg") ingredients = preprocess_ingredients(raw_detected) all_ingredients = list(set(ingredients + manual_ingredients)) st.session_state.ingredients = all_ingredients st.success(f"Ingredients: {', '.join(all_ingredients)}") # Generate dish suggestions if "ingredients" in st.session_state and st.button("✨ Suggest Dishes"): dishes = generate_dish_options(st.session_state.ingredients) st.session_state.dishes = dishes # Show recipe if "dishes" in st.session_state: # Style for radio button labels (dish list) st.markdown(""" """, unsafe_allow_html=True) dish_titles = [d['title'] for d in st.session_state.dishes] selected = st.radio("Pick a dish to cook:", dish_titles) if st.button("📜 Show Recipe"): recipe = generate_recipe(selected) if recipe["title"] == "Error": st.warning("⚠️ Gemini API is temporarily unavailable. Please wait a minute and try again.") else: # 🍽️ Recipe title styling st.markdown(f"""• {step}
""", unsafe_allow_html=True)