Update app.py
Browse files
app.py
CHANGED
|
@@ -4,31 +4,48 @@ from langchain.memory import ConversationBufferMemory
|
|
| 4 |
from langchain.chains import ConversationChain
|
| 5 |
from langchain_google_genai import ChatGoogleGenerativeAI
|
| 6 |
|
| 7 |
-
|
| 8 |
GEMINI_API_KEY = os.getenv("GEMINI_API_KEY")
|
| 9 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 10 |
if not GEMINI_API_KEY:
|
| 11 |
st.error("β GEMINI_API_KEY not found. Please add it in Hugging Face β Settings β Variables and secrets.")
|
| 12 |
else:
|
| 13 |
-
#
|
| 14 |
-
llm = ChatGoogleGenerativeAI(
|
|
|
|
|
|
|
|
|
|
| 15 |
|
| 16 |
-
#
|
| 17 |
memory = ConversationBufferMemory()
|
| 18 |
conversation = ConversationChain(
|
| 19 |
llm=llm,
|
| 20 |
memory=memory,
|
| 21 |
-
verbose=
|
| 22 |
)
|
| 23 |
|
| 24 |
-
#
|
| 25 |
-
|
| 26 |
-
|
| 27 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 28 |
|
| 29 |
-
|
| 30 |
-
|
| 31 |
|
| 32 |
-
|
| 33 |
-
|
| 34 |
-
st.
|
|
|
|
| 4 |
from langchain.chains import ConversationChain
|
| 5 |
from langchain_google_genai import ChatGoogleGenerativeAI
|
| 6 |
|
| 7 |
+
# Get Gemini API key from environment variable
|
| 8 |
GEMINI_API_KEY = os.getenv("GEMINI_API_KEY")
|
| 9 |
|
| 10 |
+
st.set_page_config(page_title="Conversational AI Data Science Tutor", page_icon="π€")
|
| 11 |
+
|
| 12 |
+
st.title("π€ Conversational AI Data Science Tutor")
|
| 13 |
+
st.write("Ask me any **Data Science** related question!")
|
| 14 |
+
|
| 15 |
if not GEMINI_API_KEY:
|
| 16 |
st.error("β GEMINI_API_KEY not found. Please add it in Hugging Face β Settings β Variables and secrets.")
|
| 17 |
else:
|
| 18 |
+
# Initialize Gemini LLM
|
| 19 |
+
llm = ChatGoogleGenerativeAI(
|
| 20 |
+
model="gemini-1.5-pro",
|
| 21 |
+
google_api_key=GEMINI_API_KEY
|
| 22 |
+
)
|
| 23 |
|
| 24 |
+
# Add memory for conversation awareness
|
| 25 |
memory = ConversationBufferMemory()
|
| 26 |
conversation = ConversationChain(
|
| 27 |
llm=llm,
|
| 28 |
memory=memory,
|
| 29 |
+
verbose=False
|
| 30 |
)
|
| 31 |
|
| 32 |
+
# Initialize chat history in session_state
|
| 33 |
+
if "messages" not in st.session_state:
|
| 34 |
+
st.session_state.messages = []
|
| 35 |
+
|
| 36 |
+
# Display previous messages
|
| 37 |
+
for msg in st.session_state.messages:
|
| 38 |
+
st.chat_message(msg["role"]).markdown(msg["content"])
|
| 39 |
+
|
| 40 |
+
# User input with chat-style box
|
| 41 |
+
if prompt := st.chat_input("Ask a data science question..."):
|
| 42 |
+
# Save user message
|
| 43 |
+
st.session_state.messages.append({"role": "user", "content": prompt})
|
| 44 |
+
st.chat_message("user").markdown(prompt)
|
| 45 |
|
| 46 |
+
# Get model response
|
| 47 |
+
response = conversation.predict(input=prompt)
|
| 48 |
|
| 49 |
+
# Save assistant message
|
| 50 |
+
st.session_state.messages.append({"role": "assistant", "content": response})
|
| 51 |
+
st.chat_message("assistant").markdown(response)
|