Spaces:
Build error
Build error
| import os | |
| import random | |
| import streamlit as st | |
| # Decorator | |
| def enable_chat_history(func): | |
| # Clear chat history after switching chatbot | |
| current_page = func.__qualname__ | |
| if "current_page" not in st.session_state: | |
| st.session_state["current_page"] = current_page | |
| if st.session_state["current_page"] != current_page: | |
| try: | |
| st.session_state.pop("current_page", None) | |
| st.session_state.pop("messages", None) | |
| except: | |
| pass | |
| # Show chat history on UI | |
| if "messages" not in st.session_state: | |
| st.session_state["messages"] = [{"role": "assistant", "content": "How can I help you?"}] | |
| for msg in st.session_state["messages"]: | |
| st.chat_message(msg["role"]).write(msg["content"]) | |
| def execute(*args, **kwargs): | |
| func(*args, **kwargs) | |
| return execute | |
| def display_msg(msg, author): | |
| """Method to display message on the UI | |
| Args: | |
| msg (str): message to display | |
| author (str): author of the message -user/assistant | |
| """ | |
| if "messages" not in st.session_state: | |
| st.session_state["messages"] = [] | |
| st.session_state["messages"].append({"role": author, "content": msg}) | |
| st.chat_message(author).write(msg) | |