Spaces:
Sleeping
Sleeping
Upload 14 files
Browse files- .devcontainer/devcontainer.json +33 -0
- README.md +0 -12
- app_v1.py +67 -0
- app_v2.py +73 -0
- app_v3.py +69 -0
- img/placeholder.md +1 -0
- img/streamlit.png +0 -0
- langchain_app.py +51 -0
- notebook/hf.env +2 -0
- streamlit.png +0 -0
.devcontainer/devcontainer.json
ADDED
@@ -0,0 +1,33 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
{
|
2 |
+
"name": "Python 3",
|
3 |
+
// Or use a Dockerfile or Docker Compose file. More info: https://containers.dev/guide/dockerfile
|
4 |
+
"image": "mcr.microsoft.com/devcontainers/python:1-3.11-bullseye",
|
5 |
+
"customizations": {
|
6 |
+
"codespaces": {
|
7 |
+
"openFiles": [
|
8 |
+
"README.md",
|
9 |
+
"streamlit_app.py"
|
10 |
+
]
|
11 |
+
},
|
12 |
+
"vscode": {
|
13 |
+
"settings": {},
|
14 |
+
"extensions": [
|
15 |
+
"ms-python.python",
|
16 |
+
"ms-python.vscode-pylance"
|
17 |
+
]
|
18 |
+
}
|
19 |
+
},
|
20 |
+
"updateContentCommand": "[ -f packages.txt ] && sudo apt update && sudo apt upgrade -y && sudo xargs apt install -y <packages.txt; [ -f requirements.txt ] && pip3 install --user -r requirements.txt; pip3 install --user streamlit; echo '✅ Packages installed and Requirements met'",
|
21 |
+
"postAttachCommand": {
|
22 |
+
"server": "streamlit run streamlit_app.py --server.enableCORS false --server.enableXsrfProtection false"
|
23 |
+
},
|
24 |
+
"portsAttributes": {
|
25 |
+
"8501": {
|
26 |
+
"label": "Application",
|
27 |
+
"onAutoForward": "openPreview"
|
28 |
+
}
|
29 |
+
},
|
30 |
+
"forwardPorts": [
|
31 |
+
8501
|
32 |
+
]
|
33 |
+
}
|
README.md
CHANGED
@@ -1,15 +1,3 @@
|
|
1 |
-
---
|
2 |
-
title: Streamlit Huggingface
|
3 |
-
emoji: 🏢
|
4 |
-
colorFrom: purple
|
5 |
-
colorTo: red
|
6 |
-
sdk: streamlit
|
7 |
-
sdk_version: 1.41.1
|
8 |
-
app_file: app.py
|
9 |
-
pinned: false
|
10 |
-
license: mit
|
11 |
-
---
|
12 |
-
|
13 |
# 🤗💬 HugChat App
|
14 |
```
|
15 |
This app is an LLM-powered chatbot built using Streamlit and HugChat.
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
# 🤗💬 HugChat App
|
2 |
```
|
3 |
This app is an LLM-powered chatbot built using Streamlit and HugChat.
|
app_v1.py
ADDED
@@ -0,0 +1,67 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
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 |
+
import os
|
7 |
+
|
8 |
+
# Streamlit page config
|
9 |
+
st.set_page_config(page_title="HugChat - An LLM-powered Streamlit app")
|
10 |
+
|
11 |
+
# Sidebar contents
|
12 |
+
with st.sidebar:
|
13 |
+
st.title('🤗💬 HugChat App')
|
14 |
+
st.markdown('''
|
15 |
+
## About
|
16 |
+
This app is an LLM-powered chatbot built using:
|
17 |
+
- [Streamlit](https://streamlit.io/)
|
18 |
+
- [HugChat](https://github.com/Soulter/hugging-chat-api)
|
19 |
+
- [OpenAssistant/oasst-sft-6-llama-30b-xor](https://huggingface.co/OpenAssistant/oasst-sft-6-llama-30b-xor) LLM model
|
20 |
+
|
21 |
+
💡 Note: No API key required!
|
22 |
+
''')
|
23 |
+
add_vertical_space(5)
|
24 |
+
st.write('Made with ❤️ by [Data Professor](https://youtube.com/dataprofessor)')
|
25 |
+
|
26 |
+
# Initialize chatbot and session state
|
27 |
+
if 'chatbot' not in st.session_state:
|
28 |
+
# Create ChatBot instance
|
29 |
+
st.session_state.chatbot = hugchat.ChatBot()
|
30 |
+
|
31 |
+
if 'generated' not in st.session_state:
|
32 |
+
st.session_state['generated'] = ["I'm HugChat, How may I help you?"]
|
33 |
+
|
34 |
+
if 'past' not in st.session_state:
|
35 |
+
st.session_state['past'] = ['Hi!']
|
36 |
+
|
37 |
+
# Layout of input/response containers
|
38 |
+
input_container = st.container()
|
39 |
+
colored_header(label='', description='', color_name='blue-30')
|
40 |
+
response_container = st.container()
|
41 |
+
|
42 |
+
# User input
|
43 |
+
def get_text():
|
44 |
+
return st.text_input("You: ", "", key="input")
|
45 |
+
|
46 |
+
with input_container:
|
47 |
+
user_input = get_text()
|
48 |
+
|
49 |
+
# AI Response Generation
|
50 |
+
def generate_response(prompt):
|
51 |
+
try:
|
52 |
+
response = st.session_state.chatbot.chat(prompt)
|
53 |
+
return response
|
54 |
+
except Exception as e:
|
55 |
+
return f"An error occurred: {e}"
|
56 |
+
|
57 |
+
# Display conversation
|
58 |
+
with response_container:
|
59 |
+
if user_input:
|
60 |
+
response = generate_response(user_input)
|
61 |
+
st.session_state.past.append(user_input)
|
62 |
+
st.session_state.generated.append(response)
|
63 |
+
|
64 |
+
if st.session_state['generated']:
|
65 |
+
for i in range(len(st.session_state['generated'])):
|
66 |
+
message(st.session_state['past'][i], is_user=True, key=f"{i}_user")
|
67 |
+
message(st.session_state["generated"][i], key=f"{i}")
|
app_v2.py
ADDED
@@ -0,0 +1,73 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
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 |
+
from hugchat.login import Login
|
7 |
+
|
8 |
+
st.set_page_config(page_title="HugChat - An LLM-powered Streamlit app")
|
9 |
+
|
10 |
+
# Sidebar contents
|
11 |
+
with st.sidebar:
|
12 |
+
st.title('🤗💬 HugChat App')
|
13 |
+
|
14 |
+
st.header('Hugging Face Login')
|
15 |
+
hf_email = st.text_input('Enter E-mail:', type='password')
|
16 |
+
hf_pass = st.text_input('Enter password:', type='password')
|
17 |
+
|
18 |
+
st.markdown('''
|
19 |
+
## About
|
20 |
+
This app is an LLM-powered chatbot built using:
|
21 |
+
- [Streamlit](https://streamlit.io/)
|
22 |
+
- [HugChat](https://github.com/Soulter/hugging-chat-api)
|
23 |
+
- [OpenAssistant/oasst-sft-6-llama-30b-xor](https://huggingface.co/OpenAssistant/oasst-sft-6-llama-30b-xor) LLM model
|
24 |
+
|
25 |
+
''')
|
26 |
+
add_vertical_space(5)
|
27 |
+
st.write('Made with ❤️ by [Data Professor](https://youtube.com/dataprofessor)')
|
28 |
+
|
29 |
+
# Generate empty lists for generated and past.
|
30 |
+
## generated stores AI generated responses
|
31 |
+
if 'generated' not in st.session_state:
|
32 |
+
st.session_state['generated'] = ["I'm HugChat, How may I help you?"]
|
33 |
+
## past stores User's questions
|
34 |
+
if 'past' not in st.session_state:
|
35 |
+
st.session_state['past'] = ['Hi!']
|
36 |
+
|
37 |
+
# Layout of input/response containers
|
38 |
+
input_container = st.container()
|
39 |
+
colored_header(label='', description='', color_name='blue-30')
|
40 |
+
response_container = st.container()
|
41 |
+
|
42 |
+
# User input
|
43 |
+
## Function for taking user provided prompt as input
|
44 |
+
def get_text():
|
45 |
+
input_text = st.text_input("You: ", "", key="input")
|
46 |
+
return input_text
|
47 |
+
## Applying the user input box
|
48 |
+
with input_container:
|
49 |
+
user_input = get_text()
|
50 |
+
|
51 |
+
# Response output
|
52 |
+
## Function for taking user prompt as input followed by producing AI generated responses
|
53 |
+
def generate_response(prompt, email, passwd):
|
54 |
+
# Hugging Face Login
|
55 |
+
sign = Login(email, passwd)
|
56 |
+
cookies = sign.login()
|
57 |
+
sign.saveCookies()
|
58 |
+
# Create ChatBot
|
59 |
+
chatbot = hugchat.ChatBot(cookies=cookies.get_dict())
|
60 |
+
response = chatbot.chat(prompt)
|
61 |
+
return response
|
62 |
+
|
63 |
+
## Conditional display of AI generated responses as a function of user provided prompts
|
64 |
+
with response_container:
|
65 |
+
if user_input and hf_email and hf_pass:
|
66 |
+
response = generate_response(user_input, hf_email, hf_pass)
|
67 |
+
st.session_state.past.append(user_input)
|
68 |
+
st.session_state.generated.append(response)
|
69 |
+
|
70 |
+
if st.session_state['generated']:
|
71 |
+
for i in range(len(st.session_state['generated'])):
|
72 |
+
message(st.session_state['past'][i], is_user=True, key=str(i) + '_user')
|
73 |
+
message(st.session_state["generated"][i], key=str(i))
|
app_v3.py
ADDED
@@ -0,0 +1,69 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
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)
|
img/placeholder.md
ADDED
@@ -0,0 +1 @@
|
|
|
|
|
1 |
+
|
img/streamlit.png
ADDED
![]() |
langchain_app.py
ADDED
@@ -0,0 +1,51 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
import streamlit as st
|
2 |
+
from langchain.chains import ConversationChain
|
3 |
+
from hugchat import hugchat
|
4 |
+
from hugchat.login import Login
|
5 |
+
|
6 |
+
st.set_page_config(page_title="HugChat - An LLM-powered Streamlit app")
|
7 |
+
st.title('🤗💬 HugChat App')
|
8 |
+
|
9 |
+
# Hugging Face Credentials
|
10 |
+
with st.sidebar:
|
11 |
+
st.header('Hugging Face Login')
|
12 |
+
hf_email = st.text_input('Enter E-mail:', type='password')
|
13 |
+
hf_pass = st.text_input('Enter password:', type='password')
|
14 |
+
|
15 |
+
# Store AI generated responses
|
16 |
+
if "messages" not in st.session_state.keys():
|
17 |
+
st.session_state.messages = [{"role": "assistant", "content": "I'm HugChat, How may I help you?"}]
|
18 |
+
|
19 |
+
# Display existing chat messages
|
20 |
+
for message in st.session_state.messages:
|
21 |
+
with st.chat_message(message["role"]):
|
22 |
+
st.write(message["content"])
|
23 |
+
|
24 |
+
# Function for generating LLM response
|
25 |
+
def generate_response(prompt, email, passwd):
|
26 |
+
# Hugging Face Login
|
27 |
+
sign = Login(email, passwd)
|
28 |
+
cookies = sign.login()
|
29 |
+
sign.saveCookies()
|
30 |
+
# Create ChatBot
|
31 |
+
chatbot = hugchat.ChatBot(cookies=cookies.get_dict())
|
32 |
+
chain = ConversationChain(llm=chatbot)
|
33 |
+
response = chain.run(input=prompt)
|
34 |
+
return response
|
35 |
+
|
36 |
+
# Prompt for user input and save
|
37 |
+
if prompt := st.chat_input():
|
38 |
+
st.session_state.messages.append({"role": "user", "content": prompt})
|
39 |
+
with st.chat_message("user"):
|
40 |
+
st.write(prompt)
|
41 |
+
|
42 |
+
# If last message is not from assistant, we need to generate a new response
|
43 |
+
if st.session_state.messages[-1]["role"] != "assistant":
|
44 |
+
# Call LLM
|
45 |
+
with st.chat_message("assistant"):
|
46 |
+
with st.spinner("Thinking..."):
|
47 |
+
response = generate_response(prompt, hf_email, hf_pass)
|
48 |
+
st.write(response)
|
49 |
+
|
50 |
+
message = {"role": "assistant", "content": response}
|
51 |
+
st.session_state.messages.append(message)
|
notebook/hf.env
ADDED
@@ -0,0 +1,2 @@
|
|
|
|
|
|
|
1 |
+
EMAIL='[email protected]'
|
2 |
+
PASS='xxxxxxxxxxx'
|
streamlit.png
ADDED
![]() |