first commit
Browse files- README.md +3 -3
- app.py +78 -0
- img/logo.png +0 -0
README.md
CHANGED
@@ -1,8 +1,8 @@
|
|
1 |
---
|
2 |
title: Nanbeige Plus Chat V0.1
|
3 |
-
emoji:
|
4 |
-
colorFrom:
|
5 |
-
colorTo:
|
6 |
sdk: streamlit
|
7 |
sdk_version: 1.32.2
|
8 |
app_file: app.py
|
|
|
1 |
---
|
2 |
title: Nanbeige Plus Chat V0.1
|
3 |
+
emoji: 🌌
|
4 |
+
colorFrom: blue
|
5 |
+
colorTo: purple
|
6 |
sdk: streamlit
|
7 |
sdk_version: 1.32.2
|
8 |
app_file: app.py
|
app.py
ADDED
@@ -0,0 +1,78 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
import time
|
2 |
+
import json
|
3 |
+
|
4 |
+
import requests
|
5 |
+
import streamlit as st
|
6 |
+
|
7 |
+
st.set_page_config(page_title="Nanbeige Chatbot")
|
8 |
+
role_map = {"BOT": "assistant", "USER": "user"}
|
9 |
+
|
10 |
+
|
11 |
+
def clear_chat_history():
|
12 |
+
st.session_state.messages = [{"sender_type": "BOT", "text": "Hello, I'm Nanbeige. How may I assist you today?"}]
|
13 |
+
|
14 |
+
|
15 |
+
with st.sidebar:
|
16 |
+
st.image('img/logo.png', use_column_width=True)
|
17 |
+
st.title("Nanbeige Chatbot💬")
|
18 |
+
st.markdown("""
|
19 |
+
<p>Created by Nanbeige Lab
|
20 |
+
<a href="https://github.com/Nanbeige">
|
21 |
+
<img src = "https://cdn-icons-png.flaticon.com/512/733/733609.png" width="23"></img>
|
22 |
+
</a>
|
23 |
+
</p>""", unsafe_allow_html=True)
|
24 |
+
|
25 |
+
temperature = st.sidebar.slider('temperature', min_value=0.01, max_value=1.0, value=0.3, step=0.01)
|
26 |
+
top_p = st.sidebar.slider('top_p', min_value=0.01, max_value=1.0, value=0.9, step=0.01)
|
27 |
+
|
28 |
+
st.sidebar.button('Clear Chat History', on_click=clear_chat_history)
|
29 |
+
|
30 |
+
# Store LLM generated responses
|
31 |
+
if "messages" not in st.session_state:
|
32 |
+
st.session_state.messages = [{"sender_type": "BOT", "text": "Hello, I'm Nanbeige. How may I assist you today?"}]
|
33 |
+
|
34 |
+
# Display or clear chat message
|
35 |
+
for message in st.session_state.messages:
|
36 |
+
with st.chat_message(role_map[message["sender_type"]]):
|
37 |
+
st.markdown(message["text"])
|
38 |
+
|
39 |
+
|
40 |
+
def generate_response():
|
41 |
+
messages = st.session_state.messages.copy()
|
42 |
+
messages.insert(0, {"sender_type": "USER", "text": "hi"})
|
43 |
+
|
44 |
+
payload = json.dumps({
|
45 |
+
'messages': messages,
|
46 |
+
'max_tokens': "4096",
|
47 |
+
'temperature': 0.3,
|
48 |
+
'top_p': 0.9,
|
49 |
+
})
|
50 |
+
|
51 |
+
headers = {
|
52 |
+
'Authorization': f'Bearer {st.secrets["secret_token"]}',
|
53 |
+
'Content-Type': 'application/json'
|
54 |
+
}
|
55 |
+
|
56 |
+
r = requests.request("POST", st.secrets["secret_url"], headers=headers, data=payload)
|
57 |
+
return r.json()['reply']
|
58 |
+
|
59 |
+
|
60 |
+
def stream_response(text):
|
61 |
+
for word in text:
|
62 |
+
yield word
|
63 |
+
time.sleep(0.02)
|
64 |
+
|
65 |
+
|
66 |
+
if prompt := st.chat_input():
|
67 |
+
st.session_state.messages.append({"sender_type": "USER", "text": prompt})
|
68 |
+
with st.chat_message("user"):
|
69 |
+
st.markdown(prompt)
|
70 |
+
|
71 |
+
if st.session_state.messages[-1]["sender_type"] != "BOT":
|
72 |
+
with st.chat_message("assistant"):
|
73 |
+
with st.spinner("Thinking..."):
|
74 |
+
response = generate_response()
|
75 |
+
st.write_stream(stream_response(response))
|
76 |
+
|
77 |
+
message = {"sender_type": "BOT", "text": response}
|
78 |
+
st.session_state.messages.append(message)
|
img/logo.png
ADDED