Akshayram1 commited on
Commit
bff26c3
Β·
verified Β·
1 Parent(s): 3061ba6

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +175 -133
app.py CHANGED
@@ -4,45 +4,118 @@ 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="centered"
30
  )
31
 
32
- st.title("AI Shopping Partner")
33
- st.header("Powered by Agno and Google Gemini")
34
- #st.cache_resource
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
35
 
36
- def get_gemini_response(api_key,prompt,image):
37
- model=genai.GenerativeModel(model_name="gemini-2.0-flash-exp")
38
- response= model.generate_content([prompt,image])
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%.",
@@ -54,130 +127,99 @@ def initialize_agent():
54
  ],
55
  tools=[FirecrawlTools()],
56
  markdown=True
57
- )
58
-
59
- #Initialize the Agent
60
- multimodal_Agent = initialize_agent()
61
-
62
- # Define acceptable file types and MIME types
63
- accepted_mime_types = ["image/jpeg", "image/png"]
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
  if image_file is not None:
75
- # Convert the uploaded file into a BytesIO stream
76
- #image_stream = io.BytesIO(image_file.read())
77
  image = Image.open(image_file)
78
-
79
- try:
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
- user_query= st.text_area("What specific insights are you looking for from the image?",
117
- placeholder="Ask any questions related to the image content. The AI agent will analyze and gather more context if necessary",
118
- help="Share the specific questions or details you want to explore from the image."
119
- )
120
-
121
- if st.button("Search this Product",key="analyse_image_button"):
122
- if not user_query:
123
- st.warning("Please enter a query to analyse this image")
124
- else:
125
  try:
126
- with st.spinner("AI is Processing this image and gathering insights..."):
127
-
128
- #Upload and process the video file
129
- #processed_image = upload_file(image_path)
130
- #st.write(f"processed_image: {processed_image}")
131
-
132
- response= get_gemini_response(API_KEY,prompt,image)
133
- #st.write(f"Product Identified: {response}")
134
-
135
-
136
-
137
- #Prompt generation for Analysis
138
- analysis_prompt =(
139
- f"""
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
- #st.write(response)
162
-
163
  except Exception as error:
164
- st.error(f"An error occured during analysis:{error}")
165
- finally:
166
- #Clean up temporary video file
167
- # Path(image_path).unlink(missing_ok=True)
168
- st.info("Clean up temporary image file")
169
- #else:
170
- #st.info("Upload a image file to start the Analysis")
171
-
172
- #Customize text area height
173
- st.markdown(
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
  import base64
 
9
  import time
10
  from pathlib import Path
11
  import tempfile
 
12
  from dotenv import load_dotenv
 
 
13
  import os
14
 
15
+ # Load environment variables
16
+ load_dotenv()
17
+
18
+ # Configure Google API
19
  API_KEY = os.getenv("GOOGLE_API_KEY")
20
  if API_KEY:
21
  genai.configure(api_key=API_KEY)
22
 
23
  # Page Configuration
 
24
  st.set_page_config(
25
  page_title="AI Shopping Partner",
26
  page_icon="πŸ€–πŸ›οΈ",
27
  layout="centered"
28
  )
29
 
30
+ # Custom CSS for styling
31
+ st.markdown("""
32
+ <style>
33
+ /* Main container styling */
34
+ .main {
35
+ background-color: #f9fafb;
36
+ padding: 2rem;
37
+ }
38
+
39
+ /* Title styling */
40
+ .title-text {
41
+ color: #2d3748;
42
+ font-size: 2.5rem !important;
43
+ font-weight: 700;
44
+ text-align: center;
45
+ margin-bottom: 1rem;
46
+ }
47
+
48
+ /* Header styling */
49
+ .header-text {
50
+ color: #4a5568 !important;
51
+ font-size: 1.2rem !important;
52
+ text-align: center;
53
+ margin-bottom: 2rem;
54
+ }
55
+
56
+ /* Upload section styling */
57
+ .upload-section {
58
+ background: white;
59
+ border-radius: 12px;
60
+ padding: 2rem;
61
+ box-shadow: 0 4px 6px -1px rgba(0, 0, 0, 0.1);
62
+ margin-bottom: 2rem;
63
+ }
64
+
65
+ /* Input field styling */
66
+ .stTextInput input, .stTextArea textarea {
67
+ border: 2px solid #e2e8f0 !important;
68
+ border-radius: 8px !important;
69
+ padding: 0.75rem !important;
70
+ }
71
+
72
+ /* Button styling */
73
+ .stButton button {
74
+ background: #4299e1 !important;
75
+ color: white !important;
76
+ border: none;
77
+ padding: 12px 24px;
78
+ border-radius: 8px;
79
+ font-weight: 600;
80
+ transition: all 0.3s;
81
+ width: 100%;
82
+ }
83
+
84
+ .stButton button:hover {
85
+ background: #3182ce !important;
86
+ transform: translateY(-1px);
87
+ box-shadow: 0 4px 6px -1px rgba(66, 153, 225, 0.3);
88
+ }
89
+
90
+ /* Result section styling */
91
+ .result-section {
92
+ background: white;
93
+ border-radius: 12px;
94
+ padding: 2rem;
95
+ margin-top: 2rem;
96
+ box-shadow: 0 4px 6px -1px rgba(0, 0, 0, 0.1);
97
+ }
98
+
99
+ /* Product card styling */
100
+ .product-card {
101
+ border-left: 4px solid #4299e1;
102
+ padding: 1rem;
103
+ margin: 1rem 0;
104
+ background: #f8fafc;
105
+ border-radius: 8px;
106
+ }
107
+ </style>
108
+ """, unsafe_allow_html=True)
109
 
110
+ # Page Content
111
+ st.markdown('<p class="title-text">πŸ€– AI Shopping Partner πŸ›οΈ</p>', unsafe_allow_html=True)
112
+ st.markdown('<p class="header-text">Discover perfect products with AI-powered recommendations</p>', unsafe_allow_html=True)
 
113
 
114
+ # Initialize the Agent
115
  def initialize_agent():
116
  return Agent(
 
117
  name="Shopping Partner",
118
+ model=Gemini(id="gemini-2.0-flash-exp"),
119
  instructions=[
120
  "You are a product recommender agent specializing in finding products that match user preferences.",
121
  "Prioritize finding products that satisfy as many user requirements as possible, but ensure a minimum match of 50%.",
 
127
  ],
128
  tools=[FirecrawlTools()],
129
  markdown=True
130
+ )
 
 
 
 
 
 
 
 
 
 
131
 
132
+ # Initialize the Agent
133
+ multimodal_agent = initialize_agent()
134
 
135
+ # Function to get Gemini response
136
+ def get_gemini_response(api_key, prompt, image):
137
+ model = genai.GenerativeModel(model_name="gemini-2.0-flash-exp")
138
+ response = model.generate_content([prompt, image])
139
+ return response.text
140
 
141
+ # Image Upload Section
142
+ with st.container():
143
+ st.markdown("""
144
+ <div class="upload-section">
145
+ <h3 style="color: #2d3748; margin-bottom: 1rem;">πŸ“Έ Upload Product Image</h3>
146
+ """, unsafe_allow_html=True)
147
+
148
+ image_file = st.file_uploader(
149
+ "Drag and drop or click to upload (JPEG, PNG)",
150
+ type=["jpg", "jpeg", "png"],
151
+ help="Max file size: 20MB",
152
+ label_visibility="collapsed"
153
+ )
154
+
155
+ st.markdown("</div>", unsafe_allow_html=True)
156
 
157
+ # Display Image
158
  if image_file is not None:
 
 
159
  image = Image.open(image_file)
160
+ st.image(image, caption="Uploaded Image", use_column_width=True)
161
+
162
+ # Preferences Section
163
+ with st.container():
164
+ st.markdown("### 🎨 Product Preferences")
165
+ col1, col2, col3 = st.columns(3)
166
+ with col1:
167
+ prompt_color = st.text_input("Preferred Color", placeholder="e.g., Navy Blue")
168
+ with col2:
169
+ prompt_purpose = st.text_input("Intended Use", placeholder="e.g., Running")
170
+ with col3:
171
+ prompt_budget = st.text_input("Budget Range", placeholder="e.g., β‚Ή5,000-10,000")
172
+
173
+ # Search Query Section
174
+ user_query = st.text_area(
175
+ "πŸ” Tell us more about what you're looking for:",
176
+ placeholder="Example: 'Looking for durable running shoes with good arch support'",
177
+ height=100
178
+ )
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
179
 
180
+ # Search Button
181
+ if st.button("πŸš€ Find Perfect Products"):
182
+ if not user_query:
183
+ st.warning("Please describe what you're looking for")
184
+ else:
185
+ with st.spinner("🧠 AI is analyzing your preferences..."):
 
 
 
186
  try:
187
+ # Step 1: Identify the product in the image
188
+ prompt = "What is in this photo?"
189
+ product_description = get_gemini_response(API_KEY, prompt, image)
190
+
191
+ # Step 2: Generate analysis prompt
192
+ analysis_prompt = f"""
193
+ I am looking for {product_description}
194
+ with the below preferences:
195
+ - Color: {prompt_color}
196
+ - Purpose: {prompt_purpose}
197
+ - Budget: {prompt_budget}
198
+ Can you provide recommendations? Always make sure that you provide hyperlinks to the product.
199
+ Additional details: {user_query}
200
+ """
201
+
202
+ # Step 3: Get recommendations from the agent
203
+ response = multimodal_agent.run(analysis_prompt, image=image)
204
+
205
+ # Display Results
206
+ st.markdown("""
207
+ <div class="result-section">
208
+ <h3 style="color: #2d3748; margin-bottom: 1rem;">πŸŽ‰ Recommended Products</h3>
209
+ """, unsafe_allow_html=True)
210
+
211
+ # Display the response content
212
+ st.markdown(response.content, unsafe_allow_html=True)
213
+
214
+ st.markdown("</div>", unsafe_allow_html=True)
215
+
 
 
 
 
 
 
 
 
216
  except Exception as error:
217
+ st.error(f"⚠️ An error occurred: {str(error)}")
218
+
219
+ # Footer
220
+ st.markdown("""
221
+ <div style="text-align: center; color: #718096; margin-top: 3rem;">
222
+ <hr style="margin-bottom: 1rem;">
223
+ <p>Powered by Gemini AI β€’ Secure Processing β€’ Always Learning</p>
224
+ </div>
225
+ """, unsafe_allow_html=True)