Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
|
@@ -1,130 +1,219 @@
|
|
| 1 |
import streamlit as st
|
| 2 |
import json
|
| 3 |
-
from transformers import AutoTokenizer, AutoModelForCausalLM
|
| 4 |
-
import torch
|
| 5 |
import time
|
| 6 |
|
| 7 |
# Page configuration
|
| 8 |
st.set_page_config(
|
| 9 |
-
page_title="Portfolio Chatbot
|
| 10 |
page_icon="π€",
|
| 11 |
-
layout="wide"
|
|
|
|
| 12 |
)
|
| 13 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 14 |
# Initialize session state
|
| 15 |
if 'messages' not in st.session_state:
|
| 16 |
st.session_state.messages = []
|
|
|
|
|
|
|
| 17 |
|
| 18 |
def load_knowledge_base():
|
| 19 |
"""Load the knowledge base from JSON file"""
|
| 20 |
-
|
| 21 |
-
|
| 22 |
-
|
| 23 |
-
|
| 24 |
-
|
| 25 |
-
|
|
|
|
|
|
|
| 26 |
|
| 27 |
-
def
|
| 28 |
-
"""Get relevant context
|
| 29 |
query_lower = query.lower()
|
| 30 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
| 31 |
|
| 32 |
-
# Project
|
| 33 |
-
if "project"
|
|
|
|
| 34 |
if "projects" in knowledge_base:
|
| 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 |
def main():
|
| 75 |
-
st.title("
|
| 76 |
-
st.write("
|
| 77 |
|
| 78 |
# Load knowledge base
|
| 79 |
knowledge_base = load_knowledge_base()
|
| 80 |
|
| 81 |
# Create two columns for layout
|
| 82 |
-
col1, col2 = st.columns([
|
| 83 |
|
| 84 |
with col1:
|
| 85 |
-
|
| 86 |
-
# Display chat messages from history
|
| 87 |
for message in st.session_state.messages:
|
| 88 |
with st.chat_message(message["role"]):
|
| 89 |
st.markdown(message["content"])
|
| 90 |
|
| 91 |
-
#
|
| 92 |
if prompt := st.chat_input("What would you like to know?"):
|
| 93 |
-
# Add user message
|
| 94 |
st.session_state.messages.append({"role": "user", "content": prompt})
|
| 95 |
|
| 96 |
-
# Get context
|
| 97 |
-
context =
|
| 98 |
-
|
| 99 |
-
# For now, just echo back a response (replace with actual model response later)
|
| 100 |
-
response = f"Test Response: Let me tell you about that based on my experience..."
|
| 101 |
|
| 102 |
-
# Display
|
| 103 |
with st.chat_message("assistant"):
|
| 104 |
st.markdown(response)
|
| 105 |
|
| 106 |
-
# Add assistant response to
|
| 107 |
st.session_state.messages.append({"role": "assistant", "content": response})
|
| 108 |
|
| 109 |
with col2:
|
| 110 |
-
st.subheader("
|
| 111 |
-
if st.button("
|
| 112 |
-
|
|
|
|
| 113 |
st.experimental_rerun()
|
| 114 |
-
|
| 115 |
-
st.
|
| 116 |
-
|
| 117 |
-
st.session_state.messages.append({
|
| 118 |
-
|
| 119 |
-
|
| 120 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 121 |
st.experimental_rerun()
|
| 122 |
|
| 123 |
-
|
| 124 |
-
|
| 125 |
-
|
| 126 |
-
"content": "What are your Python skills?"
|
| 127 |
-
})
|
| 128 |
st.experimental_rerun()
|
| 129 |
|
| 130 |
if __name__ == "__main__":
|
|
|
|
| 1 |
import streamlit as st
|
| 2 |
import json
|
|
|
|
|
|
|
| 3 |
import time
|
| 4 |
|
| 5 |
# Page configuration
|
| 6 |
st.set_page_config(
|
| 7 |
+
page_title="Manyue's Portfolio Chatbot",
|
| 8 |
page_icon="π€",
|
| 9 |
+
layout="wide",
|
| 10 |
+
initial_sidebar_state="collapsed"
|
| 11 |
)
|
| 12 |
|
| 13 |
+
# Custom CSS for better UI
|
| 14 |
+
st.markdown("""
|
| 15 |
+
<style>
|
| 16 |
+
.chat-message {
|
| 17 |
+
padding: 1.5rem;
|
| 18 |
+
border-radius: 0.5rem;
|
| 19 |
+
margin-bottom: 1rem;
|
| 20 |
+
}
|
| 21 |
+
.user-message {
|
| 22 |
+
background-color: #e9ecef;
|
| 23 |
+
}
|
| 24 |
+
.bot-message {
|
| 25 |
+
background-color: #f8f9fa;
|
| 26 |
+
}
|
| 27 |
+
.stButton>button {
|
| 28 |
+
width: 100%;
|
| 29 |
+
}
|
| 30 |
+
</style>
|
| 31 |
+
""", unsafe_allow_html=True)
|
| 32 |
+
|
| 33 |
# Initialize session state
|
| 34 |
if 'messages' not in st.session_state:
|
| 35 |
st.session_state.messages = []
|
| 36 |
+
if 'knowledge_base' not in st.session_state:
|
| 37 |
+
st.session_state.knowledge_base = None
|
| 38 |
|
| 39 |
def load_knowledge_base():
|
| 40 |
"""Load the knowledge base from JSON file"""
|
| 41 |
+
if st.session_state.knowledge_base is None:
|
| 42 |
+
try:
|
| 43 |
+
with open('knowledge_base.json', 'r', encoding='utf-8') as f:
|
| 44 |
+
st.session_state.knowledge_base = json.load(f)
|
| 45 |
+
except Exception as e:
|
| 46 |
+
st.error(f"Error loading knowledge base: {str(e)}")
|
| 47 |
+
st.session_state.knowledge_base = {}
|
| 48 |
+
return st.session_state.knowledge_base
|
| 49 |
|
| 50 |
+
def get_enhanced_context(query: str, knowledge_base: dict) -> dict:
|
| 51 |
+
"""Get relevant context with improved retrieval"""
|
| 52 |
query_lower = query.lower()
|
| 53 |
+
context = {
|
| 54 |
+
"type": "general",
|
| 55 |
+
"content": [],
|
| 56 |
+
"relevant_sections": []
|
| 57 |
+
}
|
| 58 |
|
| 59 |
+
# Project-related queries
|
| 60 |
+
if any(word in query_lower for word in ["project", "build", "develop", "create", "make", "portfolio"]):
|
| 61 |
+
context["type"] = "project"
|
| 62 |
if "projects" in knowledge_base:
|
| 63 |
+
for name, project in knowledge_base["projects"].items():
|
| 64 |
+
context["content"].append({
|
| 65 |
+
"title": name,
|
| 66 |
+
"description": project.get("description", ""),
|
| 67 |
+
"skills_used": project.get("skills_used", []),
|
| 68 |
+
"status": project.get("status", "")
|
| 69 |
+
})
|
| 70 |
+
|
| 71 |
+
# Skills and experience
|
| 72 |
+
elif any(word in query_lower for word in ["skill", "experience", "know", "capable", "ability", "expert"]):
|
| 73 |
+
context["type"] = "skill"
|
| 74 |
+
if "skills" in knowledge_base.get("personal_details", {}):
|
| 75 |
+
context["content"] = knowledge_base["personal_details"]["skills"]
|
| 76 |
+
|
| 77 |
+
# Educational background
|
| 78 |
+
elif any(word in query_lower for word in ["education", "study", "learn", "degree", "college", "university"]):
|
| 79 |
+
context["type"] = "education"
|
| 80 |
+
context["content"] = knowledge_base.get("education", {})
|
| 81 |
+
|
| 82 |
+
# Career and goals
|
| 83 |
+
elif any(word in query_lower for word in ["goal", "plan", "future", "career", "aspiration"]):
|
| 84 |
+
context["type"] = "career"
|
| 85 |
+
context["content"] = {
|
| 86 |
+
"short_term": knowledge_base.get("goals_and_aspirations", {}).get("short_term", []),
|
| 87 |
+
"long_term": knowledge_base.get("goals_and_aspirations", {}).get("long_term", [])
|
| 88 |
+
}
|
| 89 |
+
|
| 90 |
+
# Personal background
|
| 91 |
+
elif any(word in query_lower for word in ["background", "journey", "story", "transition"]):
|
| 92 |
+
context["type"] = "background"
|
| 93 |
+
context["content"] = knowledge_base.get("personal_journey", {})
|
| 94 |
|
| 95 |
+
# Add FAQ matches if available
|
| 96 |
+
for qa in knowledge_base.get("frequently_asked_questions", []):
|
| 97 |
+
if any(word in qa["question"].lower() for word in query_lower.split()):
|
| 98 |
+
context["relevant_sections"].append({
|
| 99 |
+
"type": "faq",
|
| 100 |
+
"question": qa["question"],
|
| 101 |
+
"answer": qa["answer"]
|
| 102 |
+
})
|
| 103 |
+
|
| 104 |
+
return context
|
| 105 |
+
|
| 106 |
+
def generate_response(query: str, context: dict) -> str:
|
| 107 |
+
"""Generate natural response based on context"""
|
| 108 |
+
response_parts = []
|
| 109 |
+
|
| 110 |
+
# Handle different types of queries
|
| 111 |
+
if context["type"] == "project":
|
| 112 |
+
response_parts.append("Let me tell you about my projects.")
|
| 113 |
+
for project in context["content"]:
|
| 114 |
+
response_parts.append(f"\n\n**{project['title']}**")
|
| 115 |
+
response_parts.append(f"{project['description']}")
|
| 116 |
+
if project['skills_used']:
|
| 117 |
+
response_parts.append(f"\nSkills used: {', '.join(project['skills_used'])}")
|
| 118 |
+
if project['status']:
|
| 119 |
+
response_parts.append(f"\nStatus: {project['status']}")
|
| 120 |
+
|
| 121 |
+
elif context["type"] == "skill":
|
| 122 |
+
response_parts.append("Here are my key skills and experiences:")
|
| 123 |
+
for skill, desc in context["content"].items():
|
| 124 |
+
response_parts.append(f"\n\n**{skill}**:\n{desc}")
|
| 125 |
+
|
| 126 |
+
elif context["type"] == "education":
|
| 127 |
+
response_parts.append("Regarding my educational background:")
|
| 128 |
+
if "academic_background" in context["content"]:
|
| 129 |
+
response_parts.append(context["content"]["academic_background"])
|
| 130 |
+
if "academic_achievements" in context["content"]:
|
| 131 |
+
response_parts.append("\n\nAchievements:")
|
| 132 |
+
for achievement in context["content"]["academic_achievements"]:
|
| 133 |
+
response_parts.append(f"- {achievement}")
|
| 134 |
+
|
| 135 |
+
elif context["type"] == "career":
|
| 136 |
+
response_parts.append("Let me share my career goals:")
|
| 137 |
+
response_parts.append("\n\n**Short-term goals:**")
|
| 138 |
+
for goal in context["content"]["short_term"]:
|
| 139 |
+
response_parts.append(f"- {goal}")
|
| 140 |
+
response_parts.append("\n\n**Long-term goals:**")
|
| 141 |
+
for goal in context["content"]["long_term"]:
|
| 142 |
+
response_parts.append(f"- {goal}")
|
| 143 |
+
|
| 144 |
+
elif context["type"] == "background":
|
| 145 |
+
response_parts.append(context["content"].get("mindset", ""))
|
| 146 |
+
response_parts.append("\n\n" + context["content"].get("motto_or_vision", ""))
|
| 147 |
+
|
| 148 |
+
# Add any relevant FAQ information
|
| 149 |
+
if context["relevant_sections"]:
|
| 150 |
+
for section in context["relevant_sections"]:
|
| 151 |
+
if section["type"] == "faq":
|
| 152 |
+
response_parts.append(f"\n\n{section['answer']}")
|
| 153 |
+
|
| 154 |
+
# Default response if no specific context matched
|
| 155 |
+
if not response_parts:
|
| 156 |
+
response_parts = ["I am Manyue, an aspiring AI/ML engineer. I can tell you about my projects, skills, education, or career goals. What would you like to know?"]
|
| 157 |
+
|
| 158 |
+
return "\n".join(response_parts)
|
| 159 |
|
| 160 |
def main():
|
| 161 |
+
st.title("π¬ Chat with Manyue's Portfolio")
|
| 162 |
+
st.write("Ask me about my skills, projects, education, or career goals!")
|
| 163 |
|
| 164 |
# Load knowledge base
|
| 165 |
knowledge_base = load_knowledge_base()
|
| 166 |
|
| 167 |
# Create two columns for layout
|
| 168 |
+
col1, col2 = st.columns([3, 1])
|
| 169 |
|
| 170 |
with col1:
|
| 171 |
+
# Display chat messages
|
|
|
|
| 172 |
for message in st.session_state.messages:
|
| 173 |
with st.chat_message(message["role"]):
|
| 174 |
st.markdown(message["content"])
|
| 175 |
|
| 176 |
+
# Chat input
|
| 177 |
if prompt := st.chat_input("What would you like to know?"):
|
| 178 |
+
# Add user message
|
| 179 |
st.session_state.messages.append({"role": "user", "content": prompt})
|
| 180 |
|
| 181 |
+
# Get context and generate response
|
| 182 |
+
context = get_enhanced_context(prompt, knowledge_base)
|
| 183 |
+
response = generate_response(prompt, context)
|
|
|
|
|
|
|
| 184 |
|
| 185 |
+
# Display response with typing effect
|
| 186 |
with st.chat_message("assistant"):
|
| 187 |
st.markdown(response)
|
| 188 |
|
| 189 |
+
# Add assistant response to history
|
| 190 |
st.session_state.messages.append({"role": "assistant", "content": response})
|
| 191 |
|
| 192 |
with col2:
|
| 193 |
+
st.subheader("Quick Questions")
|
| 194 |
+
if st.button("π Tell me about your projects"):
|
| 195 |
+
prompt = "What projects have you worked on?"
|
| 196 |
+
st.session_state.messages.append({"role": "user", "content": prompt})
|
| 197 |
st.experimental_rerun()
|
| 198 |
+
|
| 199 |
+
if st.button("π» What are your technical skills?"):
|
| 200 |
+
prompt = "What are your main technical skills?"
|
| 201 |
+
st.session_state.messages.append({"role": "user", "content": prompt})
|
| 202 |
+
st.experimental_rerun()
|
| 203 |
+
|
| 204 |
+
if st.button("π Educational background?"):
|
| 205 |
+
prompt = "Tell me about your education"
|
| 206 |
+
st.session_state.messages.append({"role": "user", "content": prompt})
|
| 207 |
+
st.experimental_rerun()
|
| 208 |
+
|
| 209 |
+
if st.button("π― What are your career goals?"):
|
| 210 |
+
prompt = "What are your career goals?"
|
| 211 |
+
st.session_state.messages.append({"role": "user", "content": prompt})
|
| 212 |
st.experimental_rerun()
|
| 213 |
|
| 214 |
+
st.markdown("---")
|
| 215 |
+
if st.button("ποΈ Clear Chat"):
|
| 216 |
+
st.session_state.messages = []
|
|
|
|
|
|
|
| 217 |
st.experimental_rerun()
|
| 218 |
|
| 219 |
if __name__ == "__main__":
|