Spaces:
Build error
Build error
Update app.py
Browse files
app.py
CHANGED
|
@@ -1,23 +1,19 @@
|
|
| 1 |
import streamlit as st
|
| 2 |
import uuid
|
| 3 |
import sqlite3
|
| 4 |
-
|
| 5 |
from langchain_google_genai import ChatGoogleGenerativeAI
|
| 6 |
from langchain.schema import SystemMessage, HumanMessage, AIMessage
|
| 7 |
from langchain.memory import ConversationBufferMemory
|
| 8 |
from langchain.memory.chat_message_histories import SQLChatMessageHistory
|
| 9 |
from langchain_core.output_parsers import StrOutputParser
|
| 10 |
|
| 11 |
-
# Set up the Gemini API key
|
| 12 |
-
GEMINI_API_KEY = "AIzaSyBuhWQHGfMQewIY_gdnmbzzYoori6faeUo"
|
| 13 |
-
genai.configure(api_key=GEMINI_API_KEY)
|
| 14 |
-
|
| 15 |
# Initialize session state for unique user
|
| 16 |
if "user_id" not in st.session_state:
|
| 17 |
st.session_state["user_id"] = str(uuid.uuid4()) # Unique user session
|
| 18 |
|
| 19 |
# Set up SQLite database for chat history
|
| 20 |
-
conn = sqlite3.connect("chat_history.db")
|
| 21 |
cursor = conn.cursor()
|
| 22 |
cursor.execute("""
|
| 23 |
CREATE TABLE IF NOT EXISTS chat_history (
|
|
@@ -50,7 +46,7 @@ def conversational_tutor(user_input):
|
|
| 50 |
User: {user_input}
|
| 51 |
"""
|
| 52 |
|
| 53 |
-
chat_model = ChatGoogleGenerativeAI(model="gemini-pro")
|
| 54 |
response = chat_model.invoke(prompt)
|
| 55 |
|
| 56 |
# Extract content properly
|
|
@@ -64,36 +60,47 @@ def conversational_tutor(user_input):
|
|
| 64 |
return response_text
|
| 65 |
|
| 66 |
# Streamlit UI
|
| 67 |
-
st.
|
|
|
|
| 68 |
st.write("Ask me anything about Data Science!")
|
| 69 |
|
| 70 |
# Layout with two columns
|
| 71 |
col1, col2 = st.columns([1, 2])
|
| 72 |
|
| 73 |
with col1:
|
| 74 |
-
st.write("### Chat History")
|
| 75 |
cursor.execute("SELECT id, message FROM chat_history WHERE user_id = ?", (st.session_state["user_id"],))
|
| 76 |
rows = cursor.fetchall()
|
| 77 |
messages_to_delete = []
|
|
|
|
| 78 |
for row in rows:
|
| 79 |
msg_id, user_msg = row
|
| 80 |
-
if st.button(f"Delete {user_msg[:30]}...", key=f"delete_{msg_id}"):
|
| 81 |
messages_to_delete.append(msg_id)
|
| 82 |
-
|
| 83 |
# Delete selected messages
|
| 84 |
for msg_id in messages_to_delete:
|
| 85 |
cursor.execute("DELETE FROM chat_history WHERE id = ?", (msg_id,))
|
| 86 |
conn.commit()
|
| 87 |
|
| 88 |
-
|
| 89 |
-
|
| 90 |
-
|
|
|
|
|
|
|
| 91 |
|
|
|
|
|
|
|
|
|
|
|
|
|
| 92 |
if user_input:
|
| 93 |
response = conversational_tutor(user_input)
|
| 94 |
|
| 95 |
# Display response
|
| 96 |
-
st.
|
|
|
|
|
|
|
|
|
|
| 97 |
|
| 98 |
# Store conversation in database
|
| 99 |
cursor.execute("INSERT INTO chat_history (user_id, message, response) VALUES (?, ?, ?)", (st.session_state["user_id"], user_input, response))
|
|
@@ -101,9 +108,3 @@ with col2:
|
|
| 101 |
|
| 102 |
# Store bot response in memory
|
| 103 |
memory.save_context({}, {"output": response})
|
| 104 |
-
|
| 105 |
-
# Button to clear entire chat history
|
| 106 |
-
if st.button("Clear Chat History"):
|
| 107 |
-
cursor.execute("DELETE FROM chat_history WHERE user_id = ?", (st.session_state["user_id"],))
|
| 108 |
-
conn.commit()
|
| 109 |
-
memory.clear()
|
|
|
|
| 1 |
import streamlit as st
|
| 2 |
import uuid
|
| 3 |
import sqlite3
|
| 4 |
+
|
| 5 |
from langchain_google_genai import ChatGoogleGenerativeAI
|
| 6 |
from langchain.schema import SystemMessage, HumanMessage, AIMessage
|
| 7 |
from langchain.memory import ConversationBufferMemory
|
| 8 |
from langchain.memory.chat_message_histories import SQLChatMessageHistory
|
| 9 |
from langchain_core.output_parsers import StrOutputParser
|
| 10 |
|
|
|
|
|
|
|
|
|
|
|
|
|
| 11 |
# Initialize session state for unique user
|
| 12 |
if "user_id" not in st.session_state:
|
| 13 |
st.session_state["user_id"] = str(uuid.uuid4()) # Unique user session
|
| 14 |
|
| 15 |
# Set up SQLite database for chat history
|
| 16 |
+
conn = sqlite3.connect("chat_history.db", check_same_thread=False)
|
| 17 |
cursor = conn.cursor()
|
| 18 |
cursor.execute("""
|
| 19 |
CREATE TABLE IF NOT EXISTS chat_history (
|
|
|
|
| 46 |
User: {user_input}
|
| 47 |
"""
|
| 48 |
|
| 49 |
+
chat_model = ChatGoogleGenerativeAI(model="gemini-1.5-pro", temperature=0.7, google_api_key="AIzaSyBuhWQHGfMQewIY_gdnmbzzYoori6faeUo")
|
| 50 |
response = chat_model.invoke(prompt)
|
| 51 |
|
| 52 |
# Extract content properly
|
|
|
|
| 60 |
return response_text
|
| 61 |
|
| 62 |
# Streamlit UI
|
| 63 |
+
st.set_page_config(page_title="Data Science AI Tutor", layout="wide")
|
| 64 |
+
st.title("π§ Data Science AI Tutor")
|
| 65 |
st.write("Ask me anything about Data Science!")
|
| 66 |
|
| 67 |
# Layout with two columns
|
| 68 |
col1, col2 = st.columns([1, 2])
|
| 69 |
|
| 70 |
with col1:
|
| 71 |
+
st.write("### π Chat History")
|
| 72 |
cursor.execute("SELECT id, message FROM chat_history WHERE user_id = ?", (st.session_state["user_id"],))
|
| 73 |
rows = cursor.fetchall()
|
| 74 |
messages_to_delete = []
|
| 75 |
+
|
| 76 |
for row in rows:
|
| 77 |
msg_id, user_msg = row
|
| 78 |
+
if st.button(f"π Delete {user_msg[:30]}...", key=f"delete_{msg_id}"):
|
| 79 |
messages_to_delete.append(msg_id)
|
| 80 |
+
|
| 81 |
# Delete selected messages
|
| 82 |
for msg_id in messages_to_delete:
|
| 83 |
cursor.execute("DELETE FROM chat_history WHERE id = ?", (msg_id,))
|
| 84 |
conn.commit()
|
| 85 |
|
| 86 |
+
if st.button("π Clear Chat History"):
|
| 87 |
+
cursor.execute("DELETE FROM chat_history WHERE user_id = ?", (st.session_state["user_id"],))
|
| 88 |
+
conn.commit()
|
| 89 |
+
memory.clear()
|
| 90 |
+
st.success("Chat history cleared!")
|
| 91 |
|
| 92 |
+
with col2:
|
| 93 |
+
st.write("### π€ Chatbot")
|
| 94 |
+
user_input = st.text_input("Enter your question:", placeholder="Type your data science question here...")
|
| 95 |
+
|
| 96 |
if user_input:
|
| 97 |
response = conversational_tutor(user_input)
|
| 98 |
|
| 99 |
# Display response
|
| 100 |
+
with st.chat_message("user"):
|
| 101 |
+
st.write(user_input)
|
| 102 |
+
with st.chat_message("assistant"):
|
| 103 |
+
st.write(response)
|
| 104 |
|
| 105 |
# Store conversation in database
|
| 106 |
cursor.execute("INSERT INTO chat_history (user_id, message, response) VALUES (?, ?, ?)", (st.session_state["user_id"], user_input, response))
|
|
|
|
| 108 |
|
| 109 |
# Store bot response in memory
|
| 110 |
memory.save_context({}, {"output": response})
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|