shopping_helper / app.py
Akshayram1's picture
Update app.py
c1ae0da verified
raw
history blame
5.3 kB
import streamlit as st
from PIL import Image
from phi.agent import Agent
from phi.model.google import Gemini
from phi.tools.firecrawl import FirecrawlTools
import google.generativeai as genai
import io
from dotenv import load_dotenv
import os
# Load environment variables
load_dotenv()
# Configure Gemini
API_KEY = os.getenv("GOOGLE_API_KEY")
if API_KEY:
genai.configure(api_key=API_KEY)
# Page Configuration with custom CSS
st.set_page_config(
page_title="AI Shopping Partner",
page_icon="πŸ›οΈ",
layout="wide"
)
# Custom CSS
st.markdown("""
<style>
.main {
padding: 2rem;
}
.stTitle {
color: #1E88E5;
font-family: 'Helvetica Neue', sans-serif;
}
.stHeader {
color: #333;
font-family: 'Helvetica Neue', sans-serif;
}
.upload-section {
background-color: #f8f9fa;
padding: 2rem;
border-radius: 10px;
box-shadow: 0 2px 4px rgba(0,0,0,0.1);
}
.results-section {
background-color: white;
padding: 2rem;
border-radius: 10px;
box-shadow: 0 2px 4px rgba(0,0,0,0.1);
margin-top: 2rem;
}
.stTextInput input {
border-radius: 5px;
}
.stButton button {
border-radius: 5px;
background-color: #1E88E5;
color: white;
font-weight: bold;
}
.stTextArea textarea {
border-radius: 5px;
height: 100px;
}
</style>
""", unsafe_allow_html=True)
# App Header
col1, col2, col3 = st.columns([1,2,1])
with col2:
st.title("πŸ›οΈ AI Shopping Partner")
st.markdown("##### Powered by Agno and Google Gemini")
def get_gemini_response(api_key, prompt, image):
model = genai.GenerativeModel(model_name="gemini-2.0-flash-exp")
response = model.generate_content([prompt, image])
return response.text
def initialize_agent():
return Agent(
name="Shopping Partner",
model=Gemini(id="gemini-2.0-flash-exp"),
instructions=[
"You are a product recommender agent specializing in finding products that match user preferences.",
"Prioritize finding products that satisfy as many user requirements as possible, but ensure a minimum match of 50%.",
"Search for products only from authentic and trusted e-commerce websites.",
"Verify that each product recommendation is in stock and available for purchase.",
"Avoid suggesting counterfeit or unverified products.",
"Format recommendations in a clean, organized manner with clear product attributes.",
],
tools=[FirecrawlTools()],
markdown=True
)
# Initialize the Agent
multimodal_Agent = initialize_agent()
# Main content area
st.markdown('<div class="upload-section">', unsafe_allow_html=True)
# File Upload Section
uploaded_file = st.file_uploader(
"Upload a product image to analyze",
type=["jpg", "jpeg", "png"],
help="Maximum file size: 200MB"
)
if uploaded_file:
image = Image.open(uploaded_file)
# Display image and initial analysis
col1, col2 = st.columns([1, 2])
with col1:
st.image(image, caption="Uploaded Image", use_container_width=True)
with col2:
with st.spinner("Analyzing image..."):
initial_response = get_gemini_response(API_KEY, "What is in this photo?", image)
st.info(f"**Product Identified:** {initial_response}")
# Preference Collection
st.markdown("### Your Preferences")
col1, col2, col3 = st.columns(3)
with col1:
color_pref = st.text_input("🎨 Desired Color", placeholder="e.g., Navy Blue")
with col2:
purpose = st.text_input("🎯 Intended Purpose", placeholder="e.g., Casual wear")
with col3:
budget = st.text_input("πŸ’° Budget Range", placeholder="e.g., Under $100")
specific_requirements = st.text_area(
"✨ Additional Requirements",
placeholder="Any specific features or requirements you're looking for?"
)
if st.button("πŸ” Find Similar Products", type="primary"):
if not specific_requirements:
st.warning("Please specify your requirements to get better recommendations.")
else:
st.markdown('<div class="results-section">', unsafe_allow_html=True)
with st.spinner("Searching for perfect matches..."):
analysis_prompt = f"""
I am looking for {initial_response}
with the following preferences:
Color: {color_pref}
Purpose: {purpose}
Budget: {budget}
Additional Requirements: {specific_requirements}
Please provide recommendations with direct product links.
"""
response = multimodal_Agent.run(analysis_prompt, image=image)
st.markdown("### 🌟 Recommended Products")
st.markdown(response.content)
st.markdown('</div>', unsafe_allow_html=True)
st.markdown('</div>', unsafe_allow_html=True)
# Footer
st.markdown("---")
st.markdown(
"""
<div style='text-align: center; color: #666;'>
<p>Made with ❀️ by AI Shopping Partner</p>
</div>
""",
unsafe_allow_html=True
)