Spaces:
Sleeping
Sleeping
File size: 1,834 Bytes
3e7e1c9 efcea45 88427c9 efcea45 | 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 | import streamlit as st
import google.generativeai as genai
st.set_page_config(page_title="Data Scientist Buddy", page_icon="π€")
st.title("π€ Data Scientist Buddy ")
# β
Load API key securely
API_KEY = "AIzaSyCZUKD_6aCG9tk8AbALwwmvc95g2i4QjnM"
if not API_KEY:
st.error("β GEMINI_API_KEY not found in secrets.")
st.stop()
# β
Configure Gemini
genai.configure(api_key=API_KEY)
# β
Custom system instruction (clear prompt)
system_instruction = (
"You are a helpful, friendly, and knowledgeable AI assistant specialized in data science. "
"You help students, beginners, and professionals understand concepts in Python, statistics, "
"machine learning, AI, data analysis, and visualization. Provide clear explanations, code examples, "
"and suggestions using markdown formatting when necessary. Keep responses concise but informative."
)
# β
Initialize chat with system prompt
if "chat" not in st.session_state:
model = genai.GenerativeModel(
model_name="gemini-2.5-flash", # fallback to "gemini-1.5-flash" if needed
system_instruction=system_instruction
)
st.session_state.chat = model.start_chat(history=[])
# β
Display previous chat history
for msg in st.session_state.chat.history:
with st.chat_message("user" if msg.role == "user" else "assistant"):
st.markdown(msg.parts[0].text if msg.parts else msg.text)
# β
Accept and process user input
if prompt := st.chat_input("Ask anything about data science..."):
with st.chat_message("user"):
st.markdown(prompt)
try:
response = st.session_state.chat.send_message(prompt)
with st.chat_message("assistant"):
st.markdown(response.text)
except Exception as e:
st.error("β οΈ Could not generate a response. Check your API key or model access.") |