Spaces:
Runtime error
Runtime error
Create app.py
Browse files
app.py
ADDED
|
@@ -0,0 +1,53 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import os
|
| 2 |
+
|
| 3 |
+
import streamlit as st
|
| 4 |
+
|
| 5 |
+
from chatbot.data import download_test_data
|
| 6 |
+
from chatbot.data import load_data
|
| 7 |
+
|
| 8 |
+
# add all secrets into environmental variables
|
| 9 |
+
try:
|
| 10 |
+
for key, value in st.secrets.items():
|
| 11 |
+
os.environ[key] = value
|
| 12 |
+
except FileNotFoundError as e:
|
| 13 |
+
print(e)
|
| 14 |
+
print("./streamlit/secrets.toml not found. Assuming secrets are already available" "as environmental variables...")
|
| 15 |
+
|
| 16 |
+
# Initialize message history
|
| 17 |
+
st.header("Chat with André's research 💬 📚")
|
| 18 |
+
|
| 19 |
+
if "messages" not in st.session_state.keys(): # Initialize the chat message history
|
| 20 |
+
st.session_state.messages = [{"role": "assistant", "content": "Ask me a question about André's research!"}]
|
| 21 |
+
|
| 22 |
+
|
| 23 |
+
def main():
|
| 24 |
+
# setup logger sidebar
|
| 25 |
+
# st.sidebar.text("Standard output log:")
|
| 26 |
+
# _sidebar_out = st.sidebar.empty()
|
| 27 |
+
# with rd.stdout(to=_sidebar_out, format='text'):
|
| 28 |
+
# print("test")
|
| 29 |
+
|
| 30 |
+
# setup dataset
|
| 31 |
+
download_test_data()
|
| 32 |
+
index = load_data()
|
| 33 |
+
chat_engine = index.as_chat_engine(chat_mode="condense_question", verbose=True)
|
| 34 |
+
|
| 35 |
+
if prompt := st.chat_input("Your question"): # Prompt for user input and save to chat history
|
| 36 |
+
st.session_state.messages.append({"role": "user", "content": prompt})
|
| 37 |
+
|
| 38 |
+
for message in st.session_state.messages: # Display the prior chat messages
|
| 39 |
+
with st.chat_message(message["role"]):
|
| 40 |
+
st.write(message["content"])
|
| 41 |
+
|
| 42 |
+
# If last message is not from assistant, generate a new response
|
| 43 |
+
if st.session_state.messages[-1]["role"] != "assistant":
|
| 44 |
+
with st.chat_message("assistant"):
|
| 45 |
+
with st.spinner("Thinking..."):
|
| 46 |
+
response = chat_engine.chat(prompt)
|
| 47 |
+
st.write(response.response)
|
| 48 |
+
message = {"role": "assistant", "content": response.response}
|
| 49 |
+
st.session_state.messages.append(message) # Add response to message history
|
| 50 |
+
|
| 51 |
+
|
| 52 |
+
if __name__ == "__main__":
|
| 53 |
+
main()
|