CoolFace
Apppublic

AndreJacobs514/chat-clone

sourceHugging Facemitupdated 2y agoView on Hugging Face
0likes
app.py58 linesDownload Raw Back to root
1import streamlit as st2import os3 4from backend import query, create_chain5from langchain_core.messages import HumanMessage, AIMessage6from warnings import filterwarnings7filterwarnings("ignore")8 9 10@st.cache_data(show_spinner=False)11def set_key() -> None:12    os.environ["GROQ_API_KEY"] = "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"13 14 15def app_ui_init():16    st.set_page_config(page_title="Chat Bot", page_icon="๐Ÿš€")17    st.header("Chat Bot")18    st.subheader("A simple chat bot to answer your queries")19    set_key()20 21 22def main():23    app_ui_init()24    chain = create_chain()25 26    # clear chat history button27    if st.button("Clear Chat History"):28        chain.get_session_history("1").clear()29        st.rerun()30 31    # input box for user to enter the query32    user_prompt = st.chat_input("Enter your query here")33 34    # display the response35    memory = chain.get_session_history("1").messages36    for message in memory:37        if isinstance(message, HumanMessage):38            with st.chat_message("human"):39                st.markdown(message.content)40        elif isinstance(message, AIMessage):41            with st.chat_message("ai"):42                st.markdown(message.content.replace("Assistant: ", ""))43 44    # waiting for user prompt45    if user_prompt:46        with st.spinner("Processing..."):47            response = query(chain, question=user_prompt)48 49        # display the response50        with st.chat_message("human"):51            st.markdown(user_prompt)52        with st.chat_message("ai"):53            st.markdown(response)54 55 56if __name__ == "__main__":57    main()58