Update app.py
Browse files
app.py
CHANGED
|
@@ -1,19 +1,25 @@
|
|
| 1 |
import streamlit as st
|
| 2 |
import requests
|
| 3 |
import json
|
|
|
|
| 4 |
|
| 5 |
# Constants
|
| 6 |
SPACE_URL = "https://abanm-dubs.hf.space/api/generate"
|
| 7 |
-
API_KEY = "s3cr3t_k3y" # Replace with your actual API key
|
| 8 |
|
| 9 |
# Streamlit Configurations (must come first)
|
| 10 |
st.set_page_config(page_title="🐶 DUBSChat", layout="centered")
|
| 11 |
|
|
|
|
|
|
|
|
|
|
|
|
|
| 12 |
# Sidebar Configuration
|
| 13 |
with st.sidebar:
|
| 14 |
dubs_key = st.text_input("Enter Dubs Key", key="chatbot_api_key", type="password")
|
| 15 |
st.markdown("Dubs Recall")
|
| 16 |
-
st.
|
|
|
|
|
|
|
| 17 |
|
| 18 |
def call_api(message):
|
| 19 |
"""
|
|
@@ -29,7 +35,10 @@ def call_api(message):
|
|
| 29 |
"Authorization": f"Bearer {dubs_key}",
|
| 30 |
"Content-Type": "application/json",
|
| 31 |
}
|
| 32 |
-
|
|
|
|
|
|
|
|
|
|
| 33 |
|
| 34 |
try:
|
| 35 |
# Send request with streaming enabled
|
|
@@ -81,4 +90,5 @@ if prompt := st.chat_input():
|
|
| 81 |
# Append the final assistant's response to the chat history
|
| 82 |
st.session_state["messages"].append({"role": "assistant", "content": full_response})
|
| 83 |
|
| 84 |
-
|
|
|
|
|
|
| 1 |
import streamlit as st
|
| 2 |
import requests
|
| 3 |
import json
|
| 4 |
+
from langchain.memory import ConversationBufferMemory
|
| 5 |
|
| 6 |
# Constants
|
| 7 |
SPACE_URL = "https://abanm-dubs.hf.space/api/generate"
|
|
|
|
| 8 |
|
| 9 |
# Streamlit Configurations (must come first)
|
| 10 |
st.set_page_config(page_title="🐶 DUBSChat", layout="centered")
|
| 11 |
|
| 12 |
+
# Initialize memory for context retention
|
| 13 |
+
if "memory" not in st.session_state:
|
| 14 |
+
st.session_state["memory"] = ConversationBufferMemory()
|
| 15 |
+
|
| 16 |
# Sidebar Configuration
|
| 17 |
with st.sidebar:
|
| 18 |
dubs_key = st.text_input("Enter Dubs Key", key="chatbot_api_key", type="password")
|
| 19 |
st.markdown("Dubs Recall")
|
| 20 |
+
if st.button("Show Conversation History"):
|
| 21 |
+
if "memory" in st.session_state:
|
| 22 |
+
st.text_area("Conversation History", st.session_state["memory"].buffer)
|
| 23 |
|
| 24 |
def call_api(message):
|
| 25 |
"""
|
|
|
|
| 35 |
"Authorization": f"Bearer {dubs_key}",
|
| 36 |
"Content-Type": "application/json",
|
| 37 |
}
|
| 38 |
+
# Add context to the prompt
|
| 39 |
+
context = st.session_state["memory"].buffer
|
| 40 |
+
prompt = f"{context}\nUser: {message}\nAI:"
|
| 41 |
+
payload = {"prompt": prompt} # Match the API's expected input key
|
| 42 |
|
| 43 |
try:
|
| 44 |
# Send request with streaming enabled
|
|
|
|
| 90 |
# Append the final assistant's response to the chat history
|
| 91 |
st.session_state["messages"].append({"role": "assistant", "content": full_response})
|
| 92 |
|
| 93 |
+
# Save user input and bot response to memory for context retention
|
| 94 |
+
st.session_state["memory"].save_context({"User": prompt}, {"AI": full_response})
|