Spaces:
Sleeping
Sleeping
Yunus Serhat Bıçakçı
commited on
Commit
·
9d7ba76
1
Parent(s):
daac133
update
Browse files- pages/2_🔲_ChatBot.py +72 -0
pages/2_🔲_ChatBot.py
ADDED
|
@@ -0,0 +1,72 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 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(layout="wide")
|
| 8 |
+
|
| 9 |
+
|
| 10 |
+
st.sidebar.info(
|
| 11 |
+
"""
|
| 12 |
+
- Web App URL: <https://huggingface.co/spaces/yunusserhat/Crime-Map>
|
| 13 |
+
- HuggingFace repository: <https://huggingface.co/spaces/yunusserhat/Crime-Map/tree/main>
|
| 14 |
+
"""
|
| 15 |
+
)
|
| 16 |
+
|
| 17 |
+
st.sidebar.title("Contact")
|
| 18 |
+
st.sidebar.info(
|
| 19 |
+
"""
|
| 20 |
+
Yunus Serhat Bıçakçı at [yunusserhat.com](https://yunusserhat.com) | [GitHub](https://github.com/yunusserhat) | [Twitter](https://twitter.com/yunusserhat) | [LinkedIn](https://www.linkedin.com/in/yunusserhat)
|
| 21 |
+
"""
|
| 22 |
+
)
|
| 23 |
+
|
| 24 |
+
st.title("Crime ChatBot")
|
| 25 |
+
|
| 26 |
+
|
| 27 |
+
# Generate empty lists for generated and past.
|
| 28 |
+
## generated stores AI generated responses
|
| 29 |
+
if 'generated' not in st.session_state:
|
| 30 |
+
st.session_state['generated'] = ["I'm Crime ChatBot, How may I help you?"]
|
| 31 |
+
|
| 32 |
+
## past stores User's questions
|
| 33 |
+
if 'past' not in st.session_state:
|
| 34 |
+
st.session_state['past'] = ['Hello!']
|
| 35 |
+
|
| 36 |
+
# Layout of input/response containers
|
| 37 |
+
input_container = st.container()
|
| 38 |
+
colored_header(label='', description='', color_name='blue-30')
|
| 39 |
+
response_container = st.container()
|
| 40 |
+
|
| 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 |
+
|
| 48 |
+
|
| 49 |
+
## Applying the user input box
|
| 50 |
+
with input_container:
|
| 51 |
+
user_input = get_text()
|
| 52 |
+
|
| 53 |
+
|
| 54 |
+
# Response output
|
| 55 |
+
## Function for taking user prompt as input followed by producing AI generated responses
|
| 56 |
+
def generate_response(prompt):
|
| 57 |
+
chatbot = hugchat.ChatBot()
|
| 58 |
+
response = chatbot.chat(prompt)
|
| 59 |
+
return response
|
| 60 |
+
|
| 61 |
+
|
| 62 |
+
## Conditional display of AI generated responses as a function of user provided prompts
|
| 63 |
+
with response_container:
|
| 64 |
+
if user_input:
|
| 65 |
+
response = generate_response(user_input)
|
| 66 |
+
st.session_state.past.append(user_input)
|
| 67 |
+
st.session_state.generated.append(response)
|
| 68 |
+
|
| 69 |
+
if st.session_state['generated']:
|
| 70 |
+
for i in range(len(st.session_state['generated'])):
|
| 71 |
+
message(st.session_state['past'][i], is_user=True, key=str(i) + '_user')
|
| 72 |
+
message(st.session_state["generated"][i], key=str(i))
|