Update app.py
Browse files
app.py
CHANGED
|
@@ -1,114 +1,82 @@
|
|
| 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
|
| 7 |
-
from typing import Optional
|
| 8 |
|
| 9 |
# Constants
|
| 10 |
SPACE_URL = "https://abanm-dubs.hf.space/api/generate"
|
|
|
|
| 11 |
|
| 12 |
-
# Streamlit Configurations
|
| 13 |
st.set_page_config(page_title="🐶 DUBSChat", layout="centered")
|
| 14 |
|
| 15 |
# Sidebar Configuration
|
| 16 |
with st.sidebar:
|
| 17 |
-
|
| 18 |
-
|
| 19 |
-
st.
|
| 20 |
|
| 21 |
-
|
| 22 |
-
if "sessions" not in st.session_state:
|
| 23 |
-
st.session_state["sessions"] = {"Default": ConversationBufferMemory()}
|
| 24 |
-
session_keys = list(st.session_state["sessions"].keys())
|
| 25 |
-
selected_session = st.selectbox("Select Chat Session", session_keys, index=session_keys.index(st.experimental_get_query_params().get("session", ["Default"])[0]))
|
| 26 |
-
new_session_name = st.text_input("New Session Name")
|
| 27 |
-
if st.button("Create New Session"):
|
| 28 |
-
if new_session_name and new_session_name not in st.session_state["sessions"]:
|
| 29 |
-
st.session_state["sessions"][new_session_name] = ConversationBufferMemory()
|
| 30 |
-
st.experimental_set_query_params(session=new_session_name)
|
| 31 |
-
|
| 32 |
-
# Ensure Dubs Key is Provided
|
| 33 |
-
if not st.session_state["dubs_key"]:
|
| 34 |
-
st.warning("Please enter a valid Dubs Key in the sidebar to proceed.")
|
| 35 |
-
st.stop()
|
| 36 |
-
|
| 37 |
-
# Define Custom LLM for the fine-tuned model
|
| 38 |
-
class CustomLLM(LLM):
|
| 39 |
"""
|
| 40 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 41 |
"""
|
| 42 |
-
|
| 43 |
-
|
| 44 |
-
|
| 45 |
-
|
| 46 |
-
|
| 47 |
-
|
| 48 |
-
|
| 49 |
-
|
| 50 |
-
|
| 51 |
-
|
| 52 |
-
|
| 53 |
-
|
| 54 |
-
|
| 55 |
-
|
| 56 |
-
|
| 57 |
-
|
| 58 |
-
|
| 59 |
-
|
| 60 |
-
|
| 61 |
-
|
| 62 |
-
|
| 63 |
-
|
| 64 |
-
|
| 65 |
-
|
| 66 |
-
|
| 67 |
-
|
| 68 |
-
|
| 69 |
-
|
| 70 |
-
return {"model": "custom_model"}
|
| 71 |
-
|
| 72 |
-
@property
|
| 73 |
-
def _llm_type(self):
|
| 74 |
-
"""Return type of LLM."""
|
| 75 |
-
return "custom_llm"
|
| 76 |
-
|
| 77 |
-
# LangChain Configuration
|
| 78 |
-
if "conversation" not in st.session_state:
|
| 79 |
-
st.session_state["conversation"] = ConversationChain(
|
| 80 |
-
llm=CustomLLM(),
|
| 81 |
-
memory=st.session_state["sessions"][selected_session],
|
| 82 |
-
)
|
| 83 |
|
| 84 |
# Main Chat UI
|
| 85 |
st.title("🐶 DUBSChat")
|
| 86 |
st.markdown("Empowering you with a Sustainable AI")
|
| 87 |
|
| 88 |
-
# Chat Display
|
| 89 |
-
|
| 90 |
-
|
| 91 |
-
|
| 92 |
-
|
|
|
|
| 93 |
|
| 94 |
# User Input
|
| 95 |
if prompt := st.chat_input():
|
| 96 |
-
# Append the user's message to the chat
|
| 97 |
-
|
| 98 |
st.chat_message("user").write(prompt)
|
| 99 |
|
| 100 |
with st.spinner("Translating Dubs language (Woof Woof!)"):
|
| 101 |
assistant_message_placeholder = st.chat_message("assistant").empty()
|
| 102 |
-
full_response =
|
| 103 |
-
|
| 104 |
-
|
| 105 |
-
|
| 106 |
-
f"Current conversation:\nUser: {prompt}\nAssistant:"
|
| 107 |
-
)
|
| 108 |
-
assistant_message_placeholder.write(full_response)
|
| 109 |
-
|
| 110 |
-
# Append the assistant's response to the chat memory
|
| 111 |
-
memory.chat_memory.add_ai_message(full_response)
|
| 112 |
|
| 113 |
-
#
|
| 114 |
-
st.session_state["
|
|
|
|
| 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.markdown("""Coming Soon!""")
|
| 17 |
|
| 18 |
+
def call_api(message):
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 19 |
"""
|
| 20 |
+
Calls the backend API with the user's input using streaming.
|
| 21 |
+
|
| 22 |
+
Args:
|
| 23 |
+
message (str): User's message.
|
| 24 |
+
|
| 25 |
+
Yields:
|
| 26 |
+
str: Streamed response chunks from the backend API.
|
| 27 |
"""
|
| 28 |
+
headers = {
|
| 29 |
+
"Authorization": f"Bearer {dubs_key}",
|
| 30 |
+
"Content-Type": "application/json",
|
| 31 |
+
}
|
| 32 |
+
payload = {"prompt": message} # Match the API's expected input key
|
| 33 |
+
|
| 34 |
+
try:
|
| 35 |
+
# Send request with streaming enabled
|
| 36 |
+
response = requests.post(SPACE_URL, json=payload, headers=headers, stream=True)
|
| 37 |
+
response.raise_for_status()
|
| 38 |
+
|
| 39 |
+
for chunk in response.iter_lines(decode_unicode=True):
|
| 40 |
+
if chunk.strip(): # Ensure the chunk is not empty
|
| 41 |
+
try:
|
| 42 |
+
# Parse JSON chunk
|
| 43 |
+
data = json.loads(chunk)
|
| 44 |
+
if "response" in data:
|
| 45 |
+
yield data["response"]
|
| 46 |
+
except json.JSONDecodeError:
|
| 47 |
+
yield "Error decoding response chunk."
|
| 48 |
+
except requests.exceptions.Timeout:
|
| 49 |
+
yield "Error: The API call timed out."
|
| 50 |
+
except requests.exceptions.RequestException as e:
|
| 51 |
+
yield f"Error: {str(e)}"
|
| 52 |
+
|
| 53 |
+
# Chat History Initialization
|
| 54 |
+
if "messages" not in st.session_state:
|
| 55 |
+
st.session_state["messages"] = [{"role": "assistant", "content": "Hello! How can I assist you today?"}]
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 56 |
|
| 57 |
# Main Chat UI
|
| 58 |
st.title("🐶 DUBSChat")
|
| 59 |
st.markdown("Empowering you with a Sustainable AI")
|
| 60 |
|
| 61 |
+
# Chat Display
|
| 62 |
+
for message in st.session_state["messages"]:
|
| 63 |
+
if message["role"] == "user":
|
| 64 |
+
st.chat_message("user").write(message["content"])
|
| 65 |
+
elif message["role"] == "assistant":
|
| 66 |
+
st.chat_message("assistant").write(message["content"])
|
| 67 |
|
| 68 |
# User Input
|
| 69 |
if prompt := st.chat_input():
|
| 70 |
+
# Append the user's message to the chat history
|
| 71 |
+
st.session_state["messages"].append({"role": "user", "content": prompt})
|
| 72 |
st.chat_message("user").write(prompt)
|
| 73 |
|
| 74 |
with st.spinner("Translating Dubs language (Woof Woof!)"):
|
| 75 |
assistant_message_placeholder = st.chat_message("assistant").empty()
|
| 76 |
+
full_response = "" # To accumulate the entire response
|
| 77 |
+
for response_chunk in call_api(prompt):
|
| 78 |
+
full_response += response_chunk
|
| 79 |
+
assistant_message_placeholder.write(full_response) # Update progressively
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 80 |
|
| 81 |
+
# Append the final assistant's response to the chat history
|
| 82 |
+
st.session_state["messages"].append({"role": "assistant", "content": full_response})
|