Spaces:
Paused
Paused
Update app.py
Browse files
app.py
CHANGED
|
@@ -1,80 +1,55 @@
|
|
| 1 |
-
import
|
| 2 |
-
|
| 3 |
-
|
| 4 |
-
|
| 5 |
-
|
| 6 |
-
|
| 7 |
-
|
| 8 |
-
|
| 9 |
-
|
| 10 |
-
|
| 11 |
-
|
| 12 |
-
|
| 13 |
-
|
| 14 |
-
|
| 15 |
-
|
| 16 |
-
|
| 17 |
-
|
| 18 |
-
|
| 19 |
-
)
|
| 20 |
-
|
| 21 |
-
|
| 22 |
-
|
| 23 |
-
st.set_page_config(
|
| 24 |
-
page_title="Personal Chatbot"
|
| 25 |
-
)
|
| 26 |
-
st.header("Persoanl Chatbot")
|
| 27 |
-
st.sidebar.title("Options")
|
| 28 |
-
|
| 29 |
-
def select_llm() -> LlamaCPP:
|
| 30 |
-
return LlamaCPP(
|
| 31 |
-
model_path="/hamxea/Llama-2-7b-chat-hf-activity-fine-tuned-v2",
|
| 32 |
-
temperature=0.1,
|
| 33 |
-
max_new_tokens=500,
|
| 34 |
-
context_window=3900,
|
| 35 |
-
generate_kwargs={},
|
| 36 |
-
model_kwargs={"n_gpu_layers":1},
|
| 37 |
-
messages_to_prompt=messages_to_prompt,
|
| 38 |
-
completion_to_prompt=completion_to_prompt,
|
| 39 |
-
verbose=True,
|
| 40 |
-
)
|
| 41 |
-
|
| 42 |
-
def init_messages() -> None:
|
| 43 |
-
clear_button = st.sidebar.button("Clear Conversation", key="clear")
|
| 44 |
-
if clear_button or "messages" not in st.session_state:
|
| 45 |
-
st.session_state.messages = [
|
| 46 |
-
SystemMessage(
|
| 47 |
-
content="you are a helpful AI assistant. Reply your answer in markdown format."
|
| 48 |
-
)
|
| 49 |
-
]
|
| 50 |
-
|
| 51 |
-
def get_answer(llm, messages) -> str:
|
| 52 |
-
response = llm.complete(messages)
|
| 53 |
-
return response.text
|
| 54 |
-
|
| 55 |
-
def main() -> None:
|
| 56 |
-
init_page()
|
| 57 |
-
llm = select_llm()
|
| 58 |
-
init_messages()
|
| 59 |
-
|
| 60 |
-
if user_input := st.chat_input("Input your question!"):
|
| 61 |
-
st.session_state.messages.append(HumanMessage(content=user_input))
|
| 62 |
-
with st.spinner("Bot is typing ..."):
|
| 63 |
-
answer = get_answer(llm, user_input)
|
| 64 |
-
print(answer)
|
| 65 |
-
st.session_state.messages.append(AIMessage(content=answer))
|
| 66 |
|
| 67 |
-
|
| 68 |
-
|
| 69 |
-
|
| 70 |
-
|
| 71 |
-
|
| 72 |
-
|
| 73 |
-
|
| 74 |
-
|
| 75 |
-
|
| 76 |
-
|
| 77 |
-
|
| 78 |
-
|
| 79 |
-
|
| 80 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import streamlit as st
|
| 2 |
+
from hugchat import hugchat
|
| 3 |
+
from hugchat.login import Login
|
| 4 |
+
|
| 5 |
+
# App title
|
| 6 |
+
st.set_page_config(page_title="π€π¬ HugChat")
|
| 7 |
+
|
| 8 |
+
# Hugging Face Credentials
|
| 9 |
+
with st.sidebar:
|
| 10 |
+
st.title('π€π¬ HugChat')
|
| 11 |
+
if ('EMAIL' in st.secrets) and ('PASS' in st.secrets):
|
| 12 |
+
st.success('HuggingFace Login credentials already provided!', icon='β
')
|
| 13 |
+
hf_email = st.secrets['EMAIL']
|
| 14 |
+
hf_pass = st.secrets['PASS']
|
| 15 |
+
else:
|
| 16 |
+
hf_email = st.text_input('Enter E-mail:', type='password')
|
| 17 |
+
hf_pass = st.text_input('Enter password:', type='password')
|
| 18 |
+
if not (hf_email and hf_pass):
|
| 19 |
+
st.warning('Please enter your credentials!', icon='β οΈ')
|
| 20 |
+
else:
|
| 21 |
+
st.success('Proceed to entering your prompt message!', icon='π')
|
| 22 |
+
st.markdown('π Learn how to build this app in this [blog](https://blog.streamlit.io/how-to-build-an-llm-powered-chatbot-with-streamlit/)!')
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 23 |
|
| 24 |
+
# Store LLM generated responses
|
| 25 |
+
if "messages" not in st.session_state.keys():
|
| 26 |
+
st.session_state.messages = [{"role": "assistant", "content": "How may I help you?"}]
|
| 27 |
+
|
| 28 |
+
# Display chat messages
|
| 29 |
+
for message in st.session_state.messages:
|
| 30 |
+
with st.chat_message(message["role"]):
|
| 31 |
+
st.write(message["content"])
|
| 32 |
+
|
| 33 |
+
# Function for generating LLM response
|
| 34 |
+
def generate_response(prompt_input, email, passwd):
|
| 35 |
+
# Hugging Face Login
|
| 36 |
+
sign = Login(email, passwd)
|
| 37 |
+
cookies = sign.login()
|
| 38 |
+
# Create ChatBot
|
| 39 |
+
chatbot = hugchat.ChatBot(cookies=cookies.get_dict())
|
| 40 |
+
return chatbot.chat(prompt_input)
|
| 41 |
+
|
| 42 |
+
# User-provided prompt
|
| 43 |
+
if prompt := st.chat_input(disabled=not (hf_email and hf_pass)):
|
| 44 |
+
st.session_state.messages.append({"role": "user", "content": prompt})
|
| 45 |
+
with st.chat_message("user"):
|
| 46 |
+
st.write(prompt)
|
| 47 |
+
|
| 48 |
+
# Generate a new response if last message is not from assistant
|
| 49 |
+
if st.session_state.messages[-1]["role"] != "assistant":
|
| 50 |
+
with st.chat_message("assistant"):
|
| 51 |
+
with st.spinner("Thinking..."):
|
| 52 |
+
response = generate_response(prompt, hf_email, hf_pass)
|
| 53 |
+
st.write(response)
|
| 54 |
+
message = {"role": "assistant", "content": response}
|
| 55 |
+
st.session_state.messages.append(message)
|