Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
|
@@ -222,52 +222,50 @@ db_agent = create_sql_agent(
|
|
| 222 |
# ================================================================
|
| 223 |
def _query_id_match(cust_id: str, query: str) -> bool:
|
| 224 |
"""Verify that cust_id exists in at least one expected table."""
|
|
|
|
|
|
|
|
|
|
| 225 |
# STEP 1: Resolve file path and connect to SQLite
|
| 226 |
conn = sqlite3.connect("customer_orders.db")
|
| 227 |
cur = conn.cursor()
|
| 228 |
|
| 229 |
-
#
|
| 230 |
qc = f"SELECT order_id FROM orders WHERE cust_id='{cust_id}';"
|
| 231 |
-
|
|
|
|
|
|
|
|
|
|
| 232 |
|
| 233 |
-
# STEP 3:
|
| 234 |
-
# Extract customer ID if present in the query
|
| 235 |
return_value = True
|
| 236 |
qc_cid = []
|
| 237 |
cidcnt = 0
|
|
|
|
|
|
|
|
|
|
|
|
|
| 238 |
for match in re.findall(r"\bC\d{4}\b", query, flags=re.IGNORECASE):
|
| 239 |
if match:
|
| 240 |
cidcnt += 1
|
| 241 |
qc_cid = match.upper()
|
| 242 |
-
print(
|
| 243 |
if qc_cid != cust_id:
|
| 244 |
return_value = False
|
| 245 |
|
| 246 |
-
# Extract order ID
|
| 247 |
-
qc_oid = []
|
| 248 |
-
oidcnt = 0
|
| 249 |
for match in re.findall(r"\bO\d{5}\b", query, flags=re.IGNORECASE):
|
| 250 |
if match:
|
| 251 |
oidcnt += 1
|
| 252 |
qc_oid = match.upper()
|
| 253 |
-
|
|
|
|
| 254 |
return_value = False
|
| 255 |
|
| 256 |
-
|
| 257 |
-
return_value = True
|
| 258 |
-
|
| 259 |
if oidcnt > 1 or cidcnt > 1:
|
| 260 |
return_value = False
|
| 261 |
|
| 262 |
-
#
|
| 263 |
-
#print('return_value = ', return_value)
|
| 264 |
-
#print('qc_cid = ', qc_cid)
|
| 265 |
-
#print('qc_oid = ', qc_oid)
|
| 266 |
-
#print('db_order_id = ', db_order_id)
|
| 267 |
-
#print('cust_id = ', cust_id)
|
| 268 |
-
#print('query = ', query)
|
| 269 |
-
|
| 270 |
-
# STEP 4: Close connection if not found
|
| 271 |
conn.close()
|
| 272 |
return return_value
|
| 273 |
|
|
|
|
| 222 |
# ================================================================
|
| 223 |
def _query_id_match(cust_id: str, query: str) -> bool:
|
| 224 |
"""Verify that cust_id exists in at least one expected table."""
|
| 225 |
+
import sqlite3, re
|
| 226 |
+
import pandas as pd
|
| 227 |
+
|
| 228 |
# STEP 1: Resolve file path and connect to SQLite
|
| 229 |
conn = sqlite3.connect("customer_orders.db")
|
| 230 |
cur = conn.cursor()
|
| 231 |
|
| 232 |
+
# STEP 2: Fetch order IDs linked to the customer
|
| 233 |
qc = f"SELECT order_id FROM orders WHERE cust_id='{cust_id}';"
|
| 234 |
+
db_order_id_df = pd.read_sql_query(qc, conn)
|
| 235 |
+
|
| 236 |
+
# Convert the DataFrame to a list of order IDs (strings)
|
| 237 |
+
db_order_ids = db_order_id_df["order_id"].astype(str).tolist()
|
| 238 |
|
| 239 |
+
# STEP 3: Initialize tracking variables
|
|
|
|
| 240 |
return_value = True
|
| 241 |
qc_cid = []
|
| 242 |
cidcnt = 0
|
| 243 |
+
qc_oid = []
|
| 244 |
+
oidcnt = 0
|
| 245 |
+
|
| 246 |
+
# STEP 3A: Extract customer ID from user query
|
| 247 |
for match in re.findall(r"\bC\d{4}\b", query, flags=re.IGNORECASE):
|
| 248 |
if match:
|
| 249 |
cidcnt += 1
|
| 250 |
qc_cid = match.upper()
|
| 251 |
+
print("qc_cid =", qc_cid)
|
| 252 |
if qc_cid != cust_id:
|
| 253 |
return_value = False
|
| 254 |
|
| 255 |
+
# STEP 3B: Extract order ID from user query
|
|
|
|
|
|
|
| 256 |
for match in re.findall(r"\bO\d{5}\b", query, flags=re.IGNORECASE):
|
| 257 |
if match:
|
| 258 |
oidcnt += 1
|
| 259 |
qc_oid = match.upper()
|
| 260 |
+
# ✅ Compare safely: check if the order exists in DB list
|
| 261 |
+
if qc_oid not in db_order_ids:
|
| 262 |
return_value = False
|
| 263 |
|
| 264 |
+
# STEP 3C: Reject multiple conflicting IDs in same query
|
|
|
|
|
|
|
| 265 |
if oidcnt > 1 or cidcnt > 1:
|
| 266 |
return_value = False
|
| 267 |
|
| 268 |
+
# STEP 4: Clean up and return result
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 269 |
conn.close()
|
| 270 |
return return_value
|
| 271 |
|