File size: 2,928 Bytes
e11aaf9
6bf4715
e11aaf9
 
 
 
 
 
 
 
 
 
 
 
6bf4715
 
e11aaf9
 
 
 
 
 
 
 
 
 
 
 
 
 
 
6bf4715
e11aaf9
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
6bf4715
 
 
 
 
 
 
 
 
e11aaf9
 
 
 
 
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
import streamlit as st
from google import genai  # Corrected import for the new SDK
import os

# --- PAGE CONFIG ---
st.set_page_config(page_title="AI Study Helper", page_icon="πŸ“š")

# --- API CONFIGURATION ---
api_key = os.getenv("GEMINI_API_KEY")

if not api_key:
    st.error("API Key not found. Please set GEMINI_API_KEY in Hugging Face Secrets.")
    st.stop()

# Initialize the new GenAI Client
client = genai.Client(api_key=api_key)

# --- PERSONA DEFINITIONS ---
PERSONAS = {
    "Teacher 🍎": {
        "system_prompt": "You are a strict Teacher. Your goal is to explain complex concepts in simple terms. Be concise and academic. Avoid giving answers directly; instead, guide the user to the solution.",
        "welcome": "Hello! I'm your Teacher. What topic shall we break down today?"
    },
    "Peer Mentor πŸ’‘": {
        "system_prompt": "You are a friendly career mentor. Explain gently. Give simple real-world examples to explain concepts.",
        "welcome": "Hey there. I'm your Peer Mentor. Let's talk about your progress and how this fits into the bigger picture."
    },
    "Critic βš–οΈ": {
        "system_prompt": "You are a harsh code reviewer. Point out flaws. Be strict with your words. Motivate the user by giving reality-check.",
        "welcome": "I am your Critic. Present your argument or concept, and I will find the weak points you need to fix."
    }
}

# --- UI LAYOUT ---
st.title("πŸ“š AI Study Helper")
st.sidebar.title("Configuration")

selected_persona = st.sidebar.selectbox("Choose your guide:", list(PERSONAS.keys()))
st.sidebar.info(f"Current Persona: **{selected_persona}**")

if st.sidebar.button("Clear Conversation"):
    st.session_state.messages = []
    st.rerun()

# --- CHAT LOGIC ---
if "messages" not in st.session_state:
    st.session_state.messages = []

# Display chat history
for message in st.session_state.messages:
    with st.chat_message(message["role"]):
        st.markdown(message["content"])

# User Input
if prompt := st.chat_input("What are we studying today?"):
    st.session_state.messages.append({"role": "user", "content": prompt})
    with st.chat_message("user"):
        st.markdown(prompt)

    # Generate Response
    with st.chat_message("assistant"):
        try:
            # New SDK syntax for generating content
            response = client.models.generate_content(
                model="gemini-2.0-flash", # Note: gemini-3 is current, but ensure model name is exact
                config=genai.types.GenerateContentConfig(
                    system_instruction=PERSONAS[selected_persona]["system_prompt"]
                ),
                contents=prompt
            )
            
            response_text = response.text
            st.markdown(response_text)
            st.session_state.messages.append({"role": "assistant", "content": response_text})
        except Exception as e:
            st.error(f"Error: {e}")