Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
|
@@ -30,26 +30,29 @@
|
|
| 30 |
# st.write(f"Chatbot: {resp}")
|
| 31 |
|
| 32 |
|
| 33 |
-
|
| 34 |
import os
|
| 35 |
import streamlit as st
|
| 36 |
from langchain.chains import ConversationChain
|
| 37 |
from langchain_openai import ChatOpenAI
|
| 38 |
from langchain.memory import ConversationBufferMemory
|
| 39 |
from streamlit_extras.add_vertical_space import add_vertical_space
|
| 40 |
-
from streamlit_extras.chat_elements import message
|
| 41 |
|
| 42 |
-
# Set OpenAI API Key
|
| 43 |
if "openai_api_key" in st.secrets:
|
| 44 |
os.environ["OPENAI_API_KEY"] = st.secrets["openai_api_key"]
|
| 45 |
else:
|
| 46 |
st.error("🔑 OpenAI API Key is missing! Please add it to Streamlit secrets.")
|
| 47 |
st.stop()
|
| 48 |
|
| 49 |
-
# Initialize chatbot
|
| 50 |
def init_chatbot(model="gpt-4o-mini"):
|
| 51 |
-
|
| 52 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 53 |
|
| 54 |
if "chatbot" not in st.session_state:
|
| 55 |
st.session_state.chatbot = init_chatbot()
|
|
@@ -60,16 +63,22 @@ st.markdown("""
|
|
| 60 |
body {
|
| 61 |
background-color: #f5f5f5;
|
| 62 |
}
|
| 63 |
-
.
|
| 64 |
-
|
| 65 |
-
padding: 20px;
|
| 66 |
border-radius: 10px;
|
| 67 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 68 |
}
|
| 69 |
</style>
|
| 70 |
""", unsafe_allow_html=True)
|
| 71 |
|
| 72 |
-
# Sidebar -
|
| 73 |
st.sidebar.title("⚙️ Settings")
|
| 74 |
model_choice = st.sidebar.radio("Select Model", ("gpt-4o-mini", "gpt-4", "gpt-3.5-turbo"))
|
| 75 |
|
|
@@ -79,43 +88,42 @@ if model_choice != st.session_state.chatbot.llm.model_name:
|
|
| 79 |
|
| 80 |
# Title and Description
|
| 81 |
st.title("💬 LangChain AI Chatbot")
|
| 82 |
-
st.write("### Hi, I'm a chatbot
|
| 83 |
|
| 84 |
-
# Chat history
|
| 85 |
if "chat_history" not in st.session_state:
|
| 86 |
st.session_state.chat_history = []
|
| 87 |
|
| 88 |
-
# User Input
|
| 89 |
-
user_input = st.
|
| 90 |
|
| 91 |
# Process input
|
| 92 |
if user_input:
|
| 93 |
with st.spinner("Thinking..."):
|
| 94 |
-
|
| 95 |
-
|
| 96 |
-
|
| 97 |
-
|
|
|
|
|
|
|
|
|
|
| 98 |
|
| 99 |
# Display chat history
|
| 100 |
st.write("### 🗨️ Conversation")
|
| 101 |
-
|
| 102 |
-
|
| 103 |
-
|
| 104 |
-
for role, text in st.session_state.chat_history:
|
| 105 |
-
if role == "user":
|
| 106 |
-
message(text, is_user=True, avatar_style="thumbs")
|
| 107 |
-
else:
|
| 108 |
-
message(text, is_user=False, avatar_style="bottts")
|
| 109 |
-
|
| 110 |
-
# Add some spacing
|
| 111 |
-
add_vertical_space(2)
|
| 112 |
|
| 113 |
# Collapsible Chat History
|
| 114 |
-
with st.expander("📜 Chat History"):
|
| 115 |
for role, text in st.session_state.chat_history:
|
| 116 |
st.write(f"**{role.capitalize()}**: {text}")
|
| 117 |
|
|
|
|
|
|
|
|
|
|
| 118 |
# Footer
|
| 119 |
st.markdown("---")
|
| 120 |
st.markdown("Developed with ❤️ using Streamlit & LangChain")
|
| 121 |
|
|
|
|
|
|
| 30 |
# st.write(f"Chatbot: {resp}")
|
| 31 |
|
| 32 |
|
|
|
|
| 33 |
import os
|
| 34 |
import streamlit as st
|
| 35 |
from langchain.chains import ConversationChain
|
| 36 |
from langchain_openai import ChatOpenAI
|
| 37 |
from langchain.memory import ConversationBufferMemory
|
| 38 |
from streamlit_extras.add_vertical_space import add_vertical_space
|
|
|
|
| 39 |
|
| 40 |
+
# Set up OpenAI API Key securely
|
| 41 |
if "openai_api_key" in st.secrets:
|
| 42 |
os.environ["OPENAI_API_KEY"] = st.secrets["openai_api_key"]
|
| 43 |
else:
|
| 44 |
st.error("🔑 OpenAI API Key is missing! Please add it to Streamlit secrets.")
|
| 45 |
st.stop()
|
| 46 |
|
| 47 |
+
# Initialize chatbot with memory
|
| 48 |
def init_chatbot(model="gpt-4o-mini"):
|
| 49 |
+
try:
|
| 50 |
+
memory = ConversationBufferMemory()
|
| 51 |
+
chatbot = ConversationChain(llm=ChatOpenAI(model=model), memory=memory, verbose=False)
|
| 52 |
+
return chatbot
|
| 53 |
+
except Exception as e:
|
| 54 |
+
st.error(f"⚠️ Error initializing chatbot: {e}")
|
| 55 |
+
return None
|
| 56 |
|
| 57 |
if "chatbot" not in st.session_state:
|
| 58 |
st.session_state.chatbot = init_chatbot()
|
|
|
|
| 63 |
body {
|
| 64 |
background-color: #f5f5f5;
|
| 65 |
}
|
| 66 |
+
.stChatMessage {
|
| 67 |
+
padding: 10px;
|
|
|
|
| 68 |
border-radius: 10px;
|
| 69 |
+
margin: 5px 0;
|
| 70 |
+
}
|
| 71 |
+
.user-message {
|
| 72 |
+
background-color: #dcf8c6;
|
| 73 |
+
text-align: right;
|
| 74 |
+
}
|
| 75 |
+
.bot-message {
|
| 76 |
+
background-color: #ffffff;
|
| 77 |
}
|
| 78 |
</style>
|
| 79 |
""", unsafe_allow_html=True)
|
| 80 |
|
| 81 |
+
# Sidebar - Model Selection
|
| 82 |
st.sidebar.title("⚙️ Settings")
|
| 83 |
model_choice = st.sidebar.radio("Select Model", ("gpt-4o-mini", "gpt-4", "gpt-3.5-turbo"))
|
| 84 |
|
|
|
|
| 88 |
|
| 89 |
# Title and Description
|
| 90 |
st.title("💬 LangChain AI Chatbot")
|
| 91 |
+
st.write("### Hi, I'm a chatbot powered by GPT. How can I assist you today?")
|
| 92 |
|
| 93 |
+
# Chat history storage
|
| 94 |
if "chat_history" not in st.session_state:
|
| 95 |
st.session_state.chat_history = []
|
| 96 |
|
| 97 |
+
# User Input via Chat Input (better UX)
|
| 98 |
+
user_input = st.chat_input("Type your message here...")
|
| 99 |
|
| 100 |
# Process input
|
| 101 |
if user_input:
|
| 102 |
with st.spinner("Thinking..."):
|
| 103 |
+
try:
|
| 104 |
+
response = st.session_state.chatbot.run(user_input)
|
| 105 |
+
if response:
|
| 106 |
+
st.session_state.chat_history.append(("user", user_input))
|
| 107 |
+
st.session_state.chat_history.append(("bot", response))
|
| 108 |
+
except Exception as e:
|
| 109 |
+
st.error(f"⚠️ Error generating response: {e}")
|
| 110 |
|
| 111 |
# Display chat history
|
| 112 |
st.write("### 🗨️ Conversation")
|
| 113 |
+
for role, text in st.session_state.chat_history:
|
| 114 |
+
with st.chat_message(role):
|
| 115 |
+
st.markdown(f"**{role.capitalize()}**: {text}")
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 116 |
|
| 117 |
# Collapsible Chat History
|
| 118 |
+
with st.expander("📜 View Full Chat History"):
|
| 119 |
for role, text in st.session_state.chat_history:
|
| 120 |
st.write(f"**{role.capitalize()}**: {text}")
|
| 121 |
|
| 122 |
+
# Add spacing
|
| 123 |
+
add_vertical_space(2)
|
| 124 |
+
|
| 125 |
# Footer
|
| 126 |
st.markdown("---")
|
| 127 |
st.markdown("Developed with ❤️ using Streamlit & LangChain")
|
| 128 |
|
| 129 |
+
|