Update app.py
Browse files
app.py
CHANGED
|
@@ -1,6 +1,5 @@
|
|
| 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.llms.base import LLM
|
|
@@ -14,8 +13,10 @@ st.set_page_config(page_title="🐶 DUBSChat", layout="centered")
|
|
| 14 |
|
| 15 |
# Sidebar Configuration
|
| 16 |
with st.sidebar:
|
| 17 |
-
dubs_key
|
| 18 |
-
|
|
|
|
|
|
|
| 19 |
st.markdown("### Chat Sessions")
|
| 20 |
if "sessions" not in st.session_state:
|
| 21 |
st.session_state["sessions"] = {"Default": ConversationBufferMemory()}
|
|
@@ -27,6 +28,11 @@ with st.sidebar:
|
|
| 27 |
st.session_state["sessions"][new_session_name] = ConversationBufferMemory()
|
| 28 |
st.experimental_set_query_params(session=new_session_name)
|
| 29 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 30 |
# Define Custom LLM for the fine-tuned model
|
| 31 |
class CustomLLM(LLM):
|
| 32 |
"""
|
|
@@ -34,15 +40,15 @@ class CustomLLM(LLM):
|
|
| 34 |
"""
|
| 35 |
|
| 36 |
def _call(self, prompt: str, stop: Optional[list] = None) -> str:
|
|
|
|
| 37 |
if not dubs_key:
|
| 38 |
return "Error: Missing Dubs Key. Please provide a valid key in the sidebar."
|
| 39 |
|
| 40 |
headers = {
|
| 41 |
-
|
| 42 |
-
|
| 43 |
}
|
| 44 |
-
payload = {"prompt":
|
| 45 |
-
|
| 46 |
|
| 47 |
try:
|
| 48 |
response = requests.post(SPACE_URL, json=payload, headers=headers, stream=True)
|
|
@@ -92,7 +98,10 @@ if prompt := st.chat_input():
|
|
| 92 |
with st.spinner("Translating Dubs language (Woof Woof!)"):
|
| 93 |
assistant_message_placeholder = st.chat_message("assistant").empty()
|
| 94 |
full_response = st.session_state["conversation"].run(
|
| 95 |
-
f"System:The following is a friendly conversation between a user and an AI.
|
|
|
|
|
|
|
|
|
|
| 96 |
)
|
| 97 |
assistant_message_placeholder.write(full_response)
|
| 98 |
|
|
@@ -101,3 +110,4 @@ if prompt := st.chat_input():
|
|
| 101 |
|
| 102 |
# Update the LangChain conversation to use the selected session's memory
|
| 103 |
st.session_state["conversation"].memory = memory
|
|
|
|
|
|
| 1 |
import streamlit as st
|
| 2 |
import requests
|
|
|
|
| 3 |
from langchain.chains import ConversationChain
|
| 4 |
from langchain.memory import ConversationBufferMemory
|
| 5 |
from langchain.llms.base import LLM
|
|
|
|
| 13 |
|
| 14 |
# Sidebar Configuration
|
| 15 |
with st.sidebar:
|
| 16 |
+
if "dubs_key" not in st.session_state:
|
| 17 |
+
st.session_state["dubs_key"] = ""
|
| 18 |
+
st.session_state["dubs_key"] = st.text_input("Enter Dubs Key", key="chatbot_api_key", type="password")
|
| 19 |
+
|
| 20 |
st.markdown("### Chat Sessions")
|
| 21 |
if "sessions" not in st.session_state:
|
| 22 |
st.session_state["sessions"] = {"Default": ConversationBufferMemory()}
|
|
|
|
| 28 |
st.session_state["sessions"][new_session_name] = ConversationBufferMemory()
|
| 29 |
st.experimental_set_query_params(session=new_session_name)
|
| 30 |
|
| 31 |
+
# Ensure Dubs Key is Provided
|
| 32 |
+
if not st.session_state["dubs_key"]:
|
| 33 |
+
st.warning("Please enter a valid Dubs Key in the sidebar to proceed.")
|
| 34 |
+
st.stop()
|
| 35 |
+
|
| 36 |
# Define Custom LLM for the fine-tuned model
|
| 37 |
class CustomLLM(LLM):
|
| 38 |
"""
|
|
|
|
| 40 |
"""
|
| 41 |
|
| 42 |
def _call(self, prompt: str, stop: Optional[list] = None) -> str:
|
| 43 |
+
dubs_key = st.session_state.get("dubs_key", None)
|
| 44 |
if not dubs_key:
|
| 45 |
return "Error: Missing Dubs Key. Please provide a valid key in the sidebar."
|
| 46 |
|
| 47 |
headers = {
|
| 48 |
+
"Authorization": f"Bearer {dubs_key}",
|
| 49 |
+
"Content-Type": "application/json",
|
| 50 |
}
|
| 51 |
+
payload = {"prompt": prompt} # Match the API's expected input key
|
|
|
|
| 52 |
|
| 53 |
try:
|
| 54 |
response = requests.post(SPACE_URL, json=payload, headers=headers, stream=True)
|
|
|
|
| 98 |
with st.spinner("Translating Dubs language (Woof Woof!)"):
|
| 99 |
assistant_message_placeholder = st.chat_message("assistant").empty()
|
| 100 |
full_response = st.session_state["conversation"].run(
|
| 101 |
+
f"System:The following is a friendly conversation between a user and an AI. "
|
| 102 |
+
f"The AI is talkative and provides lots of specific details from its context. "
|
| 103 |
+
f"If the AI does not know the answer to a question, it truthfully says it does not know.\n\n"
|
| 104 |
+
f"Current conversation:\nUser: {prompt}\nAssistant:"
|
| 105 |
)
|
| 106 |
assistant_message_placeholder.write(full_response)
|
| 107 |
|
|
|
|
| 110 |
|
| 111 |
# Update the LangChain conversation to use the selected session's memory
|
| 112 |
st.session_state["conversation"].memory = memory
|
| 113 |
+
|