Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
|
@@ -2,6 +2,8 @@ import streamlit as st
|
|
| 2 |
import sqlite3
|
| 3 |
import os
|
| 4 |
import ast
|
|
|
|
|
|
|
| 5 |
from langchain.agents import create_sql_agent, initialize_agent, Tool
|
| 6 |
from langchain_core.messages import SystemMessage, HumanMessage
|
| 7 |
from langchain.agents.agent_types import AgentType
|
|
@@ -14,7 +16,7 @@ warnings.filterwarnings("ignore", category=DeprecationWarning)
|
|
| 14 |
|
| 15 |
# Remove any Colab-specific code
|
| 16 |
# Streamlit uses its own secrets management
|
| 17 |
-
|
| 18 |
# Page Configuration
|
| 19 |
st.set_page_config(
|
| 20 |
page_title="Food Delivery Chatbot",
|
|
@@ -78,6 +80,138 @@ if 'db_agent' not in st.session_state:
|
|
| 78 |
st.session_state.db_agent = None
|
| 79 |
if 'llm' not in st.session_state:
|
| 80 |
st.session_state.llm = None
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 81 |
|
| 82 |
# Database setup
|
| 83 |
@st.cache_resource
|
|
@@ -487,6 +621,110 @@ def chatbot_response(cust_id: str, user_query: str) -> str:
|
|
| 487 |
|
| 488 |
return final_llm_response
|
| 489 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 490 |
# Streamlit UI
|
| 491 |
st.markdown(
|
| 492 |
'<h1 class="main-header" style="font-weight: normal; font-family: Century, sans-serif;">π΅ FoodHub - AI Chatbot π€</h1>',
|
|
@@ -611,3 +849,4 @@ st.markdown(
|
|
| 611 |
'<div style="text-align: center; color: #888;">π Thank you for choosing FoodHub! π½οΈ</div>',
|
| 612 |
unsafe_allow_html=True
|
| 613 |
)
|
|
|
|
|
|
| 2 |
import sqlite3
|
| 3 |
import os
|
| 4 |
import ast
|
| 5 |
+
import re
|
| 6 |
+
import base64
|
| 7 |
from langchain.agents import create_sql_agent, initialize_agent, Tool
|
| 8 |
from langchain_core.messages import SystemMessage, HumanMessage
|
| 9 |
from langchain.agents.agent_types import AgentType
|
|
|
|
| 16 |
|
| 17 |
# Remove any Colab-specific code
|
| 18 |
# Streamlit uses its own secrets management
|
| 19 |
+
'''
|
| 20 |
# Page Configuration
|
| 21 |
st.set_page_config(
|
| 22 |
page_title="Food Delivery Chatbot",
|
|
|
|
| 80 |
st.session_state.db_agent = None
|
| 81 |
if 'llm' not in st.session_state:
|
| 82 |
st.session_state.llm = None
|
| 83 |
+
'''
|
| 84 |
+
# --- App Configuration ---
|
| 85 |
+
st.set_page_config(page_title="FoodHub Chatbot", page_icon="π½οΈ", layout="wide")
|
| 86 |
+
|
| 87 |
+
# --- Load Local JPG Background Image ---
|
| 88 |
+
def get_base64_image(image_path):
|
| 89 |
+
with open(image_path, "rb") as img_file:
|
| 90 |
+
encoded = base64.b64encode(img_file.read()).decode()
|
| 91 |
+
return encoded
|
| 92 |
+
|
| 93 |
+
image_base64 = get_base64_image("foodhub_background_jpg.jpg") # Make sure this file exists
|
| 94 |
+
|
| 95 |
+
# --- Session State Initialization ---
|
| 96 |
+
if "authenticated" not in st.session_state:
|
| 97 |
+
st.session_state.authenticated = False
|
| 98 |
+
if "customer_id" not in st.session_state:
|
| 99 |
+
st.session_state.customer_id = None
|
| 100 |
+
if "clear_input" not in st.session_state:
|
| 101 |
+
st.session_state.clear_input = False
|
| 102 |
+
if "chat_history" not in st.session_state:
|
| 103 |
+
st.session_state.chat_history = []
|
| 104 |
+
|
| 105 |
+
if not st.session_state.authenticated:
|
| 106 |
+
# --- Inject CSS for Background ---
|
| 107 |
+
st.markdown(
|
| 108 |
+
f"""
|
| 109 |
+
<style>
|
| 110 |
+
.stApp {{
|
| 111 |
+
background-image: url("data:image/jpeg;base64,{image_base64}");
|
| 112 |
+
background-size: cover;
|
| 113 |
+
background-repeat: no-repeat;
|
| 114 |
+
background-attachment: fixed;
|
| 115 |
+
}}
|
| 116 |
+
</style>
|
| 117 |
+
""",
|
| 118 |
+
unsafe_allow_html=True
|
| 119 |
+
)
|
| 120 |
+
else:
|
| 121 |
+
# Clear background after login
|
| 122 |
+
st.markdown(f"""
|
| 123 |
+
<style>
|
| 124 |
+
/* Remove background from stApp so blurred layer shows through */
|
| 125 |
+
.stApp {{
|
| 126 |
+
background-image: none !important;
|
| 127 |
+
background-color: transparent !important;
|
| 128 |
+
}}
|
| 129 |
+
.blurred-bg {{
|
| 130 |
+
position: fixed;
|
| 131 |
+
top: 0;
|
| 132 |
+
left: 0;
|
| 133 |
+
width: 100vw;
|
| 134 |
+
height: 100vh;
|
| 135 |
+
background-image: url("data:image/jpeg;base64,{image_base64}");
|
| 136 |
+
background-size: cover;
|
| 137 |
+
background-repeat: no-repeat;
|
| 138 |
+
background-attachment: fixed;
|
| 139 |
+
z-index: -1;
|
| 140 |
+
filter: blur(6px);
|
| 141 |
+
}}
|
| 142 |
+
</style>
|
| 143 |
+
<div class="blurred-bg"></div>
|
| 144 |
+
""",
|
| 145 |
+
unsafe_allow_html=True
|
| 146 |
+
)
|
| 147 |
+
|
| 148 |
+
# --- Simple validator ---
|
| 149 |
+
def extract_cust_id(text: str):
|
| 150 |
+
"""Return cust_id in format C#### or None"""
|
| 151 |
+
m = re.search(r"\b(C\d{4})\b", text, flags=re.I)
|
| 152 |
+
return m.group(1).upper() if m else None
|
| 153 |
+
|
| 154 |
+
# --- Validate customer ID using direct SQL ---
|
| 155 |
+
def is_valid_customer(customer_id: str) -> bool:
|
| 156 |
+
cust_id = extract_cust_id(customer_id)
|
| 157 |
+
if not cust_id:
|
| 158 |
+
return True
|
| 159 |
+
|
| 160 |
+
try:
|
| 161 |
+
# Connect to your database
|
| 162 |
+
conn = sqlite3.connect("customer_orders.db") # Replace with your actual DB connection
|
| 163 |
+
cursor = conn.cursor()
|
| 164 |
+
|
| 165 |
+
# Run a simple query to check existence
|
| 166 |
+
cursor.execute("SELECT 1 FROM orders WHERE cust_id = ?", (cust_id,))
|
| 167 |
+
result = cursor.fetchone()
|
| 168 |
+
|
| 169 |
+
conn.close()
|
| 170 |
+
return result is not None
|
| 171 |
+
|
| 172 |
+
except Exception as e:
|
| 173 |
+
print(f"Database error: {e}")
|
| 174 |
+
return True
|
| 175 |
+
|
| 176 |
+
if not st.session_state.authenticated:
|
| 177 |
+
# Login form
|
| 178 |
+
col1, col2 = st.columns([2, 2]) # Adjust ratios for desired spacing
|
| 179 |
+
|
| 180 |
+
with col2:
|
| 181 |
+
col1, col2 = st.columns([1, 4])
|
| 182 |
+
with col1:
|
| 183 |
+
st.image("foodhub_logo.png", width=500)
|
| 184 |
+
with col2:
|
| 185 |
+
st.markdown("<h1 style='color: #ff4b4b; padding-top: 10px;'>Welcome to FoodHub Chatbot</h1>", unsafe_allow_html=True)
|
| 186 |
+
# Instructional message in black
|
| 187 |
+
st.markdown("<p style='color: black; font-size: 16px;'>Please enter customer ID and password to continue</p>", unsafe_allow_html=True)
|
| 188 |
+
with st.form("login_form"):
|
| 189 |
+
|
| 190 |
+
# Labels in black using label_visibility workaround
|
| 191 |
+
#st.markdown("<label style='color: black;'>Customer ID</label>", unsafe_allow_html=True)
|
| 192 |
+
customer_id = st.text_input("Customer ID", placeholder="eg: C1018")
|
| 193 |
+
|
| 194 |
+
#st.markdown("<label style='color: black;'>Password</label>", unsafe_allow_html=True)
|
| 195 |
+
password = st.text_input("Password", type="password")
|
| 196 |
+
|
| 197 |
+
submitted = st.form_submit_button("Login")
|
| 198 |
+
|
| 199 |
+
print('password submitted by user : ',password, flush=True)
|
| 200 |
+
sys.stdout.flush()
|
| 201 |
+
|
| 202 |
+
if submitted:
|
| 203 |
+
# Add your login logic here
|
| 204 |
+
if is_valid_customer(customer_id) and password == "foodhub123":
|
| 205 |
+
st.session_state.authenticated = True
|
| 206 |
+
st.session_state.customer_id = customer_id
|
| 207 |
+
|
| 208 |
+
print('user {customer_id} successfully authenticated!!', flush=True)
|
| 209 |
+
sys.stdout.flush()
|
| 210 |
+
|
| 211 |
+
st.rerun()
|
| 212 |
+
else:
|
| 213 |
+
st.error("Invalid credentials. Please try again.")
|
| 214 |
+
|
| 215 |
|
| 216 |
# Database setup
|
| 217 |
@st.cache_resource
|
|
|
|
| 621 |
|
| 622 |
return final_llm_response
|
| 623 |
|
| 624 |
+
# --- Chatbot Interface ---
|
| 625 |
+
if st.session_state.authenticated:
|
| 626 |
+
customer_id = st.session_state.get("customer_id")
|
| 627 |
+
# Ensure chat history
|
| 628 |
+
if not st.session_state.chat_history:
|
| 629 |
+
st.session_state["chat_history"] = [
|
| 630 |
+
{"role": "assistant", "content": f"Hi! How can I help you today?"}
|
| 631 |
+
]
|
| 632 |
+
spacer_left, chat_col = st.columns([2, 2])
|
| 633 |
+
|
| 634 |
+
with chat_col:
|
| 635 |
+
col1, col2 = st.columns([2, 2])
|
| 636 |
+
with col1:
|
| 637 |
+
st.image("foodhub_logo.png", width=100)
|
| 638 |
+
with col2:
|
| 639 |
+
st.markdown(
|
| 640 |
+
f"""
|
| 641 |
+
<div style='display: flex; align-items: center; justify-content: flex-start; height: 100%;'>
|
| 642 |
+
<h1 style='color: #ff4b4b; margin: 0;'>Hey {customer_id}, Welcome!</h1>
|
| 643 |
+
</div>
|
| 644 |
+
""",
|
| 645 |
+
unsafe_allow_html=True
|
| 646 |
+
)
|
| 647 |
+
|
| 648 |
+
st.markdown("---")
|
| 649 |
+
|
| 650 |
+
# Inject custom CSS for chat bubbles
|
| 651 |
+
st.markdown("""
|
| 652 |
+
<style>
|
| 653 |
+
.chat-bubble-user {
|
| 654 |
+
background-color: #dfe6e9; /* soft gray-blue, neutral on light/dark */
|
| 655 |
+
color: #000000; /* black text */
|
| 656 |
+
padding: 10px 14px;
|
| 657 |
+
border-radius: 12px;
|
| 658 |
+
margin-bottom: 6px;
|
| 659 |
+
display: inline-block;
|
| 660 |
+
max-width: 80%;
|
| 661 |
+
text-align: right;
|
| 662 |
+
}
|
| 663 |
+
.chat-bubble-bot {
|
| 664 |
+
background-color: #f1f0f0; /* light gray, works on both themes */
|
| 665 |
+
color: #000000; /* black text */
|
| 666 |
+
padding: 10px 14px;
|
| 667 |
+
border-radius: 12px;
|
| 668 |
+
margin-bottom: 6px;
|
| 669 |
+
display: inline-block;
|
| 670 |
+
max-width: 80%;
|
| 671 |
+
text-align: left;
|
| 672 |
+
}
|
| 673 |
+
</style>
|
| 674 |
+
""", unsafe_allow_html=True)
|
| 675 |
+
|
| 676 |
+
# Chat rendering
|
| 677 |
+
for m in st.session_state.chat_history:
|
| 678 |
+
left_col, right_col = st.columns([1, 1])
|
| 679 |
+
|
| 680 |
+
if m["role"] == "user":
|
| 681 |
+
with right_col:
|
| 682 |
+
st.markdown(
|
| 683 |
+
f"""
|
| 684 |
+
<div style='display: flex; justify-content: flex-end; align-items: center; margin-bottom: 8px;'>
|
| 685 |
+
<div class='chat-bubble-user'>{m['content']}</div>
|
| 686 |
+
<div style='font-size: 20px; margin-left: 8px;'>π</div>
|
| 687 |
+
</div>
|
| 688 |
+
""",
|
| 689 |
+
unsafe_allow_html=True
|
| 690 |
+
)
|
| 691 |
+
|
| 692 |
+
else:
|
| 693 |
+
with left_col:
|
| 694 |
+
st.markdown(
|
| 695 |
+
f"""
|
| 696 |
+
<div style='display: flex; justify-content: flex-start; align-items: center; margin-bottom: 8px;'>
|
| 697 |
+
<div style='font-size: 20px; margin-right: 8px;'>π€</div>
|
| 698 |
+
<div class='chat-bubble-bot'>{m['content']}</div>
|
| 699 |
+
</div>
|
| 700 |
+
""",
|
| 701 |
+
unsafe_allow_html=True
|
| 702 |
+
)
|
| 703 |
+
|
| 704 |
+
# 2) input β append β bot β append β rerun
|
| 705 |
+
user_input = st.chat_input("Ask about your order or menu...")
|
| 706 |
+
if user_input:
|
| 707 |
+
st.session_state.chat_history.append({"role":"user","content":user_input})
|
| 708 |
+
with st.spinner("Let me check that for you..."):
|
| 709 |
+
|
| 710 |
+
# Step 2: Refine response using Answer Tool
|
| 711 |
+
final_response = chatbot_response(customer_id, user_input)
|
| 712 |
+
|
| 713 |
+
print('Output of chatbot_response:',final_response, flush=True)
|
| 714 |
+
sys.stdout.flush()
|
| 715 |
+
|
| 716 |
+
st.session_state.chat_history.append({"role":"assistant","content":final_response})
|
| 717 |
+
st.rerun()
|
| 718 |
+
|
| 719 |
+
st.markdown("<div style='text-align: right; padding-top: 10px;'>", unsafe_allow_html=True)
|
| 720 |
+
if st.button("Logout"):
|
| 721 |
+
st.session_state.authenticated = False
|
| 722 |
+
st.session_state.customer_id = None
|
| 723 |
+
st.session_state.chat_history = []
|
| 724 |
+
st.rerun()
|
| 725 |
+
st.markdown("</div>", unsafe_allow_html=True)
|
| 726 |
+
|
| 727 |
+
'''
|
| 728 |
# Streamlit UI
|
| 729 |
st.markdown(
|
| 730 |
'<h1 class="main-header" style="font-weight: normal; font-family: Century, sans-serif;">π΅ FoodHub - AI Chatbot π€</h1>',
|
|
|
|
| 849 |
'<div style="text-align: center; color: #888;">π Thank you for choosing FoodHub! π½οΈ</div>',
|
| 850 |
unsafe_allow_html=True
|
| 851 |
)
|
| 852 |
+
'''
|