Spaces:
Sleeping
Sleeping
Update src/chat.py
Browse files- src/chat.py +13 -74
src/chat.py
CHANGED
|
@@ -554,8 +554,8 @@ def render_chat_tab(conn):
|
|
| 554 |
ss.clear_input = False
|
| 555 |
if "user_input" not in ss:
|
| 556 |
ss.user_input = ""
|
| 557 |
-
if "
|
| 558 |
-
ss.
|
| 559 |
|
| 560 |
# Clear text box after a successful submit
|
| 561 |
if ss.clear_input:
|
|
@@ -564,7 +564,7 @@ def render_chat_tab(conn):
|
|
| 564 |
|
| 565 |
# ---------- Input row ----------
|
| 566 |
user_input = st.text_input("You:", key="user_input")
|
| 567 |
-
st.checkbox("Show conversation history (this session)", key="
|
| 568 |
|
| 569 |
# ---------- On submit: run intent/SQL pipeline ----------
|
| 570 |
if st.button("Submit"):
|
|
@@ -574,14 +574,14 @@ def render_chat_tab(conn):
|
|
| 574 |
current_time = datetime.now().strftime("%Y-%m-%d %H:%M:%S")
|
| 575 |
#Translate to English if needed (Hindi input)
|
| 576 |
try:
|
| 577 |
-
text_en = ensure_english(
|
| 578 |
except Exception as e:
|
| 579 |
logging.warning("Exception in ensure_english: fallback to original due to: %s", e)
|
| 580 |
try:
|
| 581 |
-
src_lang = detect_lang(
|
| 582 |
except Exception as e:
|
| 583 |
logging.warning("Exception in detect_lang: fallback to original due to: %s", e)
|
| 584 |
-
logger.info(f"Translated Text ={text_en} Source Language = {src_lang}")
|
| 585 |
|
| 586 |
# 1) intent + sentiment
|
| 587 |
cls = gpt_classify_intent(text_en)
|
|
@@ -635,7 +635,8 @@ def render_chat_tab(conn):
|
|
| 635 |
except Exception as e:
|
| 636 |
logger.exception("Failed to detect langauge: %s", e)
|
| 637 |
src_lang == ENG
|
| 638 |
-
|
|
|
|
| 639 |
if src_lang == HINDI:
|
| 640 |
try:
|
| 641 |
display_reply = en_to_lang(bot_reply)
|
|
@@ -643,7 +644,8 @@ def render_chat_tab(conn):
|
|
| 643 |
except Exception:
|
| 644 |
logging.warning("Exception in en_to_lang due to: %s", e)
|
| 645 |
display_reply = bot_reply
|
| 646 |
-
|
|
|
|
| 647 |
# 4) add entry to session history
|
| 648 |
entry = {
|
| 649 |
"timestamp": current_time,
|
|
@@ -678,7 +680,6 @@ def render_chat_tab(conn):
|
|
| 678 |
except Exception as e:
|
| 679 |
logger.exception("Failed to save chat: %s", e)
|
| 680 |
|
| 681 |
-
# Prepare for clean input and display latest turn on next render
|
| 682 |
ss.clear_input = True
|
| 683 |
try:
|
| 684 |
st.rerun()
|
|
@@ -686,9 +687,7 @@ def render_chat_tab(conn):
|
|
| 686 |
st.experimental_rerun()
|
| 687 |
|
| 688 |
# ---------- RENDER AREA ----------
|
| 689 |
-
# Helper to render one turn
|
| 690 |
def _render_turn(h):
|
| 691 |
-
# User bubble
|
| 692 |
st.markdown(
|
| 693 |
f"<div class='chat-bubble customer'>"
|
| 694 |
f"<div class='chat-meta'>{h['timestamp']}</div>"
|
|
@@ -696,7 +695,6 @@ def render_chat_tab(conn):
|
|
| 696 |
f"</div>",
|
| 697 |
unsafe_allow_html=True,
|
| 698 |
)
|
| 699 |
-
# Optional table (if any)
|
| 700 |
if h.get("table_cols") and h.get("table_rows"):
|
| 701 |
try:
|
| 702 |
df = pd.DataFrame(h["table_rows"], columns=h["table_cols"])
|
|
@@ -704,7 +702,6 @@ def render_chat_tab(conn):
|
|
| 704 |
except Exception:
|
| 705 |
pass
|
| 706 |
|
| 707 |
-
# Platform analysis block
|
| 708 |
with st.container():
|
| 709 |
st.markdown(
|
| 710 |
"<div class='card soft'>"
|
|
@@ -722,7 +719,6 @@ def render_chat_tab(conn):
|
|
| 722 |
st.write(f"**Latency:** {h['ftr_ms']:.2f} ms")
|
| 723 |
st.markdown("</div>", unsafe_allow_html=True)
|
| 724 |
|
| 725 |
-
# Bot reply bubble
|
| 726 |
st.markdown(
|
| 727 |
f"<div class='chat-bubble agent'>"
|
| 728 |
f"<div class='chat-meta'>{h['timestamp']}</div>"
|
|
@@ -731,20 +727,15 @@ def render_chat_tab(conn):
|
|
| 731 |
unsafe_allow_html=True,
|
| 732 |
)
|
| 733 |
|
| 734 |
-
|
| 735 |
-
|
| 736 |
-
latest = st.session_state.history[-1]
|
| 737 |
-
# Top-level (latest only)
|
| 738 |
_render_turn(latest)
|
| 739 |
|
| 740 |
-
# History toggle section
|
| 741 |
st.markdown("### Conversation")
|
| 742 |
-
if ss.
|
| 743 |
-
# Show earlier turns from newest -> oldest, excluding latest
|
| 744 |
for h in reversed(ss.history[:-1]):
|
| 745 |
_render_turn(h)
|
| 746 |
else:
|
| 747 |
-
# When history is off, show only a compact bubble of the latest user text
|
| 748 |
st.markdown(
|
| 749 |
f"<div class='chat-bubble customer compact'>"
|
| 750 |
f"<div class='chat-meta'>{latest['timestamp']}</div>"
|
|
@@ -754,58 +745,6 @@ def render_chat_tab(conn):
|
|
| 754 |
)
|
| 755 |
|
| 756 |
|
| 757 |
-
# renderer
|
| 758 |
-
def _render_turn(turn):
|
| 759 |
-
ts = turn["timestamp"]
|
| 760 |
-
user_text = turn["user"]
|
| 761 |
-
intent_display = turn["intent"] if turn["intent"] else "UNMAPPED"
|
| 762 |
-
sentiment = turn["sentiment"]
|
| 763 |
-
resolution = turn["resolution"]
|
| 764 |
-
bot_reply = turn["reply"]
|
| 765 |
-
table_cols = turn.get("table_cols")
|
| 766 |
-
table_rows = turn.get("table_rows")
|
| 767 |
-
|
| 768 |
-
st.markdown(
|
| 769 |
-
f"""
|
| 770 |
-
<div style='background-color:#e6f2ff;padding:10px;border-radius:10px;max-width:80%;margin-bottom:5px;'>
|
| 771 |
-
<div style='font-size:0.8em;color:gray;'>{ts}</div>
|
| 772 |
-
<b>Customer:</b> {user_text}
|
| 773 |
-
</div>
|
| 774 |
-
""",
|
| 775 |
-
unsafe_allow_html=True
|
| 776 |
-
)
|
| 777 |
-
st.markdown(
|
| 778 |
-
f"""
|
| 779 |
-
<div style='background-color:#ffffff;padding:10px;border-radius:10px;max-width:80%;margin-bottom:5px;'>
|
| 780 |
-
<b>Platform Analysis:</b><br>
|
| 781 |
-
<b>Intent:</b> {intent_display}<br>
|
| 782 |
-
<b>Sentiment:</b> {sentiment}<br>
|
| 783 |
-
<b>Resolution:</b> {resolution}
|
| 784 |
-
</div>
|
| 785 |
-
""",
|
| 786 |
-
unsafe_allow_html=True
|
| 787 |
-
)
|
| 788 |
-
if table_cols and table_rows:
|
| 789 |
-
df_result = pd.DataFrame(table_rows, columns=table_cols)
|
| 790 |
-
st.table(df_result)
|
| 791 |
-
st.markdown(
|
| 792 |
-
f"""
|
| 793 |
-
<div style='background-color:#e6ffe6;padding:10px;border-radius:10px;max-width:80%;margin-left:auto;'>
|
| 794 |
-
<div style='font-size:0.8em;color:gray;'>{ts}</div>
|
| 795 |
-
<b>Support Agent:</b> {bot_reply}
|
| 796 |
-
</div>
|
| 797 |
-
""",
|
| 798 |
-
unsafe_allow_html=True
|
| 799 |
-
)
|
| 800 |
-
|
| 801 |
-
if st.session_state.history:
|
| 802 |
-
st.subheader("Conversation")
|
| 803 |
-
if ss.show_history:
|
| 804 |
-
for turn in st.session_state.history[-50:]:
|
| 805 |
-
_render_turn(turn)
|
| 806 |
-
else:
|
| 807 |
-
_render_turn(st.session_state.history[-1])
|
| 808 |
-
|
| 809 |
def render_user_export_tab(conn, user_id: int):
|
| 810 |
st.subheader("Export My Chats")
|
| 811 |
df = pd.read_sql_query(
|
|
|
|
| 554 |
ss.clear_input = False
|
| 555 |
if "user_input" not in ss:
|
| 556 |
ss.user_input = ""
|
| 557 |
+
if "show_history" not in ss: # <-- consistent key
|
| 558 |
+
ss.show_history = False
|
| 559 |
|
| 560 |
# Clear text box after a successful submit
|
| 561 |
if ss.clear_input:
|
|
|
|
| 564 |
|
| 565 |
# ---------- Input row ----------
|
| 566 |
user_input = st.text_input("You:", key="user_input")
|
| 567 |
+
st.checkbox("Show conversation history (this session)", key="show_history") # <-- same key
|
| 568 |
|
| 569 |
# ---------- On submit: run intent/SQL pipeline ----------
|
| 570 |
if st.button("Submit"):
|
|
|
|
| 574 |
current_time = datetime.now().strftime("%Y-%m-%d %H:%M:%S")
|
| 575 |
#Translate to English if needed (Hindi input)
|
| 576 |
try:
|
| 577 |
+
text_en = ensure_english(user_text)
|
| 578 |
except Exception as e:
|
| 579 |
logging.warning("Exception in ensure_english: fallback to original due to: %s", e)
|
| 580 |
try:
|
| 581 |
+
src_lang = detect_lang(user_text)
|
| 582 |
except Exception as e:
|
| 583 |
logging.warning("Exception in detect_lang: fallback to original due to: %s", e)
|
| 584 |
+
logger.info(f"Translated Text ={text_en} Source Language = {src_lang}")
|
| 585 |
|
| 586 |
# 1) intent + sentiment
|
| 587 |
cls = gpt_classify_intent(text_en)
|
|
|
|
| 635 |
except Exception as e:
|
| 636 |
logger.exception("Failed to detect langauge: %s", e)
|
| 637 |
src_lang == ENG
|
| 638 |
+
|
| 639 |
+
|
| 640 |
if src_lang == HINDI:
|
| 641 |
try:
|
| 642 |
display_reply = en_to_lang(bot_reply)
|
|
|
|
| 644 |
except Exception:
|
| 645 |
logging.warning("Exception in en_to_lang due to: %s", e)
|
| 646 |
display_reply = bot_reply
|
| 647 |
+
|
| 648 |
+
|
| 649 |
# 4) add entry to session history
|
| 650 |
entry = {
|
| 651 |
"timestamp": current_time,
|
|
|
|
| 680 |
except Exception as e:
|
| 681 |
logger.exception("Failed to save chat: %s", e)
|
| 682 |
|
|
|
|
| 683 |
ss.clear_input = True
|
| 684 |
try:
|
| 685 |
st.rerun()
|
|
|
|
| 687 |
st.experimental_rerun()
|
| 688 |
|
| 689 |
# ---------- RENDER AREA ----------
|
|
|
|
| 690 |
def _render_turn(h):
|
|
|
|
| 691 |
st.markdown(
|
| 692 |
f"<div class='chat-bubble customer'>"
|
| 693 |
f"<div class='chat-meta'>{h['timestamp']}</div>"
|
|
|
|
| 695 |
f"</div>",
|
| 696 |
unsafe_allow_html=True,
|
| 697 |
)
|
|
|
|
| 698 |
if h.get("table_cols") and h.get("table_rows"):
|
| 699 |
try:
|
| 700 |
df = pd.DataFrame(h["table_rows"], columns=h["table_cols"])
|
|
|
|
| 702 |
except Exception:
|
| 703 |
pass
|
| 704 |
|
|
|
|
| 705 |
with st.container():
|
| 706 |
st.markdown(
|
| 707 |
"<div class='card soft'>"
|
|
|
|
| 719 |
st.write(f"**Latency:** {h['ftr_ms']:.2f} ms")
|
| 720 |
st.markdown("</div>", unsafe_allow_html=True)
|
| 721 |
|
|
|
|
| 722 |
st.markdown(
|
| 723 |
f"<div class='chat-bubble agent'>"
|
| 724 |
f"<div class='chat-meta'>{h['timestamp']}</div>"
|
|
|
|
| 727 |
unsafe_allow_html=True,
|
| 728 |
)
|
| 729 |
|
| 730 |
+
if ss.history:
|
| 731 |
+
latest = ss.history[-1]
|
|
|
|
|
|
|
| 732 |
_render_turn(latest)
|
| 733 |
|
|
|
|
| 734 |
st.markdown("### Conversation")
|
| 735 |
+
if ss.show_history and len(ss.history) > 1:
|
|
|
|
| 736 |
for h in reversed(ss.history[:-1]):
|
| 737 |
_render_turn(h)
|
| 738 |
else:
|
|
|
|
| 739 |
st.markdown(
|
| 740 |
f"<div class='chat-bubble customer compact'>"
|
| 741 |
f"<div class='chat-meta'>{latest['timestamp']}</div>"
|
|
|
|
| 745 |
)
|
| 746 |
|
| 747 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 748 |
def render_user_export_tab(conn, user_id: int):
|
| 749 |
st.subheader("Export My Chats")
|
| 750 |
df = pd.read_sql_query(
|