Spaces:
Sleeping
Sleeping
Update frontend_streamlit.py
Browse files- frontend_streamlit.py +96 -129
frontend_streamlit.py
CHANGED
|
@@ -1,129 +1,96 @@
|
|
| 1 |
-
import streamlit as st
|
| 2 |
-
from chat_langraph import system, workflow, HumanMessage, AIMessage, get_all_chat_ids, ToolMessage
|
| 3 |
-
import uuid
|
| 4 |
-
from chat_title_giver import give_me_the_title
|
| 5 |
-
import
|
| 6 |
-
|
| 7 |
-
|
| 8 |
-
|
| 9 |
-
|
| 10 |
-
|
| 11 |
-
|
| 12 |
-
|
| 13 |
-
|
| 14 |
-
|
| 15 |
-
|
| 16 |
-
|
| 17 |
-
def set_config():
|
| 18 |
-
return {'configurable': {'thread_id': st.session_state.current_chat_id}}
|
| 19 |
-
|
| 20 |
-
|
| 21 |
-
def load_session_state():
|
| 22 |
-
if "chats" not in st.session_state:
|
| 23 |
-
st.session_state.chats = get_all_chat_ids()
|
| 24 |
-
if "current_chat_id" not in st.session_state:
|
| 25 |
-
if len(st.session_state.chats) > 0:
|
| 26 |
-
st.session_state.current_chat_id = st.session_state.chats[-1]
|
| 27 |
-
else:
|
| 28 |
-
|
| 29 |
-
|
| 30 |
-
st.session_state.
|
| 31 |
-
|
| 32 |
-
|
| 33 |
-
|
| 34 |
-
|
| 35 |
-
|
| 36 |
-
|
| 37 |
-
|
| 38 |
-
|
| 39 |
-
|
| 40 |
-
|
| 41 |
-
|
| 42 |
-
|
| 43 |
-
|
| 44 |
-
|
| 45 |
-
|
| 46 |
-
|
| 47 |
-
st.
|
| 48 |
-
|
| 49 |
-
|
| 50 |
-
|
| 51 |
-
|
| 52 |
-
|
| 53 |
-
|
| 54 |
-
|
| 55 |
-
|
| 56 |
-
|
| 57 |
-
|
| 58 |
-
|
| 59 |
-
|
| 60 |
-
|
| 61 |
-
|
| 62 |
-
|
| 63 |
-
|
| 64 |
-
|
| 65 |
-
|
| 66 |
-
|
| 67 |
-
|
| 68 |
-
|
| 69 |
-
|
| 70 |
-
|
| 71 |
-
|
| 72 |
-
|
| 73 |
-
|
| 74 |
-
|
| 75 |
-
|
| 76 |
-
|
| 77 |
-
|
| 78 |
-
|
| 79 |
-
|
| 80 |
-
|
| 81 |
-
|
| 82 |
-
|
| 83 |
-
|
| 84 |
-
|
| 85 |
-
|
| 86 |
-
|
| 87 |
-
|
| 88 |
-
|
| 89 |
-
|
| 90 |
-
|
| 91 |
-
|
| 92 |
-
|
| 93 |
-
|
| 94 |
-
|
| 95 |
-
|
| 96 |
-
|
| 97 |
-
is_thinking = True
|
| 98 |
-
with st.chat_message("assistant"):
|
| 99 |
-
content = st.expander("Thinking...", expanded=True)
|
| 100 |
-
resp = st.empty()
|
| 101 |
-
resp2 = None
|
| 102 |
-
with content:
|
| 103 |
-
resp2 = st.empty()
|
| 104 |
-
for messages,metadata in workflow.stream({"messages":[system,HumanMessage(user_input)]} , config={'configurable': {'thread_id': st.session_state.current_chat_id}} , stream_mode="messages"):
|
| 105 |
-
if(isinstance(messages , AIMessage) and messages.content.strip()):
|
| 106 |
-
if(messages.content.startswith("<think>")):
|
| 107 |
-
is_thinking = True
|
| 108 |
-
resp2.markdown(full_response + "|")
|
| 109 |
-
if is_thinking:
|
| 110 |
-
full_response += messages.content
|
| 111 |
-
with content:
|
| 112 |
-
resp2.markdown(full_response + "|")
|
| 113 |
-
if "</think>" in full_response:
|
| 114 |
-
is_thinking = False
|
| 115 |
-
full_response = full_response.replace("</think>", "")
|
| 116 |
-
resp2.markdown(full_response)
|
| 117 |
-
else:
|
| 118 |
-
answer_response += messages.content
|
| 119 |
-
resp.markdown(answer_response + "|")
|
| 120 |
-
if(isinstance(messages ,ToolMessage) and messages.content.strip()):
|
| 121 |
-
st.info(f"Using appropriate tool")
|
| 122 |
-
|
| 123 |
-
st.rerun()
|
| 124 |
-
|
| 125 |
-
|
| 126 |
-
|
| 127 |
-
|
| 128 |
-
|
| 129 |
-
|
|
|
|
| 1 |
+
import streamlit as st
|
| 2 |
+
from chat_langraph import system, workflow, HumanMessage, AIMessage, get_all_chat_ids, ToolMessage
|
| 3 |
+
import uuid
|
| 4 |
+
from chat_title_giver import give_me_the_title
|
| 5 |
+
import re
|
| 6 |
+
|
| 7 |
+
st.title("College chatbot")
|
| 8 |
+
st.set_page_config(layout='wide')
|
| 9 |
+
|
| 10 |
+
|
| 11 |
+
def set_title(messages):
|
| 12 |
+
if messages:
|
| 13 |
+
title = give_me_the_title(messages)
|
| 14 |
+
st.session_state.chat_dict[st.session_state.current_chat_id] = title
|
| 15 |
+
|
| 16 |
+
|
| 17 |
+
def set_config():
|
| 18 |
+
return {'configurable': {'thread_id': st.session_state.current_chat_id}}
|
| 19 |
+
|
| 20 |
+
|
| 21 |
+
def load_session_state():
|
| 22 |
+
if "chats" not in st.session_state:
|
| 23 |
+
st.session_state.chats = get_all_chat_ids()
|
| 24 |
+
if "current_chat_id" not in st.session_state:
|
| 25 |
+
if len(st.session_state.chats) > 0:
|
| 26 |
+
st.session_state.current_chat_id = st.session_state.chats[-1]
|
| 27 |
+
else:
|
| 28 |
+
new_id = str(uuid.uuid4())
|
| 29 |
+
st.session_state.chats.append(new_id)
|
| 30 |
+
st.session_state.current_chat_id = new_id
|
| 31 |
+
if "chat_dict" not in st.session_state:
|
| 32 |
+
st.session_state.chat_dict = {}
|
| 33 |
+
|
| 34 |
+
|
| 35 |
+
def render_sidebar():
|
| 36 |
+
with st.sidebar:
|
| 37 |
+
st.title("Chats")
|
| 38 |
+
if st.button("➕ New Chat"):
|
| 39 |
+
new_id = str(uuid.uuid4())
|
| 40 |
+
st.session_state.chats.append(new_id)
|
| 41 |
+
st.session_state.current_chat_id = new_id
|
| 42 |
+
config = {'configurable': {'thread_id': new_id}}
|
| 43 |
+
workflow.update_state(config, {"messages": [system]})
|
| 44 |
+
st.session_state.chat_dict[new_id] = "New Chat"
|
| 45 |
+
st.experimental_rerun()
|
| 46 |
+
for chat_id in st.session_state.chats:
|
| 47 |
+
if st.button(st.session_state.chat_dict.get(chat_id, "New Chat"), key=chat_id):
|
| 48 |
+
st.session_state.current_chat_id = chat_id
|
| 49 |
+
|
| 50 |
+
|
| 51 |
+
def loadchats():
|
| 52 |
+
if "current_chat_id" not in st.session_state:
|
| 53 |
+
return []
|
| 54 |
+
config = {'configurable': {'thread_id': st.session_state.current_chat_id}}
|
| 55 |
+
state = workflow.get_state(config)
|
| 56 |
+
messages = state.values.get("messages", [])
|
| 57 |
+
for message in messages:
|
| 58 |
+
if isinstance(message, HumanMessage):
|
| 59 |
+
if message.content and message.content.strip():
|
| 60 |
+
with st.chat_message("human"):
|
| 61 |
+
st.write(message.content)
|
| 62 |
+
elif isinstance(message, AIMessage):
|
| 63 |
+
if message.content and message.content.strip():
|
| 64 |
+
with st.chat_message("assistant"):
|
| 65 |
+
st.write(message.content)
|
| 66 |
+
elif isinstance(message, ToolMessage):
|
| 67 |
+
with st.chat_message("assistant"):
|
| 68 |
+
st.info(f"Used a tool")
|
| 69 |
+
return messages
|
| 70 |
+
|
| 71 |
+
|
| 72 |
+
load_session_state()
|
| 73 |
+
render_sidebar()
|
| 74 |
+
|
| 75 |
+
if "current_chat_id" in st.session_state:
|
| 76 |
+
loadchats()
|
| 77 |
+
user_input = st.chat_input("Your message: ")
|
| 78 |
+
if user_input is not None:
|
| 79 |
+
with st.chat_message("human"):
|
| 80 |
+
st.write(user_input)
|
| 81 |
+
|
| 82 |
+
with st.chat_message("assistant"):
|
| 83 |
+
response_placeholder = st.empty()
|
| 84 |
+
full_response = ""
|
| 85 |
+
for messages, metadata in workflow.stream(
|
| 86 |
+
{"messages": [system, HumanMessage(user_input)]},
|
| 87 |
+
config={'configurable': {'thread_id': st.session_state.current_chat_id}},
|
| 88 |
+
stream_mode="messages"
|
| 89 |
+
):
|
| 90 |
+
if isinstance(messages, AIMessage) and messages.content.strip():
|
| 91 |
+
full_response += messages.content
|
| 92 |
+
if isinstance(messages, ToolMessage) and messages.content.strip():
|
| 93 |
+
st.info(f"Using appropriate tool")
|
| 94 |
+
response_placeholder.markdown(full_response)
|
| 95 |
+
|
| 96 |
+
st.experimental_rerun()
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|