Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
@@ -4,180 +4,113 @@ 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 |
-
|
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
|
49 |
-
"
|
50 |
-
"
|
51 |
-
"
|
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
|
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 |
-
if image_file
|
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 |
-
|
80 |
-
|
81 |
-
|
82 |
-
|
83 |
-
|
84 |
-
|
85 |
-
|
86 |
-
|
87 |
-
|
88 |
-
|
89 |
-
|
90 |
-
|
91 |
-
|
92 |
-
|
93 |
-
|
94 |
-
|
95 |
-
|
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 |
-
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
|
124 |
else:
|
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 |
-
#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 |
-
|
180 |
-
|
181 |
-
|
182 |
-
|
183 |
-
|
|
|
|
|
|
|
|
|
|
|
|
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 |
+
)
|