Spaces:
Sleeping
Sleeping
Updates for interface
Browse files
app.py
CHANGED
|
@@ -1,30 +1,120 @@
|
|
| 1 |
-
import os
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 2 |
import streamlit as st
|
| 3 |
from langchain.chains import ConversationChain
|
| 4 |
from langchain_openai import ChatOpenAI
|
| 5 |
from langchain.memory import ConversationBufferMemory
|
|
|
|
|
|
|
| 6 |
|
|
|
|
| 7 |
os.environ["OPENAI_API_KEY"] = ""
|
| 8 |
|
| 9 |
-
#
|
| 10 |
@st.cache_resource
|
| 11 |
def init_chatbot():
|
| 12 |
memory = ConversationBufferMemory()
|
| 13 |
chatbot = ConversationChain(
|
| 14 |
-
llm
|
| 15 |
-
memory
|
| 16 |
-
verbose
|
| 17 |
)
|
| 18 |
-
|
| 19 |
return chatbot
|
| 20 |
|
| 21 |
-
|
| 22 |
-
st.title("Langchain Chatbot")
|
| 23 |
-
st.write("Hi, I'm a chatbot built with Langchain powered by GPT. How can I assist you today?")
|
| 24 |
|
| 25 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 26 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 27 |
if user_input:
|
| 28 |
-
with st.spinner("Thinking
|
| 29 |
-
|
| 30 |
-
st.
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
# import os
|
| 2 |
+
# import streamlit as st
|
| 3 |
+
# from langchain.chains import ConversationChain
|
| 4 |
+
# from langchain_openai import ChatOpenAI
|
| 5 |
+
# from langchain.memory import ConversationBufferMemory
|
| 6 |
+
|
| 7 |
+
# os.environ["OPENAI_API_KEY"] = ""
|
| 8 |
+
|
| 9 |
+
# # Intialize the chatbot
|
| 10 |
+
# @st.cache_resource
|
| 11 |
+
# def init_chatbot():
|
| 12 |
+
# memory = ConversationBufferMemory()
|
| 13 |
+
# chatbot = ConversationChain(
|
| 14 |
+
# llm =ChatOpenAI(model = "gpt-4o-mini"),
|
| 15 |
+
# memory = memory,
|
| 16 |
+
# verbose = False
|
| 17 |
+
# )
|
| 18 |
+
|
| 19 |
+
# return chatbot
|
| 20 |
+
|
| 21 |
+
# # Streamlit Application
|
| 22 |
+
# st.title("Langchain Chatbot")
|
| 23 |
+
# st.write("Hi, I'm a chatbot built with Langchain powered by GPT. How can I assist you today?")
|
| 24 |
+
|
| 25 |
+
# user_input = st.text_input("You:", placeholder = "Ask me anything....")
|
| 26 |
+
|
| 27 |
+
# if user_input:
|
| 28 |
+
# with st.spinner("Thinking......"):
|
| 29 |
+
# resp = chatbot.run(user_input)
|
| 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 |
os.environ["OPENAI_API_KEY"] = ""
|
| 44 |
|
| 45 |
+
# Initialize the chatbot
|
| 46 |
@st.cache_resource
|
| 47 |
def init_chatbot():
|
| 48 |
memory = ConversationBufferMemory()
|
| 49 |
chatbot = ConversationChain(
|
| 50 |
+
llm=ChatOpenAI(model="gpt-4o-mini"),
|
| 51 |
+
memory=memory,
|
| 52 |
+
verbose=False
|
| 53 |
)
|
|
|
|
| 54 |
return chatbot
|
| 55 |
|
| 56 |
+
chatbot = init_chatbot()
|
|
|
|
|
|
|
| 57 |
|
| 58 |
+
# Custom Styling
|
| 59 |
+
st.markdown("""
|
| 60 |
+
<style>
|
| 61 |
+
body {
|
| 62 |
+
background-color: #f5f5f5;
|
| 63 |
+
}
|
| 64 |
+
.chat-container {
|
| 65 |
+
background-color: #ffffff;
|
| 66 |
+
padding: 20px;
|
| 67 |
+
border-radius: 10px;
|
| 68 |
+
box-shadow: 0px 4px 6px rgba(0,0,0,0.1);
|
| 69 |
+
}
|
| 70 |
+
</style>
|
| 71 |
+
""", unsafe_allow_html=True)
|
| 72 |
|
| 73 |
+
# Sidebar - Settings
|
| 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 based on user selection
|
| 78 |
+
if model_choice:
|
| 79 |
+
chatbot.llm = ChatOpenAI(model=model_choice)
|
| 80 |
+
|
| 81 |
+
# Title and Description
|
| 82 |
+
st.title("💬 LangChain AI Chatbot")
|
| 83 |
+
st.write("### Hi, I'm a chatbot built with Langchain powered by GPT. How can I assist you today?")
|
| 84 |
+
|
| 85 |
+
# Chat history
|
| 86 |
+
if "chat_history" not in st.session_state:
|
| 87 |
+
st.session_state.chat_history = []
|
| 88 |
+
|
| 89 |
+
# User Input
|
| 90 |
+
user_input = st.text_input("You:", placeholder="Ask me anything...")
|
| 91 |
+
|
| 92 |
+
# Process input
|
| 93 |
if user_input:
|
| 94 |
+
with st.spinner("Thinking..."):
|
| 95 |
+
response = chatbot.run(user_input)
|
| 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")
|
| 101 |
+
chat_container = st.container()
|
| 102 |
+
|
| 103 |
+
with chat_container:
|
| 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")
|