|
|
import streamlit as st |
|
|
import time |
|
|
import uuid |
|
|
import requests |
|
|
import os |
|
|
import json |
|
|
|
|
|
chat_url = os.environ['CHAT_URL'] |
|
|
|
|
|
def response_generator(user_input): |
|
|
payload = json.dumps({ |
|
|
"user_query": user_input, |
|
|
"thread_id": st.session_state.session_id |
|
|
}) |
|
|
headers = { |
|
|
'Content-Type': 'application/json' |
|
|
} |
|
|
|
|
|
response = requests.request("POST", chat_url, headers=headers, data=payload) |
|
|
|
|
|
ai_response = response.json()['ai_response'] |
|
|
|
|
|
for character in ai_response: |
|
|
yield character |
|
|
time.sleep(0.001) |
|
|
|
|
|
|
|
|
st.title("BookBot 📚 🤖") |
|
|
|
|
|
|
|
|
if "session_id" not in st.session_state: |
|
|
st.session_state.session_id = str(uuid.uuid4()) |
|
|
|
|
|
|
|
|
if "messages" not in st.session_state: |
|
|
st.session_state.messages = [] |
|
|
|
|
|
|
|
|
for message in st.session_state.messages: |
|
|
with st.chat_message(message["role"]): |
|
|
st.markdown(message["content"]) |
|
|
|
|
|
|
|
|
if user_input := st.chat_input("What is up?"): |
|
|
|
|
|
st.session_state.messages.append({"role": "user", "content": user_input.strip()}) |
|
|
|
|
|
with st.chat_message("user"): |
|
|
st.markdown(user_input) |
|
|
|
|
|
|
|
|
with st.chat_message("assistant"): |
|
|
response = st.write_stream(response_generator(user_input.strip())) |
|
|
|
|
|
st.session_state.messages.append({"role": "assistant", "content": response}) |
|
|
|