devchavda11 commited on
Commit
95112bf
·
verified ·
1 Parent(s): 730fa84

Update src/streamlit_app.py

Browse files
Files changed (1) hide show
  1. src/streamlit_app.py +94 -38
src/streamlit_app.py CHANGED
@@ -1,40 +1,96 @@
1
- import altair as alt
2
- import numpy as np
3
- import pandas as pd
4
  import streamlit as st
 
 
 
 
 
 
 
5
 
6
- """
7
- # Welcome to Streamlit!
8
-
9
- Edit `/streamlit_app.py` to customize this app to your heart's desire :heart:.
10
- If you have any questions, checkout our [documentation](https://docs.streamlit.io) and [community
11
- forums](https://discuss.streamlit.io).
12
-
13
- In the meantime, below is an example of what you can do with just a few lines of code:
14
- """
15
-
16
- num_points = st.slider("Number of points in spiral", 1, 10000, 1100)
17
- num_turns = st.slider("Number of turns in spiral", 1, 300, 31)
18
-
19
- indices = np.linspace(0, 1, num_points)
20
- theta = 2 * np.pi * num_turns * indices
21
- radius = indices
22
-
23
- x = radius * np.cos(theta)
24
- y = radius * np.sin(theta)
25
-
26
- df = pd.DataFrame({
27
- "x": x,
28
- "y": y,
29
- "idx": indices,
30
- "rand": np.random.randn(num_points),
31
- })
32
-
33
- st.altair_chart(alt.Chart(df, height=700, width=700)
34
- .mark_point(filled=True)
35
- .encode(
36
- x=alt.X("x", axis=None),
37
- y=alt.Y("y", axis=None),
38
- color=alt.Color("idx", legend=None, scale=alt.Scale()),
39
- size=alt.Size("rand", legend=None, scale=alt.Scale(range=[1, 150])),
40
- ))
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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()