Spaces:
Build error
Build error
Update app.py
Browse files
app.py
CHANGED
|
@@ -1,7 +1,6 @@
|
|
| 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
|
|
@@ -61,7 +60,36 @@ def conversational_tutor(user_input):
|
|
| 61 |
|
| 62 |
# Streamlit UI
|
| 63 |
st.set_page_config(page_title="Data Science AI Tutor", layout="wide")
|
| 64 |
-
st.
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 65 |
st.write("Ask me anything about Data Science!")
|
| 66 |
|
| 67 |
# Layout with two columns
|
|
@@ -91,20 +119,30 @@ with col1:
|
|
| 91 |
|
| 92 |
with col2:
|
| 93 |
st.write("### 🤖 Chatbot")
|
| 94 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 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))
|
| 107 |
conn.commit()
|
| 108 |
|
| 109 |
# Store bot response in memory
|
| 110 |
-
memory.save_context({}, {"
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
import streamlit as st
|
| 2 |
import uuid
|
| 3 |
import sqlite3
|
|
|
|
| 4 |
from langchain_google_genai import ChatGoogleGenerativeAI
|
| 5 |
from langchain.schema import SystemMessage, HumanMessage, AIMessage
|
| 6 |
from langchain.memory import ConversationBufferMemory
|
|
|
|
| 60 |
|
| 61 |
# Streamlit UI
|
| 62 |
st.set_page_config(page_title="Data Science AI Tutor", layout="wide")
|
| 63 |
+
st.markdown("""
|
| 64 |
+
<style>
|
| 65 |
+
.chat-container {
|
| 66 |
+
max-width: 700px;
|
| 67 |
+
margin: auto;
|
| 68 |
+
}
|
| 69 |
+
.user-bubble {
|
| 70 |
+
background-color: #DCF8C6;
|
| 71 |
+
padding: 10px;
|
| 72 |
+
border-radius: 10px;
|
| 73 |
+
margin: 5px;
|
| 74 |
+
text-align: right;
|
| 75 |
+
max-width: 60%;
|
| 76 |
+
float: right;
|
| 77 |
+
clear: both;
|
| 78 |
+
}
|
| 79 |
+
.bot-bubble {
|
| 80 |
+
background-color: #E6E6E6;
|
| 81 |
+
padding: 10px;
|
| 82 |
+
border-radius: 10px;
|
| 83 |
+
margin: 5px;
|
| 84 |
+
text-align: left;
|
| 85 |
+
max-width: 60%;
|
| 86 |
+
float: left;
|
| 87 |
+
clear: both;
|
| 88 |
+
}
|
| 89 |
+
</style>
|
| 90 |
+
""", unsafe_allow_html=True)
|
| 91 |
+
|
| 92 |
+
st.title("📱 Data Science AI Tutor - WhatsApp Style Chat")
|
| 93 |
st.write("Ask me anything about Data Science!")
|
| 94 |
|
| 95 |
# Layout with two columns
|
|
|
|
| 119 |
|
| 120 |
with col2:
|
| 121 |
st.write("### 🤖 Chatbot")
|
| 122 |
+
chat_container = st.container()
|
| 123 |
+
|
| 124 |
+
# Display chat messages
|
| 125 |
+
cursor.execute("SELECT message, response FROM chat_history WHERE user_id = ?", (st.session_state["user_id"],))
|
| 126 |
+
chat_data = cursor.fetchall()
|
| 127 |
+
|
| 128 |
+
with chat_container:
|
| 129 |
+
st.markdown('<div class="chat-container">', unsafe_allow_html=True)
|
| 130 |
+
for user_msg, bot_response in chat_data:
|
| 131 |
+
st.markdown(f'<div class="user-bubble">{user_msg}</div>', unsafe_allow_html=True)
|
| 132 |
+
st.markdown(f'<div class="bot-bubble">{bot_response}</div>', unsafe_allow_html=True)
|
| 133 |
+
st.markdown('</div>', unsafe_allow_html=True)
|
| 134 |
+
|
| 135 |
+
user_input = st.text_input("Type your message...", placeholder="Ask me a data science question")
|
| 136 |
|
| 137 |
if user_input:
|
| 138 |
response = conversational_tutor(user_input)
|
| 139 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 140 |
# Store conversation in database
|
| 141 |
cursor.execute("INSERT INTO chat_history (user_id, message, response) VALUES (?, ?, ?)", (st.session_state["user_id"], user_input, response))
|
| 142 |
conn.commit()
|
| 143 |
|
| 144 |
# Store bot response in memory
|
| 145 |
+
memory.save_context({"human": user_input}, {"ai": response})
|
| 146 |
+
|
| 147 |
+
# Refresh the chat to show new messages
|
| 148 |
+
st.experimental_rerun()
|