Spaces:
Sleeping
Sleeping
Delete pages
Browse files- pages/chatbot.py +0 -47
- pages/management.py +0 -68
pages/chatbot.py
DELETED
@@ -1,47 +0,0 @@
|
|
1 |
-
import streamlit as st
|
2 |
-
from app import rag_query, memory, process_feedback
|
3 |
-
|
4 |
-
st.title("🛡️ RAG Chatbot MB Ageas Life 🛡️")
|
5 |
-
|
6 |
-
# Initialize session history
|
7 |
-
if "messages" not in st.session_state:
|
8 |
-
st.session_state.messages = []
|
9 |
-
|
10 |
-
# Hiển thị lại tin nhắn cũ
|
11 |
-
for i, message in enumerate(st.session_state.messages):
|
12 |
-
with st.chat_message(message["role"]):
|
13 |
-
st.markdown(message["content"])
|
14 |
-
if message["role"] == "assistant":
|
15 |
-
col1, col2 = st.columns([1, 15])
|
16 |
-
with col1:
|
17 |
-
if st.button("👍", key=f"thumbs_up_{i}"):
|
18 |
-
process_feedback(st.session_state.messages[i-1]["content"], message["content"], True)
|
19 |
-
with col2:
|
20 |
-
if st.button("👎", key=f"thumbs_down_{i}"):
|
21 |
-
process_feedback(st.session_state.messages[i-1]["content"], message["content"], False)
|
22 |
-
|
23 |
-
# Nhận input người dùng
|
24 |
-
if prompt := st.chat_input("Ask me any question related to MBAL"):
|
25 |
-
# Hiển thị tin nhắn người dùng
|
26 |
-
st.chat_message("user").markdown(prompt)
|
27 |
-
st.session_state.messages.append({"role": "user", "content": prompt})
|
28 |
-
memory.chat_memory.add_user_message(prompt)
|
29 |
-
|
30 |
-
# Gọi hàm RAG để trả lời
|
31 |
-
response = rag_query(prompt)
|
32 |
-
|
33 |
-
# Hiển thị tin nhắn chatbot
|
34 |
-
with st.chat_message("assistant"):
|
35 |
-
st.markdown(response)
|
36 |
-
st.session_state.messages.append({"role": "assistant", "content": response})
|
37 |
-
memory.chat_memory.add_ai_message(response)
|
38 |
-
|
39 |
-
st.rerun()
|
40 |
-
|
41 |
-
# Sidebar
|
42 |
-
# with st.sidebar:
|
43 |
-
# st.header("Lựa chọn khác")
|
44 |
-
# if st.button("Xóa lịch sử chat"):
|
45 |
-
# st.session_state.messages = []
|
46 |
-
# memory.clear()
|
47 |
-
# st.rerun()
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
pages/management.py
DELETED
@@ -1,68 +0,0 @@
|
|
1 |
-
import os
|
2 |
-
import streamlit as st
|
3 |
-
from langchain.document_loaders import DirectoryLoader, TextLoader, PyPDFLoader
|
4 |
-
from langchain.text_splitter import CharacterTextSplitter
|
5 |
-
from app import vectorstore
|
6 |
-
|
7 |
-
|
8 |
-
st.title("Document Management")
|
9 |
-
|
10 |
-
# File uploader
|
11 |
-
uploaded_file = st.file_uploader("Choose a file", type=['txt', 'pdf', 'docx'])
|
12 |
-
|
13 |
-
if uploaded_file is not None:
|
14 |
-
# Create a temporary directory to store the uploaded file
|
15 |
-
temp_dir = "temp_uploads"
|
16 |
-
os.makedirs(temp_dir, exist_ok=True)
|
17 |
-
file_path = os.path.join(temp_dir, uploaded_file.name)
|
18 |
-
|
19 |
-
# Save the uploaded file temporarily
|
20 |
-
with open(file_path, "wb") as f:
|
21 |
-
f.write(uploaded_file.getbuffer())
|
22 |
-
|
23 |
-
st.success(f"File {uploaded_file.name} successfully uploaded!")
|
24 |
-
|
25 |
-
# Process the uploaded file
|
26 |
-
if st.button("Process Document"):
|
27 |
-
with st.spinner("Processing document..."):
|
28 |
-
try:
|
29 |
-
# Load the document based on file type
|
30 |
-
if uploaded_file.type == "application/pdf":
|
31 |
-
loader = PyPDFLoader(file_path)
|
32 |
-
elif uploaded_file.type == "text/plain":
|
33 |
-
loader = TextLoader(file_path)
|
34 |
-
else:
|
35 |
-
st.error("Unsupported file type.")
|
36 |
-
st.stop()
|
37 |
-
|
38 |
-
documents = loader.load()
|
39 |
-
|
40 |
-
# Split the document into chunks
|
41 |
-
text_splitter = CharacterTextSplitter(chunk_size=800, chunk_overlap=150)
|
42 |
-
texts = text_splitter.split_documents(documents)
|
43 |
-
|
44 |
-
# Add the chunks to the vectorstore
|
45 |
-
vectorstore.add_documents(texts)
|
46 |
-
|
47 |
-
st.success(f"Document processed and added to the knowledge base!")
|
48 |
-
except Exception as e:
|
49 |
-
st.error(f"An error occurred: {e}")
|
50 |
-
|
51 |
-
# Clean up: remove the temporary file
|
52 |
-
os.remove(file_path)
|
53 |
-
|
54 |
-
# Display current documents in the knowledge base
|
55 |
-
# st.subheader("Current Documents in Knowledge Base")
|
56 |
-
# # This is a placeholder. You'll need to implement a method to retrieve and display
|
57 |
-
# # the list of documents currently in your Chroma database.
|
58 |
-
# st.write("Placeholder for document list")
|
59 |
-
|
60 |
-
# # Option to clear the entire knowledge base
|
61 |
-
# if st.button("Clear Knowledge Base"):
|
62 |
-
# if st.sidebar.checkbox("Are you sure you want to clear the entire knowledge base? This action cannot be undone."):
|
63 |
-
# try:
|
64 |
-
# # Clear the Chroma database
|
65 |
-
# vectorstore.delete()
|
66 |
-
# st.success("Knowledge base cleared!")
|
67 |
-
# except Exception as e:
|
68 |
-
# st.error(f"An error occurred: {e}")
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|