Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
|
@@ -1,6 +1,5 @@
|
|
| 1 |
import streamlit as st
|
| 2 |
import pandas as pd
|
| 3 |
-
from langchain.tools import Tool
|
| 4 |
from langchain.memory import ConversationBufferMemory
|
| 5 |
from langchain_google_genai import ChatGoogleGenerativeAI
|
| 6 |
from langchain.agents import AgentExecutor, create_openai_functions_agent
|
|
@@ -9,18 +8,21 @@ from PIL import Image
|
|
| 9 |
import PyPDF2
|
| 10 |
import os
|
| 11 |
import json
|
| 12 |
-
from dotenv import load_dotenv
|
| 13 |
from langchain.chains import LLMChain
|
| 14 |
from langchain.prompts import PromptTemplate
|
|
|
|
|
|
|
|
|
|
|
|
|
| 15 |
|
| 16 |
# Load environment variables
|
| 17 |
load_dotenv()
|
| 18 |
|
|
|
|
| 19 |
genai.configure(api_key=os.getenv('GOOGLE_API_KEY'))
|
| 20 |
|
| 21 |
# Initialize Gemini models
|
| 22 |
llm_flash_exp = ChatGoogleGenerativeAI(model="gemini-2.0-flash-exp")
|
| 23 |
-
model_flash_think = genai.GenerativeModel('gemini-2.0-flash-think')
|
| 24 |
|
| 25 |
class SmartShoppingAssistant:
|
| 26 |
def __init__(self, products_df):
|
|
@@ -29,10 +31,14 @@ class SmartShoppingAssistant:
|
|
| 29 |
self.setup_agent()
|
| 30 |
|
| 31 |
def setup_agent(self):
|
|
|
|
| 32 |
def search_products(query):
|
| 33 |
try:
|
|
|
|
| 34 |
if 'RetailPrice' in self.df.columns:
|
| 35 |
self.df['RetailPrice'] = pd.to_numeric(self.df['RetailPrice'].str.replace('$', ''), errors='coerce')
|
|
|
|
|
|
|
| 36 |
results = self.df.query(query)
|
| 37 |
return results.to_string() if not results.empty else "No products found matching your criteria."
|
| 38 |
except Exception as e:
|
|
@@ -46,71 +52,80 @@ class SmartShoppingAssistant:
|
|
| 46 |
)
|
| 47 |
]
|
| 48 |
|
| 49 |
-
#
|
| 50 |
-
|
| 51 |
-
You are a shopping assistant. Use the tools provided to find products based on user queries.
|
| 52 |
-
{input}
|
| 53 |
-
"""
|
| 54 |
-
|
| 55 |
-
# Create a PromptTemplate and LLMChain to make the model compatible
|
| 56 |
-
prompt = PromptTemplate(input_variables=["input"], template=prompt_template)
|
| 57 |
-
llm_chain = LLMChain(llm=llm_flash_exp, prompt=prompt)
|
| 58 |
-
|
| 59 |
-
# Create the agent with the adapted chain
|
| 60 |
-
agent = create_openai_functions_agent(
|
| 61 |
-
llm=llm_chain,
|
| 62 |
-
tools=tools,
|
| 63 |
-
prompt=prompt_template
|
| 64 |
-
)
|
| 65 |
-
|
| 66 |
-
self.agent_executor = AgentExecutor(
|
| 67 |
-
agent=agent,
|
| 68 |
tools=tools,
|
| 69 |
memory=self.memory,
|
|
|
|
|
|
|
| 70 |
verbose=True
|
| 71 |
)
|
| 72 |
|
| 73 |
def process_natural_language_query(self, query):
|
|
|
|
| 74 |
try:
|
| 75 |
-
|
| 76 |
prompt = f"""
|
| 77 |
-
Convert this shopping request into
|
| 78 |
{', '.join(self.df.columns)}
|
| 79 |
|
| 80 |
Shopping request: {query}
|
|
|
|
|
|
|
| 81 |
|
| 82 |
-
|
| 83 |
-
|
| 84 |
-
Return only a list of valid pandas query strings, nothing else.
|
| 85 |
"""
|
| 86 |
|
| 87 |
-
response =
|
| 88 |
-
|
| 89 |
-
|
| 90 |
-
results = []
|
| 91 |
-
for q in queries:
|
| 92 |
-
if q.strip():
|
| 93 |
-
result = self.agent_executor.invoke({"input": f"Execute this query and return matching products: {q.strip()}"})
|
| 94 |
-
results.append(result['output'])
|
| 95 |
|
| 96 |
-
|
|
|
|
|
|
|
| 97 |
except Exception as e:
|
| 98 |
return f"Error processing query: {str(e)}"
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 99 |
|
| 100 |
def main():
|
| 101 |
st.set_page_config(page_title="Smart Shopping Assistant", layout="wide")
|
|
|
|
| 102 |
st.title("🛒 Smart Shopping Assistant")
|
| 103 |
|
|
|
|
| 104 |
@st.cache_data
|
| 105 |
def load_product_data():
|
|
|
|
| 106 |
return pd.read_csv('supermarket2.csv')
|
| 107 |
|
| 108 |
df = load_product_data()
|
| 109 |
assistant = SmartShoppingAssistant(df)
|
| 110 |
|
|
|
|
| 111 |
with st.sidebar:
|
| 112 |
st.header("Upload Shopping List")
|
| 113 |
-
uploaded_file = st.file_uploader(
|
|
|
|
|
|
|
|
|
|
| 114 |
|
| 115 |
if uploaded_file:
|
| 116 |
if uploaded_file.type.startswith('image'):
|
|
@@ -120,19 +135,25 @@ def main():
|
|
| 120 |
with st.spinner("Processing image..."):
|
| 121 |
extracted_text = assistant.extract_text_from_image(image)
|
| 122 |
st.session_state.extracted_text = extracted_text
|
| 123 |
-
st.success("Text extracted!")
|
|
|
|
| 124 |
elif uploaded_file.type == 'application/pdf':
|
| 125 |
if st.button("Extract Items from PDF"):
|
| 126 |
with st.spinner("Processing PDF..."):
|
| 127 |
extracted_text = assistant.extract_text_from_pdf(uploaded_file)
|
| 128 |
st.session_state.extracted_text = extracted_text
|
| 129 |
-
st.success("Text extracted!")
|
| 130 |
|
|
|
|
| 131 |
col1, col2 = st.columns([2, 1])
|
| 132 |
|
| 133 |
with col1:
|
| 134 |
st.header("Search Products")
|
| 135 |
-
query = st.text_area(
|
|
|
|
|
|
|
|
|
|
|
|
|
| 136 |
|
| 137 |
if hasattr(st.session_state, 'extracted_text'):
|
| 138 |
st.write("Extracted text from upload:")
|
|
@@ -153,7 +174,9 @@ def main():
|
|
| 153 |
st.header("Shopping Cart")
|
| 154 |
if 'cart' not in st.session_state:
|
| 155 |
st.session_state.cart = []
|
|
|
|
|
|
|
| 156 |
st.write("Your cart is empty" if not st.session_state.cart else "Cart items here")
|
| 157 |
|
| 158 |
if __name__ == "__main__":
|
| 159 |
-
main()
|
|
|
|
| 1 |
import streamlit as st
|
| 2 |
import pandas as pd
|
|
|
|
| 3 |
from langchain.memory import ConversationBufferMemory
|
| 4 |
from langchain_google_genai import ChatGoogleGenerativeAI
|
| 5 |
from langchain.agents import AgentExecutor, create_openai_functions_agent
|
|
|
|
| 8 |
import PyPDF2
|
| 9 |
import os
|
| 10 |
import json
|
|
|
|
| 11 |
from langchain.chains import LLMChain
|
| 12 |
from langchain.prompts import PromptTemplate
|
| 13 |
+
from langchain.agents import initialize_agent, Tool
|
| 14 |
+
from langchain.agents.agent_types import AgentType
|
| 15 |
+
from PIL import Image
|
| 16 |
+
from dotenv import load_dotenv
|
| 17 |
|
| 18 |
# Load environment variables
|
| 19 |
load_dotenv()
|
| 20 |
|
| 21 |
+
# Configure Google API
|
| 22 |
genai.configure(api_key=os.getenv('GOOGLE_API_KEY'))
|
| 23 |
|
| 24 |
# Initialize Gemini models
|
| 25 |
llm_flash_exp = ChatGoogleGenerativeAI(model="gemini-2.0-flash-exp")
|
|
|
|
| 26 |
|
| 27 |
class SmartShoppingAssistant:
|
| 28 |
def __init__(self, products_df):
|
|
|
|
| 31 |
self.setup_agent()
|
| 32 |
|
| 33 |
def setup_agent(self):
|
| 34 |
+
"""Set up the LangChain agent with necessary tools"""
|
| 35 |
def search_products(query):
|
| 36 |
try:
|
| 37 |
+
# Convert price columns to numeric if they exist
|
| 38 |
if 'RetailPrice' in self.df.columns:
|
| 39 |
self.df['RetailPrice'] = pd.to_numeric(self.df['RetailPrice'].str.replace('$', ''), errors='coerce')
|
| 40 |
+
|
| 41 |
+
# Execute the query and format results
|
| 42 |
results = self.df.query(query)
|
| 43 |
return results.to_string() if not results.empty else "No products found matching your criteria."
|
| 44 |
except Exception as e:
|
|
|
|
| 52 |
)
|
| 53 |
]
|
| 54 |
|
| 55 |
+
# Initialize the agent
|
| 56 |
+
self.agent = initialize_agent(
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 57 |
tools=tools,
|
| 58 |
memory=self.memory,
|
| 59 |
+
llm=llm_flash_exp,
|
| 60 |
+
agent=AgentType.ZERO_SHOT_REACT_DESCRIPTION,
|
| 61 |
verbose=True
|
| 62 |
)
|
| 63 |
|
| 64 |
def process_natural_language_query(self, query):
|
| 65 |
+
"""Process natural language query and return relevant products"""
|
| 66 |
try:
|
| 67 |
+
# Use Gemini to generate a structured query
|
| 68 |
prompt = f"""
|
| 69 |
+
Convert this shopping request into a pandas query string for a DataFrame with columns:
|
| 70 |
{', '.join(self.df.columns)}
|
| 71 |
|
| 72 |
Shopping request: {query}
|
| 73 |
+
|
| 74 |
+
List of available products: {str(df['Product'].tolist())}
|
| 75 |
|
| 76 |
+
Return only the pandas query string, nothing else.
|
|
|
|
|
|
|
| 77 |
"""
|
| 78 |
|
| 79 |
+
response = llm_flash_exp.predict(prompt)
|
| 80 |
+
structured_query = response.strip()
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 81 |
|
| 82 |
+
# Execute the query through the agent
|
| 83 |
+
result = self.agent.run(f"Execute this query and return matching products: {structured_query}")
|
| 84 |
+
return result
|
| 85 |
except Exception as e:
|
| 86 |
return f"Error processing query: {str(e)}"
|
| 87 |
+
|
| 88 |
+
def extract_text_from_image(self, image):
|
| 89 |
+
"""Extract text from uploaded image using Gemini"""
|
| 90 |
+
try:
|
| 91 |
+
# This will need OCR if the image contains text.
|
| 92 |
+
# For now, simulate the response for demonstration purposes.
|
| 93 |
+
return "Extracted text from image (OCR not implemented in this example)."
|
| 94 |
+
except Exception as e:
|
| 95 |
+
return f"Error processing image: {str(e)}"
|
| 96 |
+
|
| 97 |
+
def extract_text_from_pdf(self, pdf_file):
|
| 98 |
+
"""Extract text from uploaded PDF"""
|
| 99 |
+
try:
|
| 100 |
+
pdf_reader = PyPDF2.PdfReader(pdf_file)
|
| 101 |
+
text = ""
|
| 102 |
+
for page in pdf_reader.pages:
|
| 103 |
+
text += page.extract_text()
|
| 104 |
+
return text
|
| 105 |
+
except Exception as e:
|
| 106 |
+
return f"Error processing PDF: {str(e)}"
|
| 107 |
|
| 108 |
def main():
|
| 109 |
st.set_page_config(page_title="Smart Shopping Assistant", layout="wide")
|
| 110 |
+
|
| 111 |
st.title("🛒 Smart Shopping Assistant")
|
| 112 |
|
| 113 |
+
# Load sample product data
|
| 114 |
@st.cache_data
|
| 115 |
def load_product_data():
|
| 116 |
+
# Replace this with your actual product data
|
| 117 |
return pd.read_csv('supermarket2.csv')
|
| 118 |
|
| 119 |
df = load_product_data()
|
| 120 |
assistant = SmartShoppingAssistant(df)
|
| 121 |
|
| 122 |
+
# Sidebar for file uploads
|
| 123 |
with st.sidebar:
|
| 124 |
st.header("Upload Shopping List")
|
| 125 |
+
uploaded_file = st.file_uploader(
|
| 126 |
+
"Upload an image or PDF of your shopping list",
|
| 127 |
+
type=['png', 'jpg', 'jpeg', 'pdf']
|
| 128 |
+
)
|
| 129 |
|
| 130 |
if uploaded_file:
|
| 131 |
if uploaded_file.type.startswith('image'):
|
|
|
|
| 135 |
with st.spinner("Processing image..."):
|
| 136 |
extracted_text = assistant.extract_text_from_image(image)
|
| 137 |
st.session_state.extracted_text = extracted_text
|
| 138 |
+
st.success("Text extracted! You can now search for these items.")
|
| 139 |
+
|
| 140 |
elif uploaded_file.type == 'application/pdf':
|
| 141 |
if st.button("Extract Items from PDF"):
|
| 142 |
with st.spinner("Processing PDF..."):
|
| 143 |
extracted_text = assistant.extract_text_from_pdf(uploaded_file)
|
| 144 |
st.session_state.extracted_text = extracted_text
|
| 145 |
+
st.success("Text extracted! You can now search for these items.")
|
| 146 |
|
| 147 |
+
# Main content area
|
| 148 |
col1, col2 = st.columns([2, 1])
|
| 149 |
|
| 150 |
with col1:
|
| 151 |
st.header("Search Products")
|
| 152 |
+
query = st.text_area(
|
| 153 |
+
"Describe what you're looking for:",
|
| 154 |
+
height=100,
|
| 155 |
+
placeholder="Example: I need healthy breakfast cereals under $5"
|
| 156 |
+
)
|
| 157 |
|
| 158 |
if hasattr(st.session_state, 'extracted_text'):
|
| 159 |
st.write("Extracted text from upload:")
|
|
|
|
| 174 |
st.header("Shopping Cart")
|
| 175 |
if 'cart' not in st.session_state:
|
| 176 |
st.session_state.cart = []
|
| 177 |
+
|
| 178 |
+
# Display cart items (to be implemented)
|
| 179 |
st.write("Your cart is empty" if not st.session_state.cart else "Cart items here")
|
| 180 |
|
| 181 |
if __name__ == "__main__":
|
| 182 |
+
main()
|