Spaces:
Sleeping
Sleeping
Temporary app.py
Browse files
app.py
ADDED
|
@@ -0,0 +1,131 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 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 Test",
|
| 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 |
+
try:
|
| 21 |
+
with open('knowledge_base.json', 'r', encoding='utf-8') as f:
|
| 22 |
+
return json.load(f)
|
| 23 |
+
except Exception as e:
|
| 24 |
+
st.error(f"Error loading knowledge base: {str(e)}")
|
| 25 |
+
return {}
|
| 26 |
+
|
| 27 |
+
def get_context(query: str, knowledge_base: dict) -> str:
|
| 28 |
+
"""Get relevant context from knowledge base based on query"""
|
| 29 |
+
query_lower = query.lower()
|
| 30 |
+
contexts = []
|
| 31 |
+
|
| 32 |
+
# Project context
|
| 33 |
+
if "project" in query_lower:
|
| 34 |
+
if "projects" in knowledge_base:
|
| 35 |
+
contexts.extend([
|
| 36 |
+
f"{name}: {desc}"
|
| 37 |
+
for name, desc in knowledge_base["projects"].items()
|
| 38 |
+
])
|
| 39 |
+
|
| 40 |
+
# Skills context
|
| 41 |
+
elif any(keyword in query_lower for keyword in ["skill", "experience", "capability"]):
|
| 42 |
+
if "personal_details" in knowledge_base and "skills" in knowledge_base["personal_details"]:
|
| 43 |
+
contexts.extend([
|
| 44 |
+
f"{skill}: {desc}"
|
| 45 |
+
for skill, desc in knowledge_base["personal_details"]["skills"].items()
|
| 46 |
+
])
|
| 47 |
+
|
| 48 |
+
# Default context
|
| 49 |
+
else:
|
| 50 |
+
contexts = [
|
| 51 |
+
f"Name: {knowledge_base.get('personal_details', {}).get('full_name', 'Manyue')}",
|
| 52 |
+
"Summary: I am an aspiring AI/ML engineer with experience in Python, Machine Learning, and Data Analysis."
|
| 53 |
+
]
|
| 54 |
+
|
| 55 |
+
return "\n".join(contexts)
|
| 56 |
+
|
| 57 |
+
def initialize_model():
|
| 58 |
+
"""Initialize the model and tokenizer"""
|
| 59 |
+
try:
|
| 60 |
+
# For testing, use a smaller model
|
| 61 |
+
model_name = "meta-llama/Llama-2-7b-chat-hf" # You'll need to adjust this
|
| 62 |
+
|
| 63 |
+
tokenizer = AutoTokenizer.from_pretrained(model_name)
|
| 64 |
+
model = AutoModelForCausalLM.from_pretrained(
|
| 65 |
+
model_name,
|
| 66 |
+
torch_dtype=torch.float16,
|
| 67 |
+
device_map="auto"
|
| 68 |
+
)
|
| 69 |
+
return tokenizer, model
|
| 70 |
+
except Exception as e:
|
| 71 |
+
st.error(f"Error initializing model: {str(e)}")
|
| 72 |
+
return None, None
|
| 73 |
+
|
| 74 |
+
def main():
|
| 75 |
+
st.title("Portfolio Chatbot Testing Interface")
|
| 76 |
+
st.write("Test the chatbot's responses and interaction patterns")
|
| 77 |
+
|
| 78 |
+
# Load knowledge base
|
| 79 |
+
knowledge_base = load_knowledge_base()
|
| 80 |
+
|
| 81 |
+
# Create two columns for layout
|
| 82 |
+
col1, col2 = st.columns([2, 1])
|
| 83 |
+
|
| 84 |
+
with col1:
|
| 85 |
+
st.subheader("Chat Interface")
|
| 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 |
+
# Accept user input
|
| 92 |
+
if prompt := st.chat_input("What would you like to know?"):
|
| 93 |
+
# Add user message to chat history
|
| 94 |
+
st.session_state.messages.append({"role": "user", "content": prompt})
|
| 95 |
+
|
| 96 |
+
# Get context for the query
|
| 97 |
+
context = get_context(prompt, knowledge_base)
|
| 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 assistant response in chat message container
|
| 103 |
+
with st.chat_message("assistant"):
|
| 104 |
+
st.markdown(response)
|
| 105 |
+
|
| 106 |
+
# Add assistant response to chat history
|
| 107 |
+
st.session_state.messages.append({"role": "assistant", "content": response})
|
| 108 |
+
|
| 109 |
+
with col2:
|
| 110 |
+
st.subheader("Testing Tools")
|
| 111 |
+
if st.button("Clear Chat History"):
|
| 112 |
+
st.session_state.messages = []
|
| 113 |
+
st.experimental_rerun()
|
| 114 |
+
|
| 115 |
+
st.subheader("Sample Questions")
|
| 116 |
+
if st.button("Tell me about your ML projects"):
|
| 117 |
+
st.session_state.messages.append({
|
| 118 |
+
"role": "user",
|
| 119 |
+
"content": "Tell me about your ML projects"
|
| 120 |
+
})
|
| 121 |
+
st.experimental_rerun()
|
| 122 |
+
|
| 123 |
+
if st.button("What are your Python skills?"):
|
| 124 |
+
st.session_state.messages.append({
|
| 125 |
+
"role": "user",
|
| 126 |
+
"content": "What are your Python skills?"
|
| 127 |
+
})
|
| 128 |
+
st.experimental_rerun()
|
| 129 |
+
|
| 130 |
+
if __name__ == "__main__":
|
| 131 |
+
main()
|