charesz commited on
Commit
a352e2d
·
verified ·
1 Parent(s): a7322de

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +80 -43
app.py CHANGED
@@ -1,31 +1,27 @@
1
  import streamlit as st
2
  import google.generativeai as genai
3
 
 
 
 
 
 
4
  # -------------------
5
  # Page Configuration
6
  # -------------------
7
  st.set_page_config(page_title="Your AI Buddy", layout="wide")
8
-
9
- st.title("💬 Your AI Buddy")
10
- st.markdown("Ask me anything! 📘🤖\n\n"
11
- "💡 *Examples:*\n"
12
- "- Summarize a news article\n"
13
- "- Explain a math problem\n"
14
- "- Give me study tips\n")
15
 
16
  # -------------------
17
- # API Key Setup
18
  # -------------------
19
- gemini_api_key = st.secrets.get("GEN_API_KEY", "")
20
  if not gemini_api_key:
21
  st.error("⚠️ Please set your 'GEN_API_KEY' in Streamlit secrets.")
22
  st.stop()
23
 
24
  genai.configure(api_key=gemini_api_key)
25
 
26
- # -------------------
27
- # Model Selection
28
- # -------------------
29
  available_models = [
30
  m.name for m in genai.list_models()
31
  if "generateContent" in m.supported_generation_methods
@@ -35,60 +31,101 @@ if not available_models:
35
  st.error("⚠️ No Gemini models available for your API key.")
36
  st.stop()
37
 
 
38
  if "model" in st.session_state and st.session_state["model"] not in available_models:
39
  del st.session_state["model"]
40
 
41
- model = st.sidebar.selectbox("Choose a Gemini Model", available_models, index=0)
42
 
43
- # -------------------
44
- # Initialize Chat Session
45
- # -------------------
46
  if "gemini_chat" not in st.session_state or st.session_state.get("model") != model:
47
  st.session_state.model = model
48
- gemini_model = genai.GenerativeModel(model)
49
- st.session_state.gemini_chat = gemini_model.start_chat(history=[])
 
 
 
 
 
 
 
 
 
 
 
 
 
 
50
  st.session_state.messages = []
51
 
52
  # Reset conversation button
53
- if st.sidebar.button("🔄 Reset Conversation"):
54
  st.session_state.messages = []
55
  gemini_model = genai.GenerativeModel(model)
56
  st.session_state.gemini_chat = gemini_model.start_chat(history=[])
57
  st.rerun()
58
 
59
  # -------------------
60
- # System Prompt
61
  # -------------------
62
- system_prompt = st.sidebar.text_area(
63
- "System Prompt (AI Behavior)",
64
- "You are a helpful AI assistant. Provide concise and accurate answers."
65
- )
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
66
 
67
  # -------------------
68
- # Chat History Display
69
  # -------------------
70
- if st.session_state.messages:
71
- for i, msg in enumerate(st.session_state.messages):
72
- if msg["role"] == "user":
73
- message(msg["content"], is_user=True, key=f"user_{i}")
74
- else:
75
- message(msg["content"], key=f"bot_{i}")
76
 
77
  # -------------------
78
  # User Input
79
  # -------------------
80
- user_input = st.chat_input("Type your question here...")
81
- if user_input:
82
- # Save user message
83
  st.session_state.messages.append({"role": "user", "content": user_input})
84
 
85
  try:
86
- with st.spinner("🤖 Thinking..."):
87
- full_input = f"{system_prompt}\n\nUser: {user_input}"
88
- resp = st.session_state.gemini_chat.send_message(full_input)
89
- bot_text = resp.text
90
- except Exception as e:
91
- bot_text = f"⚠️ Gemini could not respond right now. Please try again. ({e})"
92
-
93
- st.session_state.messages.append({"role": "assistant", "content": bot_text})
94
- st.rerun()
 
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
 
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
+ # Sidebar Options
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
+ if "messages" not in st.session_state:
59
  st.session_state.messages = []
60
 
61
  # Reset conversation button
62
+ if st.sidebar.button("Reset Conversation"):
63
  st.session_state.messages = []
64
  gemini_model = genai.GenerativeModel(model)
65
  st.session_state.gemini_chat = gemini_model.start_chat(history=[])
66
  st.rerun()
67
 
68
  # -------------------
69
+ # Custom Chat Bubble Styling
70
  # -------------------
71
+ st.markdown("""
72
+ <style>
73
+ /* General chat bubble styling */
74
+ .stChatMessage > div {
75
+ border-radius: 15px;
76
+ padding: 10px 14px;
77
+ margin: 6px 0;
78
+ max-width: 80%;
79
+ font-size: 15px;
80
+ }
81
+
82
+ /* User messages (right side) */
83
+ .stChatMessage[data-testid="stChatMessage-user"] > div {
84
+ background-color: #d1e9ff;
85
+ color: #0a2f5c;
86
+ border: 1px solid #a3cfff;
87
+ margin-left: auto;
88
+ text-align: right;
89
+ }
90
+
91
+ /* Assistant messages (left side) */
92
+ .stChatMessage[data-testid="stChatMessage-assistant"] > div {
93
+ background-color: #f1f1f1;
94
+ color: #1a1a1a;
95
+ border: 1px solid #ddd;
96
+ margin-right: auto;
97
+ text-align: left;
98
+ }
99
+
100
+ /* Avatars */
101
+ .stChatMessage [data-testid="stChatAvatar"] {
102
+ border-radius: 50%;
103
+ width: 32px;
104
+ height: 32px;
105
+ font-size: 18px;
106
+ background-color: #ffffff;
107
+ display: flex;
108
+ align-items: center;
109
+ justify-content: center;
110
+ box-shadow: 0 1px 3px rgba(0,0,0,0.2);
111
+ }
112
+ </style>
113
+ """, unsafe_allow_html=True)
114
 
115
  # -------------------
116
+ # Display Chat Messages
117
  # -------------------
118
+ for msg in st.session_state.messages:
119
+ with st.chat_message(msg["role"]):
120
+ st.markdown(msg["content"])
 
 
 
121
 
122
  # -------------------
123
  # User Input
124
  # -------------------
125
+ if user_input := st.chat_input("Type your message..."):
126
+ # Show user message
127
+ st.chat_message("user").markdown(user_input)
128
  st.session_state.messages.append({"role": "user", "content": user_input})
129
 
130
  try:
131
+ with st.spinner("Gemini is thinking..."):