Spaces:
Sleeping
Sleeping
Update app.py
Browse files
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("
|
| 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('
|
| 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
|
| 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
|
| 181 |
col1, col2 = st.columns([3, 1])
|
| 182 |
|
| 183 |
with col1:
|
| 184 |
-
#
|
| 185 |
-
|
| 186 |
-
|
| 187 |
-
|
| 188 |
-
with st.chat_message(message["role"]):
|
| 189 |
-
st.markdown(message["content"])
|
| 190 |
|
| 191 |
-
#
|
| 192 |
if prompt := st.chat_input("Ask me anything or paste a job description..."):
|
| 193 |
-
|
|
|
|
|
|
|
|
|
|
| 194 |
st.session_state.messages.append({"role": "user", "content": prompt})
|
| 195 |
|
| 196 |
-
|
| 197 |
-
|
| 198 |
-
|
| 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 |
-
|
| 203 |
-
|
| 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 |
|