File size: 3,537 Bytes
cc57ce5 0ad0496 3721109 175cd72 60612b8 175cd72 90a8520 175cd72 60612b8 175cd72 1cb1036 f1048a7 175cd72 60612b8 175cd72 60612b8 9285e3f 60612b8 f1048a7 60612b8 0509d9b 60612b8 175cd72 60612b8 175cd72 8952009 c747d59 60612b8 a352e2d 175cd72 1cb1036 175cd72 8952009 a352e2d 175cd72 0509d9b 175cd72 60612b8 1cb1036 9421310 175cd72 a352e2d 0509d9b 175cd72 1cb1036 60612b8 175cd72 0509d9b 60612b8 1cb1036 60612b8 1cb1036 | 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 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 | import streamlit as st
import google.generativeai as genai
# -------------------
# API Key Setup
# -------------------
gemini_api_key = st.secrets.get("GEN_API_KEY", "")
# -------------------
# Page Configuration
# -------------------
st.set_page_config(page_title="Academic Tutor AI", layout="wide")
st.title("📚 Academic Tutor AI")
st.write("Ask questions about your courses and get clear explanations, examples, and study tips.")
# -------------------
# Gemini Setup
# -------------------
if not gemini_api_key:
st.error("⚠️ Please set your 'GEN_API_KEY' in Streamlit secrets.")
st.stop()
genai.configure(api_key=gemini_api_key)
# Fetch available Gemini models
available_models = [
m.name for m in genai.list_models()
if "generateContent" in m.supported_generation_methods
]
if not available_models:
st.error("⚠️ No Gemini models available for your API key.")
st.stop()
# Reset session if old model is invalid
if "model" in st.session_state and st.session_state["model"] not in available_models:
del st.session_state["model"]
model = st.sidebar.selectbox("Model", available_models, index=0,
help ="Choose which AI model to use. Most users can keep the default model.")
# Initialize Gemini chat if needed
if "gemini_chat" not in st.session_state or st.session_state.get("model") != model:
st.session_state.model = model
try:
gemini_model = genai.GenerativeModel(model)
st.session_state.gemini_chat = gemini_model.start_chat(history=[])
except Exception as e:
st.error(f"⚠️ Could not initialize Gemini model: {e}")
st.stop()
# -------------------
# System Prompt for Academic Tutor
# -------------------
system_prompt = st.sidebar.text_area(
"System Prompt",
"You are a friendly academic tutor for college students. Provide clear explanations, examples, and study tips. Encourage understanding rather than just giving answers.",
help = "This defines how the AI behaves. You can customize it if you want the AI to act differently."
)
# -------------------
# Chat History State
# -------------------
if "messages" not in st.session_state:
st.session_state.messages = []
# Reset conversation button
if st.sidebar.button("Reset Conversation"):
st.session_state.messages = []
gemini_model = genai.GenerativeModel(model)
st.session_state.gemini_chat = gemini_model.start_chat(history=[])
st.experimental_rerun()
# -------------------
# Display Chat Messages
# -------------------
for msg in st.session_state.messages:
with st.chat_message(msg["role"]):
st.markdown(msg["content"])
# -------------------
# User Input
# -------------------
user_input = st.chat_input("Type your academic question here (e.g., 'Explain Bayes' Theorem with an example.')")
if user_input:
# Show user message
st.chat_message("user").markdown(user_input)
st.session_state.messages.append({"role": "user", "content": user_input})
try:
with st.spinner("🤔 Thinking..."):
# Prepend system prompt for context
full_input = f"{system_prompt}\n\nUser: {user_input}"
resp = st.session_state.gemini_chat.send_message(full_input)
bot_text = resp.text
except Exception as e:
bot_text = f"⚠️ Gemini could not respond right now. Please try again. ({e})"
with st.chat_message("assistant"):
st.markdown(bot_text)
st.session_state.messages.append({"role": "assistant", "content": bot_text})
st.experimental_rerun()
|