rairo commited on
Commit
f5d4c6e
·
verified ·
1 Parent(s): 8848251

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +40 -1
app.py CHANGED
@@ -8,6 +8,8 @@ from langchain.agents.agent_types import AgentType
8
  from difflib import get_close_matches
9
  from dotenv import load_dotenv
10
  from fpdf import FPDF
 
 
11
  import os
12
 
13
  # Load environment variables
@@ -32,7 +34,7 @@ class SmartShoppingAssistant:
32
  matches = get_close_matches(
33
  product_name.upper(),
34
  self.df[self.df['IsAvailable'] == "Yes"]['ProductName'].str.upper().tolist(),
35
- n=5, # Get more matches
36
  cutoff=threshold
37
  )
38
  return matches if matches else []
@@ -58,6 +60,28 @@ class SmartShoppingAssistant:
58
 
59
  def process_query(self, query):
60
  return self.agent.run(f"Find the best matches for: {query}")
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
61
 
62
  def generate_receipt(cart_items):
63
  pdf = FPDF()
@@ -87,6 +111,21 @@ def main():
87
  df = load_product_data()
88
  assistant = SmartShoppingAssistant(df)
89
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
90
  if 'cart' not in st.session_state:
91
  st.session_state.cart = []
92
 
 
8
  from difflib import get_close_matches
9
  from dotenv import load_dotenv
10
  from fpdf import FPDF
11
+ from PIL import Image
12
+ import PyPDF2
13
  import os
14
 
15
  # Load environment variables
 
34
  matches = get_close_matches(
35
  product_name.upper(),
36
  self.df[self.df['IsAvailable'] == "Yes"]['ProductName'].str.upper().tolist(),
37
+ n=5,
38
  cutoff=threshold
39
  )
40
  return matches if matches else []
 
60
 
61
  def process_query(self, query):
62
  return self.agent.run(f"Find the best matches for: {query}")
63
+
64
+ def extract_text_from_image(self, image):
65
+ prompt = """
66
+ Analyze this image and extract products and their quantities.
67
+ If quantities aren't specified, make reasonable assumptions based on typical shopping patterns.
68
+ List each item with its quantity.
69
+ """
70
+ try:
71
+ response = model.generate_content([prompt, image])
72
+ return response.text
73
+ except Exception as e:
74
+ return f"Error processing image: {str(e)}"
75
+
76
+ def extract_text_from_pdf(self, pdf_file):
77
+ try:
78
+ pdf_reader = PyPDF2.PdfReader(pdf_file)
79
+ text = ""
80
+ for page in pdf_reader.pages:
81
+ text += page.extract_text()
82
+ return text
83
+ except Exception as e:
84
+ return f"Error processing PDF: {str(e)}"
85
 
86
  def generate_receipt(cart_items):
87
  pdf = FPDF()
 
111
  df = load_product_data()
112
  assistant = SmartShoppingAssistant(df)
113
 
114
+ with st.sidebar:
115
+ st.header("Upload Shopping List")
116
+ uploaded_file = st.file_uploader("Upload an image or PDF of your shopping list", type=['png', 'jpg', 'jpeg', 'pdf'])
117
+ if uploaded_file:
118
+ try:
119
+ if uploaded_file.type.startswith('image'):
120
+ image = Image.open(uploaded_file)
121
+ extracted_text = assistant.extract_text_from_image(image)
122
+ st.session_state.query = extracted_text
123
+ elif uploaded_file.type == 'application/pdf':
124
+ extracted_text = assistant.extract_text_from_pdf(uploaded_file)
125
+ st.session_state.query = extracted_text
126
+ except Exception as e:
127
+ st.error(f"Error processing file: {str(e)}")
128
+
129
  if 'cart' not in st.session_state:
130
  st.session_state.cart = []
131