Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
|
@@ -7,13 +7,13 @@ from langchain_core.output_parsers import StrOutputParser
|
|
| 7 |
from langchain_community.chat_message_histories import SQLChatMessageHistory
|
| 8 |
from langchain_core.runnables.history import RunnableWithMessageHistory
|
| 9 |
|
| 10 |
-
# Load API key
|
| 11 |
GOOGLE_API_KEY = st.secrets.get("GOOGLE_API_KEY")
|
| 12 |
|
| 13 |
# Set up the Gemini 1.5 Pro model
|
| 14 |
llm = GoogleGenerativeAI(api_key=GOOGLE_API_KEY, model="gemini-1.5-pro")
|
| 15 |
|
| 16 |
-
# Initialize SQLite database
|
| 17 |
conn = sqlite3.connect("chat_history.db", check_same_thread=False)
|
| 18 |
cursor = conn.cursor()
|
| 19 |
cursor.execute("""
|
|
@@ -43,7 +43,7 @@ def chat_history(session_id):
|
|
| 43 |
connection="sqlite:///chat_history.db"
|
| 44 |
)
|
| 45 |
|
| 46 |
-
# Generate unique session ID
|
| 47 |
if "session_id" not in st.session_state:
|
| 48 |
st.session_state.session_id = str(uuid.uuid4())
|
| 49 |
|
|
@@ -92,30 +92,34 @@ if "messages" not in st.session_state:
|
|
| 92 |
|
| 93 |
chat_container = st.container()
|
| 94 |
|
| 95 |
-
# Display chat history
|
| 96 |
with chat_container:
|
| 97 |
for role, content in st.session_state.messages:
|
| 98 |
with st.chat_message(role):
|
| 99 |
st.markdown(content)
|
| 100 |
|
| 101 |
-
# User input
|
| 102 |
user_input = st.text_input("Type your message here:", key="user_message")
|
| 103 |
|
|
|
|
| 104 |
if user_input:
|
|
|
|
| 105 |
save_message(session_id, "user", user_input)
|
| 106 |
st.session_state.messages.append(("user", user_input))
|
| 107 |
|
| 108 |
-
# Invoke
|
| 109 |
config = {'configurable': {'session_id': session_id}}
|
| 110 |
response = chat.invoke({'prompt': user_input}, config)
|
| 111 |
|
|
|
|
| 112 |
save_message(session_id, "assistant", response)
|
| 113 |
st.session_state.messages.append(("assistant", response))
|
| 114 |
|
| 115 |
-
#
|
| 116 |
with chat_container:
|
| 117 |
with st.chat_message("assistant"):
|
| 118 |
st.markdown(response)
|
| 119 |
|
| 120 |
-
#
|
| 121 |
st.session_state.pop("user_message")
|
|
|
|
|
|
| 7 |
from langchain_community.chat_message_histories import SQLChatMessageHistory
|
| 8 |
from langchain_core.runnables.history import RunnableWithMessageHistory
|
| 9 |
|
| 10 |
+
# Load API key
|
| 11 |
GOOGLE_API_KEY = st.secrets.get("GOOGLE_API_KEY")
|
| 12 |
|
| 13 |
# Set up the Gemini 1.5 Pro model
|
| 14 |
llm = GoogleGenerativeAI(api_key=GOOGLE_API_KEY, model="gemini-1.5-pro")
|
| 15 |
|
| 16 |
+
# Initialize SQLite database
|
| 17 |
conn = sqlite3.connect("chat_history.db", check_same_thread=False)
|
| 18 |
cursor = conn.cursor()
|
| 19 |
cursor.execute("""
|
|
|
|
| 43 |
connection="sqlite:///chat_history.db"
|
| 44 |
)
|
| 45 |
|
| 46 |
+
# Generate unique session ID
|
| 47 |
if "session_id" not in st.session_state:
|
| 48 |
st.session_state.session_id = str(uuid.uuid4())
|
| 49 |
|
|
|
|
| 92 |
|
| 93 |
chat_container = st.container()
|
| 94 |
|
| 95 |
+
# Display chat history
|
| 96 |
with chat_container:
|
| 97 |
for role, content in st.session_state.messages:
|
| 98 |
with st.chat_message(role):
|
| 99 |
st.markdown(content)
|
| 100 |
|
| 101 |
+
# User input at the bottom
|
| 102 |
user_input = st.text_input("Type your message here:", key="user_message")
|
| 103 |
|
| 104 |
+
# If user submits a message
|
| 105 |
if user_input:
|
| 106 |
+
# Save user message
|
| 107 |
save_message(session_id, "user", user_input)
|
| 108 |
st.session_state.messages.append(("user", user_input))
|
| 109 |
|
| 110 |
+
# Invoke AI model
|
| 111 |
config = {'configurable': {'session_id': session_id}}
|
| 112 |
response = chat.invoke({'prompt': user_input}, config)
|
| 113 |
|
| 114 |
+
# Save AI response
|
| 115 |
save_message(session_id, "assistant", response)
|
| 116 |
st.session_state.messages.append(("assistant", response))
|
| 117 |
|
| 118 |
+
# Display AI response
|
| 119 |
with chat_container:
|
| 120 |
with st.chat_message("assistant"):
|
| 121 |
st.markdown(response)
|
| 122 |
|
| 123 |
+
# Clear the input field after message submission
|
| 124 |
st.session_state.pop("user_message")
|
| 125 |
+
st.rerun() # Refresh the app
|