Spaces:
Sleeping
Sleeping
Commit
·
6a2d3ac
1
Parent(s):
12adfa0
feat: import chatbot
Browse files- app.py +126 -2
- requirements.txt +2 -0
- static/ai_icon.png +0 -0
- static/styles.css +32 -0
- static/user_icon.png +0 -0
app.py
CHANGED
@@ -1,4 +1,128 @@
|
|
|
|
|
|
|
|
1 |
import streamlit as st
|
2 |
|
3 |
-
|
4 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
import os
|
2 |
+
from dataclasses import dataclass
|
3 |
+
from typing import Literal
|
4 |
import streamlit as st
|
5 |
|
6 |
+
from langchain import OpenAI
|
7 |
+
from langchain.callbacks import get_openai_callback
|
8 |
+
from langchain.chains import ConversationChain
|
9 |
+
from langchain.chains.conversation.memory import ConversationSummaryMemory
|
10 |
+
import streamlit.components.v1 as components
|
11 |
+
|
12 |
+
|
13 |
+
@dataclass
|
14 |
+
class Message:
|
15 |
+
"""Class for keeping track of a chat message."""
|
16 |
+
|
17 |
+
origin: Literal["human", "ai"]
|
18 |
+
message: str
|
19 |
+
|
20 |
+
|
21 |
+
def load_css():
|
22 |
+
with open("static/styles.css", "r") as f:
|
23 |
+
css = f"<style>{f.read()}</style>"
|
24 |
+
st.markdown(css, unsafe_allow_html=True)
|
25 |
+
|
26 |
+
|
27 |
+
def initialize_session_state():
|
28 |
+
if "history" not in st.session_state:
|
29 |
+
st.session_state.history = []
|
30 |
+
if "token_count" not in st.session_state:
|
31 |
+
st.session_state.token_count = 0
|
32 |
+
if "conversation" not in st.session_state:
|
33 |
+
llm = OpenAI(
|
34 |
+
temperature=0,
|
35 |
+
openai_api_key=os.environ["OPENAI_API_KEY"],
|
36 |
+
model_name="text-davinci-003",
|
37 |
+
)
|
38 |
+
st.session_state.conversation = ConversationChain(
|
39 |
+
llm=llm,
|
40 |
+
memory=ConversationSummaryMemory(llm=llm),
|
41 |
+
)
|
42 |
+
|
43 |
+
|
44 |
+
def on_click_callback():
|
45 |
+
with get_openai_callback() as cb:
|
46 |
+
human_prompt = st.session_state.human_prompt
|
47 |
+
llm_response = st.session_state.conversation.run(human_prompt)
|
48 |
+
st.session_state.history.append(Message("human", human_prompt))
|
49 |
+
st.session_state.history.append(Message("ai", llm_response))
|
50 |
+
st.session_state.token_count += cb.total_tokens
|
51 |
+
|
52 |
+
|
53 |
+
load_css()
|
54 |
+
initialize_session_state()
|
55 |
+
|
56 |
+
st.title("Hello Custom CSS Chatbot 🤖")
|
57 |
+
|
58 |
+
chat_placeholder = st.container()
|
59 |
+
prompt_placeholder = st.form("chat-form")
|
60 |
+
information_placeholder = st.empty()
|
61 |
+
|
62 |
+
with chat_placeholder:
|
63 |
+
for chat in st.session_state.history:
|
64 |
+
div = f"""
|
65 |
+
<div class="chat-row
|
66 |
+
{'' if chat.origin == 'ai' else 'row-reverse'}">
|
67 |
+
<img class="chat-icon" src="static/{
|
68 |
+
'ai_icon.png' if chat.origin == 'ai'
|
69 |
+
else 'user_icon.png'}"
|
70 |
+
width=32 height=32>
|
71 |
+
<div class="chat-bubble
|
72 |
+
{'ai-bubble' if chat.origin == 'ai' else 'human-bubble'}">
|
73 |
+
​{chat.message}
|
74 |
+
</div>
|
75 |
+
</div>
|
76 |
+
"""
|
77 |
+
st.markdown(div, unsafe_allow_html=True)
|
78 |
+
|
79 |
+
for _ in range(3):
|
80 |
+
st.markdown("")
|
81 |
+
|
82 |
+
with prompt_placeholder:
|
83 |
+
st.markdown("**Chat**")
|
84 |
+
cols = st.columns((6, 1))
|
85 |
+
cols[0].text_input(
|
86 |
+
"Chat",
|
87 |
+
value="Hello bot",
|
88 |
+
label_visibility="collapsed",
|
89 |
+
key="human_prompt",
|
90 |
+
)
|
91 |
+
cols[1].form_submit_button(
|
92 |
+
"Submit",
|
93 |
+
type="primary",
|
94 |
+
on_click=on_click_callback,
|
95 |
+
)
|
96 |
+
|
97 |
+
information_placeholder.caption(
|
98 |
+
f"""
|
99 |
+
Used {st.session_state.token_count} tokens \n
|
100 |
+
Debug Langchain conversation:
|
101 |
+
{st.session_state.conversation.memory.buffer}
|
102 |
+
"""
|
103 |
+
)
|
104 |
+
|
105 |
+
components.html(
|
106 |
+
"""
|
107 |
+
<script>
|
108 |
+
const streamlitDoc = window.parent.document;
|
109 |
+
|
110 |
+
const buttons = Array.from(
|
111 |
+
streamlitDoc.querySelectorAll('.stButton > button')
|
112 |
+
);
|
113 |
+
const submitButton = buttons.find(
|
114 |
+
el => el.innerText === 'Submit'
|
115 |
+
);
|
116 |
+
|
117 |
+
streamlitDoc.addEventListener('keydown', function(e) {
|
118 |
+
switch (e.key) {
|
119 |
+
case 'Enter':
|
120 |
+
submitButton.click();
|
121 |
+
break;
|
122 |
+
}
|
123 |
+
});
|
124 |
+
</script>
|
125 |
+
""",
|
126 |
+
height=0,
|
127 |
+
width=0,
|
128 |
+
)
|
requirements.txt
ADDED
@@ -0,0 +1,2 @@
|
|
|
|
|
|
|
1 |
+
streamlit
|
2 |
+
streamlit-chat
|
static/ai_icon.png
ADDED
![]() |
static/styles.css
ADDED
@@ -0,0 +1,32 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
.chat-row {
|
2 |
+
display: flex;
|
3 |
+
margin: 5px;
|
4 |
+
width: 100%;
|
5 |
+
}
|
6 |
+
|
7 |
+
.row-reverse {
|
8 |
+
flex-direction: row-reverse;
|
9 |
+
}
|
10 |
+
|
11 |
+
.chat-bubble {
|
12 |
+
font-family: "Source Sans Pro", sans-serif, "Segoe UI", "Roboto", sans-serif;
|
13 |
+
border: 1px solid transparent;
|
14 |
+
padding: 5px 10px;
|
15 |
+
margin: 0px 7px;
|
16 |
+
max-width: 70%;
|
17 |
+
}
|
18 |
+
|
19 |
+
.ai-bubble {
|
20 |
+
background: rgb(240, 242, 246);
|
21 |
+
border-radius: 10px;
|
22 |
+
}
|
23 |
+
|
24 |
+
.human-bubble {
|
25 |
+
background: linear-gradient(135deg, rgb(0, 178, 255) 0%, rgb(0, 106, 255) 100%);
|
26 |
+
color: white;
|
27 |
+
border-radius: 20px;
|
28 |
+
}
|
29 |
+
|
30 |
+
.chat-icon {
|
31 |
+
border-radius: 5px;
|
32 |
+
}
|
static/user_icon.png
ADDED
![]() |