Update app.py
Browse files
app.py
CHANGED
|
@@ -1,44 +1,55 @@
|
|
| 1 |
import streamlit as st
|
| 2 |
-
from langchain_google_genai import ChatGoogleGenerativeAI
|
| 3 |
from langchain.chains import ConversationChain
|
| 4 |
from langchain.memory import ConversationBufferMemory
|
|
|
|
| 5 |
|
| 6 |
-
#
|
| 7 |
-
st.
|
| 8 |
-
api_key = st.sidebar.text_input("Enter your Google API Key", type="password")
|
| 9 |
-
|
| 10 |
st.title("🤖 AI Conversational Data Science Tutor")
|
| 11 |
|
| 12 |
-
|
| 13 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
| 14 |
llm = ChatGoogleGenerativeAI(
|
| 15 |
-
model="gemini-1.5-
|
| 16 |
-
google_api_key=
|
| 17 |
-
temperature=0.
|
| 18 |
)
|
| 19 |
|
| 20 |
-
#
|
| 21 |
if "memory" not in st.session_state:
|
| 22 |
st.session_state.memory = ConversationBufferMemory(return_messages=True)
|
| 23 |
|
| 24 |
-
# Build the chain
|
| 25 |
conversation = ConversationChain(
|
| 26 |
llm=llm,
|
| 27 |
memory=st.session_state.memory,
|
| 28 |
-
verbose=
|
| 29 |
)
|
| 30 |
|
| 31 |
-
# Chat
|
| 32 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 33 |
if user_input:
|
|
|
|
|
|
|
| 34 |
with st.chat_message("user"):
|
| 35 |
-
st.
|
|
|
|
|
|
|
|
|
|
| 36 |
|
|
|
|
| 37 |
with st.chat_message("assistant"):
|
| 38 |
-
|
| 39 |
-
input=f"You are a Data Science Tutor. Only answer Data Science questions. User asked: {user_input}"
|
| 40 |
-
)
|
| 41 |
-
st.write(response)
|
| 42 |
|
| 43 |
else:
|
| 44 |
-
st.warning("
|
|
|
|
| 1 |
import streamlit as st
|
|
|
|
| 2 |
from langchain.chains import ConversationChain
|
| 3 |
from langchain.memory import ConversationBufferMemory
|
| 4 |
+
from langchain_google_genai import ChatGoogleGenerativeAI
|
| 5 |
|
| 6 |
+
# ---------------- Streamlit Page Config ----------------
|
| 7 |
+
st.set_page_config(page_title="🤖 AI Conversational Data Science Tutor", layout="wide")
|
|
|
|
|
|
|
| 8 |
st.title("🤖 AI Conversational Data Science Tutor")
|
| 9 |
|
| 10 |
+
# ---------------- Sidebar for Settings ----------------
|
| 11 |
+
st.sidebar.header("Settings")
|
| 12 |
+
google_api_key = st.sidebar.text_input("Enter your Google API Key", type="password")
|
| 13 |
+
|
| 14 |
+
# ---------------- Initialize Model ----------------
|
| 15 |
+
if google_api_key:
|
| 16 |
llm = ChatGoogleGenerativeAI(
|
| 17 |
+
model="gemini-1.5-pro",
|
| 18 |
+
google_api_key=google_api_key,
|
| 19 |
+
temperature=0.3,
|
| 20 |
)
|
| 21 |
|
| 22 |
+
# Conversation memory
|
| 23 |
if "memory" not in st.session_state:
|
| 24 |
st.session_state.memory = ConversationBufferMemory(return_messages=True)
|
| 25 |
|
|
|
|
| 26 |
conversation = ConversationChain(
|
| 27 |
llm=llm,
|
| 28 |
memory=st.session_state.memory,
|
| 29 |
+
verbose=True,
|
| 30 |
)
|
| 31 |
|
| 32 |
+
# ---------------- Chat Interface ----------------
|
| 33 |
+
if "messages" not in st.session_state:
|
| 34 |
+
st.session_state.messages = []
|
| 35 |
+
|
| 36 |
+
for message in st.session_state.messages:
|
| 37 |
+
with st.chat_message(message["role"]):
|
| 38 |
+
st.markdown(message["content"])
|
| 39 |
+
|
| 40 |
+
user_input = st.chat_input("Ask your Data Science question...")
|
| 41 |
if user_input:
|
| 42 |
+
# Store user message
|
| 43 |
+
st.session_state.messages.append({"role": "user", "content": user_input})
|
| 44 |
with st.chat_message("user"):
|
| 45 |
+
st.markdown(user_input)
|
| 46 |
+
|
| 47 |
+
# Get AI response with memory
|
| 48 |
+
response = conversation.predict(input=user_input)
|
| 49 |
|
| 50 |
+
st.session_state.messages.append({"role": "assistant", "content": response})
|
| 51 |
with st.chat_message("assistant"):
|
| 52 |
+
st.markdown(response)
|
|
|
|
|
|
|
|
|
|
| 53 |
|
| 54 |
else:
|
| 55 |
+
st.warning("Please enter your Google API Key in the sidebar to continue.")
|