RUDHRESH commited on
Commit
b22bfe6
·
verified ·
1 Parent(s): 3df1e33

Upload 2 files

Browse files
Files changed (2) hide show
  1. app.py +238 -0
  2. requirements.txt +6 -3
app.py ADDED
@@ -0,0 +1,238 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import streamlit as st
2
+ from PIL import Image
3
+ import torch
4
+ from transformers import AutoImageProcessor, AutoModelForImageClassification
5
+ import pandas as pd
6
+
7
+ st.set_page_config(
8
+ page_title="CALIX",
9
+ page_icon="apple",
10
+ layout="centered",
11
+ initial_sidebar_state="collapsed"
12
+ )
13
+
14
+ # === MAKE IT A PHONE APP ===
15
+ st.markdown("""
16
+ <meta name="apple-mobile-web-app-capable" content="yes">
17
+ <meta name="apple-mobile-web-app-status-bar-style" content="black">
18
+ <meta name="apple-mobile-web-app-title" content="CALIX">
19
+ <link rel="manifest" href="/manifest.json">
20
+ """, unsafe_allow_html=True)
21
+
22
+ # === DARK THEME ===
23
+ st.markdown("""
24
+ <style>
25
+ body, .stApp {background:#121212 !important; color:#FFF !important;}
26
+ .stTextInput input {background:#1E1E1E !important; color:#FFF !important; border:1px solid #444 !important;}
27
+ .stFileUploader > div {background:#1E1E1E !important; border:2px dashed #444 !important;}
28
+ .stButton>button {background:#FF9800; color:white; border-radius:20px; padding:10px 24px; font-weight:bold;}
29
+ .stButton>button:hover {background:#F57C00;}
30
+ .nav {background:#1E1E1E; padding:15px; box-shadow:0 2px 10px rgba(0,0,0,0.3); display:flex; justify-content:center; flex-wrap:wrap; margin-bottom:20px;}
31
+ .logo {font-size:32px; color:#4CAF50; font-weight:bold; margin-right:30px;}
32
+ .link {margin:0 15px; color:#BBB; text-decoration:none; padding:8px 16px; border-radius:8px;}
33
+ .link:hover {background:#333; color:#4CAF50;}
34
+ .card {background:#1E1E1E; padding:25px; border-radius:12px; border:1px solid #333; margin:15px 0;}
35
+ .title {color:#4CAF50; font-size:42px; text-align:center; margin:30px 0;}
36
+ .footer {text-align:center; color:#666; font-size:14px; margin-top:60px; padding:15px; border-top:1px solid #333;}
37
+ .nav-btn {background:#333; color:#FFF; padding:8px 16px; border-radius:8px; border:none; margin:0 5px;}
38
+ .nav-btn:hover {background:#555;}
39
+ .stDataFrame td, .stDataFrame th {color:#FFF !important; background:#1E1E1E !important;}
40
+ </style>
41
+ """, unsafe_allow_html=True)
42
+
43
+ # === FOOTER ===
44
+ def footer():
45
+ st.markdown('<div class="footer">©2025 CALIX|AMC CSE-AIML|Open Source Powered</div>', unsafe_allow_html=True)
46
+
47
+ # === NAV BAR ===
48
+ def nav_bar():
49
+ st.markdown("""
50
+ <div class="nav">
51
+ <div class="logo">CALIX</div>
52
+ <a href="/?page=home" class="link">Home</a>
53
+ <a href="/?page=estimate" class="link">Estimate</a>
54
+ <a href="/?page=food_library" class="link">Food Library</a>
55
+ <a href="/?page=health_tips" class="link">Health Tips</a>
56
+ <a href="/?page=about" class="link">About</a>
57
+ </div>
58
+ """, unsafe_allow_html=True)
59
+
60
+ page = st.query_params.get("page", "home")
61
+
62
+ # === BETTER MODEL: eslamxm/vit-base-food101 (Fine-tuned Food-101 + 88% Accuracy) ===
63
+ @st.cache_resource(show_spinner="Loading AI...")
64
+ def load_model():
65
+ processor = AutoImageProcessor.from_pretrained("eslamxm/vit-base-food101")
66
+ model = AutoModelForImageClassification.from_pretrained("eslamxm/vit-base-food101")
67
+ return processor, model
68
+
69
+ processor, model = load_model()
70
+ food_names = [model.config.id2label[i].replace("_", " ").title() for i in range(len(model.config.id2label))]
71
+
72
+ # === LOCAL NUTRITION ===
73
+ nutrition_db = {
74
+ "Pizza": {"cal": 285, "carb": "36g", "prot": "12g", "fat": "10g"},
75
+ "Biryani": {"cal": 320, "carb": "45g", "prot": "15g", "fat": "12g"},
76
+ "Appam": {"cal": 180, "carb": "32g", "prot": "3g", "fat": "5g"},
77
+ "Samosa": {"cal": 250, "carb": "25g", "prot": "5g", "fat": "15g"},
78
+ "Idli": {"cal": 60, "carb": "12g", "prot": "2g", "fat": "0.5g"},
79
+ "Dosa": {"cal": 170, "carb": "28g", "prot": "4g", "fat": "6g"},
80
+ "Burger": {"cal": 500, "carb": "45g", "prot": "25g", "fat": "28g"},
81
+ "Pancakes": {"cal": 220, "carb": "32g", "prot": "6g", "fat": "8g"},
82
+ "Chicken Curry": {"cal": 320, "carb": "15g", "prot": "25g", "fat": "18g"},
83
+ "default": {"cal": 250, "carb": "30g", "prot": "10g", "fat": "10g"}
84
+ }
85
+
86
+ # === HISTORY ===
87
+ if "history" not in st.session_state:
88
+ st.session_state.history = []
89
+
90
+ # === 50 TIPS ===
91
+ health_tips = [
92
+ "Drink 8 glasses of water daily.", "Eat 5 servings of fruits and vegetables.", "Walk 30 minutes every day.",
93
+ "Avoid sugary drinks.", "Sleep 7-8 hours per night.", "Choose whole grains over refined.",
94
+ "Limit processed foods.", "Eat protein with every meal.", "Reduce salt intake.",
95
+ "Cook at home more often.", "Read food labels.", "Eat slowly and mindfully.",
96
+ "Include healthy fats like avocado.", "Limit alcohol consumption.", "Practice portion control.",
97
+ "Add spices instead of salt.", "Eat breakfast daily.", "Stay hydrated during exercise.",
98
+ "Choose lean proteins.", "Include fiber-rich foods.", "Limit fried foods.",
99
+ "Eat more plant-based meals.", "Avoid late-night snacking.", "Chew food thoroughly.",
100
+ "Include omega-3 rich foods.", "Reduce caffeine after noon.", "Eat colorful foods.",
101
+ "Plan meals ahead.", "Keep healthy snacks handy.", "Avoid emotional eating.",
102
+ "Exercise in the morning.", "Stand more, sit less.", "Take stairs instead of elevator.",
103
+ "Practice yoga or meditation.", "Get sunlight daily.", "Limit screen time before bed.",
104
+ "Eat fermented foods for gut health.", "Include nuts and seeds.", "Drink green tea.",
105
+ "Avoid trans fats.", "Eat fish twice a week.", "Include legumes in diet.",
106
+ "Reduce red meat intake.", "Eat seasonal foods.", "Grow your own herbs.",
107
+ "Share meals with family.", "Practice gratitude before eating.", "Try new healthy recipes.",
108
+ "Keep a food journal.", "Celebrate small wins.", "Stay consistent, not perfect."
109
+ ]
110
+
111
+ # === NAV BUTTONS ===
112
+ def nav_buttons(prev=None, next=None):
113
+ col1, col2, col3 = st.columns([1,1,1])
114
+ with col1:
115
+ if prev and st.button("Back", key=f"back_{page}"):
116
+ st.query_params["page"] = prev
117
+ st.rerun()
118
+ with col3:
119
+ if next and st.button("Next", key=f"next_{page}"):
120
+ st.query_params["page"] = next
121
+ st.rerun()
122
+
123
+ # === PAGES ===
124
+ nav_bar()
125
+
126
+ # === HOME ===
127
+ if page == "home":
128
+ st.markdown('<h1 class="title">Know What You Eat - Instantly.</h1>', unsafe_allow_html=True)
129
+ col1, col2 = st.columns(2)
130
+ with col1:
131
+ st.markdown('<div class="card">', unsafe_allow_html=True)
132
+ uploaded = st.file_uploader("Upload food photo", type=["jpg","png","jpeg"])
133
+ if uploaded:
134
+ st.session_state.uploaded_image = uploaded
135
+ st.image(uploaded, width=250)
136
+ st.markdown('</div>', unsafe_allow_html=True)
137
+ with col2:
138
+ st.markdown('<div class="card"><p><b>Detects 101 Foods:</b><br>Pizza, Biryani, Appam, Samosa, Idli, Dosa, Burger, etc.</p></div>', unsafe_allow_html=True)
139
+ nav_buttons(next="estimate")
140
+ footer()
141
+
142
+ # === ESTIMATE ===
143
+ elif page == "estimate":
144
+ st.markdown('<h1 class="title">Estimate Calories</h1>', unsafe_allow_html=True)
145
+ uploaded = st.session_state.get("uploaded_image") or st.file_uploader("Upload photo", type=["jpg","png","jpeg"])
146
+
147
+ if uploaded:
148
+ img = Image.open(uploaded).convert("RGB")
149
+ st.image(img, width=300)
150
+
151
+ with st.spinner("Detecting food..."):
152
+ inputs = processor(images=img, return_tensors="pt")
153
+ with torch.no_grad():
154
+ outputs = model(**inputs)
155
+ idx = outputs.logits.argmax(-1).item()
156
+ name = food_names[idx]
157
+
158
+ st.success(f"*Detected:* {name}")
159
+
160
+ nut = nutrition_db.get(name, nutrition_db["default"])
161
+
162
+ st.markdown(f"""
163
+ <div class="card">
164
+ <p><b>Calories:</b> {nut['cal']} kcal/100g</p>
165
+ <p><b>Carbs:</b> {nut['carb']}</p>
166
+ <p><b>Protein:</b> {nut['prot']}</p>
167
+ <p><b>Fat:</b> {nut['fat']}</p>
168
+ </div>
169
+ """, unsafe_allow_html=True)
170
+
171
+ if st.button("Add to Daily Log"):
172
+ st.session_state.history.append({
173
+ "Food": name,
174
+ "Calories": nut['cal'],
175
+ "Carbs": nut['carb'],
176
+ "Protein": nut['prot'],
177
+ "Fat": nut['fat']
178
+ })
179
+ st.success("Added!")
180
+
181
+ nav_buttons(prev="home", next="food_library")
182
+ else:
183
+ nav_buttons(prev="home")
184
+ footer()
185
+
186
+ # === FOOD LIBRARY ===
187
+ elif page == "food_library":
188
+ st.markdown('<h1 class="title">Food Library (History)</h1>', unsafe_allow_html=True)
189
+ if st.session_state.history:
190
+ df = pd.DataFrame(st.session_state.history)
191
+ st.dataframe(df, use_container_width=True)
192
+ total_cal = sum([x for x in df["Calories"] if isinstance(x, (int, float))])
193
+ st.info(f"*Total Calories Logged:* {total_cal} kcal")
194
+ else:
195
+ st.info("No food logged yet.")
196
+ nav_buttons(prev="estimate", next="health_tips")
197
+ footer()
198
+
199
+ # === HEALTH TIPS ===
200
+ elif page == "health_tips":
201
+ st.markdown('<h1 class="title">Health Tips</h1>', unsafe_allow_html=True)
202
+ tip_idx = st.session_state.get("tip_idx", 0)
203
+ st.markdown(f'<div class="card"><p>{health_tips[tip_idx]}</p></div>', unsafe_allow_html=True)
204
+ col1, col2, col3 = st.columns([1,1,1])
205
+ with col1:
206
+ if st.button("Previous"):
207
+ st.session_state.tip_idx = (tip_idx - 1) % len(health_tips)
208
+ st.rerun()
209
+ with col3:
210
+ if st.button("Next"):
211
+ st.session_state.tip_idx = (tip_idx + 1) % len(health_tips)
212
+ st.rerun()
213
+ nav_buttons(prev="food_library", next="about")
214
+ footer()
215
+
216
+ # === ABOUT ===
217
+ elif page == "about":
218
+ st.markdown('<h1 class="title">About CALIX</h1>', unsafe_allow_html=True)
219
+ st.markdown("""
220
+ *CALIX* is an AI food detector that identifies *101 foods* from photos.
221
+
222
+ ### Features
223
+ - *Detects exact names*: Pizza, Biryani, Appam, Samosa, Idli, Dosa, etc.
224
+ - *Local nutrition*: Calories, Carbs, Protein, Fat (no internet needed)
225
+ - *History log*: Saves all scans with full details
226
+ - *50 Health Tips*
227
+ - *Dark theme + navigation*
228
+
229
+ ### AI Model
230
+ - eslamxm/vit-base-food101 (Public, 101 classes from Food-101 dataset)
231
+ - Dataset: 101,000 images, 101 classes
232
+
233
+ *Built by:*
234
+ Rudhreshwaran, Shreyas, Tiya Singh, Shubham Prasad, Shubham Raj
235
+ *AMC CSE-AIML | 2025*
236
+ """)
237
+ nav_buttons(prev="health_tips")
238
+ footer()
requirements.txt CHANGED
@@ -1,3 +1,6 @@
1
- altair
2
- pandas
3
- streamlit
 
 
 
 
1
+ streamlit==1.35.0
2
+ torch==2.0.1
3
+ torchvision==0.15.2
4
+ transformers==4.35.0
5
+ Pillow==10.0.0
6
+ pandas==2.1.0