Spaces:
Sleeping
Sleeping
Update src/chat.py
Browse files- src/chat.py +54 -82
src/chat.py
CHANGED
|
@@ -542,11 +542,18 @@ def save_chat_to_db(conn, user_id, user_message, bot_reply,
|
|
| 542 |
# CHAT UI
|
| 543 |
# =========================
|
| 544 |
def render_chat_tab(conn):
|
| 545 |
-
"""
|
| 546 |
-
|
| 547 |
-
|
| 548 |
-
|
| 549 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 550 |
if "history" not in st.session_state:
|
| 551 |
st.session_state.history = []
|
| 552 |
if "clear_input" not in st.session_state:
|
|
@@ -554,67 +561,27 @@ def render_chat_tab(conn):
|
|
| 554 |
if "user_input" not in st.session_state:
|
| 555 |
st.session_state.user_input = ""
|
| 556 |
|
| 557 |
-
# Clear input after
|
| 558 |
if st.session_state.clear_input:
|
| 559 |
st.session_state.user_input = ""
|
| 560 |
st.session_state.clear_input = False
|
| 561 |
|
| 562 |
-
# ------- Input + Submit
|
| 563 |
-
# This avoids stray widgets and keeps layout tidy
|
| 564 |
with st.container(border=True):
|
| 565 |
-
st.caption("You:")
|
| 566 |
with st.form("chat_form", clear_on_submit=False):
|
| 567 |
-
# Single visible input
|
| 568 |
_ = st.text_input(
|
| 569 |
label="You:",
|
| 570 |
key="user_input",
|
| 571 |
-
label_visibility="
|
| 572 |
)
|
| 573 |
-
cols = st.columns([1,
|
| 574 |
with cols[-1]:
|
| 575 |
submitted = st.form_submit_button("Submit", use_container_width=True)
|
| 576 |
|
| 577 |
-
#
|
| 578 |
-
show_hist = st.checkbox("Show conversation history (this session)", value=False)
|
| 579 |
|
| 580 |
-
|
| 581 |
-
st.markdown("## Conversation")
|
| 582 |
-
for entry in st.session_state.history:
|
| 583 |
-
st.markdown(
|
| 584 |
-
f"""
|
| 585 |
-
<div class="msg-user">
|
| 586 |
-
<div class="msg-meta">{entry['timestamp']}</div>
|
| 587 |
-
<b>Customer:</b> {entry['user']}
|
| 588 |
-
</div>
|
| 589 |
-
""",
|
| 590 |
-
unsafe_allow_html=True,
|
| 591 |
-
)
|
| 592 |
-
|
| 593 |
-
st.markdown(
|
| 594 |
-
f"""
|
| 595 |
-
<div class="analysis-card">
|
| 596 |
-
<b>Platform Analysis:</b><br>
|
| 597 |
-
<b>Intent:</b> {entry['intent']}<br>
|
| 598 |
-
<b>Sentiment:</b> {entry['sentiment']}<br>
|
| 599 |
-
<b>Resolution:</b> {entry['resolution']}<br>
|
| 600 |
-
<b>Latency:</b> {entry['ftr_ms']} ms
|
| 601 |
-
</div>
|
| 602 |
-
""",
|
| 603 |
-
unsafe_allow_html=True,
|
| 604 |
-
)
|
| 605 |
-
|
| 606 |
-
st.markdown(
|
| 607 |
-
f"""
|
| 608 |
-
<div class="msg-bot">
|
| 609 |
-
<div class="msg-meta">{entry['timestamp']}</div>
|
| 610 |
-
<b>Support Agent:</b> {entry['reply']}
|
| 611 |
-
</div>
|
| 612 |
-
""",
|
| 613 |
-
unsafe_allow_html=True,
|
| 614 |
-
)
|
| 615 |
-
|
| 616 |
-
|
| 617 |
-
# ------- On submit, keep your logic exactly as before -------
|
| 618 |
if submitted:
|
| 619 |
if st.session_state.user_input.strip():
|
| 620 |
user_text = st.session_state.user_input
|
|
@@ -666,7 +633,7 @@ def render_chat_tab(conn):
|
|
| 666 |
|
| 667 |
ftr_ms = round((time.time() - start_time) * 1000, 2)
|
| 668 |
|
| 669 |
-
# 4) show + save
|
| 670 |
entry = {
|
| 671 |
"timestamp": current_time,
|
| 672 |
"user": user_text,
|
|
@@ -682,6 +649,7 @@ def render_chat_tab(conn):
|
|
| 682 |
if len(st.session_state.history) > 50:
|
| 683 |
st.session_state.history.pop(0)
|
| 684 |
|
|
|
|
| 685 |
uid = st.session_state["user"]["id"]
|
| 686 |
try:
|
| 687 |
save_chat_to_db(
|
|
@@ -694,27 +662,35 @@ def render_chat_tab(conn):
|
|
| 694 |
resolution=resolution,
|
| 695 |
ftr_ms=ftr_ms,
|
| 696 |
sql_query=sql_query,
|
| 697 |
-
sql_params=sql_params if intent in INTENTS else {}
|
| 698 |
)
|
| 699 |
except Exception as e:
|
| 700 |
logger.exception("Failed to save chat: %s", e)
|
| 701 |
|
|
|
|
| 702 |
st.session_state.clear_input = True
|
| 703 |
st.rerun()
|
| 704 |
|
| 705 |
-
# ------- Conversation stream
|
| 706 |
-
|
| 707 |
-
if st.session_state.history:
|
|
|
|
|
|
|
| 708 |
for h in st.session_state.history:
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 709 |
# Customer bubble
|
| 710 |
st.markdown(
|
| 711 |
f"""
|
| 712 |
-
<div class="msg
|
| 713 |
-
<div class="msg-
|
| 714 |
-
|
| 715 |
-
<span>{h['timestamp']}</span>
|
| 716 |
-
</div>
|
| 717 |
-
<div>{h['user']}</div>
|
| 718 |
</div>
|
| 719 |
""",
|
| 720 |
unsafe_allow_html=True,
|
|
@@ -722,29 +698,25 @@ def render_chat_tab(conn):
|
|
| 722 |
|
| 723 |
# Analysis card
|
| 724 |
with st.container(border=True):
|
| 725 |
-
|
| 726 |
-
|
| 727 |
-
|
| 728 |
-
|
| 729 |
-
|
| 730 |
-
|
| 731 |
-
|
| 732 |
-
|
| 733 |
-
|
| 734 |
-
|
| 735 |
-
|
| 736 |
-
|
| 737 |
-
st.write(f"**Latency:** {h['ftr_ms']} ms")
|
| 738 |
|
| 739 |
# Bot bubble
|
| 740 |
st.markdown(
|
| 741 |
f"""
|
| 742 |
-
<div class="msg
|
| 743 |
-
<div class="msg-
|
| 744 |
-
|
| 745 |
-
<span>{h['timestamp']}</span>
|
| 746 |
-
</div>
|
| 747 |
-
<div>{h['reply']}</div>
|
| 748 |
</div>
|
| 749 |
""",
|
| 750 |
unsafe_allow_html=True,
|
|
|
|
| 542 |
# CHAT UI
|
| 543 |
# =========================
|
| 544 |
def render_chat_tab(conn):
|
| 545 |
+
"""
|
| 546 |
+
Full Ecomm chatbot with intents + SQL, scoped to current user.
|
| 547 |
+
Business logic unchanged; UI tweaked so history shows only when checkbox is ticked.
|
| 548 |
+
"""
|
| 549 |
+
import time
|
| 550 |
+
from datetime import datetime
|
| 551 |
+
import streamlit as st
|
| 552 |
+
|
| 553 |
+
# ---------- Title ----------
|
| 554 |
+
st.title("💬 Olist E‑commerce Chatbot")
|
| 555 |
+
|
| 556 |
+
# ---------- Session state ----------
|
| 557 |
if "history" not in st.session_state:
|
| 558 |
st.session_state.history = []
|
| 559 |
if "clear_input" not in st.session_state:
|
|
|
|
| 561 |
if "user_input" not in st.session_state:
|
| 562 |
st.session_state.user_input = ""
|
| 563 |
|
| 564 |
+
# Clear input after successful submit
|
| 565 |
if st.session_state.clear_input:
|
| 566 |
st.session_state.user_input = ""
|
| 567 |
st.session_state.clear_input = False
|
| 568 |
|
| 569 |
+
# ---------- Input + Submit (single form) ----------
|
|
|
|
| 570 |
with st.container(border=True):
|
|
|
|
| 571 |
with st.form("chat_form", clear_on_submit=False):
|
|
|
|
| 572 |
_ = st.text_input(
|
| 573 |
label="You:",
|
| 574 |
key="user_input",
|
| 575 |
+
label_visibility="visible",
|
| 576 |
)
|
| 577 |
+
cols = st.columns([1, 0.25])
|
| 578 |
with cols[-1]:
|
| 579 |
submitted = st.form_submit_button("Submit", use_container_width=True)
|
| 580 |
|
| 581 |
+
# History toggle lives outside the form, in the same card
|
| 582 |
+
show_hist = st.checkbox("Show conversation history (this session)", key="chat_show_hist", value=False)
|
| 583 |
|
| 584 |
+
# ---------- Submit handling (BUSINESS LOGIC UNCHANGED) ----------
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 585 |
if submitted:
|
| 586 |
if st.session_state.user_input.strip():
|
| 587 |
user_text = st.session_state.user_input
|
|
|
|
| 633 |
|
| 634 |
ftr_ms = round((time.time() - start_time) * 1000, 2)
|
| 635 |
|
| 636 |
+
# 4) show + save (append to in-memory history)
|
| 637 |
entry = {
|
| 638 |
"timestamp": current_time,
|
| 639 |
"user": user_text,
|
|
|
|
| 649 |
if len(st.session_state.history) > 50:
|
| 650 |
st.session_state.history.pop(0)
|
| 651 |
|
| 652 |
+
# persist with user_id
|
| 653 |
uid = st.session_state["user"]["id"]
|
| 654 |
try:
|
| 655 |
save_chat_to_db(
|
|
|
|
| 662 |
resolution=resolution,
|
| 663 |
ftr_ms=ftr_ms,
|
| 664 |
sql_query=sql_query,
|
| 665 |
+
sql_params=sql_params if intent in INTENTS else {}
|
| 666 |
)
|
| 667 |
except Exception as e:
|
| 668 |
logger.exception("Failed to save chat: %s", e)
|
| 669 |
|
| 670 |
+
# clear input then rerun
|
| 671 |
st.session_state.clear_input = True
|
| 672 |
st.rerun()
|
| 673 |
|
| 674 |
+
# ---------- Conversation stream ----------
|
| 675 |
+
# Only render if the checkbox is checked
|
| 676 |
+
if show_hist and st.session_state.history:
|
| 677 |
+
st.subheader("Conversation")
|
| 678 |
+
|
| 679 |
for h in st.session_state.history:
|
| 680 |
+
ts = h.get("timestamp", "")
|
| 681 |
+
|
| 682 |
+
# Optional result table (if present) — above bot reply
|
| 683 |
+
if h.get("table_rows") and h.get("table_cols"):
|
| 684 |
+
with st.container(border=True):
|
| 685 |
+
st.caption(f"Query results • {ts}")
|
| 686 |
+
st.dataframe(h["table_rows"], use_container_width=True)
|
| 687 |
+
|
| 688 |
# Customer bubble
|
| 689 |
st.markdown(
|
| 690 |
f"""
|
| 691 |
+
<div class="msg-user">
|
| 692 |
+
<div class="msg-meta">{ts}</div>
|
| 693 |
+
<b>Customer:</b> {h.get('user','').replace('\n','<br>')}
|
|
|
|
|
|
|
|
|
|
| 694 |
</div>
|
| 695 |
""",
|
| 696 |
unsafe_allow_html=True,
|
|
|
|
| 698 |
|
| 699 |
# Analysis card
|
| 700 |
with st.container(border=True):
|
| 701 |
+
st.markdown(
|
| 702 |
+
f"""
|
| 703 |
+
<div class="analysis-card">
|
| 704 |
+
<b>Platform Analysis</b><br>
|
| 705 |
+
<b>Intent:</b> {h.get('intent','—')}<br>
|
| 706 |
+
<b>Resolution:</b> {h.get('resolution','—')}<br>
|
| 707 |
+
<b>Sentiment:</b> {h.get('sentiment','—')}<br>
|
| 708 |
+
<b>Latency:</b> {h.get('ftr_ms','—')} ms
|
| 709 |
+
</div>
|
| 710 |
+
""",
|
| 711 |
+
unsafe_allow_html=True,
|
| 712 |
+
)
|
|
|
|
| 713 |
|
| 714 |
# Bot bubble
|
| 715 |
st.markdown(
|
| 716 |
f"""
|
| 717 |
+
<div class="msg-bot">
|
| 718 |
+
<div class="msg-meta">{ts}</div>
|
| 719 |
+
<b>Support Agent:</b> {h.get('reply','').replace('\n','<br>')}
|
|
|
|
|
|
|
|
|
|
| 720 |
</div>
|
| 721 |
""",
|
| 722 |
unsafe_allow_html=True,
|