diff --git a/.gitignore b/.gitignore index 485dee6..ecbfcaa 100644 --- a/.gitignore +++ b/.gitignore @@ -1 +1,2 @@ .idea +.venv \ No newline at end of file diff --git a/streamlit_app.py b/streamlit_app.py index ee6d58f..b7409df 100644 --- a/streamlit_app.py +++ b/streamlit_app.py @@ -1,56 +1,215 @@ import streamlit as st import requests import base64 +import uuid -# Title of the app -st.title("Chat with Kauza Bots") + +# Define the URL of your logo image +logo_url = "https://ksysmoixgyrwexukfifh.supabase.co/storage/v1/object/sign/kauza/logo.png?token=eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJ1cmwiOiJrYXV6YS9sb2dvLnBuZyIsImlhdCI6MTcxODI2NjkxOSwiZXhwIjoxNzIwODU4OTE5fQ.9lhDbWO_1mDCVhxXKQjfd8BuBBRqpcGj05XVh8CZbn8&t=2024-06-13T08%3A21%3A51.191Z" + +# Add a logo and title in the header +st.markdown( + f""" +
+ Logo +

Chat with Kauza bots here

+
+ """, + unsafe_allow_html=True +) # Retrieve URL parameters query_params = st.query_params -bot_id = query_params.get("bot", "ZGJiZjQ4ZWQtM2I2ZC00YTIwLWEwOTEtNjA2ZDBiZDZhNDcwLTY1ZTllZjBkN2FlOGNlMDhlMDcxYjRlMg==") -decoded_data = base64.b64decode(bot_id).decode() +bot_id = query_params.get("bot", "ZGJiZjQ4ZWQtM2I2ZC00YTIwLWEwOTEtNjA2ZDBiZDZhNDcwLTY1ZTllZjBkN2FlOGNlMDhlMDcxYjRlMg") +decoded_data = base64.b64decode(bot_id+'==').decode() parts = decoded_data.split('-') agent_id = parts.pop() tenant_id = "-".join(parts) -# Display the retrieved tenant_id and agent_id -st.write(f"Tenant ID: {tenant_id}") -st.write(f"Agent ID: {agent_id}") +# Generate a unique session_id for each user session +if 'session_id' not in st.session_state: + st.session_state['session_id'] = str(uuid.uuid4()) -if tenant_id and agent_id: - # Input field for the user to enter their message - user_message = st.text_input("You: ", "") +session_id = st.session_state['session_id'] + +# Add custom CSS for button styles and positioning the input box and send button at the bottom +st.markdown( + """ + + """, + unsafe_allow_html=True +) +if tenant_id and agent_id: + # Display the chat history if 'chat_history' not in st.session_state: st.session_state['chat_history'] = [] + # Function to handle and display response elements + def display_response(response): + if "text" in response: + st.markdown(f""" +
+
{response["text"]}
+
+ """, unsafe_allow_html=True) + if "buttons" in response: + button_html = '
' + for button in response["buttons"]: + button_html += f'' + button_html += '
' + st.markdown(button_html, unsafe_allow_html=True) + if "image" in response: + st.image(response["image"]) + if "custom" in response: + st.json(response["custom"]) + if "document" in response["custom"]: + st.markdown(f""" +
+ Bot Icon +
{response["custom"]["document"]["link"]}
+
+ """, unsafe_allow_html=True) + + # Handle displaying messages and buttons in chat history for chat in st.session_state['chat_history']: - st.write(chat) + if isinstance(chat, str): + st.markdown(f""" +
+
{chat}
+
+ """, unsafe_allow_html=True) + elif isinstance(chat, dict): + display_response(chat) - # If the user enters a message, send it to the Rasa server and display the response - if st.button("Send") and user_message: - sender_id = "test_user" # You can set this to a unique ID for each user + # Input field for the user to enter their message + st.markdown('
', unsafe_allow_html=True) + st.markdown( + """ +
+ + +
+ + """, + unsafe_allow_html=True + ) + + # Input field for the user to enter their message + user_message = st.text_input("You: ", "") - # Define the URL of your custom Rasa REST endpoint + # Function to send message to kauza server + def send_message_to_kauza(message): url = f"http://kauza.serveo.net/webhooks/rest/webhook/{tenant_id}/agents/{agent_id}" - payload = { - "sender": sender_id, - "message": user_message + "sender": session_id, + "message": message } - response = requests.post(url, json=payload) + print(response.json()) + return response.json() if response.status_code == 200 else None + + # If the user enters a message, send it to the kauza server and display the response + if st.button("Send") and user_message: + st.session_state['chat_history'].append(user_message) + responses = send_message_to_kauza(user_message) - if response.status_code == 200: - responses = response.json() - bot_responses = [r['text'] for r in responses if 'text' in r] - for bot_response in bot_responses: - st.session_state['chat_history'].append(f"Bot: {bot_response}") - else: - st.write("Error: Could not reach the Rasa server.") + if responses: + for response in responses: + st.session_state['chat_history'].append(response) - st.session_state['chat_history'].append(f"You: {user_message}") - st.experimental_rerun() # Rerun to update the chat history + st.rerun() + else: - st.write("Please provide both Tenant ID and Agent ID as URL parameters to start the chat. Example: `?tenant_id=your_tenant_id&agent_id=your_agent_id`") + st.write("Please provide bot's id as URL parameters to start the chat. Example: `?bot=something`")