Spaces:
Runtime error
Runtime error
Update app.py
Browse files
app.py
CHANGED
|
@@ -1,500 +1,497 @@
|
|
| 1 |
-
import streamlit as st
|
| 2 |
-
from openai import OpenAI
|
| 3 |
-
import sqlite3
|
| 4 |
-
import pandas as pd
|
| 5 |
-
import re
|
| 6 |
-
import json
|
| 7 |
-
from sticky import sticky_container
|
| 8 |
-
import chromadb
|
| 9 |
-
from sentence_transformers import SentenceTransformer
|
| 10 |
-
from transformers import pipeline
|
| 11 |
-
import hashlib
|
| 12 |
-
import inspect
|
| 13 |
-
from tools import *
|
| 14 |
-
from var import SCHEMA_DESCRIPTIONS, SchemaVectorDB, FullVectorDB
|
| 15 |
-
import os
|
| 16 |
-
|
| 17 |
-
|
| 18 |
-
|
| 19 |
-
#
|
| 20 |
-
|
| 21 |
-
|
| 22 |
-
|
| 23 |
-
|
| 24 |
-
|
| 25 |
-
|
| 26 |
-
)
|
| 27 |
-
|
| 28 |
-
|
| 29 |
-
with open("prompts/
|
| 30 |
-
|
| 31 |
-
|
| 32 |
-
with open("prompts/
|
| 33 |
-
|
| 34 |
-
|
| 35 |
-
with open("prompts/
|
| 36 |
-
|
| 37 |
-
|
| 38 |
-
with open("prompts/
|
| 39 |
-
|
| 40 |
-
|
| 41 |
-
|
| 42 |
-
|
| 43 |
-
|
| 44 |
-
|
| 45 |
-
|
| 46 |
-
|
| 47 |
-
|
| 48 |
-
|
| 49 |
-
|
| 50 |
-
if '
|
| 51 |
-
st.session_state.
|
| 52 |
-
|
| 53 |
-
|
| 54 |
-
|
| 55 |
-
"
|
| 56 |
-
"
|
| 57 |
-
|
| 58 |
-
|
| 59 |
-
|
| 60 |
-
|
| 61 |
-
if '
|
| 62 |
-
st.session_state.
|
| 63 |
-
|
| 64 |
-
if '
|
| 65 |
-
st.session_state.
|
| 66 |
-
#
|
| 67 |
-
|
| 68 |
-
|
| 69 |
-
|
| 70 |
-
|
| 71 |
-
|
| 72 |
-
|
| 73 |
-
st.
|
| 74 |
-
|
| 75 |
-
|
| 76 |
-
|
| 77 |
-
|
| 78 |
-
|
| 79 |
-
|
| 80 |
-
|
| 81 |
-
|
| 82 |
-
|
| 83 |
-
|
| 84 |
-
|
| 85 |
-
|
| 86 |
-
|
| 87 |
-
|
| 88 |
-
|
| 89 |
-
|
| 90 |
-
|
| 91 |
-
|
| 92 |
-
|
| 93 |
-
|
| 94 |
-
|
| 95 |
-
|
| 96 |
-
|
| 97 |
-
|
| 98 |
-
|
| 99 |
-
|
| 100 |
-
|
| 101 |
-
|
| 102 |
-
|
| 103 |
-
|
| 104 |
-
|
| 105 |
-
|
| 106 |
-
|
| 107 |
-
|
| 108 |
-
|
| 109 |
-
|
| 110 |
-
|
| 111 |
-
|
| 112 |
-
|
| 113 |
-
|
| 114 |
-
|
| 115 |
-
|
| 116 |
-
|
| 117 |
-
|
| 118 |
-
|
| 119 |
-
|
| 120 |
-
|
| 121 |
-
|
| 122 |
-
|
| 123 |
-
|
| 124 |
-
|
| 125 |
-
|
| 126 |
-
|
| 127 |
-
|
| 128 |
-
|
| 129 |
-
|
| 130 |
-
|
| 131 |
-
|
| 132 |
-
|
| 133 |
-
|
| 134 |
-
|
| 135 |
-
|
| 136 |
-
|
| 137 |
-
|
| 138 |
-
|
| 139 |
-
|
| 140 |
-
|
| 141 |
-
|
| 142 |
-
|
| 143 |
-
|
| 144 |
-
|
| 145 |
-
|
| 146 |
-
|
| 147 |
-
|
| 148 |
-
|
| 149 |
-
|
| 150 |
-
|
| 151 |
-
|
| 152 |
-
|
| 153 |
-
|
| 154 |
-
|
| 155 |
-
|
| 156 |
-
|
| 157 |
-
|
| 158 |
-
|
| 159 |
-
|
| 160 |
-
|
| 161 |
-
|
| 162 |
-
|
| 163 |
-
|
| 164 |
-
|
| 165 |
-
|
| 166 |
-
|
| 167 |
-
|
| 168 |
-
|
| 169 |
-
|
| 170 |
-
|
| 171 |
-
|
| 172 |
-
for
|
| 173 |
-
|
| 174 |
-
|
| 175 |
-
|
| 176 |
-
|
| 177 |
-
|
| 178 |
-
|
| 179 |
-
|
| 180 |
-
|
| 181 |
-
|
| 182 |
-
|
| 183 |
-
|
| 184 |
-
f"
|
| 185 |
-
f"
|
| 186 |
-
f"
|
| 187 |
-
|
| 188 |
-
|
| 189 |
-
|
| 190 |
-
)
|
| 191 |
-
|
| 192 |
-
st.
|
| 193 |
-
st.session_state.
|
| 194 |
-
st.session_state.
|
| 195 |
-
|
| 196 |
-
|
| 197 |
-
|
| 198 |
-
|
| 199 |
-
|
| 200 |
-
|
| 201 |
-
|
| 202 |
-
|
| 203 |
-
|
| 204 |
-
|
| 205 |
-
|
| 206 |
-
|
| 207 |
-
|
| 208 |
-
|
| 209 |
-
|
| 210 |
-
|
| 211 |
-
|
| 212 |
-
.
|
| 213 |
-
|
| 214 |
-
}
|
| 215 |
-
.streamlit-
|
| 216 |
-
font-size: 0.
|
| 217 |
-
|
| 218 |
-
|
| 219 |
-
|
| 220 |
-
|
| 221 |
-
|
| 222 |
-
|
| 223 |
-
|
| 224 |
-
|
| 225 |
-
|
| 226 |
-
|
| 227 |
-
|
| 228 |
-
|
| 229 |
-
|
| 230 |
-
|
| 231 |
-
-
|
| 232 |
-
-
|
| 233 |
-
-
|
| 234 |
-
-
|
| 235 |
-
-
|
| 236 |
-
-
|
| 237 |
-
-
|
| 238 |
-
-
|
| 239 |
-
-
|
| 240 |
-
-
|
| 241 |
-
-
|
| 242 |
-
- The
|
| 243 |
-
-
|
| 244 |
-
-
|
| 245 |
-
-
|
| 246 |
-
-
|
| 247 |
-
-
|
| 248 |
-
|
| 249 |
-
|
| 250 |
-
|
| 251 |
-
|
| 252 |
-
|
| 253 |
-
|
| 254 |
-
|
| 255 |
-
|
| 256 |
-
-
|
| 257 |
-
-
|
| 258 |
-
-
|
| 259 |
-
-
|
| 260 |
-
-
|
| 261 |
-
-
|
| 262 |
-
|
| 263 |
-
|
| 264 |
-
|
| 265 |
-
|
| 266 |
-
|
| 267 |
-
|
| 268 |
-
|
| 269 |
-
|
| 270 |
-
-
|
| 271 |
-
-
|
| 272 |
-
|
| 273 |
-
|
| 274 |
-
|
| 275 |
-
|
| 276 |
-
|
| 277 |
-
|
| 278 |
-
|
| 279 |
-
|
| 280 |
-
#
|
| 281 |
-
|
| 282 |
-
|
| 283 |
-
|
| 284 |
-
|
| 285 |
-
|
| 286 |
-
|
| 287 |
-
|
| 288 |
-
|
| 289 |
-
|
| 290 |
-
|
| 291 |
-
|
| 292 |
-
|
| 293 |
-
st.session_state.
|
| 294 |
-
|
| 295 |
-
|
| 296 |
-
|
| 297 |
-
|
| 298 |
-
|
| 299 |
-
|
| 300 |
-
|
| 301 |
-
|
| 302 |
-
|
| 303 |
-
|
| 304 |
-
|
| 305 |
-
|
| 306 |
-
|
| 307 |
-
|
| 308 |
-
|
| 309 |
-
|
| 310 |
-
|
| 311 |
-
|
| 312 |
-
|
| 313 |
-
|
| 314 |
-
|
| 315 |
-
|
| 316 |
-
|
| 317 |
-
|
| 318 |
-
|
| 319 |
-
|
| 320 |
-
|
| 321 |
-
|
| 322 |
-
|
| 323 |
-
|
| 324 |
-
|
| 325 |
-
|
| 326 |
-
|
| 327 |
-
|
| 328 |
-
|
| 329 |
-
|
| 330 |
-
|
| 331 |
-
|
| 332 |
-
|
| 333 |
-
|
| 334 |
-
|
| 335 |
-
|
| 336 |
-
|
| 337 |
-
|
| 338 |
-
|
| 339 |
-
|
| 340 |
-
|
| 341 |
-
|
| 342 |
-
|
| 343 |
-
|
| 344 |
-
|
| 345 |
-
|
| 346 |
-
|
| 347 |
-
|
| 348 |
-
|
| 349 |
-
|
| 350 |
-
|
| 351 |
-
|
| 352 |
-
#
|
| 353 |
-
|
| 354 |
-
|
| 355 |
-
|
| 356 |
-
|
| 357 |
-
|
| 358 |
-
|
| 359 |
-
|
| 360 |
-
|
| 361 |
-
|
| 362 |
-
|
| 363 |
-
|
| 364 |
-
|
| 365 |
-
|
| 366 |
-
|
| 367 |
-
|
| 368 |
-
|
| 369 |
-
|
| 370 |
-
|
| 371 |
-
|
| 372 |
-
|
| 373 |
-
|
| 374 |
-
|
| 375 |
-
|
| 376 |
-
|
| 377 |
-
|
| 378 |
-
|
| 379 |
-
|
| 380 |
-
|
| 381 |
-
|
| 382 |
-
|
| 383 |
-
#
|
| 384 |
-
#
|
| 385 |
-
|
| 386 |
-
|
| 387 |
-
|
| 388 |
-
|
| 389 |
-
|
| 390 |
-
|
| 391 |
-
|
| 392 |
-
|
| 393 |
-
|
| 394 |
-
|
| 395 |
-
|
| 396 |
-
|
| 397 |
-
|
| 398 |
-
|
| 399 |
-
|
| 400 |
-
|
| 401 |
-
|
| 402 |
-
|
| 403 |
-
|
| 404 |
-
|
| 405 |
-
|
| 406 |
-
|
| 407 |
-
|
| 408 |
-
|
| 409 |
-
|
| 410 |
-
|
| 411 |
-
|
| 412 |
-
|
| 413 |
-
|
| 414 |
-
|
| 415 |
-
|
| 416 |
-
|
| 417 |
-
|
| 418 |
-
|
| 419 |
-
|
| 420 |
-
|
| 421 |
-
|
| 422 |
-
|
| 423 |
-
|
| 424 |
-
|
| 425 |
-
|
| 426 |
-
|
| 427 |
-
|
| 428 |
-
|
| 429 |
-
|
| 430 |
-
for
|
| 431 |
-
|
| 432 |
-
|
| 433 |
-
|
| 434 |
-
|
| 435 |
-
|
| 436 |
-
|
| 437 |
-
|
| 438 |
-
|
| 439 |
-
|
| 440 |
-
|
| 441 |
-
|
| 442 |
-
|
| 443 |
-
f"
|
| 444 |
-
f"
|
| 445 |
-
f"
|
| 446 |
-
|
| 447 |
-
|
| 448 |
-
|
| 449 |
-
)
|
| 450 |
-
|
| 451 |
-
response_summary
|
| 452 |
-
st.
|
| 453 |
-
st.session_state.
|
| 454 |
-
|
| 455 |
-
st.session_state.
|
| 456 |
-
|
| 457 |
-
|
| 458 |
-
|
| 459 |
-
|
| 460 |
-
|
| 461 |
-
|
| 462 |
-
|
| 463 |
-
|
| 464 |
-
|
| 465 |
-
|
| 466 |
-
|
| 467 |
-
|
| 468 |
-
|
| 469 |
-
|
| 470 |
-
|
| 471 |
-
|
| 472 |
-
|
| 473 |
-
elif intent == "
|
| 474 |
-
response_summary = "
|
| 475 |
-
|
| 476 |
-
|
| 477 |
-
|
| 478 |
-
|
| 479 |
-
|
| 480 |
-
|
| 481 |
-
|
| 482 |
-
|
| 483 |
-
|
| 484 |
-
|
| 485 |
-
|
| 486 |
-
|
| 487 |
-
|
| 488 |
-
|
| 489 |
-
|
| 490 |
-
|
| 491 |
-
|
| 492 |
-
|
| 493 |
-
|
| 494 |
-
st.
|
| 495 |
-
#
|
| 496 |
-
|
| 497 |
-
|
| 498 |
-
# Reset if booking done
|
| 499 |
-
|
| 500 |
-
|
|
|
|
| 1 |
+
import streamlit as st
|
| 2 |
+
from openai import OpenAI
|
| 3 |
+
import sqlite3
|
| 4 |
+
import pandas as pd
|
| 5 |
+
import re
|
| 6 |
+
import json
|
| 7 |
+
from sticky import sticky_container
|
| 8 |
+
import chromadb
|
| 9 |
+
from sentence_transformers import SentenceTransformer
|
| 10 |
+
from transformers import pipeline
|
| 11 |
+
import hashlib
|
| 12 |
+
import inspect
|
| 13 |
+
from tools import *
|
| 14 |
+
from var import SCHEMA_DESCRIPTIONS, SchemaVectorDB, FullVectorDB
|
| 15 |
+
import os
|
| 16 |
+
# Set your Groq API key
|
| 17 |
+
GROQ_API_KEY = os.environ.get("GROQ_API_KEY")
|
| 18 |
+
|
| 19 |
+
# Initialize Groq's OpenAI-compatible client
|
| 20 |
+
client = OpenAI(
|
| 21 |
+
api_key=GROQ_API_KEY,
|
| 22 |
+
base_url="https://api.groq.com/openai/v1"
|
| 23 |
+
)
|
| 24 |
+
|
| 25 |
+
# --- Load prompt templates from prompts folder ---
|
| 26 |
+
with open("prompts/determine_intent.txt", "r", encoding="utf-8") as f:
|
| 27 |
+
determine_intent_prompt = f.read()
|
| 28 |
+
|
| 29 |
+
with open("prompts/generate_reservation_conversation.txt", "r", encoding="utf-8") as f:
|
| 30 |
+
generate_reservation_conversation_prompt = f.read()
|
| 31 |
+
|
| 32 |
+
with open("prompts/interpret_sql_result.txt", "r", encoding="utf-8") as f:
|
| 33 |
+
interpret_sql_result_prompt = f.read()
|
| 34 |
+
|
| 35 |
+
with open("prompts/schema_prompt.txt", "r", encoding="utf-8") as f:
|
| 36 |
+
schema_prompt = f.read()
|
| 37 |
+
|
| 38 |
+
with open("prompts/store_user_info.txt", "r", encoding="utf-8") as f:
|
| 39 |
+
store_user_info_prompt = f.read()
|
| 40 |
+
|
| 41 |
+
|
| 42 |
+
|
| 43 |
+
st.set_page_config(page_title="FoodieSpot Assistant", layout="wide")
|
| 44 |
+
|
| 45 |
+
|
| 46 |
+
# --- Initialize State ---
|
| 47 |
+
if 'chat_history' not in st.session_state:
|
| 48 |
+
st.session_state.chat_history = []
|
| 49 |
+
|
| 50 |
+
if 'user_data' not in st.session_state:
|
| 51 |
+
st.session_state.user_data = {
|
| 52 |
+
"restaurant_name": None,
|
| 53 |
+
"user_name": None,
|
| 54 |
+
"contact": None,
|
| 55 |
+
"party_size": None,
|
| 56 |
+
"time": None
|
| 57 |
+
}
|
| 58 |
+
if 'vector_db' not in st.session_state:
|
| 59 |
+
st.session_state.vector_db = SchemaVectorDB()
|
| 60 |
+
vector_db = st.session_state.vector_db
|
| 61 |
+
if 'full_vector_db' not in st.session_state:
|
| 62 |
+
st.session_state.full_vector_db = FullVectorDB()
|
| 63 |
+
# Track last assistant reply for context
|
| 64 |
+
if 'last_assistant_reply' not in st.session_state:
|
| 65 |
+
st.session_state.last_assistant_reply = ""
|
| 66 |
+
# Fixed container at top for title + reservation
|
| 67 |
+
reservation_box = sticky_container(mode="top", border=False,z=999)
|
| 68 |
+
|
| 69 |
+
with reservation_box:
|
| 70 |
+
st.text("")
|
| 71 |
+
st.text("")
|
| 72 |
+
st.title("π½οΈ FoodieSpot Assistant")
|
| 73 |
+
cols = st.columns([3, 3, 3, 2, 2, 1])
|
| 74 |
+
|
| 75 |
+
with cols[0]:
|
| 76 |
+
restaurant_name = st.text_input(
|
| 77 |
+
"Restaurant Name",
|
| 78 |
+
value=st.session_state.user_data.get("restaurant_name") or "",
|
| 79 |
+
key="restaurant_name_input"
|
| 80 |
+
)
|
| 81 |
+
if restaurant_name!="":
|
| 82 |
+
st.session_state.user_data["restaurant_name"] = restaurant_name
|
| 83 |
+
|
| 84 |
+
with cols[1]:
|
| 85 |
+
user_name = st.text_input(
|
| 86 |
+
"Your Name",
|
| 87 |
+
value=st.session_state.user_data.get("user_name") or "",
|
| 88 |
+
key="user_name_input"
|
| 89 |
+
)
|
| 90 |
+
if user_name!="":
|
| 91 |
+
st.session_state.user_data["user_name"] = user_name
|
| 92 |
+
|
| 93 |
+
with cols[2]:
|
| 94 |
+
contact = st.text_input(
|
| 95 |
+
"Contact",
|
| 96 |
+
value=st.session_state.user_data.get("contact") or "",
|
| 97 |
+
key="contact_input"
|
| 98 |
+
)
|
| 99 |
+
if contact!="":
|
| 100 |
+
st.session_state.user_data["contact"] = contact
|
| 101 |
+
|
| 102 |
+
with cols[3]:
|
| 103 |
+
party_size = st.number_input(
|
| 104 |
+
"Party Size",
|
| 105 |
+
value=st.session_state.user_data.get("party_size") or 0,
|
| 106 |
+
key="party_size_input"
|
| 107 |
+
)
|
| 108 |
+
if party_size!=0:
|
| 109 |
+
st.session_state.user_data["party_size"] = party_size
|
| 110 |
+
|
| 111 |
+
with cols[4]:
|
| 112 |
+
time = st.number_input(
|
| 113 |
+
"Time(24hr form, 9-20, 8 ~ null)",
|
| 114 |
+
min_value=8,
|
| 115 |
+
max_value=20,
|
| 116 |
+
value=st.session_state.user_data.get("time") or 8,
|
| 117 |
+
key="time_input"
|
| 118 |
+
)
|
| 119 |
+
if time!=8:
|
| 120 |
+
st.session_state.user_data["time"] = time
|
| 121 |
+
# Place the BOOK button in the last column
|
| 122 |
+
with cols[5]:
|
| 123 |
+
st.text("")
|
| 124 |
+
st.text("")
|
| 125 |
+
book_clicked = st.button("BOOK", type="primary")
|
| 126 |
+
# Add a green BOOK button (primary style)
|
| 127 |
+
# book_clicked = st.button("BOOK", type="primary")
|
| 128 |
+
|
| 129 |
+
if book_clicked:
|
| 130 |
+
# Check if all required fields are filled
|
| 131 |
+
required_keys = ["restaurant_name", "user_name", "contact", "party_size", "time"]
|
| 132 |
+
if all(st.session_state.user_data.get(k) not in [None, "", 0, 8] for k in required_keys):
|
| 133 |
+
booking_conn = None
|
| 134 |
+
try:
|
| 135 |
+
user_data = st.session_state.user_data
|
| 136 |
+
party_size = int(user_data["party_size"])
|
| 137 |
+
tables_needed = -(-party_size // 4)
|
| 138 |
+
|
| 139 |
+
booking_conn = sqlite3.connect("db/restaurant_reservation.db")
|
| 140 |
+
booking_cursor = booking_conn.cursor()
|
| 141 |
+
|
| 142 |
+
booking_cursor.execute("SELECT id FROM restaurants WHERE LOWER(name) = LOWER(?)", (user_data["restaurant_name"],))
|
| 143 |
+
restaurant_row = booking_cursor.fetchone()
|
| 144 |
+
if not restaurant_row:
|
| 145 |
+
raise Exception("Restaurant not found.")
|
| 146 |
+
restaurant_id = restaurant_row[0]
|
| 147 |
+
|
| 148 |
+
booking_cursor.execute("""
|
| 149 |
+
SELECT t.id AS table_id, s.id AS slot_id
|
| 150 |
+
FROM tables t
|
| 151 |
+
JOIN slots s ON t.id = s.table_id
|
| 152 |
+
WHERE t.restaurant_id = ?
|
| 153 |
+
AND s.hour = ?
|
| 154 |
+
AND s.date = '2025-05-12'
|
| 155 |
+
AND s.is_reserved = 0
|
| 156 |
+
LIMIT ?
|
| 157 |
+
""", (restaurant_id, user_data["time"], tables_needed))
|
| 158 |
+
available = booking_cursor.fetchall()
|
| 159 |
+
|
| 160 |
+
if len(available) < tables_needed:
|
| 161 |
+
raise Exception("Not enough available tables.")
|
| 162 |
+
|
| 163 |
+
booking_cursor.execute("""
|
| 164 |
+
INSERT INTO reservations (restaurant_id, user_name, contact, date, time, party_size)
|
| 165 |
+
VALUES (?, ?, ?, '2025-05-12', ?, ?)
|
| 166 |
+
""", (restaurant_id, user_data["user_name"], user_data["contact"], user_data["time"], party_size))
|
| 167 |
+
reservation_id = booking_cursor.lastrowid
|
| 168 |
+
|
| 169 |
+
for table_id, _ in available:
|
| 170 |
+
booking_cursor.execute("INSERT INTO reservation_tables (reservation_id, table_id) VALUES (?, ?)", (reservation_id, table_id))
|
| 171 |
+
|
| 172 |
+
slot_ids = [slot_id for _, slot_id in available]
|
| 173 |
+
booking_cursor.executemany("UPDATE slots SET is_reserved = 1 WHERE id = ?", [(sid,) for sid in slot_ids])
|
| 174 |
+
|
| 175 |
+
booking_conn.commit()
|
| 176 |
+
|
| 177 |
+
booking_cursor.execute("SELECT name FROM restaurants WHERE id = ?", (restaurant_id,))
|
| 178 |
+
restaurant_name = booking_cursor.fetchone()[0]
|
| 179 |
+
|
| 180 |
+
confirmation_msg = (
|
| 181 |
+
f"β
Booking processed successfully!\n\n"
|
| 182 |
+
f"π Restaurant: **{restaurant_name}**\n"
|
| 183 |
+
f"β° Time: **{user_data['time']} on 2025-05-12**\n"
|
| 184 |
+
f"π½οΈ Tables Booked: **{tables_needed}**\n"
|
| 185 |
+
f"π Reservation ID: **{reservation_id}**\n\n"
|
| 186 |
+
f"π Please mention this Reservation ID at the restaurant reception when you arrive."
|
| 187 |
+
)
|
| 188 |
+
|
| 189 |
+
st.success(confirmation_msg)
|
| 190 |
+
st.session_state.chat_history.append({'role': 'assistant', 'message': confirmation_msg})
|
| 191 |
+
st.session_state.user_data["restaurant_name"] = None
|
| 192 |
+
st.session_state.user_data["party_size"] = None
|
| 193 |
+
st.session_state.user_data["time"] = None
|
| 194 |
+
st.session_state.last_assistant_reply = ""
|
| 195 |
+
except Exception as e:
|
| 196 |
+
if booking_conn:
|
| 197 |
+
booking_conn.rollback()
|
| 198 |
+
st.error(f"β Booking failed: {e}")
|
| 199 |
+
finally:
|
| 200 |
+
if booking_conn:
|
| 201 |
+
booking_cursor = None
|
| 202 |
+
booking_conn.close()
|
| 203 |
+
else:
|
| 204 |
+
st.warning("β οΈ Missing user information. Please provide all booking details first.")
|
| 205 |
+
st.text("")
|
| 206 |
+
# Inject custom CSS for smaller font and tighter layout
|
| 207 |
+
st.markdown("""
|
| 208 |
+
<style>
|
| 209 |
+
.element-container:has(.streamlit-expander) {
|
| 210 |
+
margin-bottom: 0.5rem;
|
| 211 |
+
}
|
| 212 |
+
.streamlit-expanderHeader {
|
| 213 |
+
font-size: 0.9rem;
|
| 214 |
+
}
|
| 215 |
+
.streamlit-expanderContent {
|
| 216 |
+
font-size: 0.85rem;
|
| 217 |
+
padding: 0.5rem 1rem;
|
| 218 |
+
}
|
| 219 |
+
</style>
|
| 220 |
+
""", unsafe_allow_html=True)
|
| 221 |
+
|
| 222 |
+
with st.container():
|
| 223 |
+
col1, col2, col3 = st.columns(3)
|
| 224 |
+
|
| 225 |
+
with col1:
|
| 226 |
+
with st.expander("π½οΈ Restaurants"):
|
| 227 |
+
st.markdown("""
|
| 228 |
+
- Bella Italia
|
| 229 |
+
- Spice Symphony
|
| 230 |
+
- Tokyo Ramen House
|
| 231 |
+
- Saffron Grill
|
| 232 |
+
- El Toro Loco
|
| 233 |
+
- Noodle Bar
|
| 234 |
+
- Le Petit Bistro
|
| 235 |
+
- Tandoori Nights
|
| 236 |
+
- Green Leaf Cafe
|
| 237 |
+
- Ocean Pearl
|
| 238 |
+
- Mama Mia Pizza
|
| 239 |
+
- The Dumpling Den
|
| 240 |
+
- Bangkok Express
|
| 241 |
+
- Curry Kingdom
|
| 242 |
+
- The Garden Table
|
| 243 |
+
- Skyline Dine
|
| 244 |
+
- Pasta Republic
|
| 245 |
+
- Street Tacos Co
|
| 246 |
+
- Miso Hungry
|
| 247 |
+
- Chez Marie
|
| 248 |
+
""")
|
| 249 |
+
|
| 250 |
+
with col2:
|
| 251 |
+
with st.expander("π Cuisines"):
|
| 252 |
+
st.markdown("""
|
| 253 |
+
- Italian
|
| 254 |
+
- French
|
| 255 |
+
- Chinese
|
| 256 |
+
- Japanese
|
| 257 |
+
- Indian
|
| 258 |
+
- Mexican
|
| 259 |
+
- Thai
|
| 260 |
+
- Healthy
|
| 261 |
+
- Fusion
|
| 262 |
+
""")
|
| 263 |
+
|
| 264 |
+
with col3:
|
| 265 |
+
with st.expander("β¨ Special Features"):
|
| 266 |
+
st.markdown("""
|
| 267 |
+
- Pet-Friendly
|
| 268 |
+
- Live Music
|
| 269 |
+
- Rooftop View
|
| 270 |
+
- Outdoor Seating
|
| 271 |
+
- Private Dining
|
| 272 |
+
""")
|
| 273 |
+
|
| 274 |
+
|
| 275 |
+
|
| 276 |
+
|
| 277 |
+
# --- Display previous chat history (before new input) ---
|
| 278 |
+
|
| 279 |
+
for msg in st.session_state.chat_history:
|
| 280 |
+
# Check if both 'role' and 'message' are not None
|
| 281 |
+
if msg['role'] is not None and msg['message'] is not None:
|
| 282 |
+
with st.chat_message(msg['role']):
|
| 283 |
+
st.markdown(msg['message'])
|
| 284 |
+
|
| 285 |
+
user_input = st.chat_input("Ask something about restaurants or reservations(eg. Tell me some best rated Italian cuisine restaurants)...")
|
| 286 |
+
if user_input:
|
| 287 |
+
# Show user message instantly
|
| 288 |
+
with st.chat_message("user"):
|
| 289 |
+
st.markdown(user_input)
|
| 290 |
+
st.session_state.chat_history.append({'role': 'user', 'message': user_input})
|
| 291 |
+
|
| 292 |
+
# Prepare conversation context
|
| 293 |
+
history_prompt = st.session_state.last_assistant_reply
|
| 294 |
+
|
| 295 |
+
# Store possible user info
|
| 296 |
+
user_info = store_user_info(user_input,history_prompt,store_user_info_prompt,client)
|
| 297 |
+
if user_info:
|
| 298 |
+
st.session_state.user_data.update(user_info)
|
| 299 |
+
# st.rerun()
|
| 300 |
+
|
| 301 |
+
# Detect intent
|
| 302 |
+
intent = determine_intent(user_input,determine_intent_prompt,client)
|
| 303 |
+
# st.write(intent)
|
| 304 |
+
if intent == "RUBBISH":
|
| 305 |
+
# Display user data for confirmation instead of invoking LLM
|
| 306 |
+
with st.chat_message("assistant"):
|
| 307 |
+
st.markdown("β Sorry, I didn't understand that. Could you rephrase your request?")
|
| 308 |
+
st.session_state.chat_history.append({
|
| 309 |
+
'role': 'assistant',
|
| 310 |
+
'message': "β Sorry, I didn't understand that. Could you rephrase your request?"
|
| 311 |
+
})
|
| 312 |
+
|
| 313 |
+
st.stop()
|
| 314 |
+
|
| 315 |
+
# Generate assistant reply
|
| 316 |
+
required_keys = ["restaurant_name", "user_name", "contact", "party_size", "time"]
|
| 317 |
+
user_data_complete = all(
|
| 318 |
+
k in st.session_state.user_data and st.session_state.user_data[k] not in [None, "", "NULL"]
|
| 319 |
+
for k in required_keys
|
| 320 |
+
)
|
| 321 |
+
|
| 322 |
+
|
| 323 |
+
if user_data_complete and intent != "BOOK":
|
| 324 |
+
|
| 325 |
+
# Format user data as a Markdown bullet list
|
| 326 |
+
user_details = "\n".join([f"- **{key.capitalize()}**: {value}" for key, value in st.session_state.user_data.items()])
|
| 327 |
+
|
| 328 |
+
with st.chat_message("assistant"):
|
| 329 |
+
st.markdown("β
I have all the details needed for your reservation:")
|
| 330 |
+
st.markdown(user_details)
|
| 331 |
+
st.markdown("If everything looks good, please type **`book`** to confirm the reservation.")
|
| 332 |
+
|
| 333 |
+
st.session_state.chat_history.append({
|
| 334 |
+
'role': 'assistant',
|
| 335 |
+
'message': f"β
I have all the details needed for your reservation:\n{user_details}\nPlease type **`book`** to confirm."
|
| 336 |
+
})
|
| 337 |
+
st.session_state.last_assistant_reply = "I have all the reservation details. Waiting for confirmation..."
|
| 338 |
+
st.rerun()
|
| 339 |
+
st.stop()
|
| 340 |
+
|
| 341 |
+
|
| 342 |
+
|
| 343 |
+
|
| 344 |
+
response_summary = None
|
| 345 |
+
|
| 346 |
+
if intent == "SELECT":
|
| 347 |
+
response_summary=handle_query(user_input, st.session_state.full_vector_db, client)
|
| 348 |
+
|
| 349 |
+
# First try semantic search
|
| 350 |
+
semantic_results = {}
|
| 351 |
+
|
| 352 |
+
# Search across all collections
|
| 353 |
+
restaurant_results = st.session_state.full_vector_db.semantic_search(user_input, "restaurants")
|
| 354 |
+
table_results = st.session_state.full_vector_db.semantic_search(user_input, "tables")
|
| 355 |
+
slot_results = st.session_state.full_vector_db.semantic_search(user_input, "slots")
|
| 356 |
+
|
| 357 |
+
if not is_large_output_request(user_input) and any([restaurant_results, table_results, slot_results]):
|
| 358 |
+
semantic_results = {
|
| 359 |
+
"restaurants": restaurant_results,
|
| 360 |
+
"tables": table_results,
|
| 361 |
+
"slots": slot_results
|
| 362 |
+
}
|
| 363 |
+
# Format semantic results
|
| 364 |
+
summary = []
|
| 365 |
+
for category, items in semantic_results.items():
|
| 366 |
+
if items:
|
| 367 |
+
summary.append(f"Found {len(items)} relevant {category}:")
|
| 368 |
+
summary.extend([f"- {item['name']}" if 'name' in item else f"- {item}"
|
| 369 |
+
for item in items[:3]])
|
| 370 |
+
st.write("### Semantic Search used")
|
| 371 |
+
response_summary = "\n".join(summary)
|
| 372 |
+
else:
|
| 373 |
+
# Fall back to SQL generation for large or exact output requests
|
| 374 |
+
sql = generate_sql_query_v2(user_input,SCHEMA_DESCRIPTIONS, history_prompt, vector_db, client)
|
| 375 |
+
result = execute_query(sql)
|
| 376 |
+
response_summary = interpret_result_v2(result, user_input, sql)
|
| 377 |
+
|
| 378 |
+
|
| 379 |
+
|
| 380 |
+
# sql = generate_sql_query_v2(user_input,history_prompt, vector_db, client)
|
| 381 |
+
# result = execute_query(sql)
|
| 382 |
+
# response_summary=interpret_result_v2(result, user_input, sql)
|
| 383 |
+
# if isinstance(result, pd.DataFrame):
|
| 384 |
+
# response_summary = interpret_sql_result(user_input, sql_query, result)
|
| 385 |
+
|
| 386 |
+
|
| 387 |
+
elif intent == "BOOK":
|
| 388 |
+
required_keys = ["restaurant_name", "user_name", "contact", "party_size", "time"]
|
| 389 |
+
if all(st.session_state.user_data.get(k) is not None for k in required_keys):
|
| 390 |
+
booking_conn = None
|
| 391 |
+
try:
|
| 392 |
+
user_data = st.session_state.user_data
|
| 393 |
+
party_size = int(user_data["party_size"])
|
| 394 |
+
tables_needed = -(-party_size // 4)
|
| 395 |
+
|
| 396 |
+
booking_conn = sqlite3.connect("db/restaurant_reservation.db")
|
| 397 |
+
booking_cursor = booking_conn.cursor()
|
| 398 |
+
|
| 399 |
+
booking_cursor.execute("SELECT id FROM restaurants WHERE LOWER(name) = LOWER(?)", (user_data["restaurant_name"],))
|
| 400 |
+
restaurant_row = booking_cursor.fetchone()
|
| 401 |
+
if not restaurant_row:
|
| 402 |
+
raise Exception("Restaurant not found.")
|
| 403 |
+
restaurant_id = restaurant_row[0]
|
| 404 |
+
|
| 405 |
+
booking_cursor.execute("""
|
| 406 |
+
SELECT t.id AS table_id, s.id AS slot_id
|
| 407 |
+
FROM tables t
|
| 408 |
+
JOIN slots s ON t.id = s.table_id
|
| 409 |
+
WHERE t.restaurant_id = ?
|
| 410 |
+
AND s.hour = ?
|
| 411 |
+
AND s.date = '2025-05-12'
|
| 412 |
+
AND s.is_reserved = 0
|
| 413 |
+
LIMIT ?
|
| 414 |
+
""", (restaurant_id, user_data["time"], tables_needed))
|
| 415 |
+
available = booking_cursor.fetchall()
|
| 416 |
+
# Debugging output
|
| 417 |
+
|
| 418 |
+
if len(available) < tables_needed:
|
| 419 |
+
raise Exception("Not enough available tables.")
|
| 420 |
+
|
| 421 |
+
booking_cursor.execute("""
|
| 422 |
+
INSERT INTO reservations (restaurant_id, user_name, contact, date, time, party_size)
|
| 423 |
+
VALUES (?, ?, ?, '2025-05-12', ?, ?)
|
| 424 |
+
""", (restaurant_id, user_data["user_name"], user_data["contact"], user_data["time"], party_size))
|
| 425 |
+
reservation_id = booking_cursor.lastrowid
|
| 426 |
+
|
| 427 |
+
for table_id, _ in available:
|
| 428 |
+
booking_cursor.execute("INSERT INTO reservation_tables (reservation_id, table_id) VALUES (?, ?)", (reservation_id, table_id))
|
| 429 |
+
|
| 430 |
+
slot_ids = [slot_id for _, slot_id in available]
|
| 431 |
+
booking_cursor.executemany("UPDATE slots SET is_reserved = 1 WHERE id = ?", [(sid,) for sid in slot_ids])
|
| 432 |
+
|
| 433 |
+
booking_conn.commit()
|
| 434 |
+
# Fetch the restaurant name to confirm
|
| 435 |
+
booking_cursor.execute("SELECT name FROM restaurants WHERE id = ?", (restaurant_id,))
|
| 436 |
+
restaurant_name = booking_cursor.fetchone()[0]
|
| 437 |
+
|
| 438 |
+
# Prepare confirmation details
|
| 439 |
+
confirmation_msg = (
|
| 440 |
+
f"β
Booking processed successfully!\n\n"
|
| 441 |
+
f"π Restaurant: **{restaurant_name}**\n"
|
| 442 |
+
f"β° Time: **{user_data['time']} on 2025-05-12**\n"
|
| 443 |
+
f"π½οΈ Tables Booked: **{tables_needed}**\n"
|
| 444 |
+
f"π Reservation ID: **{reservation_id}**\n\n"
|
| 445 |
+
f"π Please mention this Reservation ID at the restaurant reception when you arrive."
|
| 446 |
+
)
|
| 447 |
+
|
| 448 |
+
response_summary = confirmation_msg
|
| 449 |
+
st.success(response_summary)
|
| 450 |
+
st.session_state.chat_history.append({'role': 'assistant', 'message': response_summary})
|
| 451 |
+
response_summary="β
Booking processed successfully."
|
| 452 |
+
st.session_state.user_data["restaurant_name"]=None
|
| 453 |
+
st.session_state.user_data["party_size"]=None
|
| 454 |
+
st.session_state.user_data["time"]=None
|
| 455 |
+
st.session_state.last_assistant_reply=""
|
| 456 |
+
except Exception as e:
|
| 457 |
+
if booking_conn:
|
| 458 |
+
booking_conn.rollback()
|
| 459 |
+
response_summary = f"β Booking failed: {e}"
|
| 460 |
+
st.error(response_summary)
|
| 461 |
+
finally:
|
| 462 |
+
if booking_conn:
|
| 463 |
+
booking_cursor=None
|
| 464 |
+
booking_conn.close()
|
| 465 |
+
else:
|
| 466 |
+
st.markdown("β οΈ Missing user information. Please provide all booking details first.")
|
| 467 |
+
response_summary = "β οΈ Missing user information. Please provide all booking details first."
|
| 468 |
+
|
| 469 |
+
|
| 470 |
+
elif intent == "GREET":
|
| 471 |
+
response_summary = "π Hello! How can I help you with your restaurant reservation today?"
|
| 472 |
+
|
| 473 |
+
elif intent == "RUBBISH":
|
| 474 |
+
response_summary = "β Sorry, I didn't understand that. Could you rephrase your request?"
|
| 475 |
+
|
| 476 |
+
# Generate assistant reply
|
| 477 |
+
if response_summary!="β
Booking processed successfully.":
|
| 478 |
+
follow_up = generate_reservation_conversation(
|
| 479 |
+
user_input,
|
| 480 |
+
history_prompt,
|
| 481 |
+
response_summary or "Info stored.",
|
| 482 |
+
json.dumps(st.session_state.user_data),generate_reservation_conversation_prompt,client
|
| 483 |
+
)
|
| 484 |
+
else:
|
| 485 |
+
follow_up="Thanks for booking with FoodieSpot restaurant chain, I could assist you in new booking, also I could tell about restaurant features, pricing, etc... "
|
| 486 |
+
|
| 487 |
+
# Show assistant reply instantly
|
| 488 |
+
with st.chat_message("assistant"):
|
| 489 |
+
st.markdown(follow_up)
|
| 490 |
+
|
| 491 |
+
st.session_state.chat_history.append({'role': 'assistant', 'message': follow_up})
|
| 492 |
+
# Update it after assistant speaks
|
| 493 |
+
st.session_state.last_assistant_reply = follow_up
|
| 494 |
+
st.rerun()
|
| 495 |
+
# Reset if booking done
|
| 496 |
+
|
| 497 |
+
|
|
|
|
|
|
|
|
|