masadonline commited on
Commit
1c7f823
·
verified ·
1 Parent(s): 7df18b4

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +28 -9
app.py CHANGED
@@ -352,13 +352,21 @@ if "bot_start_time_utc" not in st.session_state: st.session_state.bot_start_time
352
  if "processed_message_sids" not in st.session_state: st.session_state.processed_message_sids = set()
353
  if "manual_chat_history" not in st.session_state: st.session_state.manual_chat_history = []
354
 
 
355
  # --- Helper: Simple Intent Classifier ---
356
  def simple_intent_classifier(query):
357
  query_lower = query.lower()
358
- if any(k in query_lower for k in ["order", "status", "track", "delivery"]): return "ORDER_STATUS"
 
 
 
 
 
 
 
359
  if any(k in query_lower for k in ["product", "item", "buy", "price", "feature", "stock"]): return "PRODUCT_INFO"
360
  if any(k in query_lower for k in ["return", "policy", "refund", "exchange", "faq", "question", "how to", "support"]): return "GENERAL_POLICY_FAQ"
361
- return "UNKNOWN"
362
 
363
  # --- Main Application Controls ---
364
  col1, col2, col3, col4 = st.columns(4)
@@ -456,22 +464,34 @@ if st.session_state.get("app_started") and st.session_state.get("rag_pipeline_re
456
  with st.chat_message("user"): st.markdown(user_query_manual)
457
 
458
  with st.spinner("Thinking..."):
459
- intent = simple_intent_classifier(user_query_manual)
 
 
 
460
  context_for_llm, raw_context_data = "No specific context.", None
461
 
462
  if intent == "ORDER_STATUS":
463
- words = user_query_manual.split()
464
- potential_oid = next((w for w in words if w.upper().startswith("ORD")), None)
465
- if potential_oid:
466
- raw_context_data = get_order_details(potential_oid.upper(), st.session_state.customer_orders_data)
 
 
 
 
 
 
 
 
467
  context_for_llm = f"Order Details: {raw_context_data}"
468
  else:
469
- context_for_llm = "Please provide an Order ID."
470
  raw_context_data = {"message": "Order ID needed."}
471
  elif intent == "PRODUCT_INFO":
472
  raw_context_data = get_product_info(user_query_manual, st.session_state.products_data)
473
  context_for_llm = f"Product Information: {raw_context_data}"
474
  elif intent == "GENERAL_POLICY_FAQ" or intent == "UNKNOWN":
 
475
  if st.session_state.faiss_index_pdfs and st.session_state.embedding_model:
476
  k_val = 2 if intent == "GENERAL_POLICY_FAQ" else 1
477
  retrieved_chunks = search_faiss_index(st.session_state.faiss_index_pdfs, user_query_manual,
@@ -491,7 +511,6 @@ if st.session_state.get("app_started") and st.session_state.get("rag_pipeline_re
491
  st.markdown(llm_response)
492
  if raw_context_data:
493
  with st.expander("Retrieved Context"):
494
- # This block is the one that needs the fix
495
  try:
496
  if isinstance(raw_context_data, str) and (raw_context_data.strip().startswith('{') or raw_context_data.strip().startswith('[')):
497
  st.json(json.loads(raw_context_data))
 
352
  if "processed_message_sids" not in st.session_state: st.session_state.processed_message_sids = set()
353
  if "manual_chat_history" not in st.session_state: st.session_state.manual_chat_history = []
354
 
355
+ # --- Helper: Simple Intent Classifier ---
356
  # --- Helper: Simple Intent Classifier ---
357
  def simple_intent_classifier(query):
358
  query_lower = query.lower()
359
+ if any(k in query_lower for k in ["order", "status", "track", "delivery"]):
360
+ # Attempt to extract an order ID more robustly
361
+ import re
362
+ match = re.search(r'(ord[a-z0-9]+)', query_lower) # Regex to find 'ord' followed by alphanumeric
363
+ if match:
364
+ return "ORDER_STATUS", match.group(1).upper() # Return intent and extracted ID
365
+ return "ORDER_STATUS", None # Indicate order status intent but no ID found yet
366
+
367
  if any(k in query_lower for k in ["product", "item", "buy", "price", "feature", "stock"]): return "PRODUCT_INFO"
368
  if any(k in query_lower for k in ["return", "policy", "refund", "exchange", "faq", "question", "how to", "support"]): return "GENERAL_POLICY_FAQ"
369
+ return "UNKNOWN", None # Return intent and None for ID if unknown
370
 
371
  # --- Main Application Controls ---
372
  col1, col2, col3, col4 = st.columns(4)
 
464
  with st.chat_message("user"): st.markdown(user_query_manual)
465
 
466
  with st.spinner("Thinking..."):
467
+ intent_result = simple_intent_classifier(user_query_manual) # Get both intent and potential_id
468
+ intent = intent_result[0]
469
+ potential_oid_from_intent = intent_result[1] # This is the extracted ID if any
470
+
471
  context_for_llm, raw_context_data = "No specific context.", None
472
 
473
  if intent == "ORDER_STATUS":
474
+ order_id_to_check = None
475
+ if potential_oid_from_intent:
476
+ order_id_to_check = potential_oid_from_intent
477
+ else:
478
+ # Fallback or additional parsing if needed
479
+ # For example, if 'ORD1001' is not found by regex, try splitting words
480
+ words = user_query_manual.split()
481
+ # Look for something that starts with 'ORD' and is not just 'order' or 'order_id'
482
+ order_id_to_check = next((w for w in words if w.upper().startswith("ORD") and len(w) > 3), None) # ensure it's longer than just "ORD"
483
+
484
+ if order_id_to_check:
485
+ raw_context_data = get_order_details(order_id_to_check.upper(), st.session_state.customer_orders_data)
486
  context_for_llm = f"Order Details: {raw_context_data}"
487
  else:
488
+ context_for_llm = "Please provide a valid Order ID (e.g., ORD1234)."
489
  raw_context_data = {"message": "Order ID needed."}
490
  elif intent == "PRODUCT_INFO":
491
  raw_context_data = get_product_info(user_query_manual, st.session_state.products_data)
492
  context_for_llm = f"Product Information: {raw_context_data}"
493
  elif intent == "GENERAL_POLICY_FAQ" or intent == "UNKNOWN":
494
+ # ... (rest of your existing logic for these intents) ...
495
  if st.session_state.faiss_index_pdfs and st.session_state.embedding_model:
496
  k_val = 2 if intent == "GENERAL_POLICY_FAQ" else 1
497
  retrieved_chunks = search_faiss_index(st.session_state.faiss_index_pdfs, user_query_manual,
 
511
  st.markdown(llm_response)
512
  if raw_context_data:
513
  with st.expander("Retrieved Context"):
 
514
  try:
515
  if isinstance(raw_context_data, str) and (raw_context_data.strip().startswith('{') or raw_context_data.strip().startswith('[')):
516
  st.json(json.loads(raw_context_data))