Spaces:
Sleeping
Sleeping
Update app.py
Browse files
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
|
| 8 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 9 |
|
| 10 |
-
|
| 11 |
load_dotenv()
|
|
|
|
|
|
|
|
|
|
| 12 |
API_KEY = os.getenv("GOOGLE_API_KEY")
|
| 13 |
if API_KEY:
|
| 14 |
genai.configure(api_key=API_KEY)
|
| 15 |
|
| 16 |
-
#
|
|
|
|
| 17 |
st.set_page_config(
|
| 18 |
page_title="AI Shopping Partner",
|
| 19 |
page_icon="π€ποΈ",
|
| 20 |
layout="centered"
|
| 21 |
)
|
| 22 |
|
| 23 |
-
|
| 24 |
-
st.
|
| 25 |
-
st.
|
| 26 |
-
st.write("Upload an image, describe your preferences, and let AI find the best shopping options for you.")
|
| 27 |
|
| 28 |
-
|
| 29 |
-
|
| 30 |
-
|
| 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
|
| 42 |
-
"
|
| 43 |
-
"
|
| 44 |
-
"
|
| 45 |
-
"
|
|
|
|
| 46 |
],
|
| 47 |
tools=[FirecrawlTools()],
|
| 48 |
markdown=True
|
| 49 |
-
|
| 50 |
|
| 51 |
-
#
|
| 52 |
multimodal_Agent = initialize_agent()
|
| 53 |
|
| 54 |
-
#
|
| 55 |
-
|
| 56 |
-
image_file = st.file_uploader("Choose an image file (JPG, JPEG, PNG)", type=["jpg", "jpeg", "png"])
|
| 57 |
|
| 58 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 59 |
image = Image.open(image_file)
|
| 60 |
-
|
| 61 |
-
|
| 62 |
-
|
| 63 |
-
|
| 64 |
-
|
| 65 |
-
|
| 66 |
-
|
| 67 |
-
|
| 68 |
-
|
| 69 |
-
|
| 70 |
-
|
| 71 |
-
|
| 72 |
-
|
| 73 |
-
|
| 74 |
-
|
| 75 |
-
|
| 76 |
-
|
| 77 |
-
|
| 78 |
-
|
| 79 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 80 |
if not user_query:
|
| 81 |
-
st.warning("
|
| 82 |
else:
|
| 83 |
-
|
| 84 |
-
|
| 85 |
-
|
| 86 |
-
|
| 87 |
-
|
| 88 |
-
|
| 89 |
-
|
| 90 |
-
|
| 91 |
-
|
| 92 |
-
|
| 93 |
-
|
| 94 |
-
|
| 95 |
-
|
| 96 |
-
|
| 97 |
-
|
| 98 |
-
|
| 99 |
-
|
| 100 |
-
|
| 101 |
-
|
| 102 |
-
|
| 103 |
-
|
| 104 |
-
|
| 105 |
-
|
| 106 |
-
|
| 107 |
-
|
| 108 |
-
|
| 109 |
-
|
| 110 |
-
|
| 111 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 112 |
}
|
| 113 |
-
|
| 114 |
-
|
| 115 |
-
|
| 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 |
+
)
|