Manyue-DataScientist commited on
Commit
5f147cb
·
verified ·
1 Parent(s): 79da7a3

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +32 -21
app.py CHANGED
@@ -152,14 +152,14 @@ def generate_response(query: str, knowledge_base: dict) -> str:
152
  return format_default_response(knowledge_base)
153
 
154
  def main():
155
- st.title("🤖 Meet Rini - AI-Powered Insights on Manyue's World")
156
 
157
  # Initialize session state
158
  if "messages" not in st.session_state:
159
  st.session_state.messages = []
160
  if "knowledge_base" not in st.session_state:
161
  try:
162
- with open('knowledge_base.json', 'r', encoding='utf-8') as f:
163
  st.session_state.knowledge_base = json.load(f)
164
  except FileNotFoundError:
165
  st.error("Knowledge base file not found.")
@@ -168,7 +168,7 @@ def main():
168
  # Display welcome message
169
  if "displayed_welcome" not in st.session_state:
170
  st.write("""
171
- Hi there! I'm Rini, Manyue's AI-powered assistant. I'm here to represent Manyue and share insights about:
172
  - My journey from commerce to ML/AI
173
  - My technical skills and projects
174
  - My fit for ML/AI roles
@@ -177,32 +177,31 @@ def main():
177
  """)
178
  st.session_state.displayed_welcome = True
179
 
180
- # Create two columns with chat history in scrollable container
181
  col1, col2 = st.columns([3, 1])
182
 
183
  with col1:
184
- # Chat container for better scrolling
185
- chat_container = st.container()
186
- with chat_container:
187
- for message in st.session_state.messages:
188
- with st.chat_message(message["role"]):
189
- st.markdown(message["content"])
190
 
191
- # Chat input at bottom
192
  if prompt := st.chat_input("Ask me anything or paste a job description..."):
193
- # Add user message
 
 
 
194
  st.session_state.messages.append({"role": "user", "content": prompt})
195
 
196
- try:
197
- # Generate and display response
198
- with st.chat_message("assistant"):
199
  response = generate_response(prompt, st.session_state.knowledge_base)
200
  st.markdown(response)
201
  st.session_state.messages.append({"role": "assistant", "content": response})
202
- except Exception as e:
203
- st.error(f"An error occurred: {str(e)}")
204
-
205
- st.rerun()
206
 
207
  with col2:
208
  st.subheader("Quick Questions")
@@ -214,13 +213,25 @@ def main():
214
  "Your view on the current market?"
215
  ]
216
 
 
217
  for question in example_questions:
218
- if st.button(question):
 
 
219
  st.session_state.messages.append({"role": "user", "content": question})
 
 
 
 
 
 
 
 
 
220
  st.rerun()
221
 
222
  st.markdown("---")
223
- if st.button("Clear Chat"):
224
  st.session_state.messages = []
225
  st.rerun()
226
 
 
152
  return format_default_response(knowledge_base)
153
 
154
  def main():
155
+ st.title("💬 Chat with Manyue's Portfolio")
156
 
157
  # Initialize session state
158
  if "messages" not in st.session_state:
159
  st.session_state.messages = []
160
  if "knowledge_base" not in st.session_state:
161
  try:
162
+ with open('manny_knowledge_base.json', 'r', encoding='utf-8') as f:
163
  st.session_state.knowledge_base = json.load(f)
164
  except FileNotFoundError:
165
  st.error("Knowledge base file not found.")
 
168
  # Display welcome message
169
  if "displayed_welcome" not in st.session_state:
170
  st.write("""
171
+ Hi! I'm Manyue's AI assistant. I can tell you about:
172
  - My journey from commerce to ML/AI
173
  - My technical skills and projects
174
  - My fit for ML/AI roles
 
177
  """)
178
  st.session_state.displayed_welcome = True
179
 
180
+ # Create two columns
181
  col1, col2 = st.columns([3, 1])
182
 
183
  with col1:
184
+ # Display existing messages
185
+ for message in st.session_state.messages:
186
+ with st.chat_message(message["role"]):
187
+ st.markdown(message["content"])
 
 
188
 
189
+ # Handle input - IMPORTANT: This needs to be outside any button handlers
190
  if prompt := st.chat_input("Ask me anything or paste a job description..."):
191
+ with st.chat_message("user"):
192
+ st.markdown(prompt)
193
+
194
+ # Add user message to session state
195
  st.session_state.messages.append({"role": "user", "content": prompt})
196
 
197
+ # Generate and display response
198
+ with st.chat_message("assistant"):
199
+ try:
200
  response = generate_response(prompt, st.session_state.knowledge_base)
201
  st.markdown(response)
202
  st.session_state.messages.append({"role": "assistant", "content": response})
203
+ except Exception as e:
204
+ st.error(f"An error occurred: {str(e)}")
 
 
205
 
206
  with col2:
207
  st.subheader("Quick Questions")
 
213
  "Your view on the current market?"
214
  ]
215
 
216
+ # Handle quick question buttons
217
  for question in example_questions:
218
+ if st.button(question, key=f"btn_{question}"): # Add unique keys
219
+ with st.chat_message("user"):
220
+ st.markdown(question)
221
  st.session_state.messages.append({"role": "user", "content": question})
222
+
223
+ with st.chat_message("assistant"):
224
+ try:
225
+ response = generate_response(question, st.session_state.knowledge_base)
226
+ st.markdown(response)
227
+ st.session_state.messages.append({"role": "assistant", "content": response})
228
+ except Exception as e:
229
+ st.error(f"An error occurred: {str(e)}")
230
+
231
  st.rerun()
232
 
233
  st.markdown("---")
234
+ if st.button("Clear Chat", key="clear_chat"): # Add unique key
235
  st.session_state.messages = []
236
  st.rerun()
237