Spaces:
Sleeping
Sleeping
Commit
·
7f749d4
1
Parent(s):
56a4238
Create botsify
Browse filesI am a Webapp Generator.
- pages/botsify +58 -0
pages/botsify
ADDED
@@ -0,0 +1,58 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
import streamlit as st
|
2 |
+
from streamlit_chat import message
|
3 |
+
from transformers import AutoTokenizer, AutoModelForCausalLM
|
4 |
+
|
5 |
+
if 'conversation' not in st.session_state:
|
6 |
+
st.session_state['conversation'] = None
|
7 |
+
if 'messages' not in st.session_state:
|
8 |
+
st.session_state['messages'] = []
|
9 |
+
if 'model_name' not in st.session_state:
|
10 |
+
st.session_state['model_name'] = "microsoft/DialoGPT-medium"
|
11 |
+
|
12 |
+
# Setting page title and header
|
13 |
+
st.set_page_config(page_title="Chat GPT Clone", page_icon=":robot_face:")
|
14 |
+
st.markdown("<h1 style='text-align: center;'>💻 ChatterBot</h1>", unsafe_allow_html=True)
|
15 |
+
st.subheader("How Can I Help You Today? 🤖")
|
16 |
+
|
17 |
+
st.sidebar.title("😎🗝️")
|
18 |
+
st.session_state['model_name'] = st.sidebar.text_input("Hugging Face Model Name", value="microsoft/DialoGPT-medium")
|
19 |
+
summarise_button = st.sidebar.button("Summarise the conversation", key="summarise")
|
20 |
+
st.sidebar.image('./chatbot.jpg', width=300, use_column_width=True)
|
21 |
+
if summarise_button:
|
22 |
+
summarise_placeholder = st.sidebar.write("Nice chatting with you my friend ❤️:\n\n" + st.session_state['conversation'])
|
23 |
+
|
24 |
+
def get_response(userInput):
|
25 |
+
|
26 |
+
if st.session_state['conversation'] is None:
|
27 |
+
tokenizer = AutoTokenizer.from_pretrained(st.session_state['model_name'])
|
28 |
+
model = AutoModelForCausalLM.from_pretrained(st.session_state['model_name'])
|
29 |
+
st.session_state['conversation'] = ""
|
30 |
+
|
31 |
+
input_ids = tokenizer.encode(userInput + tokenizer.eos_token, return_tensors="pt")
|
32 |
+
outputs = model.generate(input_ids, max_length=1024)
|
33 |
+
response = tokenizer.decode(outputs[0], skip_special_tokens=True)
|
34 |
+
|
35 |
+
st.session_state['conversation'] += "\n" + response
|
36 |
+
|
37 |
+
return response
|
38 |
+
|
39 |
+
response_container = st.container()
|
40 |
+
# Here we will have a container for user input text box
|
41 |
+
container = st.container()
|
42 |
+
|
43 |
+
with container:
|
44 |
+
with st.form(key='my_form', clear_on_submit=True):
|
45 |
+
user_input = st.text_area("Your question goes here:", key='input', height=100)
|
46 |
+
submit_button = st.form_submit_button(label='Send')
|
47 |
+
|
48 |
+
if submit_button:
|
49 |
+
st.session_state['messages'].append(user_input)
|
50 |
+
model_response = get_response(user_input)
|
51 |
+
st.session_state['messages'].append(model_response)
|
52 |
+
|
53 |
+
with response_container:
|
54 |
+
for i in range(len(st.session_state['messages'])):
|
55 |
+
if (i % 2) == 0:
|
56 |
+
message(st.session_state['messages'][i], is_user=True, key=str(i) + '_user')
|
57 |
+
else:
|
58 |
+
message(st.session_state['messages'][i], key=str(i) + '_AI')
|