Spaces:
Sleeping
Sleeping
Download app.py from Kumardba224/memory-chatbot: direct link, hf CLI and curl.
- Browser
- Download file 1.99 kB
-
https://huggingface.co/spaces/Kumardba224/memory-chatbot/resolve/main/app.py
- Command line
-
hf download hf://spaces/Kumardba224/memory-chatbot/app.py
-
curl -L -o app.py https://huggingface.co/spaces/Kumardba224/memory-chatbot/resolve/main/app.py
1.99 kB
| import streamlit as st | |
| from memory_backend import ( | |
| chat_with_memory | |
| ) | |
| # ----------------------------------- | |
| # Page Config | |
| # ----------------------------------- | |
| st.set_page_config( | |
| page_title="AI Memory Chatbot", | |
| page_icon="🧠", | |
| layout="wide" | |
| ) | |
| # ----------------------------------- | |
| # Title | |
| # ----------------------------------- | |
| st.title("🧠 Day 9 - AI Memory Chatbot") | |
| st.markdown(""" | |
| This chatbot remembers: | |
| - previous messages | |
| - conversation context | |
| - follow-up discussions | |
| """) | |
| # ----------------------------------- | |
| # Session State | |
| # ----------------------------------- | |
| if "messages" not in st.session_state: | |
| st.session_state.messages = [] | |
| # ----------------------------------- | |
| # Display Chat History | |
| # ----------------------------------- | |
| for msg in st.session_state.messages: | |
| with st.chat_message(msg["role"]): | |
| st.write(msg["content"]) | |
| # ----------------------------------- | |
| # User Input | |
| # ----------------------------------- | |
| user_input = st.chat_input( | |
| "Ask something..." | |
| ) | |
| # ----------------------------------- | |
| # Chat Logic | |
| # ----------------------------------- | |
| if user_input: | |
| # Save user message | |
| st.session_state.messages.append( | |
| { | |
| "role": "user", | |
| "content": user_input | |
| } | |
| ) | |
| # Display user message | |
| with st.chat_message("user"): | |
| st.write(user_input) | |
| # Generate response | |
| with st.chat_message("assistant"): | |
| with st.spinner("Thinking..."): | |
| response = chat_with_memory( | |
| user_input | |
| ) | |
| st.write(response) | |
| # Save assistant response | |
| st.session_state.messages.append( | |
| { | |
| "role": "assistant", | |
| "content": response | |
| } | |
| ) | |
| # ----------------------------------- | |
| # Sidebar | |
| # ----------------------------------- | |
| with st.sidebar: | |
| st.header("⚙️ Controls") | |
| if st.button("Clear Chat"): | |
| st.session_state.messages = [] | |
| st.rerun() |