chelleboyer commited on
Commit
da3175d
Β·
1 Parent(s): cbc4795

Fix message handling to use correct Chainlit API

Browse files
Files changed (1) hide show
  1. app.py +11 -11
app.py CHANGED
@@ -302,7 +302,7 @@ async def main(message: cl.Message):
302
  uploaded_img = cv2.imread(img_path)
303
  if uploaded_img is None:
304
  logger.error(f"Failed to read image from path: {img_path}")
305
- await processing_msg.update(content="❌ Error: Could not read the uploaded image. Please try again with a different image.")
306
  return
307
 
308
  logger.info(f"Successfully read image, shape: {uploaded_img.shape}")
@@ -313,7 +313,7 @@ async def main(message: cl.Message):
313
  logger.info("Uploaded image validation successful")
314
  except ValueError as e:
315
  logger.error(f"Invalid uploaded image: {str(e)}")
316
- await processing_msg.update(content=f"❌ Error: {str(e)}")
317
  return
318
 
319
  # Load reference image
@@ -321,7 +321,7 @@ async def main(message: cl.Message):
321
  ref_img = load_reference_image()
322
  if ref_img is None:
323
  logger.error("Failed to load reference image")
324
- await processing_msg.update(content="❌ Error: Reference planogram image not found.")
325
  return
326
 
327
  # Validate reference image
@@ -330,34 +330,34 @@ async def main(message: cl.Message):
330
  logger.info("Reference image validation successful")
331
  except ValueError as e:
332
  logger.error(f"Invalid reference image: {str(e)}")
333
- await processing_msg.update(content=f"❌ Error: {str(e)}")
334
  return
335
 
336
  # Compare images using CLIP
337
- await processing_msg.update(content="πŸ”„ Comparing with reference planogram...")
338
  logger.info("Starting CLIP comparison")
339
  try:
340
  comparison_result = compare_images(ref_img, uploaded_img)
341
  logger.info(f"CLIP comparison completed with result: {comparison_result}")
342
  except Exception as e:
343
  logger.error(f"CLIP comparison failed: {str(e)}", exc_info=True)
344
- await processing_msg.update(content="❌ Error during image comparison. Please try again.")
345
  return
346
 
347
  # Prepare response
348
  if comparison_result['is_similar']:
349
  # Image is accepted, proceed with empty space analysis
350
- await processing_msg.update(content="βœ… Image accepted! Analyzing empty spaces...")
351
  logger.info("Starting empty space analysis")
352
 
353
  try:
354
  # Perform empty space analysis
355
  analysis_result = check_empty_spaces(uploaded_img)
356
  logger.info("Empty space analysis completed successfully")
357
- await processing_msg.update(content=analysis_result)
358
  except Exception as e:
359
  logger.error(f"Empty space analysis failed: {str(e)}", exc_info=True)
360
- await processing_msg.update(content="❌ Error during empty space analysis. Please try again.")
361
  return
362
  else:
363
  # Image is rejected
@@ -365,11 +365,11 @@ async def main(message: cl.Message):
365
  response += f"Similarity score: {comparison_result['similarity_score']:.2f}\n"
366
  response += f"Difference percentage: {comparison_result['difference_percentage']:.2f}%\n\n"
367
  response += "Please upload a different image that better matches the reference planogram."
368
- await processing_msg.update(content=response)
369
 
370
  except Exception as e:
371
  logger.error(f"Error processing image: {str(e)}", exc_info=True)
372
- await processing_msg.update(content=f"❌ Error processing image: {str(e)}")
373
  else:
374
  # Handle text-only messages
375
  await cl.Message(
 
302
  uploaded_img = cv2.imread(img_path)
303
  if uploaded_img is None:
304
  logger.error(f"Failed to read image from path: {img_path}")
305
+ await cl.Message(content="❌ Error: Could not read the uploaded image. Please try again with a different image.").send()
306
  return
307
 
308
  logger.info(f"Successfully read image, shape: {uploaded_img.shape}")
 
313
  logger.info("Uploaded image validation successful")
314
  except ValueError as e:
315
  logger.error(f"Invalid uploaded image: {str(e)}")
316
+ await cl.Message(content=f"❌ Error: {str(e)}").send()
317
  return
318
 
319
  # Load reference image
 
321
  ref_img = load_reference_image()
322
  if ref_img is None:
323
  logger.error("Failed to load reference image")
324
+ await cl.Message(content="❌ Error: Reference planogram image not found.").send()
325
  return
326
 
327
  # Validate reference image
 
330
  logger.info("Reference image validation successful")
331
  except ValueError as e:
332
  logger.error(f"Invalid reference image: {str(e)}")
333
+ await cl.Message(content=f"❌ Error: {str(e)}").send()
334
  return
335
 
336
  # Compare images using CLIP
337
+ await cl.Message(content="πŸ”„ Comparing with reference planogram...").send()
338
  logger.info("Starting CLIP comparison")
339
  try:
340
  comparison_result = compare_images(ref_img, uploaded_img)
341
  logger.info(f"CLIP comparison completed with result: {comparison_result}")
342
  except Exception as e:
343
  logger.error(f"CLIP comparison failed: {str(e)}", exc_info=True)
344
+ await cl.Message(content="❌ Error during image comparison. Please try again.").send()
345
  return
346
 
347
  # Prepare response
348
  if comparison_result['is_similar']:
349
  # Image is accepted, proceed with empty space analysis
350
+ await cl.Message(content="βœ… Image accepted! Analyzing empty spaces...").send()
351
  logger.info("Starting empty space analysis")
352
 
353
  try:
354
  # Perform empty space analysis
355
  analysis_result = check_empty_spaces(uploaded_img)
356
  logger.info("Empty space analysis completed successfully")
357
+ await cl.Message(content=analysis_result).send()
358
  except Exception as e:
359
  logger.error(f"Empty space analysis failed: {str(e)}", exc_info=True)
360
+ await cl.Message(content="❌ Error during empty space analysis. Please try again.").send()
361
  return
362
  else:
363
  # Image is rejected
 
365
  response += f"Similarity score: {comparison_result['similarity_score']:.2f}\n"
366
  response += f"Difference percentage: {comparison_result['difference_percentage']:.2f}%\n\n"
367
  response += "Please upload a different image that better matches the reference planogram."
368
+ await cl.Message(content=response).send()
369
 
370
  except Exception as e:
371
  logger.error(f"Error processing image: {str(e)}", exc_info=True)
372
+ await cl.Message(content=f"❌ Error processing image: {str(e)}").send()
373
  else:
374
  # Handle text-only messages
375
  await cl.Message(