Spaces:
Sleeping
Sleeping
Create app.py
Browse files
app.py
ADDED
@@ -0,0 +1,82 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
import os
|
2 |
+
from embedchain import App
|
3 |
+
import streamlit as st
|
4 |
+
|
5 |
+
with st.sidebar:
|
6 |
+
huggingface_access_token = st.text_input("Hugging face Token", key="chatbot_api_key", type="password")
|
7 |
+
"[Get Hugging Face Access Token](https://huggingface.co/settings/tokens)"
|
8 |
+
"[View the source code](https://github.com/embedchain/examples/mistral-streamlit)"
|
9 |
+
|
10 |
+
|
11 |
+
config = {
|
12 |
+
'llm': {
|
13 |
+
'provider': 'huggingface',
|
14 |
+
'config': {
|
15 |
+
'model': 'mistralai/Mistral-7B-Instruct-v0.2',
|
16 |
+
'top_p': 0.5
|
17 |
+
}
|
18 |
+
},
|
19 |
+
'embedder': {
|
20 |
+
'provider': 'huggingface',
|
21 |
+
'config': {
|
22 |
+
'model': 'sentence-transformers/all-mpnet-base-v2'
|
23 |
+
}
|
24 |
+
}
|
25 |
+
}
|
26 |
+
|
27 |
+
|
28 |
+
st.title("💬 Chatbot")
|
29 |
+
st.caption("🚀 An Embedchain app powered by Mistral!")
|
30 |
+
if "messages" not in st.session_state:
|
31 |
+
st.session_state.messages = [
|
32 |
+
{
|
33 |
+
"role": "assistant",
|
34 |
+
"content": """
|
35 |
+
Hi! I'm a chatbot. I can answer questions and learn new things!\n
|
36 |
+
Ask me anything and if you want me to learn something do `/add <source>`.\n
|
37 |
+
I can learn mostly everything. :)
|
38 |
+
""",
|
39 |
+
}
|
40 |
+
]
|
41 |
+
|
42 |
+
for message in st.session_state.messages:
|
43 |
+
with st.chat_message(message["role"]):
|
44 |
+
st.markdown(message["content"])
|
45 |
+
|
46 |
+
if prompt := st.chat_input("Ask me anything!"):
|
47 |
+
if not st.session_state.chatbot_api_key:
|
48 |
+
st.error("Please enter your Hugging Face Access Token")
|
49 |
+
st.stop()
|
50 |
+
|
51 |
+
os.environ["HUGGINGFACE_ACCESS_TOKEN"] = st.session_state.chatbot_api_key
|
52 |
+
app = App.from_config(config = config)
|
53 |
+
app.add("https://en.wikipedia.org/wiki/Indian_Premier_League")
|
54 |
+
|
55 |
+
if prompt.startswith("/add"):
|
56 |
+
with st.chat_message("user"):
|
57 |
+
st.markdown(prompt)
|
58 |
+
st.session_state.messages.append({"role": "user", "content": prompt})
|
59 |
+
prompt = prompt.replace("/add", "").strip()
|
60 |
+
with st.chat_message("assistant"):
|
61 |
+
message_placeholder = st.empty()
|
62 |
+
message_placeholder.markdown("Adding to knowledge base...")
|
63 |
+
app.add(prompt)
|
64 |
+
message_placeholder.markdown(f"Added {prompt} to knowledge base!")
|
65 |
+
st.session_state.messages.append({"role": "assistant", "content": f"Added {prompt} to knowledge base!"})
|
66 |
+
st.stop()
|
67 |
+
|
68 |
+
with st.chat_message("user"):
|
69 |
+
st.markdown(prompt)
|
70 |
+
st.session_state.messages.append({"role": "user", "content": prompt})
|
71 |
+
|
72 |
+
with st.chat_message("assistant"):
|
73 |
+
msg_placeholder = st.empty()
|
74 |
+
msg_placeholder.markdown("Thinking...")
|
75 |
+
full_response = ""
|
76 |
+
|
77 |
+
for response in app.chat(prompt):
|
78 |
+
msg_placeholder.empty()
|
79 |
+
full_response += response
|
80 |
+
|
81 |
+
msg_placeholder.markdown(full_response)
|
82 |
+
st.session_state.messages.append({"role": "assistant", "content": full_response})
|