abanm commited on
Commit
fa4a809
·
verified ·
1 Parent(s): dac402b

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +33 -29
app.py CHANGED
@@ -1,25 +1,37 @@
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,10 +47,7 @@ def call_api(message):
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
@@ -59,25 +68,20 @@ def call_api(message):
59
  except requests.exceptions.RequestException as e:
60
  yield f"Error: {str(e)}"
61
 
62
- # Chat History Initialization
63
- if "messages" not in st.session_state:
64
- st.session_state["messages"] = [{"role": "assistant", "content": "Hello! How can I assist you today?"}]
65
-
66
  # Main Chat UI
67
  st.title("🐶 DUBSChat")
68
  st.markdown("Empowering you with a Sustainable AI")
69
 
70
- # Chat Display
71
- for message in st.session_state["messages"]:
72
- if message["role"] == "user":
73
- st.chat_message("user").write(message["content"])
74
- elif message["role"] == "assistant":
75
- st.chat_message("assistant").write(message["content"])
76
 
77
  # User Input
78
  if prompt := st.chat_input():
79
- # Append the user's message to the chat history
80
- st.session_state["messages"].append({"role": "user", "content": prompt})
81
  st.chat_message("user").write(prompt)
82
 
83
  with st.spinner("Translating Dubs language (Woof Woof!)"):
@@ -87,8 +91,8 @@ if prompt := st.chat_input():
87
  full_response += response_chunk
88
  assistant_message_placeholder.write(full_response) # Update progressively
89
 
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})
 
1
  import streamlit as st
2
  import requests
3
  import json
4
+ from langchain.chains import ConversationChain
5
  from langchain.memory import ConversationBufferMemory
6
+ from langchain.chat_models import ChatOpenAI
7
 
8
  # Constants
9
  SPACE_URL = "https://abanm-dubs.hf.space/api/generate"
10
+ API_KEY = "s3cr3t_k3y" # Replace with your actual API key
11
 
12
+ # Streamlit Configurations
13
  st.set_page_config(page_title="🐶 DUBSChat", layout="centered")
14
 
 
 
 
 
15
  # Sidebar Configuration
16
  with st.sidebar:
17
  dubs_key = st.text_input("Enter Dubs Key", key="chatbot_api_key", type="password")
18
+ st.markdown("### Chat Sessions")
19
+ if "sessions" not in st.session_state:
20
+ st.session_state["sessions"] = {"Default": ConversationBufferMemory()}
21
+ session_keys = list(st.session_state["sessions"].keys())
22
+ selected_session = st.selectbox("Select Chat Session", session_keys)
23
+ new_session_name = st.text_input("New Session Name")
24
+ if st.button("Create New Session"):
25
+ if new_session_name and new_session_name not in st.session_state["sessions"]:
26
+ st.session_state["sessions"][new_session_name] = ConversationBufferMemory()
27
+ st.experimental_rerun()
28
+
29
+ # LangChain Configuration
30
+ if "conversation" not in st.session_state:
31
+ st.session_state["conversation"] = ConversationChain(
32
+ llm=ChatOpenAI(temperature=0),
33
+ memory=st.session_state["sessions"][selected_session],
34
+ )
35
 
36
  def call_api(message):
37
  """
 
47
  "Authorization": f"Bearer {dubs_key}",
48
  "Content-Type": "application/json",
49
  }
50
+ payload = {"prompt": message} # Match the API's expected input key
 
 
 
51
 
52
  try:
53
  # Send request with streaming enabled
 
68
  except requests.exceptions.RequestException as e:
69
  yield f"Error: {str(e)}"
70
 
 
 
 
 
71
  # Main Chat UI
72
  st.title("🐶 DUBSChat")
73
  st.markdown("Empowering you with a Sustainable AI")
74
 
75
+ # Chat Display for the selected session
76
+ memory = st.session_state["sessions"][selected_session]
77
+ for message in memory.chat_memory.messages:
78
+ role = "user" if message.type == "human" else "assistant"
79
+ st.chat_message(role).write(message.content)
 
80
 
81
  # User Input
82
  if prompt := st.chat_input():
83
+ # Append the user's message to the chat memory
84
+ memory.chat_memory.add_user_message(prompt)
85
  st.chat_message("user").write(prompt)
86
 
87
  with st.spinner("Translating Dubs language (Woof Woof!)"):
 
91
  full_response += response_chunk
92
  assistant_message_placeholder.write(full_response) # Update progressively
93
 
94
+ # Append the assistant's response to the chat memory
95
+ memory.chat_memory.add_ai_message(full_response)
96
 
97
+ # Update the LangChain conversation to use the selected session's memory
98
+ st.session_state["conversation"].memory = memory