Spaces:
Sleeping
Sleeping
VeryMadSoul
commited on
Commit
β’
48bdd01
1
Parent(s):
e3bcff7
Update app.py
Browse files
app.py
CHANGED
@@ -1,63 +1,69 @@
|
|
1 |
import streamlit as st
|
2 |
-
from streamlit_chat import message
|
3 |
-
from streamlit_extras.colored_header import colored_header
|
4 |
-
from streamlit_extras.add_vertical_space import add_vertical_space
|
5 |
from hugchat import hugchat
|
|
|
|
|
6 |
|
7 |
-
|
|
|
8 |
|
9 |
-
#
|
10 |
with st.sidebar:
|
11 |
-
st.title('π€π¬ HugChat
|
12 |
-
st.
|
13 |
-
|
14 |
-
|
15 |
-
|
16 |
-
|
17 |
-
|
18 |
-
|
19 |
-
|
20 |
-
|
21 |
-
|
22 |
-
|
23 |
-
|
24 |
-
|
25 |
-
|
26 |
-
if
|
27 |
-
st.session_state
|
28 |
-
|
29 |
-
|
30 |
-
|
31 |
-
|
32 |
-
|
33 |
-
|
34 |
-
|
35 |
-
|
36 |
-
|
37 |
-
|
38 |
-
|
39 |
-
def
|
40 |
-
|
41 |
-
|
42 |
-
|
43 |
-
|
44 |
-
|
45 |
-
|
46 |
-
|
47 |
-
|
48 |
-
|
49 |
-
|
50 |
-
|
51 |
-
|
52 |
-
|
53 |
-
|
54 |
-
|
55 |
-
|
56 |
-
|
57 |
-
|
58 |
-
|
59 |
-
|
60 |
-
|
61 |
-
|
62 |
-
|
63 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
import streamlit as st
|
|
|
|
|
|
|
2 |
from hugchat import hugchat
|
3 |
+
from hugchat.login import Login
|
4 |
+
import os
|
5 |
|
6 |
+
# App title
|
7 |
+
st.set_page_config(page_title="π€π¬ HugChat")
|
8 |
|
9 |
+
# Hugging Face Credentials
|
10 |
with st.sidebar:
|
11 |
+
st.title('π€π¬ HugChat')
|
12 |
+
if ('EMAIL' in st.secrets) and ('PASS' in st.secrets):
|
13 |
+
st.success('HuggingFace Login credentials already provided!', icon='β
')
|
14 |
+
hf_email = st.secrets['EMAIL']
|
15 |
+
hf_pass = st.secrets['PASS']
|
16 |
+
else:
|
17 |
+
hf_email = st.text_input('Enter E-mail:', type='password')
|
18 |
+
hf_pass = st.text_input('Enter password:', type='password')
|
19 |
+
if not (hf_email and hf_pass):
|
20 |
+
st.warning('Please enter your credentials!', icon='β οΈ')
|
21 |
+
else:
|
22 |
+
st.success('Proceed to entering your prompt message!', icon='π')
|
23 |
+
st.markdown('π Learn how to build this app in this [blog](https://blog.streamlit.io/how-to-build-an-llm-powered-chatbot-with-streamlit/)!')
|
24 |
+
|
25 |
+
# Store LLM generated responses
|
26 |
+
if "messages" not in st.session_state:
|
27 |
+
st.session_state.messages = [{"role": "assistant", "content": "How may I assist you today?"}]
|
28 |
+
|
29 |
+
# Display or clear chat messages
|
30 |
+
for message in st.session_state.messages:
|
31 |
+
with st.chat_message(message["role"]):
|
32 |
+
st.write(message["content"])
|
33 |
+
|
34 |
+
def clear_chat_history():
|
35 |
+
st.session_state.messages = [{"role": "assistant", "content": "How may I assist you today?"}]
|
36 |
+
st.sidebar.button('Clear Chat History', on_click=clear_chat_history)
|
37 |
+
|
38 |
+
# Function for generating LLM response
|
39 |
+
def generate_response(prompt_input, email, passwd):
|
40 |
+
# Hugging Face Login
|
41 |
+
sign = Login(email, passwd)
|
42 |
+
cookies = sign.login()
|
43 |
+
# Create ChatBot
|
44 |
+
chatbot = hugchat.ChatBot(cookies=cookies.get_dict())
|
45 |
+
|
46 |
+
for dict_message in st.session_state.messages:
|
47 |
+
string_dialogue = "You are a helpful assistant."
|
48 |
+
if dict_message["role"] == "user":
|
49 |
+
string_dialogue += "User: " + dict_message["content"] + "\n\n"
|
50 |
+
else:
|
51 |
+
string_dialogue += "Assistant: " + dict_message["content"] + "\n\n"
|
52 |
+
|
53 |
+
prompt = f"{string_dialogue} {prompt_input} Assistant: "
|
54 |
+
return chatbot.chat(prompt)
|
55 |
+
|
56 |
+
# User-provided prompt
|
57 |
+
if prompt := st.chat_input(disabled=not (hf_email and hf_pass)):
|
58 |
+
st.session_state.messages.append({"role": "user", "content": prompt})
|
59 |
+
with st.chat_message("user"):
|
60 |
+
st.write(prompt)
|
61 |
+
|
62 |
+
# Generate a new response if last message is not from assistant
|
63 |
+
if st.session_state.messages[-1]["role"] != "assistant":
|
64 |
+
with st.chat_message("assistant"):
|
65 |
+
with st.spinner("Thinking..."):
|
66 |
+
response = generate_response(prompt, hf_email, hf_pass)
|
67 |
+
st.write(response)
|
68 |
+
message = {"role": "assistant", "content": response}
|
69 |
+
st.session_state.messages.append(message)
|