import streamlit as st st.set_page_config( page_title="Liberator", page_icon="๐Ÿฝ๏ธ", layout="centered", initial_sidebar_state="collapsed", ) from PIL import Image from backend.classifier import classify_image from backend.rag import get_ingredients, get_healthier_alternatives, seed_collection @st.cache_resource def _init(): seed_collection() _init() st.markdown( """ """, unsafe_allow_html=True, ) st.markdown('

๐Ÿฝ๏ธ Liberator

', unsafe_allow_html=True) st.markdown( '

Snap or upload a food photo for AI-powered nutritional insights

', unsafe_allow_html=True, ) with st.sidebar: st.header("About") st.write( "**Liberator** uses computer vision to identify foods, " "then retrieves nutritional advice from a curated knowledge base " "using Retrieval-Augmented Generation (RAG)." ) st.divider() st.markdown( "**Tech stack:** FastAPI ยท ChromaDB ยท HuggingFace Transformers ยท " "Google Gemini ยท Streamlit" ) # --------------- Image input --------------- tab_upload, tab_camera = st.tabs(["๐Ÿ“ Upload", "๐Ÿ“ท Camera"]) with tab_upload: uploaded_file = st.file_uploader( "Upload a food image", type=["jpg", "jpeg", "png", "webp"], ) with tab_camera: camera_photo = st.camera_input("Take a photo of your food") image = None if uploaded_file is not None: image = Image.open(uploaded_file).convert("RGB") elif camera_photo is not None: image = Image.open(camera_photo).convert("RGB") if image is not None: st.image(image, caption="Your image", use_container_width=True) if st.button("๐Ÿ” Analyze", type="primary", use_container_width=True): with st.spinner("Classifying image..."): predictions = classify_image(image) if not predictions: st.error("Classification returned no results.") else: top_food = predictions[0]["label"] confidence = predictions[0]["confidence"] st.markdown( '

๐ŸŽฏ Classification

', unsafe_allow_html=True, ) col1, col2 = st.columns(2) col1.metric("Detected Food", top_food.replace("_", " ").title()) col2.metric("Confidence", f"{confidence * 100:.1f}%") if len(predictions) > 1: with st.expander("Other possibilities"): for p in predictions[1:]: st.write( f"- **{p['label'].replace('_', ' ').title()}** " f"โ€” {p['confidence'] * 100:.1f}%" ) with st.spinner("Fetching ingredients..."): ingredients = get_ingredients(top_food) st.markdown( '

๐Ÿ“ Ingredients

', unsafe_allow_html=True, ) st.write(ingredients) with st.spinner("Generating healthier alternatives via RAG..."): alternatives, sources_used = get_healthier_alternatives(top_food) st.markdown( '

๐Ÿ’ก Healthier Alternatives

', unsafe_allow_html=True, ) st.info( f"Retrieved from {sources_used} knowledge-base documents via RAG" ) st.write(alternatives) else: st.info("๐Ÿ‘† Upload a food image or snap a photo to get started.")