Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
|
@@ -1,144 +1,143 @@
|
|
| 1 |
-
import streamlit as st
|
| 2 |
-
from langchain.document_loaders import PyPDFLoader
|
| 3 |
-
from langchain.text_splitter import RecursiveCharacterTextSplitter
|
| 4 |
-
from langchain.vectorstores import Chroma
|
| 5 |
-
from langchain.embeddings import HuggingFaceEmbeddings
|
| 6 |
-
from transformers import pipeline
|
| 7 |
-
import time
|
| 8 |
-
|
| 9 |
-
|
| 10 |
-
|
| 11 |
-
|
| 12 |
-
|
| 13 |
-
|
| 14 |
-
|
| 15 |
-
|
| 16 |
-
|
| 17 |
-
|
| 18 |
-
|
| 19 |
-
|
| 20 |
-
|
| 21 |
-
|
| 22 |
-
|
| 23 |
-
|
| 24 |
-
|
| 25 |
-
|
| 26 |
-
|
| 27 |
-
|
| 28 |
-
|
| 29 |
-
|
| 30 |
-
|
| 31 |
-
|
| 32 |
-
|
| 33 |
-
|
| 34 |
-
|
| 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 |
-
|
| 75 |
-
|
| 76 |
-
|
| 77 |
-
|
| 78 |
-
|
| 79 |
-
|
| 80 |
-
|
| 81 |
-
|
| 82 |
-
|
| 83 |
-
|
| 84 |
-
|
| 85 |
-
|
| 86 |
-
|
| 87 |
-
|
| 88 |
-
|
| 89 |
-
|
| 90 |
-
|
| 91 |
-
|
| 92 |
-
|
| 93 |
-
|
| 94 |
-
|
| 95 |
-
|
| 96 |
-
|
| 97 |
-
|
| 98 |
-
|
| 99 |
-
|
| 100 |
-
|
| 101 |
-
|
| 102 |
-
|
| 103 |
-
|
| 104 |
-
|
| 105 |
-
|
| 106 |
-
|
| 107 |
-
|
| 108 |
-
|
| 109 |
-
|
| 110 |
-
|
| 111 |
-
|
| 112 |
-
|
| 113 |
-
|
| 114 |
-
|
| 115 |
-
|
| 116 |
-
|
| 117 |
-
"""
|
| 118 |
-
|
| 119 |
-
|
| 120 |
-
|
| 121 |
-
|
| 122 |
-
|
| 123 |
-
|
| 124 |
-
|
| 125 |
-
|
| 126 |
-
|
| 127 |
-
|
| 128 |
-
|
| 129 |
-
|
| 130 |
-
|
| 131 |
-
|
| 132 |
-
|
| 133 |
-
|
| 134 |
-
|
| 135 |
-
|
| 136 |
-
|
| 137 |
-
|
| 138 |
-
displayed_response
|
| 139 |
-
|
| 140 |
-
|
| 141 |
-
|
| 142 |
-
|
| 143 |
-
{"role": "assistant", "content": response}
|
| 144 |
)
|
|
|
|
| 1 |
+
import streamlit as st
|
| 2 |
+
from langchain.document_loaders import PyPDFLoader
|
| 3 |
+
from langchain.text_splitter import RecursiveCharacterTextSplitter
|
| 4 |
+
from langchain.vectorstores import Chroma
|
| 5 |
+
from langchain.embeddings import HuggingFaceEmbeddings
|
| 6 |
+
from transformers import pipeline
|
| 7 |
+
import time
|
| 8 |
+
|
| 9 |
+
# Set page config as the first Streamlit command
|
| 10 |
+
st.set_page_config(page_title="AI-Buddy Assistant", page_icon="🤖", layout="centered")
|
| 11 |
+
|
| 12 |
+
# Custom CSS
|
| 13 |
+
st.markdown("""
|
| 14 |
+
<style>
|
| 15 |
+
.stChat {
|
| 16 |
+
font-size: 1.3rem;
|
| 17 |
+
}
|
| 18 |
+
.stTextArea textarea {
|
| 19 |
+
font-size: 1.2rem;
|
| 20 |
+
}
|
| 21 |
+
.stMarkdown {
|
| 22 |
+
font-size: 1.2rem;
|
| 23 |
+
}
|
| 24 |
+
.stButton button {
|
| 25 |
+
background-color: #ff4b4b;
|
| 26 |
+
color: white;
|
| 27 |
+
font-size: 1.2rem;
|
| 28 |
+
padding: 10px 20px;
|
| 29 |
+
border-radius: 5px;
|
| 30 |
+
border: none;
|
| 31 |
+
}
|
| 32 |
+
.stButton button:hover {
|
| 33 |
+
background-color: #ff6565;
|
| 34 |
+
}
|
| 35 |
+
</style>
|
| 36 |
+
""", unsafe_allow_html=True)
|
| 37 |
+
|
| 38 |
+
# Cache the QA system initialization
|
| 39 |
+
@st.cache_resource
|
| 40 |
+
def initialize_qa_system():
|
| 41 |
+
# Load PDF
|
| 42 |
+
pdf_path = "ai_buddy.pdf" # Update path accordingly
|
| 43 |
+
data = PyPDFLoader(pdf_path).load()
|
| 44 |
+
|
| 45 |
+
# Split documents
|
| 46 |
+
text_splitter = RecursiveCharacterTextSplitter(chunk_size=750, chunk_overlap=150)
|
| 47 |
+
splits = text_splitter.split_documents(data)
|
| 48 |
+
|
| 49 |
+
# Initialize embeddings
|
| 50 |
+
embeddings = HuggingFaceEmbeddings(
|
| 51 |
+
model_name="sentence-transformers/all-MiniLM-L6-v2"
|
| 52 |
+
)
|
| 53 |
+
|
| 54 |
+
# Create vector store
|
| 55 |
+
vector_db = Chroma.from_documents(
|
| 56 |
+
documents=splits,
|
| 57 |
+
embedding=embeddings,
|
| 58 |
+
persist_directory="vector_db"
|
| 59 |
+
)
|
| 60 |
+
|
| 61 |
+
# Initialize QA pipeline
|
| 62 |
+
qa_pipeline = pipeline("question-answering", model="deepset/roberta-base-squad2")
|
| 63 |
+
|
| 64 |
+
return vector_db, qa_pipeline
|
| 65 |
+
|
| 66 |
+
# Initialize session state for vector database and QA pipeline
|
| 67 |
+
if 'vector_db' not in st.session_state:
|
| 68 |
+
st.session_state.vector_db, st.session_state.qa_pipeline = initialize_qa_system()
|
| 69 |
+
|
| 70 |
+
# Initialize chat history
|
| 71 |
+
if "messages" not in st.session_state:
|
| 72 |
+
st.session_state.messages = [
|
| 73 |
+
{"role": "assistant", "content": "Hello! How can I assist you today?"}
|
| 74 |
+
]
|
| 75 |
+
|
| 76 |
+
st.title("🤖 AI-Buddy Assistant")
|
| 77 |
+
|
| 78 |
+
# Sidebar configuration
|
| 79 |
+
st.sidebar.title("Want to know how AI helps in your profession?")
|
| 80 |
+
professions = ["Software Engineer", "Data Scientist", "Marketing Specialist",
|
| 81 |
+
"Financial Analyst", "Teacher", "Doctor", "Project Manager",
|
| 82 |
+
"Consultant", "Business Analyst", "Other"]
|
| 83 |
+
fields = ["IT", "Healthcare", "Education", "Finance", "Marketing",
|
| 84 |
+
"Engineering", "Sales", "Human Resources", "Consulting", "Other"]
|
| 85 |
+
|
| 86 |
+
profession = st.sidebar.selectbox("Choose Your Profession", professions)
|
| 87 |
+
field = st.sidebar.selectbox("Choose Your Field/Domain", fields)
|
| 88 |
+
|
| 89 |
+
if profession == "Other":
|
| 90 |
+
profession = st.sidebar.text_input("Please specify your profession")
|
| 91 |
+
if field == "Other":
|
| 92 |
+
field = st.sidebar.text_input("Please specify your field")
|
| 93 |
+
|
| 94 |
+
description = st.sidebar.text_area("About you",
|
| 95 |
+
placeholder="Briefly describe your role")
|
| 96 |
+
|
| 97 |
+
# Function to execute query with streaming response
|
| 98 |
+
def execute_query_with_stream(question):
|
| 99 |
+
retriever = st.session_state.vector_db.as_retriever()
|
| 100 |
+
retrieved_docs = retriever.get_relevant_documents(question)
|
| 101 |
+
|
| 102 |
+
if not retrieved_docs:
|
| 103 |
+
return "No relevant information found."
|
| 104 |
+
|
| 105 |
+
context = " ".join([doc.page_content.strip() for doc in retrieved_docs])
|
| 106 |
+
response = st.session_state.qa_pipeline(question=question, context=context)
|
| 107 |
+
|
| 108 |
+
return response['answer']
|
| 109 |
+
|
| 110 |
+
# Handle sidebar submission
|
| 111 |
+
if st.sidebar.button("Get AI Insights"):
|
| 112 |
+
prompt = f"""
|
| 113 |
+
My profession is {profession} in the {field} field.
|
| 114 |
+
Here's about me: {description}.
|
| 115 |
+
How can AI and AI-Buddy help me in my role?
|
| 116 |
+
"""
|
| 117 |
+
st.session_state.messages.append({"role": "user", "content": prompt})
|
| 118 |
+
|
| 119 |
+
# Chat interface
|
| 120 |
+
if prompt := st.chat_input("Ask me anything about AI-Buddy"):
|
| 121 |
+
st.session_state.messages.append({"role": "user", "content": prompt})
|
| 122 |
+
|
| 123 |
+
# Display chat history and generate responses
|
| 124 |
+
for message in st.session_state.messages:
|
| 125 |
+
with st.chat_message(message["role"]):
|
| 126 |
+
st.write(message["content"])
|
| 127 |
+
|
| 128 |
+
if st.session_state.messages[-1]["role"] == "user":
|
| 129 |
+
with st.chat_message("assistant"):
|
| 130 |
+
with st.spinner("Thinking..."):
|
| 131 |
+
response = execute_query_with_stream(st.session_state.messages[-1]["content"])
|
| 132 |
+
|
| 133 |
+
# Simulate streaming effect
|
| 134 |
+
placeholder = st.empty()
|
| 135 |
+
displayed_response = ""
|
| 136 |
+
for char in response:
|
| 137 |
+
displayed_response += char
|
| 138 |
+
placeholder.write(displayed_response)
|
| 139 |
+
time.sleep(0.02)
|
| 140 |
+
|
| 141 |
+
st.session_state.messages.append(
|
| 142 |
+
{"role": "assistant", "content": response}
|
|
|
|
| 143 |
)
|