Akshayram1 commited on
Commit
17b8f9b
Β·
verified Β·
1 Parent(s): 3359973

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +145 -78
app.py CHANGED
@@ -4,113 +4,180 @@ 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
- import os
8
- from dotenv import load_dotenv
 
 
 
 
 
9
 
10
- # Load API key
11
  load_dotenv()
 
 
 
12
  API_KEY = os.getenv("GOOGLE_API_KEY")
13
  if API_KEY:
14
  genai.configure(api_key=API_KEY)
15
 
16
- # Streamlit Page Configuration
 
17
  st.set_page_config(
18
  page_title="AI Shopping Partner",
19
  page_icon="πŸ€–πŸ›οΈ",
20
  layout="centered"
21
  )
22
 
23
- # App Title & Subtitle
24
- st.title("πŸ€– AI Shopping Partner")
25
- st.markdown("### Find the Best Products Using AI-Powered Recommendations!")
26
- st.write("Upload an image, describe your preferences, and let AI find the best shopping options for you.")
27
 
28
- # Function to get response from Gemini AI
29
- def get_gemini_response(api_key, prompt, image):
30
- model = genai.GenerativeModel(model_name="gemini-2.0-flash-exp")
31
- response = model.generate_content([prompt, image])
32
  return response.text
33
 
34
- # Function to initialize the AI Agent
35
  def initialize_agent():
36
  return Agent(
 
37
  name="Shopping Partner",
38
  model=Gemini(id="gemini-2.0-flash-exp"),
39
  instructions=[
40
  "You are a product recommender agent specializing in finding products that match user preferences.",
41
- "Prioritize products that match at least 50% of user requirements.",
42
- "Only suggest products from trusted sources like Amazon, Flipkart, Myntra, and Nike.",
43
- "Ensure the product is in stock and available for purchase.",
44
- "Provide key details such as price, brand, and features in a well-structured format.",
45
- "Include hyperlinks to the product whenever possible.",
 
46
  ],
47
  tools=[FirecrawlTools()],
48
  markdown=True
49
- )
50
 
51
- # Initialize AI Agent
52
  multimodal_Agent = initialize_agent()
53
 
54
- # File Uploader for Image
55
- st.markdown("### πŸ“€ Upload an Image")
56
- image_file = st.file_uploader("Choose an image file (JPG, JPEG, PNG)", type=["jpg", "jpeg", "png"])
57
 
58
- if image_file:
 
 
 
 
 
 
 
 
 
 
 
59
  image = Image.open(image_file)
60
- st.image(image, caption="Uploaded Image", use_column_width=False, width=400)
61
-
62
- with st.spinner("πŸ” Analyzing image..."):
63
- prompt = "What is in this photo?"
64
- ai_response = get_gemini_response(API_KEY, prompt, image)
65
- st.success(f"🎯 Product Identified: {ai_response}")
66
-
67
- # User Preferences
68
- st.markdown("### 🎨 Customize Your Preferences")
69
- prompt_color = st.text_input("πŸ”Ή Preferred Color:", placeholder="E.g., Black, Red, White")
70
- prompt_purpose = st.text_input("🎯 Purpose of the Product:", placeholder="E.g., Running, Casual Wear")
71
- prompt_budget = st.text_input("πŸ’° Budget Range:", placeholder="E.g., Under Rs. 5000, $100-$200")
72
-
73
- # User Query
74
- user_query = st.text_area(
75
- "πŸ”Ž Additional Details",
76
- placeholder="Specify any extra requirements for the product (e.g., 'Only from Nike' or 'Must be waterproof')."
77
- )
78
-
79
- if st.button("πŸ” Search for Products"):
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
80
  if not user_query:
81
- st.warning("⚠️ Please enter some preferences or details for the search.")
82
  else:
83
- with st.spinner("πŸ”Ž Searching for best matches..."):
84
- analysis_prompt = f"""
85
- I am looking for {ai_response} with the following preferences:
86
- - Color: {prompt_color}
87
- - Purpose: {prompt_purpose}
88
- - Budget: {prompt_budget}
89
-
90
- Can you provide recommendations? Ensure the response includes product links.
91
- {user_query}
92
- """
93
-
94
- response = multimodal_Agent.run(analysis_prompt, image=image)
95
-
96
- # Display Results
97
- st.subheader("πŸ›οΈ Recommended Products & Links")
98
- st.markdown(response.content)
99
-
100
- # Styling for Better UI
101
- st.markdown(
102
- """
103
- <style>
104
- .stTextArea textarea {
105
- height: 100px;
106
- }
107
- .stButton>button {
108
- background-color: #FF4B4B;
109
- color: white;
110
- font-size: 16px;
111
- border-radius: 10px;
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
112
  }
113
- </style>
114
- """,
115
- unsafe_allow_html=True
116
- )
 
 
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%.",
49
+ "Search for products only from authentic and trusted e-commerce websites such as Google Shopping, Amazon, Flipkart, Myntra, Meesho, Nike, and other reputable platforms.",
50
+ "Verify that each product recommendation is in stock and available for purchase.",
51
+ "Avoid suggesting counterfeit or unverified products.",
52
+ "Clearly mention the key attributes of each product (e.g., price, brand, features) in the response.",
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
+ # 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
+ )