Spaces:
Sleeping
Sleeping
File size: 8,406 Bytes
88675f0 b9850d6 1439af7 88675f0 4cdcced 88675f0 f27b95d 88675f0 4cdcced 88675f0 4cdcced 88675f0 4cdcced 88675f0 4cdcced 88675f0 4cdcced 529d23c 4cdcced 88675f0 de7c179 88675f0 de7c179 529d23c 4cdcced 88675f0 529d23c |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 |
import requests
import google.generativeai as genai
import streamlit as st
from PIL import Image
# Configure Gemini API (Use your actual API key)
# genai.configure(api_key='AIzaSyD5yLv8zkGNC7YbxxODLqlMJJKTv8VWdQw')
genai.configure(api_key='AIzaSyA2KzkhAYsBCPYfvgmEuE1DFGS1GuznW4Q')
# Function to get data from OpenFoodFacts API
def get_data(product_name):
url = "https://world.openfoodfacts.org/cgi/search.pl"
params = {
'search_terms': product_name,
'search_simple': 1,
'json': 1,
}
response = requests.get(url, params=params)
data = response.json()
if 'products' not in data or len(data['products']) == 0:
return [] # Return empty if no products found
# Filter products with names and return top 5
data['products'] = [p for p in data['products'] if 'product_name' in p]
return data['products'][:1]
# Function to generate product analysis using Gemini
def generate_summary(product, tone):
name = product.get('product_name', 'Not mentioned')
brand = product.get('brands', 'Not mentioned')
nutriscore_grade = product.get('nutriscore_grade', 'Not mentioned')
eco_score = product.get('ecoscore_grade', 'Not mentioned')
packaging = product.get('packaging', 'Not mentioned')
ingredients = product.get('ingredients_text', 'Not mentioned')
nutrients = product.get('nutriments', 'Not mentioned')
nova = product.get('nova_groups_tags', 'Not mentioned')
# Generate prompt based on tone
prompt = f"""
You are an AI assistant analyzing consumer products. Here are the details:
- Name: {name}
- Brand: {brand}
- EcoScore: {eco_score}
- NutriScore: {nutriscore_grade}
- NovaScore: {nova}
- Ingredients: {ingredients}
- Nutrients: {nutrients}
- Packaging: {packaging}
Please provide a {tone} analysis including:
1. Positive aspects of the product.
2. Negative aspects of the product.
3. Health impact.
4. Environmental impact.
"""
model = genai.GenerativeModel(model_name="gemini-1.5-flash")
response = model.generate_content(prompt)
return response.text
# Streamlit interface
def main():
# Page setup and header with background image
st.set_page_config(page_title="ConsumeNice", page_icon="π½", layout="centered")
# Custom CSS for better aesthetics
st.markdown(
"""
<style>
.main {background-color: #000000;}
.reportview-container .main .block-container {
padding-top: 2rem;
padding-right: 2.5rem;
padding-left: 2.5rem;
}
h1, h2, h3, h4, h5 {color: #ffffff;}
.stButton>button {
background-color: #6c757d;
color: white;
border-radius: 8px;
}
.stButton>button:hover {
background-color: #5a6268;
}
.stTextInput>div>input {
padding: 10px;
border-radius: 6px;
border: 1px solid #ced4da;
background-color: #f8f9fa;
}
.stRadio>div>label {color: #495057 !important;}
.css-1d391kg {color: #495057 !important;}
.css-145kmo2 {color: #495057 !important;}
</style>
""",
unsafe_allow_html=True
)
# App logo and header side by side
col1, col2 = st.columns([1, 3]) # Adjust proportions as needed
with col1:
st.image(Image.open('logo.png'), width=120, caption="ConsumeNice - Know What You Consume")
with col2:
st.markdown(
"<h1 style='text-align: left; color: #ffffff;'>π½οΈ ConsumeNice - Analyze Products with AI</h1>",
unsafe_allow_html=True
)
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.")
# Sidebar for developer profiles and hackathon info
st.sidebar.markdown(
"""
<h1 style='color: #0072B2;'>π Hackathon Project</h1>
""",
unsafe_allow_html=True
)
st.sidebar.markdown("Welcome to the ConsumeNice project, developed for the hackathon to showcase AI integration in product analysis.")
# Add some icons/emojis to make it look more engaging
st.sidebar.markdown("### π§ Project Features")
# st.sidebar.markdown("- Analyze product details using OpenFoodFacts API.")
st.sidebar.markdown("- AI-generated analysis using Google Gemini AI.")
st.sidebar.markdown("- Environment, packaging, and health analysis.")
# Developer details with LinkedIn links
st.sidebar.markdown("### π¨βπ» Developers")
st.sidebar.markdown("[Srish](https://www.linkedin.com/in/srishrachamalla/) - AI/ML Developer")
st.sidebar.markdown("[Sai Teja](https://www.linkedin.com/in/saiteja-pallerla-668734225/) - Data Analyst")
# Add expander sections for additional content
with st.sidebar.expander("βΉ About ConsumeNice"):
st.write("ConsumeNice is designed to give consumers more insights into the products they consume, analyzing factors like health impact, environmental footprint, and packaging.")
with st.sidebar.expander("π Useful Resources"):
st.write("[Google Gemini AI Documentation](https://ai.google.dev/gemini-api/docs)")
st.write("[Streamlit Documentation](https://docs.streamlit.io/)")
# Add progress indicator for hackathon phases or development stages
st.sidebar.markdown("### β³ Hackathon Progress")
st.sidebar.progress(0.99) # Set progress level (0 to 1)
# Sidebar footer with final notes
st.sidebar.markdown("---")
st.sidebar.markdown(
"""
<div style="text-align: center; font-size: 0.85em;">
Developed by Srish & Sai Teja β’ Powered by Google Gemini AI
</div>
""", unsafe_allow_html=True
)
# User input fields with improved placeholders and hints
product_input = st.text_input("Enter Product Name", placeholder="e.g., Coca-Cola, Oreo, Dove Soap")
tone = st.radio("Choose Analysis Depth", options=["Simple", "In-depth"], index=0)
# ##ss
if st.button("Search"):
with st.spinner("Searching for products..."):
products = get_data(product_input)
if not products:
st.error("No products found for the given name.")
else:
# product_names = [f"{p['product_name']} (Brand: {p.get('brands', 'Unknown')})" for p in products]
# selected_product_name = st.radio("Select a Product", product_names, key='product_selection')
# print(selected_product_name)
# selected_product = next(p for p in products if f"{p['product_name']} (Brand: {p.get('brands', 'Unknown')})" == selected_product_name)
# print(selected_product)
# st.write(f"### Product Selected: {selected_product['product_name']} (Brand: {selected_product.get('brands', 'Unknown')})")
# if selected_product:
# if 'summary' not in st.session_state:
# st.session_state.summary = None
# with st.spinner("Generating AI-powered analysis..."):
# summary = generate_summary(selected_product, tone.lower())
# st.session_state.summary = summary
# st.write("### Product Analysis Summary:")
# st.success(st.session_state.summary)
product_names = [f"{p['product_name']} (Brand: {p.get('brands', 'Unknown')})" for p in products]
selected_product = products[0]
st.write(f"### Product Selected: {product_names[0]}")
with st.spinner("Generating AI-powered analysis..."):
summary = generate_summary(selected_product, tone.lower())
st.session_state.summary = summary
st.write("### Product Analysis Summary:")
st.success(st.session_state.summary)
# Footer with hackathon and design details
st.markdown("---")
st.markdown("""
<div style="text-align: center; font-size: 0.9em;">
<p><i>ConsumeNice</i> was developed for a hackathon using <b>Streamlit</b> to showcase AI integration with real-world data sources.</p>
<p>Developed by Srish & Sai Teja β’ Powered by Google Gemini AI</p>
</div>
""", unsafe_allow_html=True)
if __name__ == "__main__":
main() |