-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathchatbot.py
61 lines (47 loc) · 1.94 KB
/
chatbot.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
import streamlit as st
from hugchat import hugchat
from hugchat.login import Login
st.title("HugChat")
st.write("HugChat is a chatbot that can help you with your mental health.")
# Create a new instance of the HugChat class
with st.sidebar:
st.title("Login Hugchat")
email = st.text_input("Email")
password = st.text_input("Password", type="password")
if st.button("Login"):
try:
sign = Login(email, password)
cookies = sign.login()
st.write("Logging in...")
st.session_state.chatbot = hugchat.ChatBot(
cookies=cookies.get_dict())
st.success("Login successful!")
except Exception as e:
st.error("Invalid credentials. Please try again.")
# Initialize session state for messages if not already done
if "messages" not in st.session_state:
st.session_state.messages = [
{"role": "assistant", "content": "Hello! How can I help you today?"}
]
# Display chat messages
for message in st.session_state.messages:
with st.chat_message(message["role"]):
st.write(message["content"])
def generate_response(prompt_input, chatbot):
# Generate response from chatbot
return chatbot.chat(prompt_input)
# User-provided prompt
chatbot_initialized = "chatbot" in st.session_state
prompt = st.chat_input(disabled=not chatbot_initialized)
if prompt:
st.session_state.messages.append({"role": "user", "content": prompt})
with st.chat_message("user"):
st.write(prompt)
# Generate a new response if last message is not from assistant
if st.session_state.messages[-1]["role"] != "assistant":
with st.chat_message("assistant"):
with st.spinner("Thinking..."):
response = generate_response(prompt, st.session_state.chatbot)
st.write(response)
message = {"role": "assistant", "content": response}
st.session_state.messages.append(message)