import streamlit as st from PIL import Image import torch from transformers import AutoImageProcessor, AutoModelForImageClassification import pandas as pd st.set_page_config( page_title="CALIX", page_icon="apple", layout="centered", initial_sidebar_state="collapsed" ) # === MAKE IT A PHONE APP === st.markdown(""" """, unsafe_allow_html=True) # === DARK THEME === st.markdown(""" """, unsafe_allow_html=True) # === FOOTER === def footer(): st.markdown('', unsafe_allow_html=True) # === NAV BAR === def nav_bar(): st.markdown(""" """, unsafe_allow_html=True) page = st.query_params.get("page", "home") # === BETTER MODEL: eslamxm/vit-base-food101 (Fine-tuned Food-101 + 88% Accuracy) === @st.cache_resource(show_spinner="Loading AI...") def load_model(): processor = AutoImageProcessor.from_pretrained("eslamxm/vit-base-food101") model = AutoModelForImageClassification.from_pretrained("eslamxm/vit-base-food101") return processor, model processor, model = load_model() food_names = [model.config.id2label[i].replace("_", " ").title() for i in range(len(model.config.id2label))] # === LOCAL NUTRITION === nutrition_db = { "Pizza": {"cal": 285, "carb": "36g", "prot": "12g", "fat": "10g"}, "Biryani": {"cal": 320, "carb": "45g", "prot": "15g", "fat": "12g"}, "Appam": {"cal": 180, "carb": "32g", "prot": "3g", "fat": "5g"}, "Samosa": {"cal": 250, "carb": "25g", "prot": "5g", "fat": "15g"}, "Idli": {"cal": 60, "carb": "12g", "prot": "2g", "fat": "0.5g"}, "Dosa": {"cal": 170, "carb": "28g", "prot": "4g", "fat": "6g"}, "Burger": {"cal": 500, "carb": "45g", "prot": "25g", "fat": "28g"}, "Pancakes": {"cal": 220, "carb": "32g", "prot": "6g", "fat": "8g"}, "Chicken Curry": {"cal": 320, "carb": "15g", "prot": "25g", "fat": "18g"}, "default": {"cal": 250, "carb": "30g", "prot": "10g", "fat": "10g"} } # === HISTORY === if "history" not in st.session_state: st.session_state.history = [] # === 50 TIPS === health_tips = [ "Drink 8 glasses of water daily.", "Eat 5 servings of fruits and vegetables.", "Walk 30 minutes every day.", "Avoid sugary drinks.", "Sleep 7-8 hours per night.", "Choose whole grains over refined.", "Limit processed foods.", "Eat protein with every meal.", "Reduce salt intake.", "Cook at home more often.", "Read food labels.", "Eat slowly and mindfully.", "Include healthy fats like avocado.", "Limit alcohol consumption.", "Practice portion control.", "Add spices instead of salt.", "Eat breakfast daily.", "Stay hydrated during exercise.", "Choose lean proteins.", "Include fiber-rich foods.", "Limit fried foods.", "Eat more plant-based meals.", "Avoid late-night snacking.", "Chew food thoroughly.", "Include omega-3 rich foods.", "Reduce caffeine after noon.", "Eat colorful foods.", "Plan meals ahead.", "Keep healthy snacks handy.", "Avoid emotional eating.", "Exercise in the morning.", "Stand more, sit less.", "Take stairs instead of elevator.", "Practice yoga or meditation.", "Get sunlight daily.", "Limit screen time before bed.", "Eat fermented foods for gut health.", "Include nuts and seeds.", "Drink green tea.", "Avoid trans fats.", "Eat fish twice a week.", "Include legumes in diet.", "Reduce red meat intake.", "Eat seasonal foods.", "Grow your own herbs.", "Share meals with family.", "Practice gratitude before eating.", "Try new healthy recipes.", "Keep a food journal.", "Celebrate small wins.", "Stay consistent, not perfect." ] # === NAV BUTTONS === def nav_buttons(prev=None, next=None): col1, col2, col3 = st.columns([1,1,1]) with col1: if prev and st.button("Back", key=f"back_{page}"): st.query_params["page"] = prev st.rerun() with col3: if next and st.button("Next", key=f"next_{page}"): st.query_params["page"] = next st.rerun() # === PAGES === nav_bar() # === HOME === if page == "home": st.markdown('

Know What You Eat - Instantly.

', unsafe_allow_html=True) col1, col2 = st.columns(2) with col1: st.markdown('
', unsafe_allow_html=True) uploaded = st.file_uploader("Upload food photo", type=["jpg","png","jpeg"]) if uploaded: st.session_state.uploaded_image = uploaded st.image(uploaded, width=250) st.markdown('
', unsafe_allow_html=True) with col2: st.markdown('

Detects 101 Foods:
Pizza, Biryani, Appam, Samosa, Idli, Dosa, Burger, etc.

', unsafe_allow_html=True) nav_buttons(next="estimate") footer() # === ESTIMATE === elif page == "estimate": st.markdown('

Estimate Calories

', unsafe_allow_html=True) uploaded = st.session_state.get("uploaded_image") or st.file_uploader("Upload photo", type=["jpg","png","jpeg"]) if uploaded: img = Image.open(uploaded).convert("RGB") st.image(img, width=300) with st.spinner("Detecting food..."): inputs = processor(images=img, return_tensors="pt") with torch.no_grad(): outputs = model(**inputs) idx = outputs.logits.argmax(-1).item() name = food_names[idx] st.success(f"*Detected:* {name}") nut = nutrition_db.get(name, nutrition_db["default"]) st.markdown(f"""

Calories: {nut['cal']} kcal/100g

Carbs: {nut['carb']}

Protein: {nut['prot']}

Fat: {nut['fat']}

""", unsafe_allow_html=True) if st.button("Add to Daily Log"): st.session_state.history.append({ "Food": name, "Calories": nut['cal'], "Carbs": nut['carb'], "Protein": nut['prot'], "Fat": nut['fat'] }) st.success("Added!") nav_buttons(prev="home", next="food_library") else: nav_buttons(prev="home") footer() # === FOOD LIBRARY === elif page == "food_library": st.markdown('

Food Library (History)

', unsafe_allow_html=True) if st.session_state.history: df = pd.DataFrame(st.session_state.history) st.dataframe(df, use_container_width=True) total_cal = sum([x for x in df["Calories"] if isinstance(x, (int, float))]) st.info(f"*Total Calories Logged:* {total_cal} kcal") else: st.info("No food logged yet.") nav_buttons(prev="estimate", next="health_tips") footer() # === HEALTH TIPS === elif page == "health_tips": st.markdown('

Health Tips

', unsafe_allow_html=True) tip_idx = st.session_state.get("tip_idx", 0) st.markdown(f'

{health_tips[tip_idx]}

', unsafe_allow_html=True) col1, col2, col3 = st.columns([1,1,1]) with col1: if st.button("Previous"): st.session_state.tip_idx = (tip_idx - 1) % len(health_tips) st.rerun() with col3: if st.button("Next"): st.session_state.tip_idx = (tip_idx + 1) % len(health_tips) st.rerun() nav_buttons(prev="food_library", next="about") footer() # === ABOUT === elif page == "about": st.markdown('

About CALIX

', unsafe_allow_html=True) st.markdown(""" *CALIX* is an AI food detector that identifies *101 foods* from photos. ### Features - *Detects exact names*: Pizza, Biryani, Appam, Samosa, Idli, Dosa, etc. - *Local nutrition*: Calories, Carbs, Protein, Fat (no internet needed) - *History log*: Saves all scans with full details - *50 Health Tips* - *Dark theme + navigation* ### AI Model - eslamxm/vit-base-food101 (Public, 101 classes from Food-101 dataset) - Dataset: 101,000 images, 101 classes *Built by:* Rudhreshwaran, Shreyas, Tiya Singh, Shubham Prasad, Shubham Raj *AMC CSE-AIML | 2025* """) nav_buttons(prev="health_tips") footer()