Srish117 commited on
Commit
19a60d9
Β·
verified Β·
1 Parent(s): a64ae53

Upload 3 files

Browse files
Files changed (3) hide show
  1. logo.png +0 -0
  2. requirements.txt +4 -0
  3. sl.py +726 -0
logo.png ADDED
requirements.txt ADDED
@@ -0,0 +1,4 @@
 
 
 
 
 
1
+ Flask==2.3.2
2
+ requests==2.31.0
3
+ google-generativeai
4
+ markdown2
sl.py ADDED
@@ -0,0 +1,726 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ # import requests
2
+ # import google.generativeai as genai
3
+ # import streamlit as st
4
+
5
+ # # Configure Gemini API (Use your actual API key)
6
+ # genai.configure(api_key='AIzaSyD5yLv8zkGNC7YbxxODLqlMJJKTv8VWdQw')
7
+
8
+ # # Function to get data from OpenFoodFacts API
9
+ # def get_data(product_name):
10
+ # url = "https://world.openfoodfacts.org/cgi/search.pl"
11
+ # params = {
12
+ # 'search_terms': product_name,
13
+ # 'search_simple': 1,
14
+ # 'json': 1,
15
+ # }
16
+ # response = requests.get(url, params=params)
17
+ # data = response.json()
18
+ # if 'products' not in data or len(data['products']) == 0:
19
+ # return [] # Return empty if no products found
20
+
21
+ # # Filter products with names and return top 5
22
+ # data['products'] = [p for p in data['products'] if 'product_name' in p]
23
+ # return data['products'][:5]
24
+
25
+ # # Function to generate product analysis using Gemini
26
+ # def generate_summary(product, tone):
27
+ # name = product.get('product_name', 'Not mentioned')
28
+ # brand = product.get('brands', 'Not mentioned')
29
+ # nutriscore_grade = product.get('nutriscore_grade', 'Not mentioned')
30
+ # eco_score = product.get('ecoscore_grade', 'Not mentioned')
31
+ # packaging = product.get('packaging', 'Not mentioned')
32
+ # ingredients = product.get('ingredients_text', 'Not mentioned')
33
+ # nutrients = product.get('nutriments', 'Not mentioned')
34
+ # nova = product.get('nova_groups_tags', 'Not mentioned')
35
+
36
+ # # Generate prompt based on tone
37
+ # prompt = f"""
38
+ # You are an AI assistant analyzing consumer products. Here are the details:
39
+ # - Name: {name}
40
+ # - Brand: {brand}
41
+ # - EcoScore: {eco_score}
42
+ # - NutriScore: {nutriscore_grade}
43
+ # - NovaScore: {nova}
44
+ # - Ingredients: {ingredients}
45
+ # - Nutrients: {nutrients}
46
+ # - Packaging: {packaging}
47
+
48
+ # Please provide a {tone} analysis including:
49
+ # 1. Positive aspects of the product.
50
+ # 2. Negative aspects of the product.
51
+ # 3. Health impact.
52
+ # 4. Environmental impact.
53
+ # """
54
+
55
+ # model = genai.GenerativeModel(model_name="gemini-1.5-flash")
56
+ # response = model.generate_content(prompt)
57
+ # return response.text
58
+
59
+ # # Streamlit interface
60
+ # def main():
61
+ # st.title("ConsumeNice")
62
+ # st.write("Get product details and summaries using OpenFoodFacts and Gemini AI.")
63
+
64
+ # product_input = st.text_input("Product Name", placeholder="Enter the product name...")
65
+ # tone = st.radio("Summary Tone", options=["simple", "deeper"], index=0)
66
+
67
+ # if st.button("Search"):
68
+ # products = get_data(product_input)
69
+ # if not products:
70
+ # st.error("No products found for the given name.")
71
+ # else:
72
+ # product_names = [f"{p['product_name']} (Brand: {p.get('brands', 'Unknown')})" for p in products]
73
+ # selected_product_name = st.radio("Select a Product", product_names, key='product_selection')
74
+
75
+ # # Find the selected product details
76
+ # selected_product = next(p for p in products if f"{p['product_name']} (Brand: {p.get('brands', 'Unknown')})" == selected_product_name)
77
+
78
+ # # Display selected product name
79
+ # st.write(f"### Selected Product: {selected_product['product_name']} (Brand: {selected_product.get('brands', 'Unknown')})")
80
+
81
+ # if selected_product:
82
+ # # Use session state to retain summary across selections
83
+ # if 'summary' not in st.session_state:
84
+ # st.session_state.summary = None
85
+
86
+ # summary = generate_summary(selected_product, tone)
87
+ # st.session_state.summary = summary
88
+
89
+ # st.write(f"### Summary:")
90
+ # st.write(st.session_state.summary)
91
+
92
+ # if __name__ == "__main__":
93
+ # main()
94
+
95
+ # import requests
96
+ # import google.generativeai as genai
97
+ # import streamlit as st
98
+
99
+ # # Configure Gemini API (Use your actual API key)
100
+ # genai.configure(api_key='AIzaSyD5yLv8zkGNC7YbxxODLqlMJJKTv8VWdQw')
101
+
102
+ # # Function to get data from OpenFoodFacts API
103
+ # def get_data(product_name):
104
+ # url = "https://world.openfoodfacts.org/cgi/search.pl"
105
+ # params = {
106
+ # 'search_terms': product_name,
107
+ # 'search_simple': 1,
108
+ # 'json': 1,
109
+ # }
110
+ # response = requests.get(url, params=params)
111
+ # data = response.json()
112
+ # if 'products' not in data or len(data['products']) == 0:
113
+ # return [] # Return empty if no products found
114
+
115
+ # # Filter products with names and return top 5
116
+ # data['products'] = [p for p in data['products'] if 'product_name' in p]
117
+ # return data['products'][:5]
118
+
119
+ # # Function to generate product analysis using Gemini
120
+ # def generate_summary(product, tone):
121
+ # name = product.get('product_name', 'Not mentioned')
122
+ # brand = product.get('brands', 'Not mentioned')
123
+ # nutriscore_grade = product.get('nutriscore_grade', 'Not mentioned')
124
+ # eco_score = product.get('ecoscore_grade', 'Not mentioned')
125
+ # packaging = product.get('packaging', 'Not mentioned')
126
+ # ingredients = product.get('ingredients_text', 'Not mentioned')
127
+ # nutrients = product.get('nutriments', 'Not mentioned')
128
+ # nova = product.get('nova_groups_tags', 'Not mentioned')
129
+
130
+ # # Generate prompt based on tone
131
+ # prompt = f"""
132
+ # You are an AI assistant analyzing consumer products. Here are the details:
133
+ # - Name: {name}
134
+ # - Brand: {brand}
135
+ # - EcoScore: {eco_score}
136
+ # - NutriScore: {nutriscore_grade}
137
+ # - NovaScore: {nova}
138
+ # - Ingredients: {ingredients}
139
+ # - Nutrients: {nutrients}
140
+ # - Packaging: {packaging}
141
+
142
+ # Please provide a {tone} analysis including:
143
+ # 1. Positive aspects of the product.
144
+ # 2. Negative aspects of the product.
145
+ # 3. Health impact.
146
+ # 4. Environmental impact.
147
+ # """
148
+
149
+ # model = genai.GenerativeModel(model_name="gemini-1.5-flash")
150
+ # response = model.generate_content(prompt)
151
+ # return response.text
152
+
153
+ # # Streamlit interface
154
+ # def main():
155
+ # # Page setup and header
156
+ # st.set_page_config(page_title="ConsumeNice", page_icon="🍽️", layout="centered")
157
+ # st.title("🍽️ ConsumeNice - Product Analysis with AI")
158
+ # st.write("Welcome to **ConsumeNice**, Know What You Eat")
159
+
160
+ # st.sidebar.title("Developed by:")
161
+ # st.sidebar.markdown("**Srish**")
162
+ # st.sidebar.markdown("[LinkedIn](https://www.linkedin.com/in/srishrachamalla/)")
163
+ # st.sidebar.markdown("**Sai Teja**")
164
+ # st.sidebar.markdown("[LinkedIn](https://www.linkedin.com/in/saiteja-pallerla-668734225/)")
165
+
166
+ # st.sidebar.title("Additional Info")
167
+ # st.sidebar.markdown("This app allows you to search for a product and get an AI-generated analysis.")
168
+
169
+ # # Input fields
170
+ # product_input = st.text_input("Enter Product Name", placeholder="Type product name to search...")
171
+ # tone = st.radio("Select Summary Tone", options=["Simple", "Deeper"], index=0)
172
+
173
+ # if st.button("Search"):
174
+ # with st.spinner("Fetching product data..."):
175
+ # products = get_data(product_input)
176
+ # if not products:
177
+ # st.error("No products found for the given name.")
178
+ # else:
179
+ # product_names = [f"{p['product_name']} (Brand: {p.get('brands', 'Unknown')})" for p in products]
180
+ # selected_product_name = st.radio("Select a Product", product_names, key='product_selection')
181
+
182
+ # # Find the selected product details
183
+ # selected_product = next(p for p in products if f"{p['product_name']} (Brand: {p.get('brands', 'Unknown')})" == selected_product_name)
184
+
185
+ # # Display selected product name
186
+ # st.write(f"### Selected Product: **{selected_product['product_name']}** (Brand: {selected_product.get('brands', 'Unknown')})")
187
+
188
+ # if selected_product:
189
+ # # Use session state to retain summary across selections
190
+ # if 'summary' not in st.session_state:
191
+ # st.session_state.summary = None
192
+
193
+ # with st.spinner("Generating AI analysis..."):
194
+ # summary = generate_summary(selected_product, tone)
195
+ # st.session_state.summary = summary
196
+
197
+ # # Display summary
198
+ # st.write("### Summary:")
199
+ # st.success(st.session_state.summary)
200
+
201
+ # # Footer section
202
+ # st.markdown("---")
203
+ # st.write("### About the Hackathon Project")
204
+ # st.info("This project was developed as part of a hackathon challenge to showcase AI Skills. Built using OpenFoodFacts and Google's Gemini.")
205
+
206
+ # if __name__ == "__main__":
207
+ # main()
208
+
209
+ # import requests
210
+ # import google.generativeai as genai
211
+ # import streamlit as st
212
+ # from PIL import Image
213
+
214
+ # # Configure Gemini API (Use your actual API key)
215
+ # genai.configure(api_key='AIzaSyD5yLv8zkGNC7YbxxODLqlMJJKTv8VWdQw')
216
+
217
+ # # Function to get data from OpenFoodFacts API
218
+ # def get_data(product_name):
219
+ # url = "https://world.openfoodfacts.org/cgi/search.pl"
220
+ # params = {
221
+ # 'search_terms': product_name,
222
+ # 'search_simple': 1,
223
+ # 'json': 1,
224
+ # }
225
+ # response = requests.get(url, params=params)
226
+ # data = response.json()
227
+ # if 'products' not in data or len(data['products']) == 0:
228
+ # return [] # Return empty if no products found
229
+
230
+ # # Filter products with names and return top 5
231
+ # data['products'] = [p for p in data['products'] if 'product_name' in p]
232
+ # return data['products'][:5]
233
+
234
+ # # Function to generate product analysis using Gemini
235
+ # def generate_summary(product, tone):
236
+ # name = product.get('product_name', 'Not mentioned')
237
+ # brand = product.get('brands', 'Not mentioned')
238
+ # nutriscore_grade = product.get('nutriscore_grade', 'Not mentioned')
239
+ # eco_score = product.get('ecoscore_grade', 'Not mentioned')
240
+ # packaging = product.get('packaging', 'Not mentioned')
241
+ # ingredients = product.get('ingredients_text', 'Not mentioned')
242
+ # nutrients = product.get('nutriments', 'Not mentioned')
243
+ # nova = product.get('nova_groups_tags', 'Not mentioned')
244
+
245
+ # # Generate prompt based on tone
246
+ # prompt = f"""
247
+ # You are an AI assistant analyzing consumer products. Here are the details:
248
+ # - Name: {name}
249
+ # - Brand: {brand}
250
+ # - EcoScore: {eco_score}
251
+ # - NutriScore: {nutriscore_grade}
252
+ # - NovaScore: {nova}
253
+ # - Ingredients: {ingredients}
254
+ # - Nutrients: {nutrients}
255
+ # - Packaging: {packaging}
256
+
257
+ # Please provide a {tone} analysis including:
258
+ # 1. Positive aspects of the product.
259
+ # 2. Negative aspects of the product.
260
+ # 3. Health impact.
261
+ # 4. Environmental impact.
262
+ # """
263
+
264
+ # model = genai.GenerativeModel(model_name="gemini-1.5-flash")
265
+ # response = model.generate_content(prompt)
266
+ # return response.text
267
+
268
+ # # Streamlit interface
269
+ # def main():
270
+ # # Page setup and header with background image
271
+ # st.set_page_config(page_title="ConsumeNice", page_icon="🍽️", layout="centered")
272
+
273
+ # # Custom CSS for better aesthetics
274
+ # # st.markdown(
275
+ # # """
276
+ # # <style>
277
+ # # .main {background-color: #f0f2f6;}
278
+ # # .reportview-container .main .block-container {
279
+ # # padding-top: 2rem;
280
+ # # padding-right: 3rem;
281
+ # # padding-left: 3rem;
282
+ # # }
283
+ # # .css-1d391kg {color: #ff4b4b !important;}
284
+ # # .css-145kmo2 {color: #00b4d8 !important;}
285
+ # # h1 {color: #1f3b4d !important;}
286
+ # # .stButton>button {background-color: #f63366; color: white;}
287
+ # # .stTextInput>div>input {padding: 12px; border-radius: 6px; border: 1px solid #00b4d8;}
288
+ # # .stRadio>div>label {color: #1f3b4d !important;}
289
+ # # </style>
290
+ # # """,
291
+ # # unsafe_allow_html=True
292
+ # # )
293
+ # st.markdown(
294
+ # """
295
+ # <style>
296
+ # .main {background-color: #000000;}
297
+ # .reportview-container .main .block-container {
298
+ # padding-top: 2rem;
299
+ # padding-right: 2.5rem;
300
+ # padding-left: 2.5rem;
301
+ # }
302
+ # h1, h2, h3, h4, h5 {color: #ffffff;}
303
+ # .stButton>button {
304
+ # background-color: #6c757d;
305
+ # color: white;
306
+ # border-radius: 8px;
307
+ # }
308
+ # .stButton>button:hover {
309
+ # background-color: #5a6268;
310
+ # }
311
+ # .stTextInput>div>input {
312
+ # padding: 10px;
313
+ # border-radius: 6px;
314
+ # border: 1px solid #ced4da;
315
+ # background-color: #f8f9fa;
316
+ # }
317
+ # .stRadio>div>label {color: #495057 !important;}
318
+ # .css-1d391kg {color: #495057 !important;}
319
+ # .css-145kmo2 {color: #495057 !important;}
320
+ # </style>
321
+ # """,
322
+ # unsafe_allow_html=True
323
+ # )
324
+
325
+
326
+ # # App logo (assuming you have an image named 'logo.png' in the same directory)
327
+ # st.image(Image.open(r'C:\Projects\google hack\test\static\images\logo.png'), width=120, caption="ConsumeNice - Know What You Consume", )
328
+ # # Custom HTML for displaying the image and caption side by side
329
+ # # image = Image.open(r'C:\Projects\google hack\test\static\images\logo.png')
330
+ # # st.markdown(
331
+ # # f"""
332
+ # # <div style="display: flex; align-items: center;">
333
+ # # <img src="data:image/png;base64,{st.image(image, use_column_width=False)}" style="width: 10px;">
334
+ # # <p style="margin-left: 20px; font-size: 18px;">ConsumeNice - Know What You Consume</p>
335
+ # # </div>
336
+ # # """,
337
+ # # unsafe_allow_html=True
338
+ # # )
339
+
340
+ # # st.title("🍽️ ConsumeNice - Analyze Products with AI")
341
+ # st.markdown(
342
+ # """
343
+ # <h1 style='text-align: center; color: #fffff;'>🍽️ ConsumeNice - Analyze Products with AI</h1>
344
+ # """,
345
+ # unsafe_allow_html=True
346
+ # )
347
+ # st.write("Welcome to **ConsumeNice**, where you can search for products and get an AI-generated analysis based on their nutritional, environmental, and packaging details. Built with OpenFoodFacts and Gemini AI.")
348
+
349
+ # # Sidebar for developer profiles and hackathon info
350
+ # # st.sidebar.title("Developers - πŸ‘¨β€πŸ’»")
351
+ # # st.sidebar.markdown(
352
+ # # """
353
+ # # <h1 style='color: #0072B2;'>Developers - πŸ‘¨β€πŸ’»</h1>
354
+ # # """,
355
+ # # unsafe_allow_html=True
356
+ # # )
357
+ # # st.sidebar.markdown("[LinkedIn - Srish](https://www.linkedin.com/in/srish-linkedin-url)")
358
+
359
+ # # st.sidebar.markdown("[LinkedIn - Sai Teja](https://www.linkedin.com/in/sai-teja-linkedin-url)")
360
+
361
+ # # # st.sidebar.title("Hackathon Project")
362
+ # # st.sidebar.markdown(
363
+ # # """
364
+ # # <h1 style='color: #0072B2;'>Hackathon Project</h1>
365
+ # # """,
366
+ # # unsafe_allow_html=True
367
+ # # )
368
+ # # st.sidebar.markdown("This project showcases AI integration to analyze consumer products.")
369
+ # # Sidebar content with custom formatting
370
+ # st.sidebar.markdown(
371
+ # """
372
+ # <h1 style='color: #0072B2;'>πŸš€ Hackathon Project</h1>
373
+ # """,
374
+ # unsafe_allow_html=True
375
+ # )
376
+
377
+ # st.sidebar.markdown("Welcome to the **ConsumeNice** project, developed for the hackathon to showcase AI integration in product analysis.")
378
+
379
+ # # Add some icons/emojis to make it look more engaging
380
+ # st.sidebar.markdown("### πŸ”§ Project Features")
381
+ # st.sidebar.markdown("- Analyze product details using **OpenFoodFacts** API.")
382
+ # st.sidebar.markdown("- AI-generated analysis using **Google Gemini AI**.")
383
+ # st.sidebar.markdown("- Environment, packaging, and health analysis.")
384
+
385
+ # # Developer details with LinkedIn links
386
+ # st.sidebar.markdown("### πŸ‘¨β€πŸ’» Developers")
387
+ # st.sidebar.markdown("[**Srish**](https://www.linkedin.com/in/srishrachamalla/) - AI/ML Developer")
388
+ # st.sidebar.markdown("[**Sai Teja**](https://www.linkedin.com/in/saiteja-pallerla-668734225/) - Data Analyst")
389
+
390
+ # # Add expander sections for additional content
391
+ # with st.sidebar.expander("ℹ️ About ConsumeNice"):
392
+ # st.write("**ConsumeNice** is designed to give consumers more insights into the products they consume, analyzing factors like health impact, environmental footprint, and packaging.")
393
+
394
+ # with st.sidebar.expander("πŸ“š Useful Resources"):
395
+ # st.write("[Google Gemini AI Documentation](https://developers.google.com/ai)")
396
+ # st.write("[Streamlit Documentation](https://docs.streamlit.io/)")
397
+
398
+ # # Add progress indicator for hackathon phases or development stages
399
+ # st.sidebar.markdown("### ⏳ Hackathon Progress")
400
+ # st.sidebar.progress(0.99) # Set progress level (0 to 1)
401
+
402
+ # # Sidebar footer with final notes
403
+ # st.sidebar.markdown("---")
404
+ # st.sidebar.markdown(
405
+ # """
406
+ # <div style="text-align: center; font-size: 0.85em;">
407
+ # Developed by Srish & Sai Teja β€’ Powered by Google Gemini AI
408
+ # </div>
409
+ # """, unsafe_allow_html=True
410
+ # )
411
+
412
+
413
+ # # User input fields with improved placeholders and hints
414
+ # product_input = st.text_input("Enter Product Name", placeholder="e.g., Coca-Cola, Oreo, Dove Soap")
415
+ # tone = st.radio("Choose Analysis Depth", options=["Simple", "In-depth"], index=0)
416
+
417
+ # if st.button("Search"):
418
+ # with st.spinner("Searching for products..."):
419
+ # products = get_data(product_input)
420
+
421
+ # if not products:
422
+ # st.error("No products found for the given name.")
423
+ # else:
424
+ # product_names = [f"{p['product_name']} (Brand: {p.get('brands', 'Unknown')})" for p in products]
425
+ # selected_product_name = st.radio("Select a Product", product_names, key='product_selection')
426
+
427
+ # selected_product = next(p for p in products if f"{p['product_name']} (Brand: {p.get('brands', 'Unknown')})" == selected_product_name)
428
+
429
+ # st.write(f"### Product Selected: **{selected_product['product_name']}** (Brand: {selected_product.get('brands', 'Unknown')})")
430
+
431
+ # if selected_product:
432
+ # if 'summary' not in st.session_state:
433
+ # st.session_state.summary = None
434
+
435
+ # with st.spinner("Generating AI-powered analysis..."):
436
+ # summary = generate_summary(selected_product, tone.lower())
437
+ # st.session_state.summary = summary
438
+
439
+ # st.write("### Product Analysis Summary:")
440
+ # st.success(st.session_state.summary)
441
+
442
+ # # Footer with hackathon and design details
443
+ # st.markdown("---")
444
+ # st.markdown("""
445
+ # <div style="text-align: center; font-size: 0.9em;">
446
+ # <p><i>ConsumeNice</i> was developed for a hackathon using <b>Streamlit</b> to showcase AI integration with real-world data sources like .</p>
447
+ # <p>Developed by Srish & Sai Teja β€’ Powered by Google Gemini AI</p>
448
+ # </div>
449
+ # """, unsafe_allow_html=True)
450
+
451
+ # if __name__ == "__main__":
452
+ # main()
453
+
454
+ import requests
455
+ import google.generativeai as genai
456
+ import streamlit as st
457
+ from PIL import Image
458
+
459
+ # Configure Gemini API (Use your actual API key)
460
+ genai.configure(api_key='AIzaSyD5yLv8zkGNC7YbxxODLqlMJJKTv8VWdQw')
461
+
462
+ # Function to get data from OpenFoodFacts API
463
+ def get_data(product_name):
464
+ url = "https://world.openfoodfacts.org/cgi/search.pl"
465
+ params = {
466
+ 'search_terms': product_name,
467
+ 'search_simple': 1,
468
+ 'json': 1,
469
+ }
470
+ response = requests.get(url, params=params)
471
+ data = response.json()
472
+ if 'products' not in data or len(data['products']) == 0:
473
+ return [] # Return empty if no products found
474
+
475
+ # Filter products with names and return top 5
476
+ data['products'] = [p for p in data['products'] if 'product_name' in p]
477
+ return data['products'][:5]
478
+
479
+ # Function to generate product analysis using Gemini
480
+ def generate_summary(product, tone):
481
+ name = product.get('product_name', 'Not mentioned')
482
+ brand = product.get('brands', 'Not mentioned')
483
+ nutriscore_grade = product.get('nutriscore_grade', 'Not mentioned')
484
+ eco_score = product.get('ecoscore_grade', 'Not mentioned')
485
+ packaging = product.get('packaging', 'Not mentioned')
486
+ ingredients = product.get('ingredients_text', 'Not mentioned')
487
+ nutrients = product.get('nutriments', 'Not mentioned')
488
+ nova = product.get('nova_groups_tags', 'Not mentioned')
489
+
490
+ # Generate prompt based on tone
491
+ prompt = f"""
492
+ You are an AI assistant analyzing consumer products. Here are the details:
493
+ - Name: {name}
494
+ - Brand: {brand}
495
+ - EcoScore: {eco_score}
496
+ - NutriScore: {nutriscore_grade}
497
+ - NovaScore: {nova}
498
+ - Ingredients: {ingredients}
499
+ - Nutrients: {nutrients}
500
+ - Packaging: {packaging}
501
+
502
+ Please provide a {tone} analysis including:
503
+ 1. Positive aspects of the product.
504
+ 2. Negative aspects of the product.
505
+ 3. Health impact.
506
+ 4. Environmental impact.
507
+ """
508
+
509
+ model = genai.GenerativeModel(model_name="gemini-1.5-flash")
510
+ response = model.generate_content(prompt)
511
+ return response.text
512
+
513
+ # Streamlit interface
514
+ def main():
515
+ # Page setup and header with background image
516
+ st.set_page_config(page_title="ConsumeNice", page_icon="🍽", layout="centered")
517
+
518
+ # Custom CSS for better aesthetics
519
+ st.markdown(
520
+ """
521
+ <style>
522
+ .main {background-color: #000000;}
523
+ .reportview-container .main .block-container {
524
+ padding-top: 2rem;
525
+ padding-right: 2.5rem;
526
+ padding-left: 2.5rem;
527
+ }
528
+ h1, h2, h3, h4, h5 {color: #ffffff;}
529
+ .stButton>button {
530
+ background-color: #6c757d;
531
+ color: white;
532
+ border-radius: 8px;
533
+ }
534
+ .stButton>button:hover {
535
+ background-color: #5a6268;
536
+ }
537
+ .stTextInput>div>input {
538
+ padding: 10px;
539
+ border-radius: 6px;
540
+ border: 1px solid #ced4da;
541
+ background-color: #f8f9fa;
542
+ }
543
+ .stRadio>div>label {color: #495057 !important;}
544
+ .css-1d391kg {color: #495057 !important;}
545
+ .css-145kmo2 {color: #495057 !important;}
546
+ </style>
547
+ """,
548
+ unsafe_allow_html=True
549
+ )
550
+
551
+ # App logo and header side by side
552
+ col1, col2 = st.columns([1, 3]) # Adjust proportions as needed
553
+ with col1:
554
+ st.image(Image.open(r'C:\Projects\google hack\test\static\images\logo.png'), width=120, caption="ConsumeNice - Know What You Consume")
555
+ with col2:
556
+ st.markdown(
557
+ "<h1 style='text-align: left; color: #ffffff;'>🍽️ ConsumeNice - Analyze Products with AI</h1>",
558
+ unsafe_allow_html=True
559
+ )
560
+
561
+ st.write("Welcome to ConsumeNice, where you can search for products and get an AI-generated analysis based on their nutritional, environmental, and packaging details.")
562
+
563
+ # Sidebar for developer profiles and hackathon info
564
+ st.sidebar.markdown(
565
+ """
566
+ <h1 style='color: #0072B2;'>πŸš€ Hackathon Project</h1>
567
+ """,
568
+ unsafe_allow_html=True
569
+ )
570
+ st.sidebar.markdown("Welcome to the ConsumeNice project, developed for the hackathon to showcase AI integration in product analysis.")
571
+
572
+ # Add some icons/emojis to make it look more engaging
573
+ st.sidebar.markdown("### πŸ”§ Project Features")
574
+ # st.sidebar.markdown("- Analyze product details using OpenFoodFacts API.")
575
+ st.sidebar.markdown("- AI-generated analysis using Google Gemini AI.")
576
+ st.sidebar.markdown("- Environment, packaging, and health analysis.")
577
+
578
+ # Developer details with LinkedIn links
579
+ st.sidebar.markdown("### πŸ‘¨β€πŸ’» Developers")
580
+ st.sidebar.markdown("[Srish](https://www.linkedin.com/in/srishrachamalla/) - AI/ML Developer")
581
+ st.sidebar.markdown("[Sai Teja](https://www.linkedin.com/in/saiteja-pallerla-668734225/) - Data Analyst")
582
+
583
+ # Add expander sections for additional content
584
+ with st.sidebar.expander("β„Ή About ConsumeNice"):
585
+ st.write("ConsumeNice is designed to give consumers more insights into the products they consume, analyzing factors like health impact, environmental footprint, and packaging.")
586
+
587
+ with st.sidebar.expander("πŸ“š Useful Resources"):
588
+ st.write("[Google Gemini AI Documentation](https://ai.google.dev/gemini-api/docs)")
589
+ st.write("[Streamlit Documentation](https://docs.streamlit.io/)")
590
+
591
+ # Add progress indicator for hackathon phases or development stages
592
+ st.sidebar.markdown("### ⏳ Hackathon Progress")
593
+ st.sidebar.progress(0.99) # Set progress level (0 to 1)
594
+
595
+ # Sidebar footer with final notes
596
+ st.sidebar.markdown("---")
597
+ st.sidebar.markdown(
598
+ """
599
+ <div style="text-align: center; font-size: 0.85em;">
600
+ Developed by Srish & Sai Teja β€’ Powered by Google Gemini AI
601
+ </div>
602
+ """, unsafe_allow_html=True
603
+ )
604
+
605
+ # User input fields with improved placeholders and hints
606
+ product_input = st.text_input("Enter Product Name", placeholder="e.g., Coca-Cola, Oreo, Dove Soap")
607
+ tone = st.radio("Choose Analysis Depth", options=["Simple", "In-depth"], index=0)
608
+
609
+ # if st.button("Search"):
610
+ # with st.spinner("Searching for products..."):
611
+ # products = get_data(product_input)
612
+
613
+ # if not products:
614
+ # st.error("No products found for the given name.")
615
+ # else:
616
+ # st.write("### Select a Product")
617
+
618
+ # # Loop through each product and display a radio button with product image and name
619
+ # selected_product_name = None
620
+ # for product in products:
621
+ # col1, col2 = st.columns([1, 4]) # Adjust column ratios as needed
622
+
623
+ # # Display the product image if available
624
+ # with col1:
625
+ # image_url = product.get('image_url', None)
626
+ # if image_url:
627
+ # st.image(image_url, width=60)
628
+ # else:
629
+ # st.text("No Image")
630
+
631
+ # # Display the product name with radio buttons
632
+ # with col2:
633
+ # if st.radio("Select", [product['product_name']], key=f"product_{product['product_name']}"):
634
+ # selected_product_name = product['product_name']
635
+
636
+ # # Once a product is selected, find its details
637
+ # if selected_product_name:
638
+ # selected_product = next(p for p in products if p['product_name'] == selected_product_name)
639
+ # st.write(f"### Product Selected: {selected_product['product_name']} (Brand: {selected_product.get('brands', 'Unknown')})")
640
+
641
+ # if selected_product:
642
+ # if 'summary' not in st.session_state:
643
+ # st.session_state.summary = None
644
+
645
+ # with st.spinner("Generating AI-powered analysis..."):
646
+ # summary = generate_summary(selected_product, tone.lower())
647
+ # st.session_state.summary = summary
648
+
649
+ # st.write("### Product Analysis Summary:")
650
+ # st.success(st.session_state.summary)
651
+
652
+ # ##ss
653
+ if st.button("Search"):
654
+ with st.spinner("Searching for products..."):
655
+ products = get_data(product_input)
656
+
657
+ if not products:
658
+ st.error("No products found for the given name.")
659
+ else:
660
+ product_names = [f"{p['product_name']} (Brand: {p.get('brands', 'Unknown')})" for p in products]
661
+ selected_product_name = st.radio("Select a Product", product_names, key='product_selection')
662
+ print(selected_product_name)
663
+
664
+ selected_product = next(p for p in products if f"{p['product_name']} (Brand: {p.get('brands', 'Unknown')})" == selected_product_name)
665
+ print(selected_product)
666
+
667
+ st.write(f"### Product Selected: {selected_product['product_name']} (Brand: {selected_product.get('brands', 'Unknown')})")
668
+
669
+ if selected_product:
670
+ if 'summary' not in st.session_state:
671
+ st.session_state.summary = None
672
+
673
+ with st.spinner("Generating AI-powered analysis..."):
674
+ summary = generate_summary(selected_product, tone.lower())
675
+ st.session_state.summary = summary
676
+
677
+ st.write("### Product Analysis Summary:")
678
+ st.success(st.session_state.summary)
679
+ # if st.button("Search"):
680
+ # with st.spinner("Searching for products..."):
681
+ # products = get_data(product_input)
682
+
683
+ # if not products:
684
+ # st.error("No products found for the given name.")
685
+ # else:
686
+ # # Display the product names in a radio button
687
+ # product_names = [f"{p['product_name']} (Brand: {p.get('brands', 'Unknown')})" for p in products]
688
+
689
+ # # Allow user to select a product but wait for confirmation
690
+ # selected_product_name = st.radio("Select a Product", product_names, key='product_selection')
691
+ # print(selected_product_name)
692
+ # # Add a "Select" button for confirmation
693
+ # if st.button("Confirm Selection"):
694
+ # # Find the selected product from the products list
695
+ # selected_product = next(p for p in products if f"{p['product_name']} (Brand: {p.get('brands', 'Unknown')})" == selected_product_name)
696
+ # print(selected_product)
697
+
698
+ # # Display selected product details
699
+ # st.write(f"### Product Selected: **{selected_product['product_name']}** (Brand: {selected_product.get('brands', 'Unknown')})")
700
+
701
+ # if selected_product:
702
+ # if 'summary' not in st.session_state:
703
+ # st.session_state.summary = None
704
+
705
+ # with st.spinner("Generating AI-powered analysis..."):
706
+ # summary = generate_summary(selected_product, tone.lower())
707
+ # st.session_state.summary = summary
708
+
709
+ # st.write("### Product Analysis Summary:")
710
+ # st.success(st.session_state.summary)
711
+
712
+
713
+ # Footer with hackathon and design details
714
+ st.markdown("---")
715
+ st.markdown("""
716
+ <div style="text-align: center; font-size: 0.9em;">
717
+ <p><i>ConsumeNice</i> was developed for a hackathon using <b>Streamlit</b> to showcase AI integration with real-world data sources.</p>
718
+ <p>Developed by Srish & Sai Teja β€’ Powered by Google Gemini AI</p>
719
+ </div>
720
+ """, unsafe_allow_html=True)
721
+
722
+ if __name__ == "__main__":
723
+ main()
724
+
725
+
726
+