Spaces:
Sleeping
Sleeping
Ray Leung
commited on
Create app.py
Browse files
app.py
ADDED
|
@@ -0,0 +1,55 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import streamlit as st
|
| 2 |
+
from hugchat import hugchat
|
| 3 |
+
from hugchat.login import Login
|
| 4 |
+
|
| 5 |
+
# App title
|
| 6 |
+
st.set_page_config(page_title="π€π¬ HugChat")
|
| 7 |
+
|
| 8 |
+
# Hugging Face Credentials
|
| 9 |
+
with st.sidebar:
|
| 10 |
+
st.title('π€π¬ HugChat')
|
| 11 |
+
if ('EMAIL' in st.secrets) and ('PASS' in st.secrets):
|
| 12 |
+
st.success('HuggingFace Login credentials already provided!', icon='β
')
|
| 13 |
+
hf_email = st.secrets['EMAIL']
|
| 14 |
+
hf_pass = st.secrets['PASS']
|
| 15 |
+
else:
|
| 16 |
+
hf_email = st.text_input('Enter E-mail:', type='password')
|
| 17 |
+
hf_pass = st.text_input('Enter password:', type='password')
|
| 18 |
+
if not (hf_email and hf_pass):
|
| 19 |
+
st.warning('Please enter your credentials!', icon='β οΈ')
|
| 20 |
+
else:
|
| 21 |
+
st.success('Proceed to entering your prompt message!', icon='π')
|
| 22 |
+
st.markdown('π Learn how to build this app in this [blog](https://blog.streamlit.io/how-to-build-an-llm-powered-chatbot-with-streamlit/)!')
|
| 23 |
+
|
| 24 |
+
# Store LLM generated responses
|
| 25 |
+
if "messages" not in st.session_state.keys():
|
| 26 |
+
st.session_state.messages = [{"role": "assistant", "content": "How may I help you?"}]
|
| 27 |
+
|
| 28 |
+
# Display chat messages
|
| 29 |
+
for message in st.session_state.messages:
|
| 30 |
+
with st.chat_message(message["role"]):
|
| 31 |
+
st.write(message["content"])
|
| 32 |
+
|
| 33 |
+
# Function for generating LLM response
|
| 34 |
+
def generate_response(prompt_input, email, passwd):
|
| 35 |
+
# Hugging Face Login
|
| 36 |
+
sign = Login(email, passwd)
|
| 37 |
+
cookies = sign.login()
|
| 38 |
+
# Create ChatBot
|
| 39 |
+
chatbot = hugchat.ChatBot(cookies=cookies.get_dict())
|
| 40 |
+
return chatbot.chat(prompt_input)
|
| 41 |
+
|
| 42 |
+
# User-provided prompt
|
| 43 |
+
if prompt := st.chat_input(disabled=not (hf_email and hf_pass)):
|
| 44 |
+
st.session_state.messages.append({"role": "user", "content": prompt})
|
| 45 |
+
with st.chat_message("user"):
|
| 46 |
+
st.write(prompt)
|
| 47 |
+
|
| 48 |
+
# Generate a new response if last message is not from assistant
|
| 49 |
+
if st.session_state.messages[-1]["role"] != "assistant":
|
| 50 |
+
with st.chat_message("assistant"):
|
| 51 |
+
with st.spinner("Thinking..."):
|
| 52 |
+
response = generate_response(prompt, hf_email, hf_pass)
|
| 53 |
+
st.write(response)
|
| 54 |
+
message = {"role": "assistant", "content": response}
|
| 55 |
+
st.session_state.messages.append(message)
|