rairo commited on
Commit
ce015e7
·
verified ·
1 Parent(s): ae087bf

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +104 -1
app.py CHANGED
@@ -214,4 +214,107 @@ def generate_receipt():
214
 
215
  total = 0
216
  for item in st.session_state.cart:
217
- price = item['RetailPrice']
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
214
 
215
  total = 0
216
  for item in st.session_state.cart:
217
+ price = item['RetailPrice']
218
+ * item['Quantity']
219
+ pdf.cell(200, 10,
220
+ txt=f"{item['ProductName']} x{item['Quantity']} - ${price:.2f}",
221
+ ln=1)
222
+ total += price
223
+
224
+ pdf.cell(200, 10, txt=f"Total: ${total:.2f}", ln=1)
225
+ return pdf.output(dest='S').encode('latin1')
226
+
227
+ # Update main function
228
+ def main():
229
+ st.set_page_config(page_title="Smart Shopping Assistant", layout="wide")
230
+ st.title("🛒 Smart Shopping Assistant")
231
+
232
+ @st.cache_data
233
+ def load_product_data():
234
+ return pd.read_csv('supermarket4i.csv') # Ensure correct filename
235
+
236
+ df = load_product_data()
237
+ assistant = SmartShoppingAssistant(df)
238
+
239
+ with st.sidebar:
240
+ st.header("Upload Shopping List")
241
+ uploaded_file = st.file_uploader(
242
+ "Upload an image or PDF of your shopping list",
243
+ type=['png', 'jpg', 'jpeg', 'pdf']
244
+ )
245
+
246
+ if uploaded_file:
247
+ try:
248
+ if uploaded_file.type.startswith('image'):
249
+ with st.spinner("Extracting items from image..."):
250
+ image = Image.open(uploaded_file)
251
+ extracted_text = assistant.extract_text_from_image(image)
252
+ st.session_state.query = extracted_text
253
+ elif uploaded_file.type == 'application/pdf':
254
+ with st.spinner("Extracting items from PDF..."):
255
+ extracted_text = assistant.extract_text_from_pdf(uploaded_file)
256
+ st.session_state.query = extracted_text
257
+ except Exception as e:
258
+ st.error(f"Error processing file: {str(e)}")
259
+
260
+ col1, col2 = st.columns([2, 1])
261
+
262
+ with col1:
263
+ st.header("Search Products")
264
+ query = st.text_area(
265
+ "Describe what you're looking for (include quantities if needed):",
266
+ height=100,
267
+ value=st.session_state.get('query', '')
268
+ )
269
+
270
+ if st.button("Search"):
271
+ if query:
272
+ with st.spinner("Searching..."):
273
+ results = assistant.process_natural_language_query(query)
274
+ st.session_state.last_results = results
275
+
276
+ if isinstance(results, str):
277
+ st.write(results) # Display error messages directly
278
+ elif isinstance(results, pd.DataFrame) and not results.empty: # Check if results is a DataFrame
279
+ total_price = 0
280
+ for _, row in results.iterrows():
281
+ st.write(f"{row['ProductName']} | ${row['RetailPrice']:.2f} x {row['Quantity']} = ${row['RetailPrice'] * row['Quantity']:.2f}")
282
+ total_price += row['RetailPrice'] * row['Quantity']
283
+ if st.button("Add to Cart", key=f"add_{row['ProductName']}"): # Unique key for each button
284
+ add_to_cart(row.to_dict())
285
+ st.write(f"**Total: ${total_price:.2f}**") # Display total price for search results
286
+ else:
287
+ st.write("No products found matching your criteria.") # Handle empty DataFrame case
288
+
289
+
290
+ with col2:
291
+ st.header("Shopping Cart")
292
+ if 'cart' in st.session_state and st.session_state.cart:
293
+ total = 0
294
+ for item in st.session_state.cart:
295
+ cols = st.columns([3,1,1])
296
+ with cols[0]:
297
+ st.write(f"{item['ProductName']} x{item['Quantity']}")
298
+ with cols[1]:
299
+ st.write(f"${item['RetailPrice'] * item['Quantity']:.2f}")
300
+ with cols[2]:
301
+ if st.button("❌", key=f"del_{item['ProductName']}"):
302
+ remove_from_cart(item['ProductName'])
303
+ st.rerun()
304
+ total += item['RetailPrice'] * item['Quantity']
305
+ st.divider()
306
+ st.write(f"**Total: ${total:.2f}**")
307
+
308
+ if st.button("Checkout"):
309
+ receipt = generate_receipt()
310
+ st.download_button(
311
+ label="Download Receipt",
312
+ data=receipt,
313
+ file_name="bon_marche_receipt.pdf",
314
+ mime="application/pdf"
315
+ )
316
+ else:
317
+ st.write("Your cart is empty")
318
+
319
+ if __name__ == "__main__":
320
+ main()