Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
|
@@ -39,21 +39,20 @@ 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 |
-
|
| 44 |
-
|
| 45 |
-
|
| 46 |
-
|
| 47 |
-
|
|
|
|
|
|
|
|
|
|
| 48 |
memory = ConversationBufferMemory()
|
| 49 |
-
|
| 50 |
-
llm=ChatOpenAI(model="gpt-4o-mini"),
|
| 51 |
-
memory=memory,
|
| 52 |
-
verbose=False
|
| 53 |
-
)
|
| 54 |
-
return chatbot
|
| 55 |
|
| 56 |
-
chatbot
|
|
|
|
| 57 |
|
| 58 |
# Custom Styling
|
| 59 |
st.markdown("""
|
|
@@ -74,13 +73,13 @@ st.markdown("""
|
|
| 74 |
st.sidebar.title("⚙️ Settings")
|
| 75 |
model_choice = st.sidebar.radio("Select Model", ("gpt-4o-mini", "gpt-4", "gpt-3.5-turbo"))
|
| 76 |
|
| 77 |
-
# Update model
|
| 78 |
-
if model_choice:
|
| 79 |
-
chatbot
|
| 80 |
|
| 81 |
# Title and Description
|
| 82 |
st.title("💬 LangChain AI Chatbot")
|
| 83 |
-
st.write("### Hi, I'm a chatbot built with
|
| 84 |
|
| 85 |
# Chat history
|
| 86 |
if "chat_history" not in st.session_state:
|
|
@@ -92,9 +91,10 @@ user_input = st.text_input("You:", placeholder="Ask me anything...")
|
|
| 92 |
# Process input
|
| 93 |
if user_input:
|
| 94 |
with st.spinner("Thinking..."):
|
| 95 |
-
response = chatbot.run(user_input)
|
| 96 |
-
|
| 97 |
-
|
|
|
|
| 98 |
|
| 99 |
# Display chat history
|
| 100 |
st.write("### 🗨️ Conversation")
|
|
|
|
| 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 (Ensure you add it in Streamlit Secrets)
|
| 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 |
memory = ConversationBufferMemory()
|
| 52 |
+
return ConversationChain(llm=ChatOpenAI(model=model), memory=memory, verbose=False)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 53 |
|
| 54 |
+
if "chatbot" not in st.session_state:
|
| 55 |
+
st.session_state.chatbot = init_chatbot()
|
| 56 |
|
| 57 |
# Custom Styling
|
| 58 |
st.markdown("""
|
|
|
|
| 73 |
st.sidebar.title("⚙️ Settings")
|
| 74 |
model_choice = st.sidebar.radio("Select Model", ("gpt-4o-mini", "gpt-4", "gpt-3.5-turbo"))
|
| 75 |
|
| 76 |
+
# Update chatbot model if changed
|
| 77 |
+
if model_choice != st.session_state.chatbot.llm.model_name:
|
| 78 |
+
st.session_state.chatbot = init_chatbot(model_choice)
|
| 79 |
|
| 80 |
# Title and Description
|
| 81 |
st.title("💬 LangChain AI Chatbot")
|
| 82 |
+
st.write("### Hi, I'm a chatbot built with LangChain powered by GPT. How can I assist you today?")
|
| 83 |
|
| 84 |
# Chat history
|
| 85 |
if "chat_history" not in st.session_state:
|
|
|
|
| 91 |
# Process input
|
| 92 |
if user_input:
|
| 93 |
with st.spinner("Thinking..."):
|
| 94 |
+
response = st.session_state.chatbot.run(user_input)
|
| 95 |
+
if response:
|
| 96 |
+
st.session_state.chat_history.append(("user", user_input))
|
| 97 |
+
st.session_state.chat_history.append(("bot", response))
|
| 98 |
|
| 99 |
# Display chat history
|
| 100 |
st.write("### 🗨️ Conversation")
|