devchavda11 commited on
Commit
0a1d217
·
verified ·
1 Parent(s): c2dd056

Update frontend_streamlit.py

Browse files
Files changed (1) hide show
  1. 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 os
6
- import base64
7
- import re
8
-
9
- st.title("College chatbot")
10
- st.set_page_config(layout='wide')
11
-
12
- def set_title(messages):
13
- if messages:
14
- title = give_me_the_title(messages)
15
- st.session_state.chat_dict[st.session_state.current_chat_id] = title
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
- new_id = str(uuid.uuid4())
30
- st.session_state.chats.append(new_id)
31
- st.session_state.current_chat_id = new_id
32
- if "chat_dict" not in st.session_state:
33
- st.session_state.chat_dict = {}
34
- if "uploaded_image" not in st.session_state:
35
- st.session_state.uploaded_image = None
36
-
37
- def render_sidebar():
38
- with st.sidebar:
39
- st.title("Chats")
40
- if st.button("➕ New Chat"):
41
- new_id = str(uuid.uuid4())
42
- st.session_state.chats.append(new_id)
43
- st.session_state.current_chat_id = new_id
44
- config = {'configurable': {'thread_id': new_id}}
45
- workflow.update_state(config, {"messages": [system]})
46
- st.session_state.chat_dict[new_id] = "New Chat"
47
- st.rerun()
48
- for chat_id in st.session_state.chats:
49
- if st.button(st.session_state.chat_dict.get(chat_id, "New Chat"), key=chat_id):
50
- st.session_state.current_chat_id = chat_id
51
-
52
- def loadchats():
53
- if "current_chat_id" not in st.session_state:
54
- return []
55
- config = {'configurable': {'thread_id': st.session_state.current_chat_id}}
56
- state = workflow.get_state(config)
57
- messages = state.values.get("messages", [])
58
- for message in messages:
59
- if isinstance(message, HumanMessage):
60
- if message.content and message.content.strip():
61
- with st.chat_message("human"):
62
- st.write(message.content)
63
- elif isinstance(message, AIMessage):
64
- content = st.expander("Thinking...", expanded=False)
65
- if message.content and message.content.strip():
66
- text = message.content
67
- m = re.search(r'</\s*think\s*>', text, re.IGNORECASE)
68
- if m:
69
- thinking_text = text[:m.start()].strip()
70
- rest = text[m.end():].strip()
71
- if thinking_text:
72
- with content:
73
- st.write(thinking_text)
74
- if rest:
75
- with st.chat_message("assistant"):
76
- st.write(rest)
77
- else:
78
- with st.chat_message("assistant"):
79
- st.write(text)
80
- elif isinstance(message, ToolMessage):
81
- with st.chat_message("assistant"):
82
- st.info(f"Used a tool")
83
- return messages
84
-
85
- load_session_state()
86
- render_sidebar()
87
- if("current_chat_id" in st.session_state):
88
- loadchats()
89
- user_input = st.chat_input("Your message: ")
90
- if(user_input is not None):
91
- response_placeholder = st.empty()
92
- with st.chat_message("human"):
93
- st.write(user_input)
94
-
95
- full_response = ""
96
- answer_response = ""
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()