Spaces:
Sleeping
Sleeping
Delete streamlit_app.py
Browse files- streamlit_app.py +0 -96
streamlit_app.py
DELETED
|
@@ -1,96 +0,0 @@
|
|
| 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 |
-
import os
|
| 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()
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|