ahmadsanafarooq commited on
Commit
e11aaf9
·
verified ·
1 Parent(s): 9155455

Create app.py

Browse files
Files changed (1) hide show
  1. app.py +72 -0
app.py ADDED
@@ -0,0 +1,72 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import streamlit as st
2
+ import google.genai as genai
3
+ import os
4
+
5
+ # --- PAGE CONFIG ---
6
+ st.set_page_config(page_title="AI Study Helper", page_icon="📚")
7
+
8
+ # --- API CONFIGURATION ---
9
+ # On Hugging Face Spaces, set 'GEMINI_API_KEY' in the 'Secrets' section of settings
10
+ api_key = os.getenv("GEMINI_API_KEY")
11
+
12
+ if not api_key:
13
+ st.error("API Key not found. Please set GEMINI_API_KEY in Hugging Face Secrets.")
14
+ st.stop()
15
+
16
+ genai.configure(api_key=api_key)
17
+ model = genai.GenerativeModel("gemini-3-flash-preview")
18
+
19
+ # --- PERSONA DEFINITIONS ---
20
+ PERSONAS = {
21
+ "Teacher 🍎": {
22
+ "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.",
23
+ "welcome": "Hello! I'm your Teacher. What topic shall we break down today?"
24
+ },
25
+ "Peer Mentor 💡": {
26
+ "system_prompt": "You are a friendly career mentor. Explain gently. Give simple real-world examples to explain concepts.",
27
+ "welcome": "Hey there. I'm your Peer Mentor. Let's talk about your progress and how this fits into the bigger picture."
28
+ },
29
+ "Critic ⚖️": {
30
+ "system_prompt": "You are a harsh code reviewer. Point out flaws. Be strict with your words. Motivate the user by giving reality-check.",
31
+ "welcome": "I am your Critic. Present your argument or concept, and I will find the weak points you need to fix."
32
+ }
33
+ }
34
+
35
+ # --- UI LAYOUT ---
36
+ st.title("📚 AI Study Helper")
37
+ st.sidebar.title("Configuration")
38
+
39
+ selected_persona = st.sidebar.selectbox("Choose your guide:", list(PERSONAS.keys()))
40
+ st.sidebar.info(f"Current Persona: **{selected_persona}**")
41
+
42
+ if st.sidebar.button("Clear Conversation"):
43
+ st.session_state.messages = []
44
+ st.rerun()
45
+
46
+ # --- CHAT LOGIC ---
47
+ if "messages" not in st.session_state:
48
+ st.session_state.messages = []
49
+
50
+ # Display chat history
51
+ for message in st.session_state.messages:
52
+ with st.chat_message(message["role"]):
53
+ st.markdown(message["content"])
54
+
55
+ # User Input
56
+ if prompt := st.chat_input("What are we studying today?"):
57
+ st.session_state.messages.append({"role": "user", "content": prompt})
58
+ with st.chat_message("user"):
59
+ st.markdown(prompt)
60
+
61
+ # Generate Response
62
+ with st.chat_message("assistant"):
63
+ # Combine persona system prompt with the user query
64
+ full_prompt = f"System: {PERSONAS[selected_persona]}\n\nUser: {prompt}"
65
+
66
+ try:
67
+ response = model.generate_content(full_prompt)
68
+ response_text = response.text
69
+ st.markdown(response_text)
70
+ st.session_state.messages.append({"role": "assistant", "content": response_text})
71
+ except Exception as e:
72
+ st.error(f"Error: {e}")