Spaces:
Runtime error
Runtime error
Create app.py
Browse files
app.py
ADDED
@@ -0,0 +1,62 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
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 |
+
st.set_page_config(page_title="HugChat - An LLM-powered Streamlit app")
|
8 |
+
|
9 |
+
# Sidebar contents
|
10 |
+
with st.sidebar:
|
11 |
+
st.title('π€π¬ HugChat App')
|
12 |
+
st.markdown('''
|
13 |
+
## About
|
14 |
+
This app is an LLM-powered chatbot built using:
|
15 |
+
- [Streamlit](https://streamlit.io/)
|
16 |
+
- [OpenAssistant/oasst-sft-6-llama-30b-xor](https://huggingface.co/OpenAssistant/oasst-sft-6-llama-30b-xor) LLM model
|
17 |
+
|
18 |
+
π‘ Note: No API key required!
|
19 |
+
''')
|
20 |
+
add_vertical_space(5)
|
21 |
+
st.write('Made with β€οΈ by [Data Professor](https://youtube.com/dataprofessor)')
|
22 |
+
|
23 |
+
# Generate empty lists for generated and past.
|
24 |
+
## generated stores AI generated responses
|
25 |
+
if 'generated' not in st.session_state:
|
26 |
+
st.session_state['generated'] = ["I'm HugChat, How may I help you?"]
|
27 |
+
## past stores User's questions
|
28 |
+
if 'past' not in st.session_state:
|
29 |
+
st.session_state['past'] = ['Hi!']
|
30 |
+
|
31 |
+
# Layout of input/response containers
|
32 |
+
input_container = st.container()
|
33 |
+
colored_header(label='', description='', color_name='blue-30')
|
34 |
+
response_container = st.container()
|
35 |
+
|
36 |
+
# User input
|
37 |
+
## Function for taking user provided prompt as input
|
38 |
+
def get_text():
|
39 |
+
input_text = st.text_input("You: ", "", key="input")
|
40 |
+
return input_text
|
41 |
+
## Applying the user input box
|
42 |
+
with input_container:
|
43 |
+
user_input = get_text()
|
44 |
+
|
45 |
+
# Response output
|
46 |
+
## Function for taking user prompt as input followed by producing AI generated responses
|
47 |
+
def generate_response(prompt):
|
48 |
+
chatbot = hugchat.ChatBot()
|
49 |
+
response = chatbot.chat(prompt)
|
50 |
+
return response
|
51 |
+
|
52 |
+
## Conditional display of AI generated responses as a function of user provided prompts
|
53 |
+
with response_container:
|
54 |
+
if user_input:
|
55 |
+
response = generate_response(user_input)
|
56 |
+
st.session_state.past.append(user_input)
|
57 |
+
st.session_state.generated.append(response)
|
58 |
+
|
59 |
+
if st.session_state['generated']:
|
60 |
+
for i in range(len(st.session_state['generated'])):
|
61 |
+
message(st.session_state['past'][i], is_user=True, key=str(i) + '_user')
|
62 |
+
message(st.session_state["generated"][i], key=str(i))
|