Spaces:
Build error
Build error
Update app.py
Browse files
app.py
CHANGED
|
@@ -45,7 +45,7 @@ def conversational_tutor(user_input):
|
|
| 45 |
User: {user_input}
|
| 46 |
"""
|
| 47 |
|
| 48 |
-
chat_model = ChatGoogleGenerativeAI(model="gemini-1.5-pro", temperature=0.7, google_api_key="
|
| 49 |
response = chat_model.invoke(prompt)
|
| 50 |
|
| 51 |
# Extract content properly
|
|
@@ -86,63 +86,53 @@ st.markdown("""
|
|
| 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
|
| 96 |
-
|
|
|
|
| 97 |
|
| 98 |
-
|
| 99 |
-
|
| 100 |
-
|
| 101 |
-
rows = cursor.fetchall()
|
| 102 |
-
messages_to_delete = []
|
| 103 |
-
|
| 104 |
-
for row in rows:
|
| 105 |
-
msg_id, user_msg = row
|
| 106 |
-
if st.button(f"π Delete {user_msg[:30]}...", key=f"delete_{msg_id}"):
|
| 107 |
-
messages_to_delete.append(msg_id)
|
| 108 |
-
|
| 109 |
-
# Delete selected messages
|
| 110 |
-
for msg_id in messages_to_delete:
|
| 111 |
-
cursor.execute("DELETE FROM chat_history WHERE id = ?", (msg_id,))
|
| 112 |
-
conn.commit()
|
| 113 |
|
| 114 |
-
|
| 115 |
-
|
| 116 |
-
|
| 117 |
-
memory.clear()
|
| 118 |
-
st.success("Chat history cleared!")
|
| 119 |
|
| 120 |
-
|
| 121 |
-
|
| 122 |
-
|
| 123 |
-
|
| 124 |
-
|
| 125 |
-
|
| 126 |
-
|
| 127 |
-
|
| 128 |
-
|
| 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 |
-
|
|
|
|
|
|
|
| 136 |
|
| 137 |
-
|
| 138 |
-
|
| 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()
|
|
|
|
| 45 |
User: {user_input}
|
| 46 |
"""
|
| 47 |
|
| 48 |
+
chat_model = ChatGoogleGenerativeAI(model="gemini-1.5-pro", temperature=0.7, google_api_key="AIzaSyBItvysBggCo5T2iE61M209b24e36FmiBo")
|
| 49 |
response = chat_model.invoke(prompt)
|
| 50 |
|
| 51 |
# Extract content properly
|
|
|
|
| 86 |
float: left;
|
| 87 |
clear: both;
|
| 88 |
}
|
| 89 |
+
.chatbox {
|
| 90 |
+
height: 500px;
|
| 91 |
+
overflow-y: auto;
|
| 92 |
+
padding: 10px;
|
| 93 |
+
border: 1px solid #ccc;
|
| 94 |
+
border-radius: 10px;
|
| 95 |
+
background: #f9f9f9;
|
| 96 |
+
}
|
| 97 |
+
.input-container {
|
| 98 |
+
position: fixed;
|
| 99 |
+
bottom: 10px;
|
| 100 |
+
width: 100%;
|
| 101 |
+
text-align: center;
|
| 102 |
+
background: white;
|
| 103 |
+
padding: 10px;
|
| 104 |
+
}
|
| 105 |
</style>
|
| 106 |
""", unsafe_allow_html=True)
|
| 107 |
|
| 108 |
st.title("π± Data Science AI Tutor - WhatsApp Style Chat")
|
| 109 |
st.write("Ask me anything about Data Science!")
|
| 110 |
|
| 111 |
+
# Layout with a single chat interface
|
| 112 |
+
chat_container = st.container()
|
| 113 |
+
st.markdown('<div class="chatbox">', unsafe_allow_html=True)
|
| 114 |
|
| 115 |
+
# Display chat messages
|
| 116 |
+
cursor.execute("SELECT message, response FROM chat_history WHERE user_id = ?", (st.session_state["user_id"],))
|
| 117 |
+
chat_data = cursor.fetchall()
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 118 |
|
| 119 |
+
for user_msg, bot_response in chat_data:
|
| 120 |
+
st.markdown(f'<div class="user-bubble">{user_msg}</div>', unsafe_allow_html=True)
|
| 121 |
+
st.markdown(f'<div class="bot-bubble">{bot_response}</div>', unsafe_allow_html=True)
|
|
|
|
|
|
|
| 122 |
|
| 123 |
+
st.markdown('</div>', unsafe_allow_html=True)
|
| 124 |
+
|
| 125 |
+
# Input field for new messages
|
| 126 |
+
st.markdown('<div class="input-container">', unsafe_allow_html=True)
|
| 127 |
+
user_input = st.text_input("Type your message...", placeholder="Ask me a data science question")
|
| 128 |
+
st.markdown('</div>', unsafe_allow_html=True)
|
| 129 |
+
|
| 130 |
+
if user_input:
|
| 131 |
+
response = conversational_tutor(user_input)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 132 |
|
| 133 |
+
# Store conversation in database
|
| 134 |
+
cursor.execute("INSERT INTO chat_history (user_id, message, response) VALUES (?, ?, ?)", (st.session_state["user_id"], user_input, response))
|
| 135 |
+
conn.commit()
|
| 136 |
|
| 137 |
+
# Store bot response in memory
|
| 138 |
+
memory.save_context({"human": user_input}, {"ai": response})
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|