Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
@@ -4,180 +4,166 @@ from phi.agent import Agent
|
|
4 |
from phi.model.google import Gemini
|
5 |
from phi.tools.firecrawl import FirecrawlTools
|
6 |
import google.generativeai as genai
|
7 |
-
from google.generativeai import upload_file,get_file
|
8 |
import io
|
9 |
-
import base64
|
10 |
-
|
11 |
-
import time
|
12 |
-
from pathlib import Path
|
13 |
-
import tempfile
|
14 |
-
|
15 |
from dotenv import load_dotenv
|
16 |
-
load_dotenv()
|
17 |
-
|
18 |
import os
|
19 |
|
|
|
|
|
|
|
|
|
20 |
API_KEY = os.getenv("GOOGLE_API_KEY")
|
21 |
if API_KEY:
|
22 |
genai.configure(api_key=API_KEY)
|
23 |
|
24 |
-
# Page Configuration
|
25 |
-
|
26 |
st.set_page_config(
|
27 |
page_title="AI Shopping Partner",
|
28 |
-
page_icon="
|
29 |
-
layout="
|
30 |
)
|
31 |
|
32 |
-
|
33 |
-
st.
|
34 |
-
|
35 |
-
|
36 |
-
|
37 |
-
|
38 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
39 |
return response.text
|
40 |
|
41 |
def initialize_agent():
|
42 |
return Agent(
|
43 |
-
|
44 |
name="Shopping Partner",
|
45 |
model=Gemini(id="gemini-2.0-flash-exp"),
|
46 |
instructions=[
|
47 |
"You are a product recommender agent specializing in finding products that match user preferences.",
|
48 |
"Prioritize finding products that satisfy as many user requirements as possible, but ensure a minimum match of 50%.",
|
49 |
-
"Search for products only from authentic and trusted e-commerce websites
|
50 |
"Verify that each product recommendation is in stock and available for purchase.",
|
51 |
"Avoid suggesting counterfeit or unverified products.",
|
52 |
-
"
|
53 |
-
"Format the recommendations neatly and ensure clarity for ease of user understanding.",
|
54 |
],
|
55 |
tools=[FirecrawlTools()],
|
56 |
markdown=True
|
57 |
-
|
58 |
|
59 |
-
#Initialize the Agent
|
60 |
multimodal_Agent = initialize_agent()
|
61 |
|
62 |
-
#
|
63 |
-
|
64 |
-
|
65 |
-
#File Uploader
|
66 |
-
image_file = st.file_uploader("Upload a image File to Analyse and provide relevant shopping links",type=["jpg","jpeg","png"],help="Upload max 200mb image for AI Analysis")
|
67 |
-
image= None
|
68 |
-
|
69 |
-
#Prompt
|
70 |
-
prompt= "What is in this photo?"
|
71 |
-
|
72 |
-
|
73 |
|
74 |
-
|
75 |
-
|
76 |
-
|
77 |
-
|
78 |
-
|
79 |
-
|
80 |
-
# Open the image using PIL
|
81 |
-
#image = Image.open(image_stream)
|
82 |
-
|
83 |
-
# Display the image in Streamlit
|
84 |
-
st.image(image, caption="Uploaded Image", use_container_width=False,width=400)
|
85 |
-
with st.spinner("AI is processing this image and gathering insights..."):
|
86 |
-
response= get_gemini_response(API_KEY,prompt,image)
|
87 |
-
st.write(f"Product Identified using AI: {response}")
|
88 |
-
|
89 |
-
except Exception as e:
|
90 |
-
st.error(f"Error: Unable to open image. {e}")
|
91 |
-
|
92 |
-
# Specify the mime_type if Streamlit cannot auto-detect
|
93 |
-
#mime_type = image_file.type
|
94 |
-
|
95 |
-
#if mime_type:
|
96 |
-
#st.write(f"File MIME type detected: {mime_type}")
|
97 |
-
# Proceed with file processing
|
98 |
-
#else:
|
99 |
-
#st.error("Could not determine MIME type for the uploaded file. Please upload a valid file.")
|
100 |
-
|
101 |
-
#if mime_type in accepted_mime_types:
|
102 |
-
#st.write(f"File uploaded: {image_file.name}")
|
103 |
-
# Process the file as needed
|
104 |
-
#else:
|
105 |
-
#st.error(f"Unsupported file type: {mime_type}")
|
106 |
-
|
107 |
-
|
108 |
-
|
109 |
-
#prompt= st.text_input("Input prompt(e.g., 'What is in this photo?'):",key="input")
|
110 |
-
promptColor= st.text_input("'What Color you are looking for?'",key="inputcolor")
|
111 |
-
promptPurpose= st.text_input("'For what purpose you are looking for this product?'",key="inputpurpose")
|
112 |
-
promptBudget= st.text_input("'What is your budget?'",key="inputbudget")
|
113 |
-
|
114 |
-
|
115 |
|
116 |
-
|
117 |
-
|
118 |
-
help="Share the specific questions or details you want to explore from the image."
|
119 |
-
)
|
120 |
|
121 |
-
|
122 |
-
|
123 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
124 |
else:
|
125 |
-
|
126 |
-
|
127 |
-
|
128 |
-
|
129 |
-
|
130 |
-
|
131 |
-
|
132 |
-
|
133 |
-
|
134 |
-
|
135 |
-
|
136 |
-
|
137 |
-
|
138 |
-
|
139 |
-
|
140 |
-
I am looking for
|
141 |
-
{response}
|
142 |
-
with the below preferences:
|
143 |
-
{promptColor}
|
144 |
-
{promptPurpose}
|
145 |
-
{promptBudget}
|
146 |
-
Can you provide recommendations. Always make sure that you provide hyperlinks to the product.
|
147 |
-
{user_query}
|
148 |
-
"""
|
149 |
-
)
|
150 |
-
|
151 |
-
#AI Agent Processing
|
152 |
-
response = multimodal_Agent.run(analysis_prompt,image=image)
|
153 |
-
|
154 |
-
# multimodal_Agent.print_response(
|
155 |
-
# "I am looking for running shoes with the following preferences: Color: Black Purpose: Comfortable for long-distance running Budget: Under Rs. 10,000. Can you provide recommendations. Also provide links to the product. Search in Myntra"
|
156 |
-
# )
|
157 |
-
|
158 |
-
#Display the result
|
159 |
-
st.subheader("Relevant search links for the product")
|
160 |
st.markdown(response.content)
|
161 |
-
|
162 |
-
|
163 |
-
|
164 |
-
|
165 |
-
|
166 |
-
|
167 |
-
|
168 |
-
|
169 |
-
#
|
170 |
-
|
171 |
-
|
172 |
-
|
173 |
-
|
174 |
-
|
175 |
-
<style>
|
176 |
-
.stTextArea textarea{
|
177 |
-
height:100px;
|
178 |
-
}
|
179 |
-
</style>
|
180 |
-
""",
|
181 |
-
unsafe_allow_html=True
|
182 |
-
|
183 |
-
)
|
|
|
4 |
from phi.model.google import Gemini
|
5 |
from phi.tools.firecrawl import FirecrawlTools
|
6 |
import google.generativeai as genai
|
|
|
7 |
import io
|
|
|
|
|
|
|
|
|
|
|
|
|
8 |
from dotenv import load_dotenv
|
|
|
|
|
9 |
import os
|
10 |
|
11 |
+
# Load environment variables
|
12 |
+
load_dotenv()
|
13 |
+
|
14 |
+
# Configure Gemini
|
15 |
API_KEY = os.getenv("GOOGLE_API_KEY")
|
16 |
if API_KEY:
|
17 |
genai.configure(api_key=API_KEY)
|
18 |
|
19 |
+
# Page Configuration with custom CSS
|
|
|
20 |
st.set_page_config(
|
21 |
page_title="AI Shopping Partner",
|
22 |
+
page_icon="ποΈ",
|
23 |
+
layout="wide"
|
24 |
)
|
25 |
|
26 |
+
# Custom CSS
|
27 |
+
st.markdown("""
|
28 |
+
<style>
|
29 |
+
.main {
|
30 |
+
padding: 2rem;
|
31 |
+
}
|
32 |
+
.stTitle {
|
33 |
+
color: #1E88E5;
|
34 |
+
font-family: 'Helvetica Neue', sans-serif;
|
35 |
+
}
|
36 |
+
.stHeader {
|
37 |
+
color: #333;
|
38 |
+
font-family: 'Helvetica Neue', sans-serif;
|
39 |
+
}
|
40 |
+
.upload-section {
|
41 |
+
background-color: #f8f9fa;
|
42 |
+
padding: 2rem;
|
43 |
+
border-radius: 10px;
|
44 |
+
box-shadow: 0 2px 4px rgba(0,0,0,0.1);
|
45 |
+
}
|
46 |
+
.results-section {
|
47 |
+
background-color: white;
|
48 |
+
padding: 2rem;
|
49 |
+
border-radius: 10px;
|
50 |
+
box-shadow: 0 2px 4px rgba(0,0,0,0.1);
|
51 |
+
margin-top: 2rem;
|
52 |
+
}
|
53 |
+
.stTextInput input {
|
54 |
+
border-radius: 5px;
|
55 |
+
}
|
56 |
+
.stButton button {
|
57 |
+
border-radius: 5px;
|
58 |
+
background-color: #1E88E5;
|
59 |
+
color: white;
|
60 |
+
font-weight: bold;
|
61 |
+
}
|
62 |
+
.stTextArea textarea {
|
63 |
+
border-radius: 5px;
|
64 |
+
height: 100px;
|
65 |
+
}
|
66 |
+
</style>
|
67 |
+
""", unsafe_allow_html=True)
|
68 |
+
|
69 |
+
# App Header
|
70 |
+
col1, col2, col3 = st.columns([1,2,1])
|
71 |
+
with col2:
|
72 |
+
st.title("ποΈ AI Shopping Partner")
|
73 |
+
st.markdown("##### Powered by Agno and Google Gemini")
|
74 |
+
|
75 |
+
def get_gemini_response(api_key, prompt, image):
|
76 |
+
model = genai.GenerativeModel(model_name="gemini-2.0-flash-exp")
|
77 |
+
response = model.generate_content([prompt, image])
|
78 |
return response.text
|
79 |
|
80 |
def initialize_agent():
|
81 |
return Agent(
|
|
|
82 |
name="Shopping Partner",
|
83 |
model=Gemini(id="gemini-2.0-flash-exp"),
|
84 |
instructions=[
|
85 |
"You are a product recommender agent specializing in finding products that match user preferences.",
|
86 |
"Prioritize finding products that satisfy as many user requirements as possible, but ensure a minimum match of 50%.",
|
87 |
+
"Search for products only from authentic and trusted e-commerce websites.",
|
88 |
"Verify that each product recommendation is in stock and available for purchase.",
|
89 |
"Avoid suggesting counterfeit or unverified products.",
|
90 |
+
"Format recommendations in a clean, organized manner with clear product attributes.",
|
|
|
91 |
],
|
92 |
tools=[FirecrawlTools()],
|
93 |
markdown=True
|
94 |
+
)
|
95 |
|
96 |
+
# Initialize the Agent
|
97 |
multimodal_Agent = initialize_agent()
|
98 |
|
99 |
+
# Main content area
|
100 |
+
st.markdown('<div class="upload-section">', unsafe_allow_html=True)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
101 |
|
102 |
+
# File Upload Section
|
103 |
+
uploaded_file = st.file_uploader(
|
104 |
+
"Upload a product image to analyze",
|
105 |
+
type=["jpg", "jpeg", "png"],
|
106 |
+
help="Maximum file size: 200MB"
|
107 |
+
)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
108 |
|
109 |
+
if uploaded_file:
|
110 |
+
image = Image.open(uploaded_file)
|
|
|
|
|
111 |
|
112 |
+
# Display image and initial analysis
|
113 |
+
col1, col2 = st.columns([1, 2])
|
114 |
+
with col1:
|
115 |
+
st.image(image, caption="Uploaded Image", use_container_width=True)
|
116 |
+
with col2:
|
117 |
+
with st.spinner("Analyzing image..."):
|
118 |
+
initial_response = get_gemini_response(API_KEY, "What is in this photo?", image)
|
119 |
+
st.info(f"**Product Identified:** {initial_response}")
|
120 |
+
|
121 |
+
# Preference Collection
|
122 |
+
st.markdown("### Your Preferences")
|
123 |
+
col1, col2, col3 = st.columns(3)
|
124 |
+
with col1:
|
125 |
+
color_pref = st.text_input("π¨ Desired Color", placeholder="e.g., Navy Blue")
|
126 |
+
with col2:
|
127 |
+
purpose = st.text_input("π― Intended Purpose", placeholder="e.g., Casual wear")
|
128 |
+
with col3:
|
129 |
+
budget = st.text_input("π° Budget Range", placeholder="e.g., Under $100")
|
130 |
+
|
131 |
+
specific_requirements = st.text_area(
|
132 |
+
"β¨ Additional Requirements",
|
133 |
+
placeholder="Any specific features or requirements you're looking for?"
|
134 |
+
)
|
135 |
+
|
136 |
+
if st.button("π Find Similar Products", type="primary"):
|
137 |
+
if not specific_requirements:
|
138 |
+
st.warning("Please specify your requirements to get better recommendations.")
|
139 |
else:
|
140 |
+
st.markdown('<div class="results-section">', unsafe_allow_html=True)
|
141 |
+
with st.spinner("Searching for perfect matches..."):
|
142 |
+
analysis_prompt = f"""
|
143 |
+
I am looking for {initial_response}
|
144 |
+
with the following preferences:
|
145 |
+
Color: {color_pref}
|
146 |
+
Purpose: {purpose}
|
147 |
+
Budget: {budget}
|
148 |
+
Additional Requirements: {specific_requirements}
|
149 |
+
Please provide recommendations with direct product links.
|
150 |
+
"""
|
151 |
+
|
152 |
+
response = multimodal_Agent.run(analysis_prompt, image=image)
|
153 |
+
|
154 |
+
st.markdown("### π Recommended Products")
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
155 |
st.markdown(response.content)
|
156 |
+
st.markdown('</div>', unsafe_allow_html=True)
|
157 |
+
|
158 |
+
st.markdown('</div>', unsafe_allow_html=True)
|
159 |
+
|
160 |
+
# Footer
|
161 |
+
st.markdown("---")
|
162 |
+
st.markdown(
|
163 |
+
"""
|
164 |
+
<div style='text-align: center; color: #666;'>
|
165 |
+
<p>Made with β€οΈ by AI Shopping Partner</p>
|
166 |
+
</div>
|
167 |
+
""",
|
168 |
+
unsafe_allow_html=True
|
169 |
+
)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|