Update app.py
Browse files
app.py
CHANGED
|
@@ -1,168 +1,99 @@
|
|
| 1 |
import streamlit as st
|
| 2 |
import google.generativeai as genai
|
| 3 |
-
from google.generativeai import types
|
| 4 |
-
|
| 5 |
-
# --- CONFIGURATION ---
|
| 6 |
-
# Task 3: Use Case Adaptation (Academic Tutor)
|
| 7 |
-
APP_TITLE = "🧠 Academic Tutor Assistant powered by Gemini"
|
| 8 |
-
DEFAULT_SYSTEM_PROMPT = (
|
| 9 |
-
"You are an expert **Academic Tutor**. Your goal is to help the user understand complex "
|
| 10 |
-
"concepts, review essays, solve problems, and provide study guidance. "
|
| 11 |
-
"Respond with clear, structured, and encouraging explanations. "
|
| 12 |
-
"Always ask a follow-up question to encourage deeper learning."
|
| 13 |
-
)
|
| 14 |
|
| 15 |
-
#
|
|
|
|
|
|
|
|
|
|
| 16 |
|
| 17 |
-
|
| 18 |
-
|
| 19 |
-
|
| 20 |
-
|
| 21 |
-
|
| 22 |
-
st.stop()
|
| 23 |
-
try:
|
| 24 |
-
genai.configure(api_key=api_key)
|
| 25 |
-
return genai
|
| 26 |
-
except Exception as e:
|
| 27 |
-
st.error(f"⚠️ Error configuring Gemini client: {e}")
|
| 28 |
-
st.stop()
|
| 29 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 30 |
|
| 31 |
-
|
| 32 |
-
"""Fetches and filters available chat-capable models."""
|
| 33 |
-
try:
|
| 34 |
-
available_models = [
|
| 35 |
-
m.name for m in genai.list_models()
|
| 36 |
-
if "generateContent" in m.supported_generation_methods
|
| 37 |
-
and ("gemini-2.5" in m.name or "gemini-1.5" in m.name) # Filter for high-quality models
|
| 38 |
-
]
|
| 39 |
-
if not available_models:
|
| 40 |
-
st.error("⚠️ No suitable Gemini models available for your API key.")
|
| 41 |
-
st.stop()
|
| 42 |
-
return available_models
|
| 43 |
-
except Exception as e:
|
| 44 |
-
st.error(f"⚠️ Error fetching models: {e}")
|
| 45 |
-
st.stop()
|
| 46 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 47 |
|
| 48 |
-
|
| 49 |
-
"
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 50 |
try:
|
| 51 |
-
|
| 52 |
-
|
| 53 |
-
system_instruction=system_prompt
|
| 54 |
-
)
|
| 55 |
-
gemini_model = genai.GenerativeModel(model_name)
|
| 56 |
-
st.session_state.gemini_chat = gemini_model.start_chat(
|
| 57 |
-
history=[],
|
| 58 |
-
config=config # Apply the system instruction here
|
| 59 |
-
)
|
| 60 |
except Exception as e:
|
| 61 |
-
st.error(f"⚠️ Could not initialize Gemini model
|
| 62 |
st.stop()
|
| 63 |
|
| 64 |
-
#
|
| 65 |
-
|
| 66 |
-
#
|
| 67 |
-
st.
|
| 68 |
-
|
| 69 |
-
|
| 70 |
-
"Ask any academic question, paste your homework problem, or submit a paragraph for review. Let's learn together!"
|
| 71 |
)
|
| 72 |
|
| 73 |
-
#
|
| 74 |
-
|
| 75 |
-
|
| 76 |
-
|
| 77 |
-
|
| 78 |
-
# 3. Sidebar Controls (Task 4: Usability Enhancement)
|
| 79 |
-
with st.sidebar:
|
| 80 |
-
st.header("⚙️ Settings")
|
| 81 |
-
|
| 82 |
-
# Model Selection
|
| 83 |
-
available_models = get_available_models()
|
| 84 |
-
default_index = 0
|
| 85 |
-
if "gemini-2.5-flash" in available_models:
|
| 86 |
-
default_index = available_models.index("gemini-2.5-flash")
|
| 87 |
-
|
| 88 |
-
new_model = st.selectbox(
|
| 89 |
-
"Select Model",
|
| 90 |
-
available_models,
|
| 91 |
-
index=default_index,
|
| 92 |
-
key="_selected_model_name"
|
| 93 |
-
)
|
| 94 |
-
|
| 95 |
-
# System Prompt (Task 3: Use Case Adaptation)
|
| 96 |
-
system_prompt_key = "_system_prompt_area"
|
| 97 |
-
system_prompt = st.text_area(
|
| 98 |
-
"Tutor Persona / System Instruction",
|
| 99 |
-
DEFAULT_SYSTEM_PROMPT,
|
| 100 |
-
height=180,
|
| 101 |
-
key=system_prompt_key
|
| 102 |
-
)
|
| 103 |
-
|
| 104 |
-
# Check for configuration change and re-initialize chat
|
| 105 |
-
if (st.session_state.get("model") != new_model) or \
|
| 106 |
-
(st.session_state.get("system_prompt") != system_prompt):
|
| 107 |
-
|
| 108 |
-
# Save new values and re-initialize chat
|
| 109 |
-
st.session_state.model = new_model
|
| 110 |
-
st.session_state.system_prompt = system_prompt
|
| 111 |
-
st.session_state.messages = []
|
| 112 |
-
init_chat_session(new_model, system_prompt)
|
| 113 |
-
st.info("Tutor persona updated. Conversation restarted.")
|
| 114 |
-
|
| 115 |
-
# Reset Conversation Button (Task 4: Usability Enhancement)
|
| 116 |
-
if st.button("🔄 Reset Conversation"):
|
| 117 |
-
st.session_state.messages = []
|
| 118 |
-
init_chat_session(st.session_state.model, st.session_state.system_prompt)
|
| 119 |
-
# Note: No st.rerun() needed due to Streamlit's natural flow
|
| 120 |
-
|
| 121 |
-
# 4. Chat Initialization (First run check)
|
| 122 |
-
if "gemini_chat" not in st.session_state:
|
| 123 |
-
st.session_state.model = new_model
|
| 124 |
-
st.session_state.system_prompt = system_prompt
|
| 125 |
st.session_state.messages = []
|
| 126 |
-
init_chat_session(new_model, system_prompt)
|
| 127 |
|
| 128 |
-
#
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 129 |
|
| 130 |
-
#
|
|
|
|
|
|
|
| 131 |
for msg in st.session_state.messages:
|
| 132 |
with st.chat_message(msg["role"]):
|
| 133 |
st.markdown(msg["content"])
|
| 134 |
|
| 135 |
-
|
| 136 |
-
#
|
| 137 |
-
|
| 138 |
-
|
|
|
|
|
|
|
| 139 |
st.session_state.messages.append({"role": "user", "content": user_input})
|
| 140 |
-
with st.chat_message("user"):
|
| 141 |
-
st.markdown(user_input)
|
| 142 |
|
| 143 |
-
# 6b. Send message to Gemini
|
| 144 |
try:
|
| 145 |
-
with st.spinner("
|
| 146 |
-
#
|
| 147 |
-
|
| 148 |
-
|
| 149 |
-
|
| 150 |
-
full_response = ""
|
| 151 |
-
with st.chat_message("assistant"):
|
| 152 |
-
message_placeholder = st.empty()
|
| 153 |
-
for chunk in response:
|
| 154 |
-
full_response += chunk.text
|
| 155 |
-
message_placeholder.markdown(full_response + "▌")
|
| 156 |
-
message_placeholder.markdown(full_response) # Final rendering
|
| 157 |
-
|
| 158 |
-
bot_text = full_response
|
| 159 |
-
|
| 160 |
except Exception as e:
|
| 161 |
-
bot_text = f"⚠️
|
| 162 |
-
|
| 163 |
-
|
|
|
|
| 164 |
|
| 165 |
-
# 6c. Save assistant message
|
| 166 |
st.session_state.messages.append({"role": "assistant", "content": bot_text})
|
| 167 |
-
|
| 168 |
-
# Task 4: Removed st.rerun() for better stability.
|
|
|
|
| 1 |
import streamlit as st
|
| 2 |
import google.generativeai as genai
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 3 |
|
| 4 |
+
# -------------------
|
| 5 |
+
# API Key Setup
|
| 6 |
+
# -------------------
|
| 7 |
+
gemini_api_key = st.secrets.get("GEN_API_KEY", "")
|
| 8 |
|
| 9 |
+
# -------------------
|
| 10 |
+
# Page Configuration
|
| 11 |
+
# -------------------
|
| 12 |
+
st.set_page_config(page_title="Your AI Buddy", layout="wide")
|
| 13 |
+
st.title("💡 Need answers? Just type below!")
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 14 |
|
| 15 |
+
# -------------------
|
| 16 |
+
# Gemini Setup
|
| 17 |
+
# -------------------
|
| 18 |
+
if not gemini_api_key:
|
| 19 |
+
st.error("⚠️ Please set your 'GEN_API_KEY' in Streamlit secrets.")
|
| 20 |
+
st.stop()
|
| 21 |
|
| 22 |
+
genai.configure(api_key=gemini_api_key)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 23 |
|
| 24 |
+
# Fetch available Gemini models
|
| 25 |
+
available_models = [
|
| 26 |
+
m.name for m in genai.list_models()
|
| 27 |
+
if "generateContent" in m.supported_generation_methods
|
| 28 |
+
]
|
| 29 |
|
| 30 |
+
if not available_models:
|
| 31 |
+
st.error("⚠️ No Gemini models available for your API key.")
|
| 32 |
+
st.stop()
|
| 33 |
+
|
| 34 |
+
# Reset session if old model is invalid
|
| 35 |
+
if "model" in st.session_state and st.session_state["model"] not in available_models:
|
| 36 |
+
del st.session_state["model"]
|
| 37 |
+
|
| 38 |
+
model = st.sidebar.selectbox("Model", available_models, index=0)
|
| 39 |
+
|
| 40 |
+
# Initialize Gemini chat if needed
|
| 41 |
+
if "gemini_chat" not in st.session_state or st.session_state.get("model") != model:
|
| 42 |
+
st.session_state.model = model
|
| 43 |
try:
|
| 44 |
+
gemini_model = genai.GenerativeModel(model)
|
| 45 |
+
st.session_state.gemini_chat = gemini_model.start_chat(history=[])
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 46 |
except Exception as e:
|
| 47 |
+
st.error(f"⚠️ Could not initialize Gemini model: {e}")
|
| 48 |
st.stop()
|
| 49 |
|
| 50 |
+
# -------------------
|
| 51 |
+
# System Prompt
|
| 52 |
+
# -------------------
|
| 53 |
+
system_prompt = st.sidebar.text_area(
|
| 54 |
+
"System Prompt",
|
| 55 |
+
"You are a helpful AI assistant. Provide concise and accurate answers."
|
|
|
|
| 56 |
)
|
| 57 |
|
| 58 |
+
# -------------------
|
| 59 |
+
# Chat History State
|
| 60 |
+
# -------------------
|
| 61 |
+
if "messages" not in st.session_state:
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 62 |
st.session_state.messages = []
|
|
|
|
| 63 |
|
| 64 |
+
# Reset conversation button
|
| 65 |
+
if st.sidebar.button("Reset Conversation"):
|
| 66 |
+
st.session_state.messages = []
|
| 67 |
+
gemini_model = genai.GenerativeModel(model)
|
| 68 |
+
st.session_state.gemini_chat = gemini_model.start_chat(history=[])
|
| 69 |
+
st.rerun()
|
| 70 |
|
| 71 |
+
# -------------------
|
| 72 |
+
# Display Chat Messages
|
| 73 |
+
# -------------------
|
| 74 |
for msg in st.session_state.messages:
|
| 75 |
with st.chat_message(msg["role"]):
|
| 76 |
st.markdown(msg["content"])
|
| 77 |
|
| 78 |
+
# -------------------
|
| 79 |
+
# User Input
|
| 80 |
+
# -------------------
|
| 81 |
+
if user_input := st.chat_input("Type your message..."):
|
| 82 |
+
# Show user message
|
| 83 |
+
st.chat_message("user").markdown(user_input)
|
| 84 |
st.session_state.messages.append({"role": "user", "content": user_input})
|
|
|
|
|
|
|
| 85 |
|
|
|
|
| 86 |
try:
|
| 87 |
+
with st.spinner("Gemini is thinking..."):
|
| 88 |
+
# Prepend system prompt for context
|
| 89 |
+
full_input = f"{system_prompt}\n\nUser: {user_input}"
|
| 90 |
+
resp = st.session_state.gemini_chat.send_message(full_input)
|
| 91 |
+
bot_text = resp.text
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 92 |
except Exception as e:
|
| 93 |
+
bot_text = f"⚠️ Gemini could not respond right now. Please try again. ({e})"
|
| 94 |
+
|
| 95 |
+
with st.chat_message("assistant"):
|
| 96 |
+
st.markdown(bot_text)
|
| 97 |
|
|
|
|
| 98 |
st.session_state.messages.append({"role": "assistant", "content": bot_text})
|
| 99 |
+
st.rerun()
|
|
|